]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm: use get_oder() and check size is is_power_of_2
authorBarry Song <v-songbaohua@oppo.com>
Wed, 14 Aug 2024 22:34:16 +0000 (10:34 +1200)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 17 Aug 2024 00:53:23 +0000 (17:53 -0700)
Using get_order() is more robust according to Baolin.  It is also better
to filter illegal size such as 3KB, 16KB according to David.

Link: https://lkml.kernel.org/r/20240814224635.43272-1-21cnbao@gmail.com
Signed-off-by: Barry Song <v-songbaohua@oppo.com>
Suggested-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Suggested-by: David Hildenbrand <david@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lance Yang <ioworker0@gmail.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/huge_memory.c

index d32ade9900599ccf1f2ce6b0d0cd57fe40d05103..cf8e34f62976f812210610ee51f2da5a654f86c9 100644 (file)
@@ -929,14 +929,17 @@ static inline int get_order_from_str(const char *size_str)
        int order;
 
        size = memparse(size_str, &endptr);
-       order = fls(size >> PAGE_SHIFT) - 1;
-       if ((1 << order) & ~THP_ORDERS_ALL_ANON) {
-               pr_err("invalid size %s(order %d) in thp_anon boot parameter\n",
-                       size_str, order);
-               return -EINVAL;
-       }
+
+       if (!is_power_of_2(size >> PAGE_SHIFT))
+               goto err;
+       order = get_order(size);
+       if ((1 << order) & ~THP_ORDERS_ALL_ANON)
+               goto err;
 
        return order;
+err:
+       pr_err("invalid size %s in thp_anon boot parameter\n", size_str);
+       return -EINVAL;
 }
 
 static char str_dup[PAGE_SIZE] __meminitdata;