Commit 59f876fb authored by Kirill A. Shutemov's avatar Kirill A. Shutemov Committed by Andrew Morton

mm: avoid passing 0 to __ffs()

23baf831 ("mm, treewide: redefine MAX_ORDER sanely") results in
various boot failures (hang) on arm targets Debug messages reveal the
reason.

########### MAX_ORDER=10 start=0 __ffs(start)=-1 min()=10 min_t=-1
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If start==0, __ffs(start) returns 0xfffffff or (as int) -1, which min_t()
interprets as such, while min() apparently uses the returned unsigned long
value. Obviously a negative order isn't received well by the rest of the
code.

[akpm@linux-foundation.org: fix comment, per Mike]
  Link: https://lkml.kernel.org/r/ZDBa7HWZK69dKKzH@kernel.org
Link: https://lkml.kernel.org/r/20230406072529.vupqyrzqnhyozeyh@box.shutemov.name
Fixes: 23baf831 ("mm, treewide: redefine MAX_ORDER sanely")
Signed-off-by: default avatar"Kirill A. Shutemov" <kirill@shutemov.name>
Reported-by: default avatarGuenter Roeck <linux@roeck-us.net>
  Link: https://lkml.kernel.org/r/9460377a-38aa-4f39-ad57-fb73725f92db@roeck-us.netReviewed-by: default avatarMike Rapoport (IBM) <rppt@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent f8f238ff
......@@ -2043,7 +2043,16 @@ static void __init __free_pages_memory(unsigned long start, unsigned long end)
int order;
while (start < end) {
order = min_t(int, MAX_ORDER, __ffs(start));
/*
* Free the pages in the largest chunks alignment allows.
*
* __ffs() behaviour is undefined for 0. start == 0 is
* MAX_ORDER-aligned, set order to MAX_ORDER for the case.
*/
if (start)
order = min_t(int, MAX_ORDER, __ffs(start));
else
order = MAX_ORDER;
while (start + (1UL << order) > end)
order--;
......
......@@ -605,7 +605,18 @@ static void online_pages_range(unsigned long start_pfn, unsigned long nr_pages)
* this and the first chunk to online will be pageblock_nr_pages.
*/
for (pfn = start_pfn; pfn < end_pfn;) {
int order = min_t(int, MAX_ORDER, __ffs(pfn));
int order;
/*
* Free to online pages in the largest chunks alignment allows.
*
* __ffs() behaviour is undefined for 0. start == 0 is
* MAX_ORDER-aligned, Set order to MAX_ORDER for the case.
*/
if (pfn)
order = min_t(int, MAX_ORDER, __ffs(pfn));
else
order = MAX_ORDER;
(*online_page_callback)(pfn_to_page(pfn), order);
pfn += (1UL << order);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment