[2/6] mm: kfence: check kfence pool size at building time

Message ID 20230328095807.7014-3-songmuchun@bytedance.com
State New
Headers
Series Simplify kfence code |

Commit Message

Muchun Song March 28, 2023, 9:58 a.m. UTC
  Check kfence pool size at building time to expose problem ASAP.

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
 mm/kfence/core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
  

Comments

Marco Elver March 28, 2023, 10:14 a.m. UTC | #1
On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
<kasan-dev@googlegroups.com> wrote:
>
> Check kfence pool size at building time to expose problem ASAP.
>
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
>  mm/kfence/core.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index de62a84d4830..6781af1dfa66 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -841,10 +841,9 @@ static int kfence_init_late(void)
>                 return -ENOMEM;
>         __kfence_pool = page_to_virt(pages);
>  #else
> -       if (nr_pages > MAX_ORDER_NR_PAGES) {
> -               pr_warn("KFENCE_NUM_OBJECTS too large for buddy allocator\n");
> -               return -EINVAL;
> -       }
> +       BUILD_BUG_ON_MSG(get_order(KFENCE_POOL_SIZE) > MAX_ORDER,
> +                        "CONFIG_KFENCE_NUM_OBJECTS is too large for buddy allocator");
> +

It's perfectly valid to want to use KFENCE with a very large pool that
is initialized on boot, and simply sacrifice the ability to initialize
late.

Nack.
  
Muchun Song March 28, 2023, 1:03 p.m. UTC | #2
> On Mar 28, 2023, at 18:14, Marco Elver <elver@google.com> wrote:
> 
> On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
> <kasan-dev@googlegroups.com> wrote:
>> 
>> Check kfence pool size at building time to expose problem ASAP.
>> 
>> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
>> ---
>> mm/kfence/core.c | 7 +++----
>> 1 file changed, 3 insertions(+), 4 deletions(-)
>> 
>> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
>> index de62a84d4830..6781af1dfa66 100644
>> --- a/mm/kfence/core.c
>> +++ b/mm/kfence/core.c
>> @@ -841,10 +841,9 @@ static int kfence_init_late(void)
>>                return -ENOMEM;
>>        __kfence_pool = page_to_virt(pages);
>> #else
>> -       if (nr_pages > MAX_ORDER_NR_PAGES) {
>> -               pr_warn("KFENCE_NUM_OBJECTS too large for buddy allocator\n");
>> -               return -EINVAL;
>> -       }
>> +       BUILD_BUG_ON_MSG(get_order(KFENCE_POOL_SIZE) > MAX_ORDER,
>> +                        "CONFIG_KFENCE_NUM_OBJECTS is too large for buddy allocator");
>> +
> 
> It's perfectly valid to want to use KFENCE with a very large pool that
> is initialized on boot, and simply sacrifice the ability to initialize
> late.

You are right. I didn’t realize this.

Thanks 

> 
> Nack.
  

Patch

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index de62a84d4830..6781af1dfa66 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -841,10 +841,9 @@  static int kfence_init_late(void)
 		return -ENOMEM;
 	__kfence_pool = page_to_virt(pages);
 #else
-	if (nr_pages > MAX_ORDER_NR_PAGES) {
-		pr_warn("KFENCE_NUM_OBJECTS too large for buddy allocator\n");
-		return -EINVAL;
-	}
+	BUILD_BUG_ON_MSG(get_order(KFENCE_POOL_SIZE) > MAX_ORDER,
+			 "CONFIG_KFENCE_NUM_OBJECTS is too large for buddy allocator");
+
 	__kfence_pool = alloc_pages_exact(KFENCE_POOL_SIZE, GFP_KERNEL);
 	if (!__kfence_pool)
 		return -ENOMEM;