| From: Barry Song <v-songbaohua@oppo.com> |
| Subject: mm: use get_oder() and check size is is_power_of_2 |
| Date: Thu, 15 Aug 2024 10:34:16 +1200 |
| |
| 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 | 15 +++++++++------ |
| 1 file changed, 9 insertions(+), 6 deletions(-) |
| |
| --- a/mm/huge_memory.c~mm-override-mthp-enabled-defaults-at-kernel-cmdline-fix |
| +++ a/mm/huge_memory.c |
| @@ -929,14 +929,17 @@ static inline int get_order_from_str(con |
| 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; |
| _ |