diff mbox series

[1/2] kfence: allow providing __kfence_pool in arch specific way

Message ID 20210524172529.3d23c3e7@xhacker.debian (mailing list archive)
State New, archived
Headers show
Series arm64: remove page granularity limitation from KFENCE | expand

Commit Message

Jisheng Zhang May 24, 2021, 9:25 a.m. UTC
Some architectures may want to allocate the __kfence_pool differently
for example, allocate the __kfence_pool earlier before paging_init().
We also delay the memset() to kfence_init_pool().

Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com>
---
 mm/kfence/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Marco Elver May 24, 2021, 10:36 a.m. UTC | #1
On Mon, 24 May 2021 at 11:26, Jisheng Zhang <Jisheng.Zhang@synaptics.com> wrote:
> Some architectures may want to allocate the __kfence_pool differently
> for example, allocate the __kfence_pool earlier before paging_init().
> We also delay the memset() to kfence_init_pool().
>
> Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com>
> ---
>  mm/kfence/core.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index e18fbbd5d9b4..65f0210edb65 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -430,6 +430,8 @@ static bool __init kfence_init_pool(void)
>         if (!__kfence_pool)
>                 return false;
>
> +       memset(__kfence_pool, 0, KFENCE_POOL_SIZE);
> +

Use memzero_explicit().

Also, for the arm64 case, is delaying the zeroing relevant? You still
call kfence_alloc_pool() in patch 2/2, and zeroing it on
memblock_alloc() is not wrong, correct?

Essentially if there's not going to be any benefit to us doing the
zeroing ourselves, I'd simply leave it as-is and keep using
memblock_alloc(). And if there's some odd architecture that doesn't
even want to use kfence_alloc_pool(), they could just zero the memory
themselves. But we really should use kfence_alloc_pool(), because
otherwise it'll just become unmaintainable if on changes to
kfence_alloc_pool() we have to go and find other special architectures
that don't use it and adjust them, too.

Thanks,
-- Marco

>         if (!arch_kfence_init_pool())
>                 goto err;
>
> @@ -645,10 +647,10 @@ static DECLARE_DELAYED_WORK(kfence_timer, toggle_allocation_gate);
>
>  void __init kfence_alloc_pool(void)
>  {
> -       if (!kfence_sample_interval)
> +       if (!kfence_sample_interval || __kfence_pool)
>                 return;
>
> -       __kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
> +       __kfence_pool = memblock_alloc_raw(KFENCE_POOL_SIZE, PAGE_SIZE);
>
>         if (!__kfence_pool)
>                 pr_err("failed to allocate pool\n");
> --
> 2.31.0
>
Jisheng Zhang May 25, 2021, 1:27 a.m. UTC | #2
On Mon, 24 May 2021 12:36:34 +0200
Marco Elver <elver@google.com> wrote:

> 
> 
> On Mon, 24 May 2021 at 11:26, Jisheng Zhang <Jisheng.Zhang@synaptics.com> wrote:
> > Some architectures may want to allocate the __kfence_pool differently
> > for example, allocate the __kfence_pool earlier before paging_init().
> > We also delay the memset() to kfence_init_pool().
> >
> > Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com>
> > ---
> >  mm/kfence/core.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> > index e18fbbd5d9b4..65f0210edb65 100644
> > --- a/mm/kfence/core.c
> > +++ b/mm/kfence/core.c
> > @@ -430,6 +430,8 @@ static bool __init kfence_init_pool(void)
> >         if (!__kfence_pool)
> >                 return false;
> >
> > +       memset(__kfence_pool, 0, KFENCE_POOL_SIZE);
> > +  
> 
> Use memzero_explicit().
> 
> Also, for the arm64 case, is delaying the zeroing relevant? You still
> call kfence_alloc_pool() in patch 2/2, and zeroing it on
> memblock_alloc() is not wrong, correct?

memblock_alloc() returns virtual address which can't be used before paging_init()
so I delayed the memset to kfence_init_pool.

> 
> Essentially if there's not going to be any benefit to us doing the
> zeroing ourselves, I'd simply leave it as-is and keep using
> memblock_alloc(). And if there's some odd architecture that doesn't
> even want to use kfence_alloc_pool(), they could just zero the memory
> themselves. But we really should use kfence_alloc_pool(), because
> otherwise it'll just become unmaintainable if on changes to
> kfence_alloc_pool() we have to go and find other special architectures
> that don't use it and adjust them, too.
> 
> Thanks,
> -- Marco
> 
> >         if (!arch_kfence_init_pool())
> >                 goto err;
> >
> > @@ -645,10 +647,10 @@ static DECLARE_DELAYED_WORK(kfence_timer, toggle_allocation_gate);
> >
> >  void __init kfence_alloc_pool(void)
> >  {
> > -       if (!kfence_sample_interval)
> > +       if (!kfence_sample_interval || __kfence_pool)
> >                 return;
> >
> > -       __kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
> > +       __kfence_pool = memblock_alloc_raw(KFENCE_POOL_SIZE, PAGE_SIZE);
> >
> >         if (!__kfence_pool)
> >                 pr_err("failed to allocate pool\n");
> > --
> > 2.31.0
> >
diff mbox series

Patch

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index e18fbbd5d9b4..65f0210edb65 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -430,6 +430,8 @@  static bool __init kfence_init_pool(void)
 	if (!__kfence_pool)
 		return false;
 
+	memset(__kfence_pool, 0, KFENCE_POOL_SIZE);
+
 	if (!arch_kfence_init_pool())
 		goto err;
 
@@ -645,10 +647,10 @@  static DECLARE_DELAYED_WORK(kfence_timer, toggle_allocation_gate);
 
 void __init kfence_alloc_pool(void)
 {
-	if (!kfence_sample_interval)
+	if (!kfence_sample_interval || __kfence_pool)
 		return;
 
-	__kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
+	__kfence_pool = memblock_alloc_raw(KFENCE_POOL_SIZE, PAGE_SIZE);
 
 	if (!__kfence_pool)
 		pr_err("failed to allocate pool\n");