RFC: add init_on_alloc/init_on_free boot options
Subsystems (14)
cover RFC: add init_on_alloc/init_on_free boot options
Provide init_on_alloc and init_on_free boot options.
These are aimed at preventing possible information leaks and making the control-flow bugs that depend on uninitialized values more deterministic.
Enabling either of the options guarantees that the memory returned by the page allocator and SL[AOU]B is initialized with zeroes.
Enabling init_on_free also guarantees that pages and heap objects are initialized right after they're freed, so it won't be possible to access stale data by using a dangling pointer.
Alexander Potapenko (4):
mm: security: introduce init_on_alloc=1 and init_on_free=1 boot
options
lib: introduce test_meminit module
gfp: mm: introduce __GFP_NOINIT
net: apply __GFP_NOINIT to AF_UNIX sk_buff allocations
.../admin-guide/kernel-parameters.txt | 8 + drivers/infiniband/core/uverbs_ioctl.c | 2 +- include/linux/gfp.h | 6 +- include/linux/mm.h | 22 ++ include/net/sock.h | 5 + kernel/kexec_core.c | 4 +- lib/Kconfig.debug | 8 + lib/Makefile | 1 + lib/test_meminit.c | 205 ++++++++++++++++++ mm/dmapool.c | 2 +- mm/page_alloc.c | 62 +++++- mm/slab.c | 18 +- mm/slab.h | 16 ++ mm/slob.c | 23 +- mm/slub.c | 28 ++- net/core/sock.c | 31 ++- net/unix/af_unix.c | 13 +- security/Kconfig.hardening | 16 ++ 18 files changed, 439 insertions(+), 31 deletions(-) create mode 100644 lib/test_meminit.c
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 1/4 mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options
The new options are needed to prevent possible information leaks and make control-flow bugs that depend on uninitialized values more deterministic.
init_on_alloc=1 makes the kernel initialize newly allocated pages and heap objects with zeroes. Initialization is done at allocation time at the places where checks for __GFP_ZERO are performed.
init_on_free=1 makes the kernel initialize freed pages and heap objects with zeroes upon their deletion. This helps to ensure sensitive data doesn't leak via use-after-free accesses.
Both init_on_alloc=1 and init_on_free=1 guarantee that the allocator returns zeroed memory. The only exception is slab caches with constructors. Those are never zero-initialized to preserve their semantics.
For SLOB allocator init_on_free=1 also implies init_on_alloc=1 behavior, i.e. objects are zeroed at both allocation and deallocation time. This is done because SLOB may otherwise return multiple freelist pointers in the allocated object. For SLAB and SLUB enabling either init_on_alloc or init_on_free leads to one-time initialization of the object.
Both init_on_alloc and init_on_free default to zero, but those defaults can be overridden with CONFIG_INIT_ON_ALLOC_DEFAULT_ON and CONFIG_INIT_ON_FREE_DEFAULT_ON.
Slowdown for the new features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +7.62% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.75% sys time (st.err 2.14%)
Linux build with -j12, init_on_free=1: +8.38% wall time (st.err 0.39%) Linux build with -j12, init_on_free=1: +24.42% sys time (st.err 0.52%) Linux build with -j12, init_on_alloc=1: -0.13% wall time (st.err 0.42%) Linux build with -j12, init_on_alloc=1: +0.57% sys time (st.err 0.40%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
--- .../admin-guide/kernel-parameters.txt | 8 +++ drivers/infiniband/core/uverbs_ioctl.c | 2 +- include/linux/mm.h | 22 +++++++ kernel/kexec_core.c | 2 +- mm/dmapool.c | 2 +- mm/page_alloc.c | 62 +++++++++++++++++-- mm/slab.c | 16 ++++- mm/slab.h | 16 +++++ mm/slob.c | 22 ++++++- mm/slub.c | 27 ++++++-- net/core/sock.c | 2 +- security/Kconfig.hardening | 16 +++++ 12 files changed, 179 insertions(+), 18 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
@@ -1671,6 +1671,14 @@ initrd= [BOOT] Specify the location of the initial ramdisk + init_on_alloc= [MM] Fill newly allocated pages and heap objects with + zeroes. + Format: 0 | 1 + Default set by CONFIG_INIT_ON_ALLOC_DEFAULT_ON. + init_on_free= [MM] Fill freed pages and heap objects with zeroes. + Format: 0 | 1 + Default set by CONFIG_INIT_ON_FREE_DEFAULT_ON. + init_pkru= [x86] Specify the default memory protection keys rights register contents for all processes. 0x55555554 by default (disallow access to all but pkey 0). Can
drivers/infiniband/core/uverbs_ioctl.c +1 -1
@@ -127,7 +127,7 @@ __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, res = (void *)pbundle->internal_buffer + pbundle->internal_used; pbundle->internal_used =ALIGN (new_used,sizeof (*pbundle->internal_buffer)); -if (flags & __GFP_ZERO) +if (want_init_on_alloc (flags))memset (res,0 , size);return res; }
include/linux/mm.h +22 -0
@@ -2610,6 +2610,28 @@ static inline void kernel_poison_pages(struct page *page, int numpages, int enable) { }#endif +#ifdef CONFIG_INIT_ON_ALLOC_DEFAULT_ON +DECLARE_STATIC_KEY_TRUE (init_on_alloc); +#else +DECLARE_STATIC_KEY_FALSE (init_on_alloc); +#endif +static inline bool want_init_on_alloc (gfp_t flags) +{ +if (static_branch_unlikely (&init_on_alloc)) +return true; +return flags & __GFP_ZERO; +} + +#ifdef CONFIG_INIT_ON_FREE_DEFAULT_ON +DECLARE_STATIC_KEY_TRUE (init_on_free); +#else +DECLARE_STATIC_KEY_FALSE (init_on_free); +#endif +static inline bool want_init_on_free (void ) +{ +return static_branch_unlikely (&init_on_free); +} +#ifdef CONFIG_DEBUG_PAGEALLOC extern bool _debug_pagealloc_enabled;extern void __kernel_map_pages (struct page *page,int numpages,int enable);
kernel/kexec_core.c +1 -1
@@ -315,7 +315,7 @@ static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)arch_kexec_post_alloc_pages (page_address (pages), count, gfp_mask); -if (gfp_mask & __GFP_ZERO) +if (want_init_on_alloc (gfp_mask))for (i =0 ; i < count; i++)clear_highpage (pages + i); }
mm/dmapool.c +1 -1
@@ -381,7 +381,7 @@ void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,#endif spin_unlock_irqrestore (&pool->lock, flags); -if (mem_flags & __GFP_ZERO) +if (want_init_on_alloc (mem_flags))memset (retval,0 , pool->size);return retval;
mm/page_alloc.c +58 -4
99 lines not on this page · raw message
mm/slab.c +13 -3
@@ -1891,6 +1891,14 @@ static bool set_objfreelist_slab_cache(struct kmem_cache *cachep, cachep->num =0 ; +/* +* If slab auto-initialization on free is enabled, store the freelist +* off-slab, so that its contents don't end up in one of the allocated +* objects. +*/ +if (unlikely (slab_want_init_on_free (cachep))) +return false; +if (cachep->ctor || flags &SLAB_TYPESAFE_BY_RCU )return false;
@@ -3330,7 +3338,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,local_irq_restore (save_flags); ptr =cache_alloc_debugcheck_after (cachep, flags, ptr, caller); -if (unlikely (flags & __GFP_ZERO) && ptr) +if (unlikely (slab_want_init_on_alloc (flags, cachep)) && ptr)memset (ptr,0 , cachep->object_size);slab_post_alloc_hook (cachep, flags,1 , &ptr);
@@ -3387,7 +3395,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller) objp =cache_alloc_debugcheck_after (cachep, flags, objp, caller);prefetchw (objp); -if (unlikely (flags & __GFP_ZERO) && objp) +if (unlikely (slab_want_init_on_alloc (flags, cachep)) && objp)memset (objp,0 , cachep->object_size);slab_post_alloc_hook (cachep, flags,1 , &objp);
@@ -3508,6 +3516,8 @@ void ___cache_free(struct kmem_cache *cachep, void *objp,struct array_cache *ac =cpu_cache_get (cachep);check_irq_off (); +if (unlikely (slab_want_init_on_free (cachep))) +memset (objp,0 , cachep->object_size);kmemleak_free_recursive (objp, cachep->flags); objp =cache_free_debugcheck (cachep, objp, caller);
@@ -3595,7 +3605,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,cache_alloc_debugcheck_after_bulk (s, flags, size, p, _RET_IP_);/* Clear memory outside IRQ disabled section */ -if (unlikely (flags & __GFP_ZERO)) +if (unlikely (slab_want_init_on_alloc (flags, s)))for (i =0 ; i < size; i++)memset (p[i],0 , s->object_size);
mm/slab.h +16 -0
@@ -524,4 +524,20 @@ static inline int cache_random_seq_create(struct kmem_cache *cachep,static inline void cache_random_seq_destroy (struct kmem_cache *cachep) { }#endif /* CONFIG_SLAB_FREELIST_RANDOM */ +static inline bool slab_want_init_on_alloc (gfp_t flags,struct kmem_cache *c) +{ +if (static_branch_unlikely (&init_on_alloc)) +return !(c->ctor); +else +return flags & __GFP_ZERO; +} + +static inline bool slab_want_init_on_free (struct kmem_cache *c) +{ +if (static_branch_unlikely (&init_on_free)) +return !(c->ctor); +else +return false; +} +#endif /* MM_SLAB_H */
mm/slob.c +20 -2
@@ -212,6 +212,19 @@ static void slob_free_pages(void *b, int order)free_pages ((unsigned long )b, order); } +/* +* init_on_free=1 also implies initialization at allocation time. +* This is because newly allocated objects may contain freelist pointers +* somewhere in the middle. +*/ +static inline bool slob_want_init_on_alloc (gfp_t flags,struct kmem_cache *c) +{ +if (static_branch_unlikely (&init_on_alloc) || +static_branch_unlikely (&init_on_free)) +return c ? (!c->ctor) : true; +return flags & __GFP_ZERO; +} +/* * Allocate a slob block within a given slob_page sp. */
@@ -330,8 +343,6 @@ static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)BUG_ON (!b);spin_unlock_irqrestore (&slob_lock, flags); } -if (unlikely (gfp & __GFP_ZERO)) -memset (b,0 , size);return b; }
@@ -366,6 +377,9 @@ static void slob_free(void *block, int size)return ; } +if (unlikely (want_init_on_free ())) +memset (block,0 , size); +if (!slob_page_free (sp)) {/* This slob page is about to become partially free. Easy! */ sp->units = units;
@@ -461,6 +475,8 @@ __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller) }kmemleak_alloc (ret, size,1 , gfp); +if (unlikely (slob_want_init_on_alloc (gfp,0 ))) +memset (ret,0 , size);return ret; }
@@ -559,6 +575,8 @@ static void *slob_alloc_node(struct kmem_cache *c, gfp_t flags, int node)WARN_ON_ONCE (flags & __GFP_ZERO); c->ctor (b); } +if (unlikely (slob_want_init_on_alloc (flags, c))) +memset (b,0 , c->size);kmemleak_alloc_recursive (b, c->size,1 , c->flags, flags);return b;
mm/slub.c +22 -5
@@ -1432,6 +1432,19 @@ static __always_inline bool slab_free_hook(struct kmem_cache *s, void *x)static inline bool slab_free_freelist_hook (struct kmem_cache *s,void **head,void **tail) { + +void *object; +void *next = *head; +void *old_tail = *tail ? *tail : *head; + +if (slab_want_init_on_free (s)) +do { + object = next; + next =get_freepointer (s, object); +memset (object,0 , s->size); +set_freepointer (s, object, next); + }while (object != old_tail); +/* * Compiler cannot detect this function can be removed if slab_free_hook() * evaluates to nothing. Thus, catch all relevant config debug options here.
@@ -1441,9 +1454,7 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s,defined (CONFIG_DEBUG_OBJECTS_FREE ) || \defined (CONFIG_KASAN ) - void *object; -void *next = *head; -void *old_tail = *tail ? *tail : *head; + next = *head;/* Head and tail of the reconstructed freelist */ *head =NULL ;
@@ -2749,8 +2760,14 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,prefetch_freepointer (s, next_object);stat (s,ALLOC_FASTPATH ); } +/* +* If the object has been wiped upon free, make sure it's fully +* initialized by zeroing out freelist pointer. +*/ +if (slab_want_init_on_free (s)) + *(void **)object =0 ; -if (unlikely (gfpflags & __GFP_ZERO) && object) +if (unlikely (slab_want_init_on_alloc (gfpflags, s)) && object)memset (object,0 , s->object_size);slab_post_alloc_hook (s, gfpflags,1 , &object);
@@ -3172,7 +3189,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,local_irq_enable ();/* Clear memory outside IRQ disabled fastpath loop */ -if (unlikely (flags & __GFP_ZERO)) { +if (unlikely (slab_want_init_on_alloc (flags, s))) {int j;for (j =0 ; j < i; j++)
net/core/sock.c +1 -1
@@ -1601,7 +1601,7 @@ static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority, sk =kmem_cache_alloc (slab, priority & ~__GFP_ZERO);if (!sk)return sk; -if (priority & __GFP_ZERO) +if (want_init_on_alloc (priority))sk_prot_clear_nulls (sk, prot->obj_size); }else sk =kmalloc (prot->obj_size, priority);
security/Kconfig.hardening +16 -0
@@ -159,6 +159,22 @@ config STACKLEAK_RUNTIME_DISABLE runtime to control kernel stack erasing for kernels built with CONFIG_GCC_PLUGIN_STACKLEAK. +config INIT_ON_ALLOC_DEFAULT_ON + bool "Set init_on_alloc=1 by default" + default false + help + Enable init_on_alloc=1 by default, making the kernel initialize every + page and heap allocation with zeroes. + init_on_alloc can be overridden via command line. + +config INIT_ON_FREE_DEFAULT_ON + bool "Set init_on_free=1 by default" + default false + help + Enable init_on_free=1 by default, making the kernel initialize freed + pages and slab memory with zeroes. + init_on_free can be overridden via command line. + endmenu endmenu
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 2/4 lib: introduce test_meminit module
In reply to Alexander PotapenkoAdd tests for heap and pagealloc initialization.
These can be used to check init_on_alloc and init_on_free implementations as well as other approaches to initialization.
--- lib/Kconfig.debug | 8 ++ lib/Makefile | 1 + lib/test_meminit.c | 205 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 lib/test_meminit.c
lib/Kconfig.debug +8 -0
@@ -2010,6 +2010,14 @@ config TEST_STACKINIT If unsure, say N. +config TEST_MEMINIT + tristate "Test level of heap/page initialization" + help + Test if the kernel is zero-initializing heap and page allocations. + This can be useful to test init_on_alloc and init_on_free features. + + If unsure, say N. + endif # RUNTIME_TESTING_MENU config MEMTEST
lib/Makefile +1 -0
@@ -90,6 +90,7 @@ obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o +obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/
lib/test_meminit.c new file +205 -0
205 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 3/4 gfp: mm: introduce __GFP_NOINIT
In reply to Alexander PotapenkoWhen passed to an allocator (either pagealloc or SL[AOU]B), __GFP_NOINIT tells it to not initialize the requested memory if the init_on_alloc boot option is enabled. This can be useful in the cases newly allocated memory is going to be initialized by the caller right away.
__GFP_NOINIT doesn't affect init_on_free behavior, except for SLOB, where init_on_free implies init_on_alloc.
__GFP_NOINIT basically defeats the hardening against information leaks provided by init_on_alloc, so one should use it with caution.
This patch also adds __GFP_NOINIT to alloc_pages() calls in SL[AOU]B. Doing so is safe, because the heap allocators initialize the pages they receive before passing memory to the callers.
Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +6.84% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.25% sys time (st.err 0.72%)
Linux build with -j12, init_on_free=1: +8.52% wall time (st.err 0.42%) Linux build with -j12, init_on_free=1: +24.31% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.16% wall time (st.err 0.40%) Linux build with -j12, init_on_alloc=1: +1.24% sys time (st.err 0.39%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
--- include/linux/gfp.h | 6 +++++- include/linux/mm.h | 2 +- kernel/kexec_core.c | 2 +- mm/slab.c | 2 +- mm/slob.c | 3 ++- mm/slub.c | 1 + 6 files changed, 11 insertions(+), 5 deletions(-)
include/linux/gfp.h +5 -1
@@ -44,6 +44,7 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#define ___GFP_NOINIT 0x1000000u/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /*
@@ -208,16 +209,19 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_NOINIT requests non-initialized memory from the underlying allocator. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_NOINIT ((__force gfp_t)___GFP_NOINIT)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25)#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
include/linux/mm.h +1 -1
@@ -2618,7 +2618,7 @@ DECLARE_STATIC_KEY_FALSE(init_on_alloc);static inline bool want_init_on_alloc (gfp_t flags) {if (static_branch_unlikely (&init_on_alloc)) -return true; +return !(flags & __GFP_NOINIT);return flags & __GFP_ZERO; }
kernel/kexec_core.c +1 -1
@@ -302,7 +302,7 @@ static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order) {struct page *pages; - pages =alloc_pages (gfp_mask & ~__GFP_ZERO, order); + pages =alloc_pages ((gfp_mask & ~__GFP_ZERO) | __GFP_NOINIT, order);if (pages) {unsigned int count, i;
mm/slab.c +1 -1
@@ -1393,7 +1393,7 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,struct page *page;int nr_pages; - flags |= cachep->allocflags; + flags |= (cachep->allocflags | __GFP_NOINIT); page =__alloc_pages_node (nodeid, flags, cachep->gfporder);if (!page) {
mm/slob.c +2 -1
@@ -192,6 +192,7 @@ static void *slob_new_pages(gfp_t gfp, int order, int node) {void *page; + gfp |= __GFP_NOINIT;#ifdef CONFIG_NUMA if (node !=NUMA_NO_NODE ) page =__alloc_pages_node (node, gfp, order);
@@ -221,7 +222,7 @@ static inline bool slob_want_init_on_alloc(gfp_t flags, struct kmem_cache *c) {if (static_branch_unlikely (&init_on_alloc) ||static_branch_unlikely (&init_on_free)) -return c ? (!c->ctor) : true; +return c ? (!c->ctor) : !(flags & __GFP_NOINIT);return flags & __GFP_ZERO; }
mm/slub.c +1 -0
@@ -1504,6 +1504,7 @@ static inline struct page *alloc_slab_page(struct kmem_cache *s,struct page *page;unsigned int order =oo_order (oo); + flags |= __GFP_NOINIT;if (node ==NUMA_NO_NODE ) page =alloc_pages (flags, order);else
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 4/4 net: apply __GFP_NOINIT to AF_UNIX sk_buff allocations
In reply to Alexander PotapenkoAdd sock_alloc_send_pskb_noinit(), which is similar to sock_alloc_send_pskb(), but allocates with __GFP_NOINIT. This helps reduce the slowdown on hackbench in the init_on_alloc mode from 6.84% to 3.45%.
Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +7.71% sys time (st.err 0.45%) hackbench, init_on_alloc=1: +3.45% sys time (st.err 0.86%)
Linux build with -j12, init_on_free=1: +8.34% wall time (st.err 0.39%) Linux build with -j12, init_on_free=1: +24.13% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.04% wall time (st.err 0.46%) Linux build with -j12, init_on_alloc=1: +0.50% sys time (st.err 0.45%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
--- include/net/sock.h | 5 +++++ net/core/sock.c | 29 +++++++++++++++++++++++++---- net/unix/af_unix.c | 13 +++++++------ 3 files changed, 37 insertions(+), 10 deletions(-)
include/net/sock.h +5 -0
@@ -1612,6 +1612,11 @@ struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,struct sk_buff *sock_alloc_send_pskb (struct sock *sk,unsigned long header_len,unsigned long data_len,int noblock,int *errcode,int max_page_order); +struct sk_buff *sock_alloc_send_pskb_noinit (struct sock *sk, +unsigned long header_len, +unsigned long data_len, +int noblock,int *errcode, +int max_page_order);void *sock_kmalloc (struct sock *sk,int size,gfp_t priority);void sock_kfree_s (struct sock *sk,void *mem,int size);void sock_kzfree_s (struct sock *sk,void *mem,int size);
net/core/sock.c +25 -4
@@ -2187,9 +2187,11 @@ static long sock_wait_for_wmem(struct sock *sk, long timeo)* Generic send/receive buffer handlers */ -struct sk_buff *sock_alloc_send_pskb (struct sock *sk,unsigned long header_len, -unsigned long data_len,int noblock, -int *errcode,int max_page_order) +struct sk_buff *sock_alloc_send_pskb_internal (struct sock *sk, +unsigned long header_len, +unsigned long data_len, +int noblock,int *errcode, +int max_page_order,gfp_t gfp) {struct sk_buff *skb;long timeo;
@@ -2218,7 +2220,7 @@ struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len, timeo =sock_wait_for_wmem (sk, timeo); } skb =alloc_skb_with_frags (header_len, data_len, max_page_order, - errcode, sk->sk_allocation); + errcode, sk->sk_allocation | gfp);if (skb)skb_set_owner_w (skb, sk);return skb;
@@ -2229,8 +2231,27 @@ struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len, *errcode = err;return NULL ; } + +struct sk_buff *sock_alloc_send_pskb (struct sock *sk,unsigned long header_len, +unsigned long data_len,int noblock, +int *errcode,int max_page_order) +{ +return sock_alloc_send_pskb_internal (sk, header_len, data_len, + noblock, errcode, max_page_order,/*gfp*/ 0 ); +}EXPORT_SYMBOL (sock_alloc_send_pskb); +struct sk_buff *sock_alloc_send_pskb_noinit (struct sock *sk, +unsigned long header_len, +unsigned long data_len, +int noblock,int *errcode, +int max_page_order) +{ +return sock_alloc_send_pskb_internal (sk, header_len, data_len, + noblock, errcode, max_page_order,/*gfp*/ __GFP_NOINIT); +} +EXPORT_SYMBOL (sock_alloc_send_pskb_noinit); +struct sk_buff *sock_alloc_send_skb (struct sock *sk,unsigned long size,int noblock,int *errcode) {
net/unix/af_unix.c +7 -6
@@ -1627,9 +1627,9 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,BUILD_BUG_ON (SKB_MAX_ALLOC <PAGE_SIZE ); } - skb =sock_alloc_send_pskb (sk, len - data_len, data_len, - msg->msg_flags &MSG_DONTWAIT , &err, -PAGE_ALLOC_COSTLY_ORDER ); + skb =sock_alloc_send_pskb_noinit (sk, len - data_len, data_len, + msg->msg_flags &MSG_DONTWAIT , &err, +PAGE_ALLOC_COSTLY_ORDER );if (skb ==NULL ) goto out;
@@ -1824,9 +1824,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, data_len =min_t (size_t, size,PAGE_ALIGN (data_len)); - skb =sock_alloc_send_pskb (sk, size - data_len, data_len, - msg->msg_flags &MSG_DONTWAIT , &err, -get_order (UNIX_SKB_FRAGS_SZ )); + skb =sock_alloc_send_pskb_noinit (sk, size - data_len, data_len, + msg->msg_flags &MSG_DONTWAIT , + &err, +get_order (UNIX_SKB_FRAGS_SZ ));if (!skb) goto out_err;
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Re: [PATCH 1/4] mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options
In reply to Alexander PotapenkoQuoted 3 lines · Alexander Potapenko
The new options are needed to prevent possible information leaks and make control-flow bugs that depend on uninitialized values more deterministic.
I like having this available on both alloc and free. This makes it much more configurable for the end users who can adapt to their work loads, etc.
Quoted 3 lines
Linux build with -j12, init_on_free=1: +24.42% sys time (st.err 0.52%) [...]
Linux build with -j12, init_on_alloc=1: +0.57% sys time (st.err 0.40%)
Any idea why there is such a massive difference here? This seems to high just for cache-locality effects of touching all the freed pages.
Signature · 1 line
Kees Cook
Re: [PATCH 3/4] gfp: mm: introduce __GFP_NOINIT
In reply to Alexander PotapenkoQuoted 60 lines · Alexander Potapenko
When passed to an allocator (either pagealloc or SL[AOU]B),
__GFP_NOINITtells it to not initialize the requested memory if theinit_on_allocboot option is enabled. This can be useful in the cases newly allocated memory is going to be initialized by the caller right away.
__GFP_NOINITdoesn't affectinit_on_freebehavior, except for SLOB, whereinit_on_freeimpliesinit_on_alloc.
__GFP_NOINITbasically defeats the hardening against information leaks provided byinit_on_alloc, so one should use it with caution.This patch also adds
__GFP_NOINITtoalloc_pages()calls in SL[AOU]B. Doing so is safe, because the heap allocators initialize the pages they receive before passing memory to the callers.Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +6.84% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.25% sys time (st.err 0.72%)
Linux build with -j12, init_on_free=1: +8.52% wall time (st.err 0.42%) Linux build with -j12, init_on_free=1: +24.31% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.16% wall time (st.err 0.40%) Linux build with -j12, init_on_alloc=1: +1.24% sys time (st.err 0.39%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
Signed-off-by Alexander Potapenko <[email protected]>Cc Andrew Morton <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>Cc Laura Abbott <[email protected]>Cc Randy Dunlap <[email protected]>Cc Jann Horn <[email protected]>Cc Mark Rutland <[email protected]>--- include/linux/gfp.h | 6 +++++- include/linux/mm.h | 2 +- kernel/kexec_core.c | 2 +- mm/slab.c | 2 +- mm/slob.c | 3 ++- mm/slub.c | 1 + 6 files changed, 11 insertions(+), 5 deletions(-)include/linux/gfp.h +0
-0
include/linux/gfp.h struct vm_area_struct; · line 47
Quoted 4 lines
include/linux/gfp.h +1
-0@@ -44,6 +44,7 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#define ___GFP_NOINIT 0x1000000u
I mentioned this in the other patch, but I think this needs to be moved ahead of GFP_NOLOCKDEP and adjust the values for GFP_NOLOCKDEP and to leave the IS_ENABLED() test in __GFP_BITS_SHIFT alone.
Quoted 11 lines
include/linux/gfp.h +2
-0/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -208,16 +209,19 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_NOINIT requests non-initialized memory from the underlying allocator. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)
include/linux/gfp.h struct vm_area_struct; · line 224
Quoted 10 lines
include/linux/gfp.h +2
-1#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_NOINIT ((__force gfp_t)___GFP_NOINIT)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25)
AIUI, this will break non-CONFIG_LOCKDEP kernels: it should just be:
-#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))
Quoted 8 lines
include/linux/gfp.h +0
-0#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/** include/linux/mm.h +0
-0
include/linux/mm.h DECLARE_STATIC_KEY_FALSE(init_on_alloc); · line 2622
Quoted 6 lines
include/linux/mm.h +1
-1@@ -2618,7 +2618,7 @@ DECLARE_STATIC_KEY_FALSE(init_on_alloc);static inline bool want_init_on_alloc (gfp_t flags) {if (static_branch_unlikely (&init_on_alloc)) -return true; +return !(flags & __GFP_NOINIT);return flags & __GFP_ZERO;
What do you think about renaming __GFP_NOINIT to __GFP_NO_AUTOINIT or something?
Regardless, yes, this is nice.
Signature · 1 line
Kees Cook
Randy Dunlap
reviewerRe: [PATCH 1/4] mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options
In reply to Alexander Potapenkosecurity/Kconfig.hardening config STACKLEAK_RUNTIME_DISABLE · line 164
Quoted 11 lines · Alexander Potapenko
security/Kconfig.hardening +3
-0@@ -159,6 +159,22 @@ config STACKLEAK_RUNTIME_DISABLE runtime to control kernel stack erasing for kernels built with CONFIG_GCC_PLUGIN_STACKLEAK. +config INIT_ON_ALLOC_DEFAULT_ON + bool "Set init_on_alloc=1 by default" + default false
That should be spelled "default n" but since that is already the default, just omit the line completely.
security/Kconfig.hardening config STACKLEAK_RUNTIME_DISABLE · line 172
Quoted 8 lines
security/Kconfig.hardening +8
-0+ help + Enable init_on_alloc=1 by default, making the kernel initialize every + page and heap allocation with zeroes. + init_on_alloc can be overridden via command line. + +config INIT_ON_FREE_DEFAULT_ON + bool "Set init_on_free=1 by default" + default false
ditto.
Quoted 8 lines
security/Kconfig.hardening +5
-0+ help + Enable init_on_free=1 by default, making the kernel initialize freed + pages and slab memory with zeroes. + init_on_free can be overridden via command line. + endmenu endmenu
Signature · 1 line
~Randy
Re: [PATCH 3/4] gfp: mm: introduce __GFP_NOINIT
In reply to Kees CookFrom: Kees Cook <[email protected]>
Date: Wed, May 8, 2019 at 9:16 PM
To: Alexander Potapenko
Cc: Andrew Morton, Christoph Lameter, Kees Cook, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark Rutland
Quoted 56 lines · Alexander Potapenko
Quoted 55 lines
When passed to an allocator (either pagealloc or SL[AOU]B),
__GFP_NOINITtells it to not initialize the requested memory if theinit_on_allocboot option is enabled. This can be useful in the cases newly allocated memory is going to be initialized by the caller right away.
__GFP_NOINITdoesn't affectinit_on_freebehavior, except for SLOB, whereinit_on_freeimpliesinit_on_alloc.
__GFP_NOINITbasically defeats the hardening against information leaks provided byinit_on_alloc, so one should use it with caution.This patch also adds
__GFP_NOINITtoalloc_pages()calls in SL[AOU]B. Doing so is safe, because the heap allocators initialize the pages they receive before passing memory to the callers.Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +6.84% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.25% sys time (st.err 0.72%)
Linux build with -j12, init_on_free=1: +8.52% wall time (st.err 0.42%) Linux build with -j12, init_on_free=1: +24.31% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.16% wall time (st.err 0.40%) Linux build with -j12, init_on_alloc=1: +1.24% sys time (st.err 0.39%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
Signed-off-by Alexander Potapenko <[email protected]>Cc Andrew Morton <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>Cc Laura Abbott <[email protected]>Cc Randy Dunlap <[email protected]>Cc Jann Horn <[email protected]>Cc Mark Rutland <[email protected]>--- include/linux/gfp.h | 6 +++++- include/linux/mm.h | 2 +- kernel/kexec_core.c | 2 +- mm/slab.c | 2 +- mm/slob.c | 3 ++- mm/slub.c | 1 + 6 files changed, 11 insertions(+), 5 deletions(-)
Quoted 13 lines
include/linux/gfp.h struct vm_area_struct; · line 47
Quoted 9 lines
include/linux/gfp.h +1
-0@@ -44,6 +44,7 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#define ___GFP_NOINIT 0x1000000uI mentioned this in the other patch, but I think this needs to be moved ahead of
GFP_NOLOCKDEPand adjust the values forGFP_NOLOCKDEPand to leave theIS_ENABLED()test in__GFP_BITS_SHIFTalone.
Do we really need this blinking GFP_NOLOCKDEP bit at all? This approach doesn't scale, we can't even have a second feature that has a bit depending on the config settings.
Cannot we just fix the number of bits instead?
Quoted 48 lines
Quoted 11 lines
include/linux/gfp.h +2
-0/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -208,16 +209,19 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_NOINIT requests non-initialized memory from the underlying allocator. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)include/linux/gfp.h struct vm_area_struct; · line 224
Quoted 10 lines
include/linux/gfp.h +2
-1#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_NOINIT ((__force gfp_t)___GFP_NOINIT)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25)AIUI, this will break non-CONFIG_LOCKDEP kernels: it should just be:
(quoted diff) +1
-1-#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))Quoted 9 lines
include/linux/gfp.h +0
-0#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/** include/linux/mm.h +0
-0include/linux/mm.h DECLARE_STATIC_KEY_FALSE(init_on_alloc); · line 2622
Quoted 6 lines
include/linux/mm.h +1
-1@@ -2618,7 +2618,7 @@ DECLARE_STATIC_KEY_FALSE(init_on_alloc);static inline bool want_init_on_alloc (gfp_t flags) {if (static_branch_unlikely (&init_on_alloc)) -return true; +return !(flags & __GFP_NOINIT);return flags & __GFP_ZERO;What do you think about renaming
__GFP_NOINITto__GFP_NO_AUTOINITor something?Regardless, yes, this is nice.
--
Kees Cook
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH 1/4] mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options
In reply to Kees CookFrom: Kees Cook <[email protected]>
Date: Wed, May 8, 2019 at 9:02 PM
To: Alexander Potapenko
Cc: Andrew Morton, Christoph Lameter, Kees Cook, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark Rutland
Quoted 9 lines · Alexander Potapenko
Quoted 3 lines
The new options are needed to prevent possible information leaks and make control-flow bugs that depend on uninitialized values more deterministic.
I like having this available on both alloc and free. This makes it much more configurable for the end users who can adapt to their work loads, etc.
Quoted 6 lines
Quoted 3 lines
Linux build with -j12, init_on_free=1: +24.42% sys time (st.err 0.52%) [...]
Linux build with -j12, init_on_alloc=1: +0.57% sys time (st.err 0.40%)Any idea why there is such a massive difference here? This seems to high just for cache-locality effects of touching all the freed pages.
I've measured a single make -j12 again under perf stat.
The numbers for init_on_alloc=1 were:
4936513177 cache-misses # 8.056 % of all
cache refs (44.44%)
61278262461 cache-references
(44.45%)
42844784 page-faults
1449630221347 L1-dcache-loads
(44.45%)
50569965485 L1-dcache-load-misses # 3.49% of all
L1-dcache hits (44.44%)
299987258588 L1-icache-load-misses
(44.44%)
1449857258648 dTLB-loads
(44.45%)
826292490 dTLB-load-misses # 0.06% of all
dTLB cache hits (44.44%)
22028472701 iTLB-loads
(44.44%)
858451905 iTLB-load-misses # 3.90% of all
iTLB cache hits (44.45%)
162.120107145 seconds time elapsed, and for init_on_free=1:
6666716777 cache-misses # 10.862 % of all
cache refs (44.45%)
61378258434 cache-references
(44.46%)
42850913 page-faults
1449986416063 L1-dcache-loads
(44.45%)
51277338771 L1-dcache-load-misses # 3.54% of all
L1-dcache hits (44.45%)
298295905805 L1-icache-load-misses
(44.44%)
1450378031344 dTLB-loads
(44.43%)
807011341 dTLB-load-misses # 0.06% of all
dTLB cache hits (44.44%)
22044976638 iTLB-loads
(44.44%)
846377845 iTLB-load-misses # 3.84% of all
iTLB cache hits (44.45%)
164.427054893 seconds time elapsed(note that we don't see the speed difference under perf)
init_on_free=1 causes 1.73B more cache misses than init_on_alloc=1. If I'm understanding correctly, a cache miss costs 12-14 cycles on my 3GHz Skylake CPU, which can explain explain a 7-8-second difference between the two modes.
But as I just realized this is both kernel and userspace, so while the difference is almost correct for wall time (120s for init_on_alloc, 130s for init_on_free) this doesn't tell much about the time spent in the kernel.
Quoted 2 lines
--
Kees Cook
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH 3/4] gfp: mm: introduce __GFP_NOINIT
In reply to Alexander PotapenkoQuoted 39 lines · Alexander Potapenko
From: Kees Cook <[email protected]>
Date: Wed, May 8, 2019 at 9:16 PM
To: Alexander Potapenko
Cc: Andrew Morton, Christoph Lameter, Kees Cook, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark RutlandQuoted 30 lines · Alexander Potapenko
Quoted 29 lines
When passed to an allocator (either pagealloc or SL[AOU]B),
__GFP_NOINITtells it to not initialize the requested memory if theinit_on_allocboot option is enabled. This can be useful in the cases newly allocated memory is going to be initialized by the caller right away.
__GFP_NOINITdoesn't affectinit_on_freebehavior, except for SLOB, whereinit_on_freeimpliesinit_on_alloc.
__GFP_NOINITbasically defeats the hardening against information leaks provided byinit_on_alloc, so one should use it with caution.This patch also adds
__GFP_NOINITtoalloc_pages()calls in SL[AOU]B. Doing so is safe, because the heap allocators initialize the pages they receive before passing memory to the callers.Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +6.84% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.25% sys time (st.err 0.72%)
Linux build with -j12, init_on_free=1: +8.52% wall time (st.err 0.42%) Linux build with -j12, init_on_free=1: +24.31% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.16% wall time (st.err 0.40%) Linux build with -j12, init_on_alloc=1: +1.24% sys time (st.err 0.39%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
Not sure, but I think this patch will clash with Matthew's posted patch series *Remove 'order' argument from many mm functions*.
Quoted 107 lines
Quoted 26 lines
Quoted 26 lines
Signed-off-by Alexander Potapenko <[email protected]>Cc Andrew Morton <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>Cc Laura Abbott <[email protected]>Cc Randy Dunlap <[email protected]>Cc Jann Horn <[email protected]>Cc Mark Rutland <[email protected]>--- include/linux/gfp.h | 6 +++++- include/linux/mm.h | 2 +- kernel/kexec_core.c | 2 +- mm/slab.c | 2 +- mm/slob.c | 3 ++- mm/slub.c | 1 + 6 files changed, 11 insertions(+), 5 deletions(-)Quoted 13 lines
include/linux/gfp.h struct vm_area_struct; · line 47
Quoted 9 lines
include/linux/gfp.h +1
-0@@ -44,6 +44,7 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#define ___GFP_NOINIT 0x1000000uI mentioned this in the other patch, but I think this needs to be moved ahead of
GFP_NOLOCKDEPand adjust the values forGFP_NOLOCKDEPand to leave theIS_ENABLED()test in__GFP_BITS_SHIFTalone.Do we really need this blinking
GFP_NOLOCKDEPbit at all? This approach doesn't scale, we can't even have a second feature that has a bit depending on the config settings.
Cannot we just fix the number of bits instead?Quoted 44 lines
Quoted 11 lines
include/linux/gfp.h +2
-0/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -208,16 +209,19 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_NOINIT requests non-initialized memory from the underlying allocator. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)include/linux/gfp.h struct vm_area_struct; · line 224
Quoted 10 lines
include/linux/gfp.h +2
-1#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_NOINIT ((__force gfp_t)___GFP_NOINIT)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25)AIUI, this will break non-CONFIG_LOCKDEP kernels: it should just be:
(quoted diff) +1
-1-#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))Quoted 9 lines
include/linux/gfp.h +0
-0#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/** include/linux/mm.h +0
-0include/linux/mm.h DECLARE_STATIC_KEY_FALSE(init_on_alloc); · line 2622
Quoted 6 lines
include/linux/mm.h +1
-1@@ -2618,7 +2618,7 @@ DECLARE_STATIC_KEY_FALSE(init_on_alloc);static inline bool want_init_on_alloc (gfp_t flags) {if (static_branch_unlikely (&init_on_alloc)) -return true; +return !(flags & __GFP_NOINIT);return flags & __GFP_ZERO;What do you think about renaming
__GFP_NOINITto__GFP_NO_AUTOINITor something?Quoted 4 lines
Regardless, yes, this is nice.
--
Kees Cook--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH 3/4] gfp: mm: introduce __GFP_NOINIT
From: Souptick Joarder <[email protected]>
Date: Sat, May 11, 2019 at 9:28 AM
To: Alexander Potapenko
Cc: Kees Cook, Andrew Morton, Christoph Lameter, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark Rutland, Matthew Wilcox
Quoted 37 lines · Alexander Potapenko
Quoted 36 lines
From: Kees Cook <[email protected]>
Date: Wed, May 8, 2019 at 9:16 PM
To: Alexander Potapenko
Cc: Andrew Morton, Christoph Lameter, Kees Cook, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark RutlandQuoted 27 lines · Alexander Potapenko
Quoted 26 lines
When passed to an allocator (either pagealloc or SL[AOU]B),
__GFP_NOINITtells it to not initialize the requested memory if theinit_on_allocboot option is enabled. This can be useful in the cases newly allocated memory is going to be initialized by the caller right away.
__GFP_NOINITdoesn't affectinit_on_freebehavior, except for SLOB, whereinit_on_freeimpliesinit_on_alloc.
__GFP_NOINITbasically defeats the hardening against information leaks provided byinit_on_alloc, so one should use it with caution.This patch also adds
__GFP_NOINITtoalloc_pages()calls in SL[AOU]B. Doing so is safe, because the heap allocators initialize the pages they receive before passing memory to the callers.Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +6.84% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.25% sys time (st.err 0.72%)
Linux build with -j12, init_on_free=1: +8.52% wall time (st.err 0.42%) Linux build with -j12, init_on_free=1: +24.31% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.16% wall time (st.err 0.40%) Linux build with -j12, init_on_alloc=1: +1.24% sys time (st.err 0.39%)
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
Not sure, but I think this patch will clash with Matthew's posted patch series *Remove 'order' argument from many mm functions*.
Not sure I can do much with that before those patches reach mainline. Once they do, I'll update my patches.
Please let me know if there's a better way to resolve such conflicts.
Quoted 107 lines
Quoted 107 lines
Quoted 26 lines
Quoted 26 lines
Signed-off-by Alexander Potapenko <[email protected]>Cc Andrew Morton <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>Cc Laura Abbott <[email protected]>Cc Randy Dunlap <[email protected]>Cc Jann Horn <[email protected]>Cc Mark Rutland <[email protected]>--- include/linux/gfp.h | 6 +++++- include/linux/mm.h | 2 +- kernel/kexec_core.c | 2 +- mm/slab.c | 2 +- mm/slob.c | 3 ++- mm/slub.c | 1 + 6 files changed, 11 insertions(+), 5 deletions(-)Quoted 13 lines
include/linux/gfp.h struct vm_area_struct; · line 47
Quoted 9 lines
include/linux/gfp.h +1
-0@@ -44,6 +44,7 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#define ___GFP_NOINIT 0x1000000uI mentioned this in the other patch, but I think this needs to be moved ahead of
GFP_NOLOCKDEPand adjust the values forGFP_NOLOCKDEPand to leave theIS_ENABLED()test in__GFP_BITS_SHIFTalone.Do we really need this blinking
GFP_NOLOCKDEPbit at all? This approach doesn't scale, we can't even have a second feature that has a bit depending on the config settings.
Cannot we just fix the number of bits instead?Quoted 44 lines
Quoted 11 lines
include/linux/gfp.h +2
-0/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -208,16 +209,19 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_NOINIT requests non-initialized memory from the underlying allocator. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)include/linux/gfp.h struct vm_area_struct; · line 224
Quoted 10 lines
include/linux/gfp.h +2
-1#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_NOINIT ((__force gfp_t)___GFP_NOINIT)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25)AIUI, this will break non-CONFIG_LOCKDEP kernels: it should just be:
(quoted diff) +1
-1-#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))Quoted 9 lines
include/linux/gfp.h +0
-0#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/** include/linux/mm.h +0
-0include/linux/mm.h DECLARE_STATIC_KEY_FALSE(init_on_alloc); · line 2622
Quoted 6 lines
include/linux/mm.h +1
-1@@ -2618,7 +2618,7 @@ DECLARE_STATIC_KEY_FALSE(init_on_alloc);static inline bool want_init_on_alloc (gfp_t flags) {if (static_branch_unlikely (&init_on_alloc)) -return true; +return !(flags & __GFP_NOINIT);return flags & __GFP_ZERO;What do you think about renaming
__GFP_NOINITto__GFP_NO_AUTOINITor something?Quoted 4 lines
Regardless, yes, this is nice.
--
Kees Cook--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH 3/4] gfp: mm: introduce __GFP_NOINIT
Quoted 51 lines · Alexander Potapenko
From: Souptick Joarder <[email protected]>
Date: Sat, May 11, 2019 at 9:28 AM
To: Alexander Potapenko
Cc: Kees Cook, Andrew Morton, Christoph Lameter, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark Rutland, Matthew WilcoxQuoted 41 lines · Alexander Potapenko
Quoted 39 lines
From: Kees Cook <[email protected]>
Date: Wed, May 8, 2019 at 9:16 PM
To: Alexander Potapenko
Cc: Andrew Morton, Christoph Lameter, Kees Cook, Laura Abbott, Linux-MM, linux-security-module, Kernel Hardening, Masahiro Yamada, James Morris, Serge E. Hallyn, Nick Desaulniers, Kostya Serebryany, Dmitry Vyukov, Sandeep Patil, Randy Dunlap, Jann Horn, Mark RutlandQuoted 30 lines · Alexander Potapenko
Quoted 29 lines
When passed to an allocator (either pagealloc or SL[AOU]B),
__GFP_NOINITtells it to not initialize the requested memory if theinit_on_allocboot option is enabled. This can be useful in the cases newly allocated memory is going to be initialized by the caller right away.
__GFP_NOINITdoesn't affectinit_on_freebehavior, except for SLOB, whereinit_on_freeimpliesinit_on_alloc.
__GFP_NOINITbasically defeats the hardening against information leaks provided byinit_on_alloc, so one should use it with caution.This patch also adds
__GFP_NOINITtoalloc_pages()calls in SL[AOU]B. Doing so is safe, because the heap allocators initialize the pages they receive before passing memory to the callers.Slowdown for the initialization features compared to init_on_free=0, init_on_alloc=0:
hackbench, init_on_free=1: +6.84% sys time (st.err 0.74%) hackbench, init_on_alloc=1: +7.25% sys time (st.err 0.72%)
Linux build with -j12, init_on_free=1: +8.52% wall time (st.err 0.42%) Linux build with -j12, init_on_free=1: +24.31% sys time (st.err 0.47%) Linux build with -j12, init_on_alloc=1: -0.16% wall time (st.err 0.40%) Linux build with -j12, init_on_alloc=1: +1.24% sys time (st.err 0.39%)
The slowdown for init_on_free=0, init_on_alloc=0 compared to the baseline is within the standard error.
Quoted 5 lines
Quoted 2 lines
Not sure, but I think this patch will clash with Matthew's posted patch series *Remove 'order' argument from many mm functions*.
Not sure I can do much with that before those patches reach mainline. Once they do, I'll update my patches.
Please let me know if there's a better way to resolve such conflicts.
I just thought to highlight about a possible conflict. Nothing else :) IMO, if other patch series merge into -next tree before this, then this series can be updated against -next.
... And I am sure others will have a better suggestion.
Quoted 121 lines
Quoted 99 lines
Quoted 99 lines
Quoted 26 lines
Quoted 26 lines
Signed-off-by Alexander Potapenko <[email protected]>Cc Andrew Morton <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>Cc Laura Abbott <[email protected]>Cc Randy Dunlap <[email protected]>Cc Jann Horn <[email protected]>Cc Mark Rutland <[email protected]>--- include/linux/gfp.h | 6 +++++- include/linux/mm.h | 2 +- kernel/kexec_core.c | 2 +- mm/slab.c | 2 +- mm/slob.c | 3 ++- mm/slub.c | 1 + 6 files changed, 11 insertions(+), 5 deletions(-)Quoted 13 lines
include/linux/gfp.h struct vm_area_struct; · line 47
Quoted 9 lines
include/linux/gfp.h +1
-0@@ -44,6 +44,7 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#define ___GFP_NOINIT 0x1000000uI mentioned this in the other patch, but I think this needs to be moved ahead of
GFP_NOLOCKDEPand adjust the values forGFP_NOLOCKDEPand to leave theIS_ENABLED()test in__GFP_BITS_SHIFTalone.Do we really need this blinking
GFP_NOLOCKDEPbit at all? This approach doesn't scale, we can't even have a second feature that has a bit depending on the config settings.
Cannot we just fix the number of bits instead?Quoted 44 lines
Quoted 11 lines
include/linux/gfp.h +2
-0/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* @@ -208,16 +209,19 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_NOINIT requests non-initialized memory from the underlying allocator. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)include/linux/gfp.h struct vm_area_struct; · line 224
Quoted 10 lines
include/linux/gfp.h +2
-1#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_NOINIT ((__force gfp_t)___GFP_NOINIT)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25)AIUI, this will break non-CONFIG_LOCKDEP kernels: it should just be:
(quoted diff) +1
-1-#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))Quoted 9 lines
include/linux/gfp.h +0
-0#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/** include/linux/mm.h +0
-0include/linux/mm.h DECLARE_STATIC_KEY_FALSE(init_on_alloc); · line 2622
Quoted 6 lines
include/linux/mm.h +1
-1@@ -2618,7 +2618,7 @@ DECLARE_STATIC_KEY_FALSE(init_on_alloc);static inline bool want_init_on_alloc (gfp_t flags) {if (static_branch_unlikely (&init_on_alloc)) -return true; +return !(flags & __GFP_NOINIT);return flags & __GFP_ZERO;What do you think about renaming
__GFP_NOINITto__GFP_NO_AUTOINITor something?Quoted 4 lines
Regardless, yes, this is nice.
--
Kees Cook--
Alexander Potapenko
Software EngineerQuoted 8 lines
Quoted 8 lines
Google Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg