linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: kfence: export kfence_enabled as global variables
@ 2023-02-07  6:15 Zhenhua Huang
  2023-02-07  7:19 ` Marco Elver
  0 siblings, 1 reply; 7+ messages in thread
From: Zhenhua Huang @ 2023-02-07  6:15 UTC (permalink / raw)
  To: catalin.marinas, will, glider, elver, akpm
  Cc: Zhenhua Huang, linux-arm-kernel, linux-mm

Export the variable to ease the judgement of whether kfence enabled
at runtime. It should be more precise than through kernel config
"CONFIG_KFENCE".

For example We can disable kfence at runtime using bootargs
"kfence.sample_interval=0" but CONFIG_KFENCE enabled.
It was false positive.

Signed-off-by: Zhenhua Huang <quic_zhenhuah@quicinc.com>
---
 arch/arm64/mm/pageattr.c | 3 ++-
 include/linux/kfence.h   | 2 ++
 mm/kfence/core.c         | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
index 79dd201..208d780 100644
--- a/arch/arm64/mm/pageattr.c
+++ b/arch/arm64/mm/pageattr.c
@@ -7,6 +7,7 @@
 #include <linux/module.h>
 #include <linux/sched.h>
 #include <linux/vmalloc.h>
+#include <linux/kfence.h>
 
 #include <asm/cacheflush.h>
 #include <asm/set_memory.h>
@@ -27,7 +28,7 @@ bool can_set_direct_map(void)
 	 * protect/unprotect single pages.
 	 */
 	return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() ||
-		IS_ENABLED(CONFIG_KFENCE);
+		kfence_enabled;
 }
 
 static int change_page_range(pte_t *ptep, unsigned long addr, void *data)
diff --git a/include/linux/kfence.h b/include/linux/kfence.h
index 726857a..9fac824 100644
--- a/include/linux/kfence.h
+++ b/include/linux/kfence.h
@@ -26,6 +26,7 @@ extern unsigned long kfence_sample_interval;
  */
 #define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
 extern char *__kfence_pool;
+extern bool kfence_enabled;
 
 DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
 extern atomic_t kfence_allocation_gate;
@@ -222,6 +223,7 @@ bool __kfence_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *sla
 
 #else /* CONFIG_KFENCE */
 
+#define kfence_enabled 0
 static inline bool is_kfence_address(const void *addr) { return false; }
 static inline void kfence_alloc_pool(void) { }
 static inline void kfence_init(void) { }
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 5349c37..e5bf93d 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -48,7 +48,7 @@
 
 /* === Data ================================================================= */
 
-static bool kfence_enabled __read_mostly;
+bool kfence_enabled __read_mostly;
 static bool disabled_by_warn __read_mostly;
 
 unsigned long kfence_sample_interval __read_mostly = CONFIG_KFENCE_SAMPLE_INTERVAL;
-- 
2.7.4


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm: kfence: export kfence_enabled as global variables
  2023-02-07  6:15 [PATCH] mm: kfence: export kfence_enabled as global variables Zhenhua Huang
@ 2023-02-07  7:19 ` Marco Elver
  2023-02-07  7:46   ` Zhenhua Huang
  0 siblings, 1 reply; 7+ messages in thread
From: Marco Elver @ 2023-02-07  7:19 UTC (permalink / raw)
  To: Zhenhua Huang
  Cc: catalin.marinas, will, glider, akpm, linux-arm-kernel, linux-mm,
	Mark Rutland

On Tue, 7 Feb 2023 at 07:15, Zhenhua Huang <quic_zhenhuah@quicinc.com> wrote:
>
> Export the variable to ease the judgement of whether kfence enabled
> at runtime. It should be more precise than through kernel config
> "CONFIG_KFENCE".
>
> For example We can disable kfence at runtime using bootargs
> "kfence.sample_interval=0" but CONFIG_KFENCE enabled.
> It was false positive.
>
> Signed-off-by: Zhenhua Huang <quic_zhenhuah@quicinc.com>
> ---
>  arch/arm64/mm/pageattr.c | 3 ++-
>  include/linux/kfence.h   | 2 ++
>  mm/kfence/core.c         | 2 +-
>  3 files changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
> index 79dd201..208d780 100644
> --- a/arch/arm64/mm/pageattr.c
> +++ b/arch/arm64/mm/pageattr.c
> @@ -7,6 +7,7 @@
>  #include <linux/module.h>
>  #include <linux/sched.h>
>  #include <linux/vmalloc.h>
> +#include <linux/kfence.h>
>
>  #include <asm/cacheflush.h>
>  #include <asm/set_memory.h>
> @@ -27,7 +28,7 @@ bool can_set_direct_map(void)
>          * protect/unprotect single pages.
>          */
>         return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() ||
> -               IS_ENABLED(CONFIG_KFENCE);
> +               kfence_enabled;

Unfortunately this won't work, because it's possible to enable KFENCE
after the kernel has booted with e.g.: echo 100 >
/sys/module/kfence/parameters/sample_interval

What is the problem you have encountered? Is the page-granular direct
map causing issues?

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm: kfence: export kfence_enabled as global variables
  2023-02-07  7:19 ` Marco Elver
@ 2023-02-07  7:46   ` Zhenhua Huang
  2023-02-07 10:28     ` Alexander Potapenko
  2023-02-07 10:48     ` Mark Rutland
  0 siblings, 2 replies; 7+ messages in thread
From: Zhenhua Huang @ 2023-02-07  7:46 UTC (permalink / raw)
  To: Marco Elver
  Cc: catalin.marinas, will, glider, akpm, linux-arm-kernel, linux-mm,
	Mark Rutland

Thanks Marco!

On 2023/2/7 15:19, Marco Elver wrote:
> On Tue, 7 Feb 2023 at 07:15, Zhenhua Huang <quic_zhenhuah@quicinc.com> wrote:
>>
>> Export the variable to ease the judgement of whether kfence enabled
>> at runtime. It should be more precise than through kernel config
>> "CONFIG_KFENCE".
>>
>> For example We can disable kfence at runtime using bootargs
>> "kfence.sample_interval=0" but CONFIG_KFENCE enabled.
>> It was false positive.
>>
>> Signed-off-by: Zhenhua Huang <quic_zhenhuah@quicinc.com>
>> ---
>>   arch/arm64/mm/pageattr.c | 3 ++-
>>   include/linux/kfence.h   | 2 ++
>>   mm/kfence/core.c         | 2 +-
>>   3 files changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
>> index 79dd201..208d780 100644
>> --- a/arch/arm64/mm/pageattr.c
>> +++ b/arch/arm64/mm/pageattr.c
>> @@ -7,6 +7,7 @@
>>   #include <linux/module.h>
>>   #include <linux/sched.h>
>>   #include <linux/vmalloc.h>
>> +#include <linux/kfence.h>
>>
>>   #include <asm/cacheflush.h>
>>   #include <asm/set_memory.h>
>> @@ -27,7 +28,7 @@ bool can_set_direct_map(void)
>>           * protect/unprotect single pages.
>>           */
>>          return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() ||
>> -               IS_ENABLED(CONFIG_KFENCE);
>> +               kfence_enabled;
> 
> Unfortunately this won't work, because it's possible to enable KFENCE
> after the kernel has booted with e.g.: echo 100 >
> /sys/module/kfence/parameters/sample_interval
Yeah, got it. Thanks for catching it.
> 
> What is the problem you have encountered? Is the page-granular direct
> map causing issues?
We're working on a low memory target, page-granular mapping costed more 
(2M per 1GB) memory. Due to GKI constraints, it is not easy to disable 
CONFIG_KFENCE. So my intention was to move the judgement to runtime 
configurable w/ CONFIG_KFENCE on...

Do you have any further suggestion/proposal on this? Many Thanks!

Thanks,
Zhenhua

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm: kfence: export kfence_enabled as global variables
  2023-02-07  7:46   ` Zhenhua Huang
@ 2023-02-07 10:28     ` Alexander Potapenko
  2023-02-07 10:42       ` Zhenhua Huang
  2023-02-07 10:48     ` Mark Rutland
  1 sibling, 1 reply; 7+ messages in thread
From: Alexander Potapenko @ 2023-02-07 10:28 UTC (permalink / raw)
  To: Zhenhua Huang
  Cc: Marco Elver, catalin.marinas, will, akpm, linux-arm-kernel,
	linux-mm, Mark Rutland

> >
> > What is the problem you have encountered? Is the page-granular direct
> > map causing issues?
> We're working on a low memory target, page-granular mapping costed more
> (2M per 1GB) memory. Due to GKI constraints, it is not easy to disable
> CONFIG_KFENCE. So my intention was to move the judgement to runtime
> configurable w/ CONFIG_KFENCE on...
>
> Do you have any further suggestion/proposal on this? Many Thanks!

Right now CONFIG_KFENCE allocates 512Kb for the GKI kernel (only 63
objects instead of the default 255):
https://cs.android.com/android/kernel/superproject/+/common-android-mainline:common/arch/arm64/configs/gki_defconfig;l=686?q=CONFIG_KFENCE_NUM_OBJECTS
Where do 2M come from?

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm: kfence: export kfence_enabled as global variables
  2023-02-07 10:28     ` Alexander Potapenko
@ 2023-02-07 10:42       ` Zhenhua Huang
  0 siblings, 0 replies; 7+ messages in thread
From: Zhenhua Huang @ 2023-02-07 10:42 UTC (permalink / raw)
  To: Alexander Potapenko
  Cc: Marco Elver, catalin.marinas, will, akpm, linux-arm-kernel,
	linux-mm, Mark Rutland



On 2023/2/7 18:28, Alexander Potapenko wrote:
>>>
>>> What is the problem you have encountered? Is the page-granular direct
>>> map causing issues?
>> We're working on a low memory target, page-granular mapping costed more
>> (2M per 1GB) memory. Due to GKI constraints, it is not easy to disable
>> CONFIG_KFENCE. So my intention was to move the judgement to runtime
>> configurable w/ CONFIG_KFENCE on...
>>
>> Do you have any further suggestion/proposal on this? Many Thanks!
> 
> Right now CONFIG_KFENCE allocates 512Kb for the GKI kernel (only 63
> objects instead of the default 255):
> https://cs.android.com/android/kernel/superproject/+/common-android-mainline:common/arch/arm64/configs/gki_defconfig;l=686?q=CONFIG_KFENCE_NUM_OBJECTS
> Where do 2M come from?
Hi Alexander,
It is not from KFENCE cost itself, but from the requirement it needs 
page-granular mapping.
2M per 1GB cost is from linear mapping for page granularity. You can see 
can_set_direct_map() is used for whether allowing section mapping or not.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm: kfence: export kfence_enabled as global variables
  2023-02-07  7:46   ` Zhenhua Huang
  2023-02-07 10:28     ` Alexander Potapenko
@ 2023-02-07 10:48     ` Mark Rutland
  2023-02-07 12:35       ` Zhenhua Huang
  1 sibling, 1 reply; 7+ messages in thread
From: Mark Rutland @ 2023-02-07 10:48 UTC (permalink / raw)
  To: Zhenhua Huang
  Cc: Marco Elver, catalin.marinas, will, glider, akpm,
	linux-arm-kernel, linux-mm

On Tue, Feb 07, 2023 at 03:46:53PM +0800, Zhenhua Huang wrote:
> Thanks Marco!
> 
> On 2023/2/7 15:19, Marco Elver wrote:
> > On Tue, 7 Feb 2023 at 07:15, Zhenhua Huang <quic_zhenhuah@quicinc.com> wrote:
> > > 
> > > Export the variable to ease the judgement of whether kfence enabled
> > > at runtime. It should be more precise than through kernel config
> > > "CONFIG_KFENCE".
> > > 
> > > For example We can disable kfence at runtime using bootargs
> > > "kfence.sample_interval=0" but CONFIG_KFENCE enabled.
> > > It was false positive.
> > > 
> > > Signed-off-by: Zhenhua Huang <quic_zhenhuah@quicinc.com>
> > > ---
> > >   arch/arm64/mm/pageattr.c | 3 ++-
> > >   include/linux/kfence.h   | 2 ++
> > >   mm/kfence/core.c         | 2 +-
> > >   3 files changed, 5 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
> > > index 79dd201..208d780 100644
> > > --- a/arch/arm64/mm/pageattr.c
> > > +++ b/arch/arm64/mm/pageattr.c
> > > @@ -7,6 +7,7 @@
> > >   #include <linux/module.h>
> > >   #include <linux/sched.h>
> > >   #include <linux/vmalloc.h>
> > > +#include <linux/kfence.h>
> > > 
> > >   #include <asm/cacheflush.h>
> > >   #include <asm/set_memory.h>
> > > @@ -27,7 +28,7 @@ bool can_set_direct_map(void)
> > >           * protect/unprotect single pages.
> > >           */
> > >          return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() ||
> > > -               IS_ENABLED(CONFIG_KFENCE);
> > > +               kfence_enabled;
> > 
> > Unfortunately this won't work, because it's possible to enable KFENCE
> > after the kernel has booted with e.g.: echo 100 >
> > /sys/module/kfence/parameters/sample_interval
> Yeah, got it. Thanks for catching it.
> > 
> > What is the problem you have encountered? Is the page-granular direct
> > map causing issues?
> We're working on a low memory target, page-granular mapping costed more (2M
> per 1GB) memory. Due to GKI constraints, it is not easy to disable
> CONFIG_KFENCE. So my intention was to move the judgement to runtime
> configurable w/ CONFIG_KFENCE on...
> 
> Do you have any further suggestion/proposal on this? Many Thanks!

Just to check, the cost is because we're mapping *all* of memory at page
granulatrity, right? If we were to just map the KFENCE region a page
granularity, would that be a sufficient saving? 

We didn't do that so far because it was simpler to just map everything at page
granularity (and that's also required by rodata_full, which I though android
used?).

If it's really important (and rodata_full isn't being used), we could try to do
that.

To do that we'd need to choose the KFENCE region *before* arm64 creates the
fine-grain translation tables, which probable needs an arch_ hook.

Thanks,
Mark.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm: kfence: export kfence_enabled as global variables
  2023-02-07 10:48     ` Mark Rutland
@ 2023-02-07 12:35       ` Zhenhua Huang
  0 siblings, 0 replies; 7+ messages in thread
From: Zhenhua Huang @ 2023-02-07 12:35 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Marco Elver, catalin.marinas, will, glider, akpm,
	linux-arm-kernel, linux-mm

Thanks Mark!

On 2023/2/7 18:48, Mark Rutland wrote:
> On Tue, Feb 07, 2023 at 03:46:53PM +0800, Zhenhua Huang wrote:
>> Thanks Marco!
>>
>> On 2023/2/7 15:19, Marco Elver wrote:
>>> On Tue, 7 Feb 2023 at 07:15, Zhenhua Huang <quic_zhenhuah@quicinc.com> wrote:
>>>>
>>>> Export the variable to ease the judgement of whether kfence enabled
>>>> at runtime. It should be more precise than through kernel config
>>>> "CONFIG_KFENCE".
>>>>
>>>> For example We can disable kfence at runtime using bootargs
>>>> "kfence.sample_interval=0" but CONFIG_KFENCE enabled.
>>>> It was false positive.
>>>>
>>>> Signed-off-by: Zhenhua Huang <quic_zhenhuah@quicinc.com>
>>>> ---
>>>>    arch/arm64/mm/pageattr.c | 3 ++-
>>>>    include/linux/kfence.h   | 2 ++
>>>>    mm/kfence/core.c         | 2 +-
>>>>    3 files changed, 5 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
>>>> index 79dd201..208d780 100644
>>>> --- a/arch/arm64/mm/pageattr.c
>>>> +++ b/arch/arm64/mm/pageattr.c
>>>> @@ -7,6 +7,7 @@
>>>>    #include <linux/module.h>
>>>>    #include <linux/sched.h>
>>>>    #include <linux/vmalloc.h>
>>>> +#include <linux/kfence.h>
>>>>
>>>>    #include <asm/cacheflush.h>
>>>>    #include <asm/set_memory.h>
>>>> @@ -27,7 +28,7 @@ bool can_set_direct_map(void)
>>>>            * protect/unprotect single pages.
>>>>            */
>>>>           return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() ||
>>>> -               IS_ENABLED(CONFIG_KFENCE);
>>>> +               kfence_enabled;
>>>
>>> Unfortunately this won't work, because it's possible to enable KFENCE
>>> after the kernel has booted with e.g.: echo 100 >
>>> /sys/module/kfence/parameters/sample_interval
>> Yeah, got it. Thanks for catching it.
>>>
>>> What is the problem you have encountered? Is the page-granular direct
>>> map causing issues?
>> We're working on a low memory target, page-granular mapping costed more (2M
>> per 1GB) memory. Due to GKI constraints, it is not easy to disable
>> CONFIG_KFENCE. So my intention was to move the judgement to runtime
>> configurable w/ CONFIG_KFENCE on...
>>
>> Do you have any further suggestion/proposal on this? Many Thanks!
> 
> Just to check, the cost is because we're mapping *all* of memory at page
> granulatrity, right? If we were to just map the KFENCE region a page
> granularity, would that be a sufficient saving?
Yes,that's expected saving which I would like to address here.
> 
> We didn't do that so far because it was simpler to just map everything at page
> granularity (and that's also required by rodata_full, which I though android
> used?).
Yes, by default we're setting rodata_full. While for low-ram target, we 
also can lower the security requirement. This is why I am pursuing..
BTW, rodata_full can be easily runtime configured through bootargs.
> 
> If it's really important (and rodata_full isn't being used), we could try to do
> that.
> 
> To do that we'd need to choose the KFENCE region *before* arm64 creates the
> fine-grain translation tables, which probable needs an arch_ hook.
It sounds a good solution. Let me also dig into this. Many Thanks!
> 
> Thanks,
> Mark.
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-02-07 12:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-07  6:15 [PATCH] mm: kfence: export kfence_enabled as global variables Zhenhua Huang
2023-02-07  7:19 ` Marco Elver
2023-02-07  7:46   ` Zhenhua Huang
2023-02-07 10:28     ` Alexander Potapenko
2023-02-07 10:42       ` Zhenhua Huang
2023-02-07 10:48     ` Mark Rutland
2023-02-07 12:35       ` Zhenhua Huang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).