incoming
Subsystems (75)
Andrew Morton
maintainerPATCH 118/178 vm/test_vmalloc.sh: adapt for updated driver interface
In reply to Andrew MortonFrom: "Uladzislau Rezki (Sony)" <[email protected]>
Subject: vm/test_vmalloc.sh: adapt for updated driver interface
A 'single_cpu_test' parameter is odd and it does not exist anymore. Instead there was introduced a 'nr_threads' one. If it is not set it behaves as the former parameter.
That is why update a "stress mode" according to this change specifying number of workers which are equal to number of CPUs. Also update an output of help message based on a new interface.
---
tools/testing/selftests/vm/test_vmalloc.sh | 21 +++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
tools/testing/selftests/vm/test_vmalloc.sh renamed from tools/testing/selftests/vm/test_vmalloc.sh~vm-test_vmallocsh-adapt-for-updated-driver-interface +11 -10
@@ -11,6 +11,7 @@ TEST_NAME="vmalloc" DRIVER="test_${TEST_NAME}" +NUM_CPUS=`grep -c ^processor /proc/cpuinfo` # 1 if fails exitcode=1
@@ -22,9 +23,9 @@ ksft_skip=4 # Static templates for performance, stressing and smoke tests. # Also it is possible to pass any supported parameters manualy. # -PERF_PARAM="single_cpu_test=1 sequential_test_order=1 test_repeat_count=3" -SMOKE_PARAM="single_cpu_test=1 test_loop_count=10000 test_repeat_count=10" -STRESS_PARAM="test_repeat_count=20" +PERF_PARAM="sequential_test_order=1 test_repeat_count=3" +SMOKE_PARAM="test_loop_count=10000 test_repeat_count=10" +STRESS_PARAM="nr_threads=$NUM_CPUS test_repeat_count=20" check_test_requirements() {
@@ -58,8 +59,8 @@ run_perfformance_check() run_stability_check() { - echo "Run stability tests. In order to stress vmalloc subsystem we run" - echo "all available test cases on all available CPUs simultaneously." + echo "Run stability tests. In order to stress vmalloc subsystem all" + echo "available test cases are run by NUM_CPUS workers simultaneously." echo "It will take time, so be patient." modprobe $DRIVER $STRESS_PARAM > /dev/null 2>&1
@@ -92,17 +93,17 @@ usage() echo "# Shows help message" echo "./${DRIVER}.sh" echo - echo "# Runs 1 test(id_1), repeats it 5 times on all online CPUs" - echo "./${DRIVER}.sh run_test_mask=1 test_repeat_count=5" + echo "# Runs 1 test(id_1), repeats it 5 times by NUM_CPUS workers" + echo "./${DRIVER}.sh nr_threads=$NUM_CPUS run_test_mask=1 test_repeat_count=5" echo echo -n "# Runs 4 tests(id_1|id_2|id_4|id_16) on one CPU with " echo "sequential order" - echo -n "./${DRIVER}.sh single_cpu_test=1 sequential_test_order=1 " + echo -n "./${DRIVER}.sh sequential_test_order=1 " echo "run_test_mask=23" echo - echo -n "# Runs all tests on all online CPUs, shuffled order, repeats " + echo -n "# Runs all tests by NUM_CPUS workers, shuffled order, repeats " echo "20 times" - echo "./${DRIVER}.sh test_repeat_count=20" + echo "./${DRIVER}.sh nr_threads=$NUM_CPUS test_repeat_count=20" echo echo "# Performance analysis" echo "./${DRIVER}.sh performance"
_
Andrew Morton
maintainerPATCH 119/178 mm/vmalloc: refactor the preloading loagic
In reply to Andrew MortonFrom: "Uladzislau Rezki (Sony)" <[email protected]>
Subject: mm/vmalloc: refactor the preloading loagic
Instead of keeping open-coded style, move the code related to preloading into a separate function. Therefore introduce the preload_this_cpu_lock() routine that prelaods a current CPU with one extra vmap_area object.
There is no functional change as a result of this patch.
---
mm/vmalloc.c | 60 ++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 33 deletions(-)
mm/vmalloc.c renamed from mm/vmalloc.c~mm-vmalloc-refactor-the-preloading-loagic +27 -33
@@ -1430,6 +1430,29 @@ static void free_vmap_area(struct vmap_aspin_unlock (&free_vmap_area_lock); } +static inline void +preload_this_cpu_lock (spinlock_t *lock,gfp_t gfp_mask,int node) +{ +struct vmap_area *va =NULL ; + +/* +* Preload this CPU with one extra vmap_area object. It is used +* when fit type of free area is NE_FIT_TYPE. It guarantees that +* a CPU that does an allocation is preloaded. +* +* We do it in non-atomic context, thus it allows us to use more +* permissive allocation masks to be more stable under low memory +* condition and high memory pressure. +*/ +if (!this_cpu_read (ne_fit_preload_node)) + va =kmem_cache_alloc_node (vmap_area_cachep, gfp_mask, node); + +spin_lock (lock); + +if (va &&__this_cpu_cmpxchg (ne_fit_preload_node,NULL , va)) +kmem_cache_free (vmap_area_cachep, va); +} +/* * Allocate a region of KVA of the specified size and alignment, within the * vstart and vend.
@@ -1439,7 +1462,7 @@ static struct vmap_area *alloc_vmap_area unsigned long vstart, unsigned long vend, int node, gfp_t gfp_mask) { - struct vmap_area *va, *pva; + struct vmap_area *va;unsigned long addr;int purged =0 ;int ret;
@@ -1465,43 +1488,14 @@ static struct vmap_area *alloc_vmap_areakmemleak_scan_area (&va->rb_node,SIZE_MAX , gfp_mask); retry: -/* -* Preload this CPU with one extra vmap_area object. It is used -* when fit type of free area is NE_FIT_TYPE. Please note, it -* does not guarantee that an allocation occurs on a CPU that -* is preloaded, instead we minimize the case when it is not. -* It can happen because of cpu migration, because there is a -* race until the below spinlock is taken. -* -* The preload is done in non-atomic context, thus it allows us -* to use more permissive allocation masks to be more stable under -* low memory condition and high memory pressure. In rare case, -* if not preloaded, GFP_NOWAIT is used. -* -* Set "pva" to NULL here, because of "retry" path. -*/ - pva =NULL ; - -if (!this_cpu_read (ne_fit_preload_node)) -/* -* Even if it fails we do not really care about that. -* Just proceed as it is. If needed "overflow" path -* will refill the cache we allocate from. -*/ - pva =kmem_cache_alloc_node (vmap_area_cachep, gfp_mask, node); - -spin_lock (&free_vmap_area_lock); - -if (pva &&__this_cpu_cmpxchg (ne_fit_preload_node,NULL , pva)) -kmem_cache_free (vmap_area_cachep, pva); +preload_this_cpu_lock (&free_vmap_area_lock, gfp_mask, node); + addr =__alloc_vmap_area (size, align, vstart, vend); +spin_unlock (&free_vmap_area_lock);/* * If an allocation fails, the "vend" address is * returned. Therefore trigger the overflow path. */ - addr =__alloc_vmap_area (size, align, vstart, vend); -spin_unlock (&free_vmap_area_lock); -if (unlikely (addr == vend)) goto overflow;
_
Andrew Morton
maintainerPATCH 120/178 mm/vmalloc: remove an empty line
In reply to Andrew MortonFrom: "Uladzislau Rezki (Sony)" <[email protected]>
Subject: mm/vmalloc: remove an empty line
---
mm/vmalloc.c | 1 - 1 file changed, 1 deletion(-)
mm/vmalloc.c renamed from mm/vmalloc.c~mm-vmalloc-remove-an-empty-line +0 -1
@@ -1503,7 +1503,6 @@ retry: va->va_end = addr + size; va->vm =NULL ; -spin_lock (&vmap_area_lock);insert_vmap_area (va, &vmap_area_root, &vmap_area_list);spin_unlock (&vmap_area_lock);
_
Andrew Morton
maintainerPATCH 121/178 mm/doc: fix fault_flag_allow_retry_first kerneldoc
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/doc: fix fault_flag_allow_retry_first kerneldoc
make htmldocs reports:
./include/linux/mm.h:496: warning: Function parameter or member 'flags' not described in 'fault_flag_allow_retry_first'
Add a description.
---
include/linux/mm.h | 1 + 1 file changed, 1 insertion(+)
include/linux/mm.h renamed from include/linux/mm.h~mm-doc-fix-fault_flag_allow_retry_first-kerneldoc +1 -0
@@ -485,6 +485,7 @@ extern pgprot_t protection_map[16];/** * fault_flag_allow_retry_first - check ALLOW_RETRY the first time +* @flags: Fault flags. * * This is mostly used for places where we want to try to avoid taking * the mmap_lock for too long a time when waiting for another condition
_
Andrew Morton
maintainerPATCH 122/178 mm/doc: fix page_maybe_dma_pinned kerneldoc
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/doc: fix page_maybe_dma_pinned kerneldoc
make htmldocs reports:
./include/linux/mm.h:1341: warning: Excess function parameter 'Return' description in 'page_maybe_dma_pinned'
Fix a few other formatting nits while I'm editing this description.
---
include/linux/mm.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~mm-doc-fix-page_maybe_dma_pinned-kerneldoc +5 -5
@@ -1271,10 +1271,11 @@ void unpin_user_page_range_dirty_lock(stvoid unpin_user_pages (struct page **pages,unsigned long npages);/** -* page_maybe_dma_pinned() - report if a page is pinned for DMA. +* page_maybe_dma_pinned - Report if a page is pinned for DMA. +* @page: The page. * * This function checks if a page has been pinned via a call to -* pin_user_pages*(). +* a function in the pin_user_pages() family. * * For non-huge pages, the return value is partially fuzzy: false is not fuzzy, * because it means "definitely not pinned for DMA", but true means "probably
@@ -1292,9 +1293,8 @@ void unpin_user_pages(struct page **page* * For more information, please see Documentation/core-api/pin_user_pages.rst. * -* @page: pointer to page to be queried. -* @Return: True, if it is likely that the page has been "dma-pinned". -* False, if the page is definitely not dma-pinned. +* Return: True, if it is likely that the page has been "dma-pinned". +* False, if the page is definitely not dma-pinned. */ static inline bool page_maybe_dma_pinned (struct page *page) {
_
Andrew Morton
maintainerPATCH 123/178 mm/doc: turn fault flags into an enum
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/doc: turn fault flags into an enum
The kernel-doc script complains about ./include/linux/mm.h:425: warning: wrong kernel-doc identifier on line:
* Fault flag definitions.
I don't know how to document a series of #defines, so turn these definitions into an enum and document that instead.
---
include/linux/mm.h | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~mm-doc-turn-fault-flags-into-an-enum +15 -14
@@ -432,8 +432,7 @@ extern unsigned int kobjsize(const voidextern pgprot_t protection_map[16 ];/** -* Fault flag definitions. -* +* enum fault_flag - Fault flag definitions. * @FAULT_FLAG_WRITE: Fault was a write fault. * @FAULT_FLAG_MKWRITE: Fault was mkwrite of existing PTE. * @FAULT_FLAG_ALLOW_RETRY: Allow to retry the fault if blocked.
@@ -464,16 +463,18 @@ extern pgprot_t protection_map[16];* signals before a retry to make sure the continuous page faults can still be * interrupted if necessary. */ -#define FAULT_FLAG_WRITE 0x01 -#define FAULT_FLAG_MKWRITE 0x02 -#define FAULT_FLAG_ALLOW_RETRY 0x04 -#define FAULT_FLAG_RETRY_NOWAIT 0x08 -#define FAULT_FLAG_KILLABLE 0x10 -#define FAULT_FLAG_TRIED 0x20 -#define FAULT_FLAG_USER 0x40 -#define FAULT_FLAG_REMOTE 0x80 -#define FAULT_FLAG_INSTRUCTION 0x100 -#define FAULT_FLAG_INTERRUPTIBLE 0x200 +enum fault_flag { +FAULT_FLAG_WRITE =1 <<0 , +FAULT_FLAG_MKWRITE =1 <<1 , +FAULT_FLAG_ALLOW_RETRY =1 <<2 , +FAULT_FLAG_RETRY_NOWAIT =1 <<3 , +FAULT_FLAG_KILLABLE =1 <<4 , +FAULT_FLAG_TRIED =1 <<5 , +FAULT_FLAG_USER =1 <<6 , +FAULT_FLAG_REMOTE =1 <<7 , +FAULT_FLAG_INSTRUCTION =1 <<8 , +FAULT_FLAG_INTERRUPTIBLE =1 <<9 , +};/* * The default fault flags that should be used by most of the
@@ -496,7 +497,7 @@ extern pgprot_t protection_map[16];* Return: true if the page fault allows retry and this is the first * attempt of the fault handling; false otherwise. */ -static inline bool fault_flag_allow_retry_first (unsigned int flags) +static inline bool fault_flag_allow_retry_first (enum fault_flag flags) {return (flags &FAULT_FLAG_ALLOW_RETRY ) && (!(flags &FAULT_FLAG_TRIED ));
@@ -531,7 +532,7 @@ struct vm_fault {pgoff_t pgoff;/* Logical page offset based on vma */ unsigned long address;/* Faulting virtual address */ }; -unsigned int flags;/* FAULT_FLAG_xxx flags +enum fault_flag flags;/* FAULT_FLAG_xxx flags * XXX: should really be 'const' */ pmd_t *pmd;/* Pointer to pmd entry matching * the 'address' */
_
Andrew Morton
maintainerPATCH 124/178 mm/doc: add mm.h and mm_types.h to the mm-api document
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/doc: add mm.h and mm_types.h to the mm-api document
kerneldoc in include/linux/mm.h and include/linux/mm_types.h wasn't being included in the html build.
---
Documentation/core-api/mm-api.rst | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/core-api/mm-api.rst renamed from Documentation/core-api/mm-api.rst~mm-doc-add-mmh-and-mm_typesh-to-the-mm-api-document +4 -0
@@ -92,3 +92,7 @@ More Memory Management Functions :export: .. kernel-doc:: mm/page_alloc.c +.. kernel-doc:: include/linux/mm_types.h + :internal: +.. kernel-doc:: include/linux/mm.h + :internal:
_
Andrew Morton
maintainerPATCH 125/178 MAINTAINERS: assign pagewalk.h to MEMORY MANAGEMENT
In reply to Andrew MortonFrom: Lukas Bulwahn <[email protected]>
Subject: MAINTAINERS: assign pagewalk.h to MEMORY MANAGEMENT
Patch series "kernel-doc and MAINTAINERS clean-up".
Roughly 900 warnings of about 21.000 kernel-doc warnings in the kernel tree warn with 'cannot understand function prototype:', i.e., the kernel-doc parser cannot parse the function's signature. The majority, about 600 cases of those, are just struct definitions following the kernel-doc description. Further, spot-check investigations suggest that the authors of the specific kernel-doc descriptions simply were not aware that the general format for a kernel-doc description for a structure requires to prefix the struct name with the keyword 'struct', as in 'struct struct_name - Brief description.'. Details on kernel-doc are at the Link below.
Without the struct keyword, kernel-doc does not check if the kernel-doc description fits to the actual struct definition in the source code. Fortunately, in roughly a quarter of these cases, the kernel-doc description is actually complete wrt. its corresponding struct definition. So, the trivial change adding the struct keyword will allow us to keep the kernel-doc descriptions more consistent for future changes, by checking for new kernel-doc warnings.
Also, some of the files in ./include/ are not assigned to a specific MAINTAINERS section and hence have no dedicated maintainer. So, if needed, the files in ./include/ are also assigned to the fitting MAINTAINERS section, as I need to identify whom to send the clean-up patch anyway.
Here is the change from this kernel-doc janitorial work in the ./include/ directory for MEMORY MANAGEMENT.
This patch (of 2):
Commit a520110e4a15 ("mm: split out a new pagewalk.h header from mm.h") adds a new file in ./include/linux, but misses to update MAINTAINERS accordingly. Hence, ./scripts/get_maintainers.pl ./include/linux/pagewalk.h points only to lkml as general fallback for all files, whereas the original ./include/linux/mm.h clearly marks this file part of MEMORY MANAGEMENT.
Assign ./include/linux/pagewalk.h to MEMORY MANAGEMENT.
---
MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS renamed from MAINTAINERS~maintainers-assign-pagewalkh-to-memory-management +1 -0
@@ -11744,6 +11744,7 @@ F: include/linux/gfp.h F: include/linux/memory_hotplug.h F: include/linux/mm.h F: include/linux/mmzone.h +F: include/linux/pagewalk.h F: include/linux/vmalloc.h F: mm/
_
Andrew Morton
maintainerPATCH 126/178 pagewalk: prefix struct kernel-doc descriptions
In reply to Andrew MortonFrom: Lukas Bulwahn <[email protected]>
Subject: pagewalk: prefix struct kernel-doc descriptions
The script './scripts/kernel-doc -none ./include/linux/pagewalk.h' reports:
include/linux/pagewalk.h:37: warning: cannot understand function prototype: 'struct mm_walk_ops ' include/linux/pagewalk.h:85: warning: cannot understand function prototype: 'struct mm_walk '
A kernel-doc description for a structure requires to prefix the struct name with the keyword 'struct'. So, do that such that no further kernel-doc warnings are reported for this file.
---
include/linux/pagewalk.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/pagewalk.h renamed from include/linux/pagewalk.h~pagewalk-prefix-struct-kernel-doc-descriptions +2 -2
@@ -7,7 +7,7 @@struct mm_walk ;/** -* mm_walk_ops - callbacks for walk_page_range +* struct mm_walk_ops - callbacks for walk_page_range * @pgd_entry: if set, called for each non-empty PGD (top-level) entry * @p4d_entry: if set, called for each non-empty P4D entry * @pud_entry: if set, called for each non-empty PUD entry
@@ -71,7 +71,7 @@ enum page_walk_action { };/** -* mm_walk - walk_page_range data +* struct mm_walk - walk_page_range data * @ops: operation to call during the walk * @mm: mm_struct representing the target process of page table walk * @pgd: pointer to PGD; only valid with no_vma (otherwise set to NULL)
_
Andrew Morton
maintainerPATCH 127/178 mm/kasan: switch from strlcpy to strscpy
In reply to Andrew MortonFrom: Zhiyuan Dai <[email protected]>
Subject: mm/kasan: switch from strlcpy to strscpy
strlcpy is marked as deprecated in Documentation/process/deprecated.rst, and there is no functional difference when the caller expects truncation (when not checking the return value). strscpy is relatively better as it also avoids scanning the whole source string.
---
mm/kasan/report_generic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/kasan/report_generic.c renamed from mm/kasan/report_generic.c~mm-kasan-switch-from-strlcpy-to-strscpy +1 -1
@@ -148,7 +148,7 @@ static bool __must_check tokenize_frame_ }/* Copy token (+ 1 byte for '\0'). */ -strlcpy (token, *frame_descr, tok_len +1 ); +strscpy (token, *frame_descr, tok_len +1 ); }/* Advance frame_descr past separator. */
_
Andrew Morton
maintainerPATCH 128/178 kasan: fix kasan_byte_accessible() to be consistent with actual checks
In reply to Andrew MortonFrom: Peter Collingbourne <[email protected]>
Subject: kasan: fix kasan_byte_accessible() to be consistent with actual checks
We can sometimes end up with kasan_byte_accessible() being called on non-slab memory. For example ksize() and krealloc() may end up calling it on KFENCE allocated memory. In this case the memory will be tagged with KASAN_SHADOW_INIT, which a subsequent patch ("kasan: initialize shadow to TAG_INVALID for SW_TAGS") will set to the same value as KASAN_TAG_INVALID, causing kasan_byte_accessible() to fail when called on non-slab memory.
This highlighted the fact that the check in kasan_byte_accessible() was inconsistent with checks as implemented for loads and stores (kasan_check_range() in SW tags mode and hardware-implemented checks in HW tags mode). kasan_check_range() does not have a check for KASAN_TAG_INVALID, and instead has a comparison against KASAN_SHADOW_START. In HW tags mode, we do not have either, but we do set TCR_EL1.TCMA which corresponds with the comparison against KASAN_TAG_KERNEL.
Therefore, update kasan_byte_accessible() for both SW and HW tags modes to correspond with the respective checks on loads and stores.
---
mm/kasan/kasan.h | 3 +-- mm/kasan/sw_tags.c | 10 +++++++--- 2 files changed, 8 insertions(+), 5 deletions(-)
mm/kasan/kasan.h renamed from mm/kasan/kasan.h~kasan-fix-kasan_byte_accessible-to-be-consistent-with-actual-checks +1 -2
@@ -396,8 +396,7 @@ static inline bool kasan_byte_accessibleu8 ptr_tag =get_tag (addr);u8 mem_tag =hw_get_mem_tag ((void *)addr); -return (mem_tag !=KASAN_TAG_INVALID ) && - (ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag); + return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag; }#else /* CONFIG_KASAN_HW_TAGS */
mm/kasan/sw_tags.c renamed from mm/kasan/sw_tags.c~kasan-fix-kasan_byte_accessible-to-be-consistent-with-actual-checks +7 -3
@@ -121,10 +121,14 @@ bool kasan_check_range(unsigned long addbool kasan_byte_accessible (const void *addr) {u8 tag =get_tag (addr); -u8 shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (kasan_reset_tag (addr))); +void *untagged_addr =kasan_reset_tag (addr); +u8 shadow_byte; -return (shadow_byte !=KASAN_TAG_INVALID ) && - (tag ==KASAN_TAG_KERNEL || tag == shadow_byte); +if (untagged_addr <kasan_shadow_to_mem ((void *)KASAN_SHADOW_START )) +return false; + + shadow_byte =READ_ONCE (*(u8 *)kasan_mem_to_shadow (untagged_addr)); +return tag ==KASAN_TAG_KERNEL || tag == shadow_byte; }#define DEFINE_HWASAN_LOAD_STORE (size) \
_
Andrew Morton
maintainerPATCH 129/178 kasan: initialize shadow to TAG_INVALID for SW_TAGS
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: initialize shadow to TAG_INVALID for SW_TAGS
Currently, KASAN_SW_TAGS uses 0xFF as the default tag value for unallocated memory. The underlying idea is that since that memory hasn't been allocated yet, it's only supposed to be dereferenced through a pointer with the native 0xFF tag.
While this is a good idea in terms on consistency, practically it doesn't bring any benefit. Since the 0xFF pointer tag is a match-all tag, it doesn't matter what tag the accessed memory has. No accesses through 0xFF-tagged pointers are considered buggy by KASAN.
This patch changes the default tag value for unallocated memory to 0xFE, which is the tag KASAN uses for inaccessible memory. This doesn't affect accesses through 0xFF-tagged pointer to this memory, but this allows KASAN to detect wild and large out-of-bounds invalid memory accesses through otherwise-tagged pointers.
This is a prepatory patch for the next one, which changes the tag-based KASAN modes to not poison the boot memory.
---
include/linux/kasan.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kasan.h renamed from include/linux/kasan.h~kasan-initialize-shadow-to-tag_invalid-for-sw_tags +2 -1
@@ -30,7 +30,8 @@ struct kunit_kasan_expectation {/* Software KASAN implementations use shadow memory. */ #ifdef CONFIG_KASAN_SW_TAGS -#define KASAN_SHADOW_INIT 0xFF +/* This matches KASAN_TAG_INVALID. */ +#define KASAN_SHADOW_INIT 0xFE#else #define KASAN_SHADOW_INIT 0#endif
_
Andrew Morton
maintainerPATCH 130/178 mm, kasan: don't poison boot memory with tag-based modes
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: mm, kasan: don't poison boot memory with tag-based modes
During boot, all non-reserved memblock memory is exposed to page_alloc via memblock_free_pages->__free_pages_core(). This results in kasan_free_pages() being called, which poisons that memory.
Poisoning all that memory lengthens boot time. The most noticeable effect is observed with the HW_TAGS mode. A boot-time impact may potentially also affect systems with large amount of RAM.
This patch changes the tag-based modes to not poison the memory during the memblock->page_alloc transition.
An exception is made for KASAN_GENERIC. Since it marks all new memory as accessible, not poisoning the memory released from memblock will lead to KASAN missing invalid boot-time accesses to that memory.
With KASAN_SW_TAGS, as it uses the invalid 0xFE tag as the default tag for all memory, it won't miss bad boot-time accesses even if the poisoning of memblock memory is removed.
With KASAN_HW_TAGS, the default memory tags values are unspecified. Therefore, if memblock poisoning is removed, this KASAN mode will miss the mentioned type of boot-time bugs with a 1/16 probability. This is taken as an acceptable trafe-off.
Internally, the poisoning is removed as follows. __free_pages_core() is used when exposing fresh memory during system boot and when onlining memory during hotplug. This patch adds a new FPI_SKIP_KASAN_POISON flag and passes it to __free_pages_ok() through free_pages_prepare() from __free_pages_core(). If FPI_SKIP_KASAN_POISON is set, kasan_free_pages() is not called.
All memory allocated normally when the boot is over keeps getting poisoned as usual.
---
mm/page_alloc.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-kasan-dont-poison-boot-memory-with-tag-based-modes +34 -11
@@ -109,6 +109,17 @@ typedef int __bitwise fpi_t;*/ #define FPI_TO_TAIL ((__force fpi_t)BIT(1)) +/* +* Don't poison memory with KASAN (only for the tag-based modes). +* During boot, all non-reserved memblock memory is exposed to page_alloc. +* Poisoning all that memory lengthens boot time, especially on systems with +* large amount of RAM. This flag is used to skip that poisoning. +* This is only done for the tag-based KASAN modes, as those are able to +* detect memory corruptions with the memory tags assigned by default. +* All memory allocated normally after boot gets poisoned as usual. +*/ +#define FPI_SKIP_KASAN_POISON ((__force fpi_t)BIT(2)) +/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ static DEFINE_MUTEX (pcp_batch_high_lock);#define MIN_PERCPU_PAGELIST_FRACTION (8)
@@ -385,10 +396,15 @@ static DEFINE_STATIC_KEY_TRUE(deferred_p* on-demand allocation and then freed again before the deferred pages * initialization is done, but this is not likely to happen. */ -static inline void kasan_free_nondeferred_pages (struct page *page,int order) +static inline void kasan_free_nondeferred_pages (struct page *page,int order, +fpi_t fpi_flags) { -if (!static_branch_unlikely (&deferred_pages)) -kasan_free_pages (page, order); +if (static_branch_unlikely (&deferred_pages)) +return ; +if (!IS_ENABLED (CONFIG_KASAN_GENERIC ) && + (fpi_flags &FPI_SKIP_KASAN_POISON )) +return ; +kasan_free_pages (page, order); }/* Returns true if the struct page for the pfn is uninitialised */
@@ -439,7 +455,14 @@ defer_init(int nid, unsigned long pfn, u return false; }#else -#define kasan_free_nondeferred_pages (p, o) kasan_free_pages(p, o) +static inline void kasan_free_nondeferred_pages (struct page *page,int order, +fpi_t fpi_flags) +{ +if (!IS_ENABLED (CONFIG_KASAN_GENERIC ) && + (fpi_flags &FPI_SKIP_KASAN_POISON )) +return ; +kasan_free_pages (page, order); +}static inline bool early_page_uninitialised (unsigned long pfn) {
@@ -1217,7 +1240,7 @@ static void kernel_init_free_pages(struc } static __always_inline bool free_pages_prepare(struct page *page, -unsigned int order,bool check_free) +unsigned int order,bool check_free,fpi_t fpi_flags) {int bad =0 ;
@@ -1286,7 +1309,7 @@ static __always_inline bool free_pages_p* With hardware tag-based KASAN, memory tags must be set before the * page becomes unavailable via debug_pagealloc or arch_free_page. */ -kasan_free_nondeferred_pages (page, order); +kasan_free_nondeferred_pages (page, order, fpi_flags);/* * arch_free_page() can make the page's contents inaccessible. s390
@@ -1308,7 +1331,7 @@ static __always_inline bool free_pages_p*/ static bool free_pcp_prepare (struct page *page) { -return free_pages_prepare (page,0 , true); +return free_pages_prepare (page,0 , true,FPI_NONE ); }static bool bulkfree_pcp_prepare (struct page *page)
@@ -1328,9 +1351,9 @@ static bool bulkfree_pcp_prepare(structstatic bool free_pcp_prepare (struct page *page) {if (debug_pagealloc_enabled_static ()) -return free_pages_prepare (page,0 , true); +return free_pages_prepare (page,0 , true,FPI_NONE );else -return free_pages_prepare (page,0 , false); +return free_pages_prepare (page,0 , false,FPI_NONE ); }static bool bulkfree_pcp_prepare (struct page *page)
@@ -1538,7 +1561,7 @@ static void __free_pages_ok(struct pageint migratetype;unsigned long pfn =page_to_pfn (page); -if (!free_pages_prepare (page, order, true)) +if (!free_pages_prepare (page, order, true, fpi_flags))return ; migratetype =get_pfnblock_migratetype (page, pfn);
@@ -1575,7 +1598,7 @@ void __free_pages_core(struct page *page* Bypass PCP and place fresh pages right to the tail, primarily * relevant for memory onlining. */ -__free_pages_ok (page, order,FPI_TO_TAIL ); +__free_pages_ok (page, order,FPI_TO_TAIL |FPI_SKIP_KASAN_POISON ); }#ifdef CONFIG_NEED_MULTIPLE_NODES
_
Andrew Morton
maintainerPATCH 131/178 arm64: kasan: allow to init memory when setting tags
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: arm64: kasan: allow to init memory when setting tags
Patch series "kasan: integrate with init_on_alloc/free", v3.
This patch series integrates HW_TAGS KASAN with init_on_alloc/free by initializing memory via the same arm64 instruction that sets memory tags.
This is expected to improve HW_TAGS KASAN performance when init_on_alloc/free is enabled. The exact perfomance numbers are unknown as MTE-enabled hardware doesn't exist yet.
This patch (of 5):
This change adds an argument to mte_set_mem_tag_range() that allows to enable memory initialization when settinh the allocation tags. The implementation uses stzg instruction instead of stg when this argument indicates to initialize memory.
Combining setting allocation tags with memory initialization will improve HW_TAGS KASAN performance when init_on_alloc/free is enabled.
This change doesn't integrate memory initialization with KASAN, this is done is subsequent patches in this series.
---
arch/arm64/include/asm/memory.h | 4 +- arch/arm64/include/asm/mte-kasan.h | 39 +++++++++++++++++---------- mm/kasan/kasan.h | 9 +++--- 3 files changed, 32 insertions(+), 20 deletions(-)
arch/arm64/include/asm/memory.h renamed from arch/arm64/include/asm/memory.h~arm64-kasan-allow-to-init-memory-when-setting-tags +2 -2
@@ -250,8 +250,8 @@ static inline const void *__tag_set(cons#define arch_init_tags (max_tag) mte_init_tags(max_tag)#define arch_get_random_tag () mte_get_random_tag()#define arch_get_mem_tag (addr) mte_get_mem_tag(addr) -#define arch_set_mem_tag_range (addr, size, tag) \ - mte_set_mem_tag_range((addr), (size), (tag)) +#define arch_set_mem_tag_range (addr, size, tag, init) \ + mte_set_mem_tag_range((addr), (size), (tag), (init))#endif /* CONFIG_KASAN_HW_TAGS */ /*
arch/arm64/include/asm/mte-kasan.h renamed from arch/arm64/include/asm/mte-kasan.h~arm64-kasan-allow-to-init-memory-when-setting-tags +25 -14
@@ -53,7 +53,8 @@ static inline u8 mte_get_random_tag(void* Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and * size must be non-zero and MTE_GRANULE_SIZE aligned. */ -static inline void mte_set_mem_tag_range (void *addr,size_t size,u8 tag) +static inline void mte_set_mem_tag_range (void *addr,size_t size, +u8 tag,bool init) {u64 curr, end;
@@ -63,18 +64,27 @@ static inline void mte_set_mem_tag_range curr = (u64 )__tag_set (addr, tag); end = curr + size; -do { -/* -* 'asm volatile' is required to prevent the compiler to move -* the statement outside of the loop. -*/ - asmvolatile (__MTE_PREAMBLE"stg %0, [%0]" - : - :"r" (curr) - :"memory" ); - - curr +=MTE_GRANULE_SIZE ; - }while (curr != end); +/* +* 'asm volatile' is required to prevent the compiler to move +* the statement outside of the loop. +*/ +if (init) { +do { + asmvolatile (__MTE_PREAMBLE"stzg %0, [%0]" + : + :"r" (curr) + :"memory" ); + curr +=MTE_GRANULE_SIZE ; + }while (curr != end); + }else { +do { + asmvolatile (__MTE_PREAMBLE"stg %0, [%0]" + : + :"r" (curr) + :"memory" ); + curr +=MTE_GRANULE_SIZE ; + }while (curr != end); + } }void mte_enable_kernel_sync (void );
@@ -101,7 +111,8 @@ static inline u8 mte_get_random_tag(voidreturn 0xFF ; } -static inline void mte_set_mem_tag_range (void *addr,size_t size,u8 tag) +static inline void mte_set_mem_tag_range (void *addr,size_t size, +u8 tag,bool init) { }
mm/kasan/kasan.h renamed from mm/kasan/kasan.h~arm64-kasan-allow-to-init-memory-when-setting-tags +5 -4
@@ -314,7 +314,7 @@ static inline const void *arch_kasan_set#define arch_get_mem_tag (addr) (0xFF)#endif #ifndef arch_set_mem_tag_range -#define arch_set_mem_tag_range (addr, size, tag) ((void *)(addr)) +#define arch_set_mem_tag_range (addr, size, tag, init) ((void *)(addr))#endif #define hw_enable_tagging_sync () arch_enable_tagging_sync()
@@ -324,7 +324,8 @@ static inline const void *arch_kasan_set#define hw_force_async_tag_fault () arch_force_async_tag_fault()#define hw_get_random_tag () arch_get_random_tag()#define hw_get_mem_tag (addr) arch_get_mem_tag(addr) -#define hw_set_mem_tag_range (addr, size, tag) arch_set_mem_tag_range((addr), (size), (tag)) +#define hw_set_mem_tag_range (addr, size, tag, init) \ + arch_set_mem_tag_range((addr), (size), (tag), (init))#else /* CONFIG_KASAN_HW_TAGS */
@@ -371,7 +372,7 @@ static inline void kasan_poison(const voif (WARN_ON (size &KASAN_GRANULE_MASK ))return ; -hw_set_mem_tag_range ((void *)addr, size, value); +hw_set_mem_tag_range ((void *)addr, size, value, false); }static inline void kasan_unpoison (const void *addr,size_t size)
@@ -388,7 +389,7 @@ static inline void kasan_unpoison(constreturn ; size =round_up (size,KASAN_GRANULE_SIZE ); -hw_set_mem_tag_range ((void *)addr, size, tag); +hw_set_mem_tag_range ((void *)addr, size, tag, false); }static inline bool kasan_byte_accessible (const void *addr)
_
Andrew Morton
maintainerPATCH 132/178 kasan: init memory in kasan_(un)poison for HW_TAGS
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: init memory in kasan_(un)poison for HW_TAGS
This change adds an argument to kasan_poison() and kasan_unpoison() that allows initializing memory along with setting the tags for HW_TAGS.
Combining setting allocation tags with memory initialization will improve HW_TAGS KASAN performance when init_on_alloc/free is enabled.
This change doesn't integrate memory initialization with KASAN, this is done is subsequent patches in this series.
---
lib/test_kasan.c | 4 ++-- mm/kasan/common.c | 28 ++++++++++++++-------------- mm/kasan/generic.c | 12 ++++++------ mm/kasan/kasan.h | 14 ++++++++------ mm/kasan/shadow.c | 10 +++++----- mm/kasan/sw_tags.c | 2 +- 6 files changed, 36 insertions(+), 34 deletions(-)
lib/test_kasan.c renamed from lib/test_kasan.c~kasan-init-memory-in-kasan_unpoison-for-hw_tags +2 -2
@@ -1049,14 +1049,14 @@ static void match_all_mem_tag(struct kuncontinue ;/* Mark the first memory granule with the chosen memory tag. */ -kasan_poison (ptr,KASAN_GRANULE_SIZE , (u8 )tag); +kasan_poison (ptr,KASAN_GRANULE_SIZE , (u8 )tag, false);/* This access must cause a KASAN report. */ KUNIT_EXPECT_KASAN_FAIL (test, *ptr =0 ); }/* Recover the memory tag and free. */ -kasan_poison (ptr,KASAN_GRANULE_SIZE ,get_tag (ptr)); +kasan_poison (ptr,KASAN_GRANULE_SIZE ,get_tag (ptr), false);kfree (ptr); }
mm/kasan/common.c renamed from mm/kasan/common.c~kasan-init-memory-in-kasan_unpoison-for-hw_tags +14 -14
@@ -60,7 +60,7 @@ void kasan_disable_current(void)void __kasan_unpoison_range (const void *address,size_t size) { -kasan_unpoison (address, size); +kasan_unpoison (address, size, false); }#ifdef CONFIG_KASAN_STACK
@@ -69,7 +69,7 @@ void kasan_unpoison_task_stack(struct ta {void *base =task_stack_page (task); -kasan_unpoison (base,THREAD_SIZE ); +kasan_unpoison (base,THREAD_SIZE , false); }/* Unpoison the stack for the current task beyond a watermark sp value. */
@@ -82,7 +82,7 @@ asmlinkage void kasan_unpoison_task_stac*/ void *base = (void *)((unsigned long )watermark & ~(THREAD_SIZE -1 )); - kasan_unpoison(base, watermark - base); + kasan_unpoison(base, watermark - base, false); }#endif /* CONFIG_KASAN_STACK */
@@ -108,14 +108,14 @@ void __kasan_alloc_pages(struct page *pa tag =kasan_random_tag ();for (i =0 ; i < (1 << order); i++)page_kasan_tag_set (page + i, tag); -kasan_unpoison (page_address (page),PAGE_SIZE << order); +kasan_unpoison (page_address (page),PAGE_SIZE << order, false); }void __kasan_free_pages (struct page *page,unsigned int order) {if (likely (!PageHighMem (page)))kasan_poison (page_address (page),PAGE_SIZE << order, -KASAN_FREE_PAGE ); +KASAN_FREE_PAGE , false); }/*
@@ -251,18 +251,18 @@ void __kasan_poison_slab(struct page *pafor (i =0 ; i <compound_nr (page); i++)page_kasan_tag_reset (page + i);kasan_poison (page_address (page),page_size (page), -KASAN_KMALLOC_REDZONE ); +KASAN_KMALLOC_REDZONE , false); }void __kasan_unpoison_object_data (struct kmem_cache *cache,void *object) { -kasan_unpoison (object, cache->object_size); +kasan_unpoison (object, cache->object_size, false); }void __kasan_poison_object_data (struct kmem_cache *cache,void *object) {kasan_poison (object,round_up (cache->object_size,KASAN_GRANULE_SIZE ), -KASAN_KMALLOC_REDZONE ); +KASAN_KMALLOC_REDZONE , false); }/*
@@ -351,7 +351,7 @@ static inline bool ____kasan_slab_free(s }kasan_poison (object,round_up (cache->object_size,KASAN_GRANULE_SIZE ), -KASAN_KMALLOC_FREE ); +KASAN_KMALLOC_FREE , false);if ((IS_ENABLED (CONFIG_KASAN_GENERIC ) && !quarantine))return false;
@@ -407,7 +407,7 @@ void __kasan_slab_free_mempool(void *ptrif (unlikely (!PageSlab (page))) {if (____kasan_kfree_large (ptr, ip))return ; -kasan_poison (ptr,page_size (page),KASAN_FREE_PAGE ); +kasan_poison (ptr,page_size (page),KASAN_FREE_PAGE , false); }else {____kasan_slab_free (page->slab_cache, ptr, ip, false); }
@@ -453,7 +453,7 @@ void * __must_check __kasan_slab_alloc(s* Unpoison the whole object. * For kmalloc() allocations, kasan_kmalloc() will do precise poisoning. */ -kasan_unpoison (tagged_object, cache->object_size); +kasan_unpoison (tagged_object, cache->object_size, false);/* Save alloc info (if possible) for non-kmalloc() allocations. */ if (kasan_stack_collection_enabled ())
@@ -496,7 +496,7 @@ static inline void *____kasan_kmalloc(st redzone_end =round_up ((unsigned long )(object + cache->object_size),KASAN_GRANULE_SIZE );kasan_poison ((void *)redzone_start, redzone_end - redzone_start, -KASAN_KMALLOC_REDZONE ); +KASAN_KMALLOC_REDZONE , false);/* * Save alloc info (if possible) for kmalloc() allocations.
@@ -546,7 +546,7 @@ void * __must_check __kasan_kmalloc_larg KASAN_GRANULE_SIZE); redzone_end = (unsigned long )ptr +page_size (virt_to_page (ptr));kasan_poison ((void *)redzone_start, redzone_end - redzone_start, -KASAN_PAGE_REDZONE ); +KASAN_PAGE_REDZONE , false);return (void *)ptr; }
@@ -563,7 +563,7 @@ void * __must_check __kasan_krealloc(con* Part of it might already have been unpoisoned, but it's unknown * how big that part is. */ -kasan_unpoison (object, size); +kasan_unpoison (object, size, false); page =virt_to_head_page (object);
mm/kasan/generic.c renamed from mm/kasan/generic.c~kasan-init-memory-in-kasan_unpoison-for-hw_tags +6 -6
@@ -208,11 +208,11 @@ static void register_global(struct kasan {size_t aligned_size =round_up (global->size,KASAN_GRANULE_SIZE ); -kasan_unpoison (global->beg, global->size); +kasan_unpoison (global->beg, global->size, false);kasan_poison (global->beg + aligned_size, global->size_with_redzone - aligned_size, -KASAN_GLOBAL_REDZONE ); +KASAN_GLOBAL_REDZONE , false); }void __asan_register_globals (struct kasan_global *globals,size_t size)
@@ -292,11 +292,11 @@ void __asan_alloca_poison(unsigned longWARN_ON (!IS_ALIGNED (addr,KASAN_ALLOCA_REDZONE_SIZE ));kasan_unpoison ((const void *)(addr + rounded_down_size), - size - rounded_down_size); + size - rounded_down_size, false);kasan_poison (left_redzone,KASAN_ALLOCA_REDZONE_SIZE , -KASAN_ALLOCA_LEFT ); +KASAN_ALLOCA_LEFT , false);kasan_poison (right_redzone, padding_size +KASAN_ALLOCA_REDZONE_SIZE , -KASAN_ALLOCA_RIGHT ); +KASAN_ALLOCA_RIGHT , false); }EXPORT_SYMBOL (__asan_alloca_poison);
@@ -306,7 +306,7 @@ void __asan_allocas_unpoison(const voidif (unlikely (!stack_top || stack_top > stack_bottom))return ; -kasan_unpoison (stack_top, stack_bottom - stack_top); +kasan_unpoison (stack_top, stack_bottom - stack_top, false); }EXPORT_SYMBOL (__asan_allocas_unpoison);
mm/kasan/kasan.h renamed from mm/kasan/kasan.h~kasan-init-memory-in-kasan_unpoison-for-hw_tags +8 -6
@@ -359,7 +359,7 @@ static inline u8 kasan_random_tag(void)#ifdef CONFIG_KASAN_HW_TAGS -static inline void kasan_poison (const void *addr,size_t size,u8 value) +static inline void kasan_poison (const void *addr,size_t size,u8 value,bool init) { addr =kasan_reset_tag (addr);
@@ -372,10 +372,10 @@ static inline void kasan_poison(const voif (WARN_ON (size &KASAN_GRANULE_MASK ))return ; -hw_set_mem_tag_range ((void *)addr, size, value, false); +hw_set_mem_tag_range ((void *)addr, size, value, init); } -static inline void kasan_unpoison (const void *addr,size_t size) +static inline void kasan_unpoison (const void *addr,size_t size,bool init) {u8 tag =get_tag (addr);
@@ -389,7 +389,7 @@ static inline void kasan_unpoison(constreturn ; size =round_up (size,KASAN_GRANULE_SIZE ); -hw_set_mem_tag_range ((void *)addr, size, tag, false); +hw_set_mem_tag_range ((void *)addr, size, tag, init); }static inline bool kasan_byte_accessible (const void *addr)
@@ -407,22 +407,24 @@ static inline bool kasan_byte_accessible* @addr - range start address, must be aligned to KASAN_GRANULE_SIZE * @size - range size, must be aligned to KASAN_GRANULE_SIZE * @value - value that's written to metadata for the range +* @init - whether to initialize the memory range (only for hardware tag-based) * * The size gets aligned to KASAN_GRANULE_SIZE before marking the range. */ -void kasan_poison (const void *addr,size_t size,u8 value); +void kasan_poison (const void *addr,size_t size,u8 value,bool init);/** * kasan_unpoison - mark the memory range as accessible * @addr - range start address, must be aligned to KASAN_GRANULE_SIZE * @size - range size, can be unaligned +* @init - whether to initialize the memory range (only for hardware tag-based) * * For the tag-based modes, the @size gets aligned to KASAN_GRANULE_SIZE before * marking the range. * For the generic mode, the last granule of the memory range gets partially * unpoisoned based on the @size. */ -void kasan_unpoison (const void *addr,size_t size); +void kasan_unpoison (const void *addr,size_t size,bool init);bool kasan_byte_accessible (const void *addr);
mm/kasan/shadow.c renamed from mm/kasan/shadow.c~kasan-init-memory-in-kasan_unpoison-for-hw_tags +5 -5
@@ -69,7 +69,7 @@ void *memcpy(void *dest, const void *srcreturn __memcpy (dest, src, len); } -void kasan_poison (const void *addr,size_t size,u8 value) +void kasan_poison (const void *addr,size_t size,u8 value,bool init) {void *shadow_start, *shadow_end;
@@ -106,7 +106,7 @@ void kasan_poison_last_granule(const voi }#endif -void kasan_unpoison (const void *addr,size_t size) +void kasan_unpoison (const void *addr,size_t size,bool init) {u8 tag =get_tag (addr);
@@ -129,7 +129,7 @@ void kasan_unpoison(const void *addr, sireturn ;/* Unpoison all granules that cover the object. */ -kasan_poison (addr,round_up (size,KASAN_GRANULE_SIZE ), tag); +kasan_poison (addr,round_up (size,KASAN_GRANULE_SIZE ), tag, false);/* Partially poison the last granule for the generic mode. */ if (IS_ENABLED (CONFIG_KASAN_GENERIC ))
@@ -344,7 +344,7 @@ void kasan_poison_vmalloc(const void *streturn ; size =round_up (size,KASAN_GRANULE_SIZE ); -kasan_poison (start, size,KASAN_VMALLOC_INVALID ); +kasan_poison (start, size,KASAN_VMALLOC_INVALID , false); }void kasan_unpoison_vmalloc (const void *start,unsigned long size)
@@ -352,7 +352,7 @@ void kasan_unpoison_vmalloc(const void *if (!is_vmalloc_or_module_addr (start))return ; -kasan_unpoison (start, size); +kasan_unpoison (start, size, false); } static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr,
mm/kasan/sw_tags.c renamed from mm/kasan/sw_tags.c~kasan-init-memory-in-kasan_unpoison-for-hw_tags +1 -1
@@ -163,7 +163,7 @@ EXPORT_SYMBOL(__hwasan_storeN_noabort);void __hwasan_tag_memory (unsigned long addr,u8 tag,unsigned long size) { -kasan_poison ((void *)addr, size, tag); +kasan_poison ((void *)addr, size, tag, false); }EXPORT_SYMBOL (__hwasan_tag_memory);
_
Andrew Morton
maintainerPATCH 133/178 kasan, mm: integrate page_alloc init with HW_TAGS
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan, mm: integrate page_alloc init with HW_TAGS
This change uses the previously added memory initialization feature of HW_TAGS KASAN routines for page_alloc memory when init_on_alloc/free is enabled.
With this change, kernel_init_free_pages() is no longer called when both HW_TAGS KASAN and init_on_alloc/free are enabled. Instead, memory is initialized in KASAN runtime.
To avoid discrepancies with which memory gets initialized that can be caused by future changes, both KASAN and kernel_init_free_pages() hooks are put together and a warning comment is added.
This patch changes the order in which memory initialization and page poisoning hooks are called. This doesn't lead to any side-effects, as whenever page poisoning is enabled, memory initialization gets disabled.
Combining setting allocation tags with memory initialization improves HW_TAGS KASAN performance when init_on_alloc/free is enabled.
---
include/linux/kasan.h | 30 +++++++++++++++++++++-------- mm/kasan/common.c | 8 +++---- mm/mempool.c | 4 +-- mm/page_alloc.c | 41 ++++++++++++++++++++++++++++++---------- 4 files changed, 59 insertions(+), 24 deletions(-)
include/linux/kasan.h renamed from include/linux/kasan.h~kasan-mm-integrate-page_alloc-init-with-hw_tags +22 -8
@@ -96,6 +96,11 @@ static __always_inline bool kasan_enable return static_branch_likely(&kasan_flag_enabled); } +static inline bool kasan_has_integrated_init (void ) +{ +return kasan_enabled (); +} +#else /* CONFIG_KASAN_HW_TAGS */ static inline bool kasan_enabled (void )
@@ -103,6 +108,11 @@ static inline bool kasan_enabled(void) return true; } +static inline bool kasan_has_integrated_init (void ) +{ +return false; +} +#endif /* CONFIG_KASAN_HW_TAGS */ slab_flags_t __kasan_never_merge (void );
@@ -120,20 +130,20 @@ static __always_inline void kasan_unpois__kasan_unpoison_range (addr, size); } -void __kasan_alloc_pages (struct page *page,unsigned int order); +void __kasan_alloc_pages (struct page *page,unsigned int order,bool init); static __always_inline void kasan_alloc_pages(struct page *page, -unsigned int order) +unsigned int order,bool init) {if (kasan_enabled ()) -__kasan_alloc_pages (page, order); +__kasan_alloc_pages (page, order, init); } -void __kasan_free_pages (struct page *page,unsigned int order); +void __kasan_free_pages (struct page *page,unsigned int order,bool init); static __always_inline void kasan_free_pages(struct page *page, -unsigned int order) +unsigned int order,bool init) {if (kasan_enabled ()) -__kasan_free_pages (page, order); +__kasan_free_pages (page, order, init); } void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
@@ -277,13 +287,17 @@ static inline bool kasan_enabled(void) {return false; } +static inline bool kasan_has_integrated_init (void ) +{ +return false; +}static inline slab_flags_t kasan_never_merge (void ) {return 0 ; }static inline void kasan_unpoison_range (const void *address,size_t size) {} -static inline void kasan_alloc_pages (struct page *page,unsigned int order) {} -static inline void kasan_free_pages (struct page *page,unsigned int order) {} +static inline void kasan_alloc_pages (struct page *page,unsigned int order,bool init) {} +static inline void kasan_free_pages (struct page *page,unsigned int order,bool init) {}static inline void kasan_cache_create (struct kmem_cache *cache,unsigned int *size,slab_flags_t *flags) {}
mm/kasan/common.c renamed from mm/kasan/common.c~kasan-mm-integrate-page_alloc-init-with-hw_tags +4 -4
@@ -97,7 +97,7 @@ slab_flags_t __kasan_never_merge(void)return 0 ; } -void __kasan_alloc_pages (struct page *page,unsigned int order) +void __kasan_alloc_pages (struct page *page,unsigned int order,bool init) {u8 tag;unsigned long i;
@@ -108,14 +108,14 @@ void __kasan_alloc_pages(struct page *pa tag =kasan_random_tag ();for (i =0 ; i < (1 << order); i++)page_kasan_tag_set (page + i, tag); -kasan_unpoison (page_address (page),PAGE_SIZE << order, false); +kasan_unpoison (page_address (page),PAGE_SIZE << order, init); } -void __kasan_free_pages (struct page *page,unsigned int order) +void __kasan_free_pages (struct page *page,unsigned int order,bool init) {if (likely (!PageHighMem (page)))kasan_poison (page_address (page),PAGE_SIZE << order, -KASAN_FREE_PAGE , false); +KASAN_FREE_PAGE , init); }/*
mm/mempool.c renamed from mm/mempool.c~kasan-mm-integrate-page_alloc-init-with-hw_tags +2 -2
@@ -106,7 +106,7 @@ static __always_inline void kasan_poisonif (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)kasan_slab_free_mempool (element);else if (pool->alloc == mempool_alloc_pages) -kasan_free_pages (element, (unsigned long )pool->pool_data); +kasan_free_pages (element, (unsigned long )pool->pool_data, false); }static void kasan_unpoison_element (mempool_t *pool,void *element)
@@ -114,7 +114,7 @@ static void kasan_unpoison_element(mempoif (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)kasan_unpoison_range (element,__ksize (element));else if (pool->alloc == mempool_alloc_pages) -kasan_alloc_pages (element, (unsigned long )pool->pool_data); +kasan_alloc_pages (element, (unsigned long )pool->pool_data, false); } static __always_inline void add_element(mempool_t *pool, void *element)
mm/page_alloc.c renamed from mm/page_alloc.c~kasan-mm-integrate-page_alloc-init-with-hw_tags +31 -10
96 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 134/178 kasan, mm: integrate slab init_on_alloc with HW_TAGS
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan, mm: integrate slab init_on_alloc with HW_TAGS
This change uses the previously added memory initialization feature of HW_TAGS KASAN routines for slab memory when init_on_alloc is enabled.
With this change, memory initialization memset() is no longer called when both HW_TAGS KASAN and init_on_alloc are enabled. Instead, memory is initialized in KASAN runtime.
The memory initialization memset() is moved into slab_post_alloc_hook() that currently directly follows the initialization loop. A new argument is added to slab_post_alloc_hook() that indicates whether to initialize the memory or not.
To avoid discrepancies with which memory gets initialized that can be caused by future changes, both KASAN hook and initialization memset() are put together and a warning comment is added.
Combining setting allocation tags with memory initialization improves HW_TAGS KASAN performance when init_on_alloc is enabled.
---
include/linux/kasan.h | 8 ++++---- mm/kasan/common.c | 4 ++-- mm/slab.c | 28 +++++++++++++--------------- mm/slab.h | 17 +++++++++++++---- mm/slub.c | 27 +++++++++++---------------- 5 files changed, 43 insertions(+), 41 deletions(-)
include/linux/kasan.h renamed from include/linux/kasan.h~kasan-mm-integrate-slab-init_on_alloc-with-hw_tags +4 -4
@@ -226,12 +226,12 @@ static __always_inline void kasan_slab_f }void *__must_check __kasan_slab_alloc (struct kmem_cache *s, -void *object,gfp_t flags); +void *object,gfp_t flags,bool init); static __always_inline void * __must_check kasan_slab_alloc( -struct kmem_cache *s,void *object,gfp_t flags) +struct kmem_cache *s,void *object,gfp_t flags,bool init) {if (kasan_enabled ()) -return __kasan_slab_alloc (s, object, flags); +return __kasan_slab_alloc (s, object, flags, init);return object; }
@@ -320,7 +320,7 @@ static inline bool kasan_slab_free(strucstatic inline void kasan_kfree_large (void *ptr) {}static inline void kasan_slab_free_mempool (void *ptr) {}static inline void *kasan_slab_alloc (struct kmem_cache *s,void *object, -gfp_t flags) +gfp_t flags,bool init) {return object; }
mm/kasan/common.c renamed from mm/kasan/common.c~kasan-mm-integrate-slab-init_on_alloc-with-hw_tags +2 -2
@@ -428,7 +428,7 @@ static void set_alloc_info(struct kmem_c }void *__must_check __kasan_slab_alloc (struct kmem_cache *cache, -void *object,gfp_t flags) +void *object,gfp_t flags,bool init) {u8 tag;void *tagged_object;
@@ -453,7 +453,7 @@ void * __must_check __kasan_slab_alloc(s* Unpoison the whole object. * For kmalloc() allocations, kasan_kmalloc() will do precise poisoning. */ -kasan_unpoison (tagged_object, cache->object_size, false); +kasan_unpoison (tagged_object, cache->object_size, init);/* Save alloc info (if possible) for non-kmalloc() allocations. */ if (kasan_stack_collection_enabled ())
mm/slab.c renamed from mm/slab.c~kasan-mm-integrate-slab-init_on_alloc-with-hw_tags +13 -15
@@ -3216,6 +3216,7 @@ slab_alloc_node(struct kmem_cache *cachevoid *ptr;int slab_node =numa_mem_id ();struct obj_cgroup *objcg =NULL ; +bool init = false; flags &= gfp_allowed_mask; cachep =slab_pre_alloc_hook (cachep, &objcg,1 , flags);
@@ -3254,12 +3255,10 @@ slab_alloc_node(struct kmem_cache *cache out:local_irq_restore (save_flags); ptr =cache_alloc_debugcheck_after (cachep, flags, ptr, caller); - -if (unlikely (slab_want_init_on_alloc (flags, cachep)) && ptr) -memset (ptr,0 , cachep->object_size); + init =slab_want_init_on_alloc (flags, cachep); out_hooks: -slab_post_alloc_hook (cachep, objcg, flags,1 , &ptr); +slab_post_alloc_hook (cachep, objcg, flags,1 , &ptr, init);return ptr; }
@@ -3301,6 +3300,7 @@ slab_alloc(struct kmem_cache *cachep, gfunsigned long save_flags;void *objp;struct obj_cgroup *objcg =NULL ; +bool init = false; flags &= gfp_allowed_mask; cachep =slab_pre_alloc_hook (cachep, &objcg,1 , flags);
@@ -3317,12 +3317,10 @@ slab_alloc(struct kmem_cache *cachep, gflocal_irq_restore (save_flags); objp =cache_alloc_debugcheck_after (cachep, flags, objp, caller);prefetchw (objp); - -if (unlikely (slab_want_init_on_alloc (flags, cachep)) && objp) -memset (objp,0 , cachep->object_size); + init =slab_want_init_on_alloc (flags, cachep); out: -slab_post_alloc_hook (cachep, objcg, flags,1 , &objp); +slab_post_alloc_hook (cachep, objcg, flags,1 , &objp, init);return objp; }
@@ -3542,18 +3540,18 @@ int kmem_cache_alloc_bulk(struct kmem_cacache_alloc_debugcheck_after_bulk (s, flags, size, p, _RET_IP_); -/* Clear memory outside IRQ disabled section */ -if (unlikely (slab_want_init_on_alloc (flags, s))) -for (i =0 ; i < size; i++) -memset (p[i],0 , s->object_size); - -slab_post_alloc_hook (s, objcg, flags, size, p); +/* +* memcg and kmem_cache debug support and memory initialization. +* Done outside of the IRQ disabled section. +*/ +slab_post_alloc_hook (s, objcg, flags, size, p, +slab_want_init_on_alloc (flags, s));/* FIXME: Trace call missing. Christoph would like a bulk variant */ return size; error:local_irq_enable ();cache_alloc_debugcheck_after_bulk (s, flags, i, p, _RET_IP_); -slab_post_alloc_hook (s, objcg, flags, i, p); +slab_post_alloc_hook (s, objcg, flags, i, p, false);__kmem_cache_free_bulk (s, i, p);return 0 ; }
mm/slab.h renamed from mm/slab.h~kasan-mm-integrate-slab-init_on_alloc-with-hw_tags +13 -4
@@ -509,15 +509,24 @@ static inline struct kmem_cache *slab_pr }static inline void slab_post_alloc_hook (struct kmem_cache *s, -struct obj_cgroup *objcg, -gfp_t flags,size_t size,void **p) +struct obj_cgroup *objcg,gfp_t flags, +size_t size,void **p,bool init) {size_t i; flags &= gfp_allowed_mask; + +/* +* As memory initialization might be integrated into KASAN, +* kasan_slab_alloc and initialization memset must be +* kept together to avoid discrepancies in behavior. +* +* As p[i] might get tagged, memset and kmemleak hook come after KASAN. +*/ for (i =0 ; i < size; i++) { - p[i] =kasan_slab_alloc (s, p[i], flags); -/* As p[i] might get tagged, call kmemleak hook after KASAN. */ + p[i] =kasan_slab_alloc (s, p[i], flags, init); +if (p[i] && init && !kasan_has_integrated_init ()) +memset (p[i],0 , s->object_size);kmemleak_alloc_recursive (p[i], s->object_size,1 , s->flags, flags); }
mm/slub.c renamed from mm/slub.c~kasan-mm-integrate-slab-init_on_alloc-with-hw_tags +11 -16
@@ -2835,6 +2835,7 @@ static __always_inline void *slab_alloc_struct page *page;unsigned long tid;struct obj_cgroup *objcg =NULL ; +bool init = false; s =slab_pre_alloc_hook (s, &objcg,1 , gfpflags);if (!s)
@@ -2912,12 +2913,10 @@ redo: }maybe_wipe_obj_freeptr (s, object); - -if (unlikely (slab_want_init_on_alloc (gfpflags, s)) && object) -memset (kasan_reset_tag (object),0 , s->object_size); + init =slab_want_init_on_alloc (gfpflags, s); out: -slab_post_alloc_hook (s, objcg, gfpflags,1 , &object); +slab_post_alloc_hook (s, objcg, gfpflags,1 , &object, init);return object; }
@@ -3369,20 +3368,16 @@ int kmem_cache_alloc_bulk(struct kmem_ca c->tid =next_tid (c->tid);local_irq_enable (); -/* Clear memory outside IRQ disabled fastpath loop */ -if (unlikely (slab_want_init_on_alloc (flags, s))) { -int j; - -for (j =0 ; j < i; j++) -memset (kasan_reset_tag (p[j]),0 , s->object_size); - } - -/* memcg and kmem_cache debug support */ -slab_post_alloc_hook (s, objcg, flags, size, p); +/* +* memcg and kmem_cache debug support and memory initialization. +* Done outside of the IRQ disabled fastpath loop. +*/ +slab_post_alloc_hook (s, objcg, flags, size, p, +slab_want_init_on_alloc (flags, s));return i; error:local_irq_enable (); -slab_post_alloc_hook (s, objcg, flags, i, p); +slab_post_alloc_hook (s, objcg, flags, i, p, false);__kmem_cache_free_bulk (s, i, p);return 0 ; }
@@ -3592,7 +3587,7 @@ static void early_kmem_cache_node_alloc(init_object (kmem_cache_node, n,SLUB_RED_ACTIVE );init_tracking (kmem_cache_node, n);#endif - n =kasan_slab_alloc (kmem_cache_node, n,GFP_KERNEL ); + n =kasan_slab_alloc (kmem_cache_node, n,GFP_KERNEL , false); page->freelist =get_freepointer (kmem_cache_node, n); page->inuse =1 ; page->frozen =0 ;
_
Andrew Morton
maintainerPATCH 135/178 kasan, mm: integrate slab init_on_free with HW_TAGS
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan, mm: integrate slab init_on_free with HW_TAGS
This change uses the previously added memory initialization feature of HW_TAGS KASAN routines for slab memory when init_on_free is enabled.
With this change, memory initialization memset() is no longer called when both HW_TAGS KASAN and init_on_free are enabled. Instead, memory is initialized in KASAN runtime.
For SLUB, the memory initialization memset() is moved into slab_free_hook() that currently directly follows the initialization loop. A new argument is added to slab_free_hook() that indicates whether to initialize the memory or not.
To avoid discrepancies with which memory gets initialized that can be caused by future changes, both KASAN hook and initialization memset() are put together and a warning comment is added.
Combining setting allocation tags with memory initialization improves HW_TAGS KASAN performance when init_on_free is enabled.
---
include/linux/kasan.h | 10 +++++---- mm/kasan/common.c | 13 ++++++------ mm/slab.c | 15 ++++++++++--- mm/slub.c | 43 ++++++++++++++++++++++------------------ 4 files changed, 48 insertions(+), 33 deletions(-)
include/linux/kasan.h renamed from include/linux/kasan.h~kasan-mm-integrate-slab-init_on_free-with-hw_tags +6 -4
@@ -203,11 +203,13 @@ static __always_inline void * __must_chereturn (void *)object; } -bool __kasan_slab_free (struct kmem_cache *s,void *object,unsigned long ip); -static __always_inline bool kasan_slab_free(struct kmem_cache *s, void *object) +bool __kasan_slab_free (struct kmem_cache *s,void *object, +unsigned long ip,bool init); +static __always_inline bool kasan_slab_free(struct kmem_cache *s, +void *object,bool init) {if (kasan_enabled ()) -return __kasan_slab_free (s, object, _RET_IP_); +return __kasan_slab_free (s, object, _RET_IP_, init);return false; }
@@ -313,7 +315,7 @@ static inline void *kasan_init_slab_obj( {return (void *)object; } -static inline bool kasan_slab_free (struct kmem_cache *s,void *object) +static inline bool kasan_slab_free (struct kmem_cache *s,void *object,bool init) {return false; }
mm/kasan/common.c renamed from mm/kasan/common.c~kasan-mm-integrate-slab-init_on_free-with-hw_tags +7 -6
@@ -322,8 +322,8 @@ void * __must_check __kasan_init_slab_obreturn (void *)object; } -static inline bool ____kasan_slab_free (struct kmem_cache *cache, -void *object,unsigned long ip,bool quarantine) +static inline bool ____kasan_slab_free (struct kmem_cache *cache,void *object, +unsigned long ip,bool quarantine,bool init) {u8 tag;void *tagged_object;
@@ -351,7 +351,7 @@ static inline bool ____kasan_slab_free(s }kasan_poison (object,round_up (cache->object_size,KASAN_GRANULE_SIZE ), -KASAN_KMALLOC_FREE , false); +KASAN_KMALLOC_FREE , init);if ((IS_ENABLED (CONFIG_KASAN_GENERIC ) && !quarantine))return false;
@@ -362,9 +362,10 @@ static inline bool ____kasan_slab_free(sreturn kasan_quarantine_put (cache, object); } -bool __kasan_slab_free (struct kmem_cache *cache,void *object,unsigned long ip) +bool __kasan_slab_free (struct kmem_cache *cache,void *object, +unsigned long ip,bool init) { -return ____kasan_slab_free (cache, object, ip, true); +return ____kasan_slab_free (cache, object, ip, true, init); }static inline bool ____kasan_kfree_large (void *ptr,unsigned long ip)
@@ -409,7 +410,7 @@ void __kasan_slab_free_mempool(void *ptrreturn ;kasan_poison (ptr,page_size (page),KASAN_FREE_PAGE , false); }else { -____kasan_slab_free (page->slab_cache, ptr, ip, false); +____kasan_slab_free (page->slab_cache, ptr, ip, false, false); } }
mm/slab.c renamed from mm/slab.c~kasan-mm-integrate-slab-init_on_free-with-hw_tags +11 -4
@@ -3425,17 +3425,24 @@ free_done: static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,unsigned long caller) { +bool init; +if (is_kfence_address (objp)) {kmemleak_free_recursive (objp, cachep->flags);__kfence_free (objp);return ; } -if (unlikely (slab_want_init_on_free (cachep))) +/* +* As memory initialization might be integrated into KASAN, +* kasan_slab_free and initialization memset must be +* kept together to avoid discrepancies in behavior. +*/ + init =slab_want_init_on_free (cachep); +if (init && !kasan_has_integrated_init ())memset (objp,0 , cachep->object_size); - -/* Put the object into the quarantine, don't touch it for now. */ -if (kasan_slab_free (cachep, objp)) +/* KASAN might put objp into memory quarantine, delaying its reuse. */ +if (kasan_slab_free (cachep, objp, init))return ;/* Use KCSAN to help debug racy use-after-free. */
mm/slub.c renamed from mm/slub.c~kasan-mm-integrate-slab-init_on_free-with-hw_tags +24 -19
76 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 136/178 kasan: docs: clean up sections
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: clean up sections
Update KASAN documentation:
- Give some sections clearer names.
- Remove unneeded subsections in the "Tests" section.
- Move the "For developers" section and split into subsections.
---
Documentation/dev-tools/kasan.rst | 54 ++++++++++++++-------------- 1 file changed, 27 insertions(+), 27 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-clean-up-sections +27 -27
@@ -177,24 +177,6 @@ particular KASAN features. report or also panic the kernel (default: ``report``). Note, that tag checking gets disabled after the first reported bug. -For developers -~~~~~~~~~~~~~~ - -Software KASAN modes use compiler instrumentation to insert validity checks. -Such instrumentation might be incompatible with some part of the kernel, and -therefore needs to be disabled. To disable instrumentation for specific files -or directories, add a line similar to the following to the respective kernel -Makefile: - -- For a single file (e.g. main.o):: - - KASAN_SANITIZE_main.o := n - -- For all files in one directory:: - - KASAN_SANITIZE := n - - Implementation details ----------------------
@@ -308,8 +290,8 @@ support MTE (but supports TBI). Hardware tag-based KASAN only reports the first found bug. After that MTE tag checking gets disabled. -What memory accesses are sanitised by KASAN? --------------------------------------------- +Shadow memory +------------- The kernel maps memory in a number of different parts of the address space. This poses something of a problem for KASAN, which requires
@@ -320,8 +302,8 @@ The range of kernel virtual addresses is real memory to support a real shadow region for every address that could be accessed by the kernel. -By default -~~~~~~~~~~ +Default behaviour +~~~~~~~~~~~~~~~~~ By default, architectures only map real memory over the shadow region for the linear mapping (and potentially other small areas). For all
@@ -371,8 +353,29 @@ unmapped. This will require changes in a This allows ``VMAP_STACK`` support on x86, and can simplify support of architectures that do not have a fixed module region. -CONFIG_KASAN_KUNIT_TEST and CONFIG_KASAN_MODULE_TEST ----------------------------------------------------- +For developers +-------------- + +Ignoring accesses +~~~~~~~~~~~~~~~~~ + +Software KASAN modes use compiler instrumentation to insert validity checks. +Such instrumentation might be incompatible with some part of the kernel, and +therefore needs to be disabled. To disable instrumentation for specific files +or directories, add a line similar to the following to the respective kernel +Makefile: + +- For a single file (e.g. main.o):: + + KASAN_SANITIZE_main.o := n + +- For all files in one directory:: + + KASAN_SANITIZE := n + + +Tests +~~~~~ KASAN tests consist of two parts:
@@ -418,21 +421,18 @@ Or, if one of the tests failed:: There are a few ways to run KUnit-compatible KASAN tests. 1. Loadable module -~~~~~~~~~~~~~~~~~~ With ``CONFIG_KUNIT`` enabled, ``CONFIG_KASAN_KUNIT_TEST`` can be built as a loadable module and run on any architecture that supports KASAN by loading the module with insmod or modprobe. The module is called ``test_kasan``. 2. Built-In -~~~~~~~~~~~ With ``CONFIG_KUNIT`` built-in, ``CONFIG_KASAN_KUNIT_TEST`` can be built-in on any architecure that supports KASAN. These and any other KUnit tests enabled will run and print the results at boot as a late-init call. 3. Using kunit_tool -~~~~~~~~~~~~~~~~~~~ With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it's also possible use ``kunit_tool`` to see the results of these and other KUnit tests
_
Andrew Morton
maintainerPATCH 137/178 kasan: docs: update overview section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update overview section
Update the "Overview" section in KASAN documentation:
- Outline main use cases for each mode.
- Mention that HW_TAGS mode need compiler support too.
- Move the part about SLUB/SLAB support from "Usage" to "Overview".
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-overview-section +19 -8
@@ -11,17 +11,31 @@ designed to find out-of-bound and use-af 2. software tag-based KASAN (similar to userspace HWASan), 3. hardware tag-based KASAN (based on hardware memory tagging). -Software KASAN modes (1 and 2) use compile-time instrumentation to insert -validity checks before every memory access, and therefore require a compiler +Generic KASAN is mainly used for debugging due to a large memory overhead. +Software tag-based KASAN can be used for dogfood testing as it has a lower +memory overhead that allows using it with real workloads. Hardware tag-based +KASAN comes with low memory and performance overheads and, therefore, can be +used in production. Either as an in-field memory bug detector or as a security +mitigation. + +Software KASAN modes (#1 and #2) use compile-time instrumentation to insert +validity checks before every memory access and, therefore, require a compiler version that supports that. -Generic KASAN is supported in both GCC and Clang. With GCC it requires version +Generic KASAN is supported in GCC and Clang. With GCC, it requires version 8.3.0 or later. Any supported Clang version is compatible, but detection of out-of-bounds accesses for global variables is only supported since Clang 11. -Tag-based KASAN is only supported in Clang. +Software tag-based KASAN mode is only supported in Clang. + +The hardware KASAN mode (#3) relies on hardware to perform the checks but +still requires a compiler version that supports memory tagging instructions. +This mode is supported in GCC 10+ and Clang 11+. -Currently generic KASAN is supported for the x86_64, arm, arm64, xtensa, s390 +Both software KASAN modes work with SLUB and SLAB memory allocators, +while the hardware tag-based KASAN currently only supports SLUB. + +Currently, generic KASAN is supported for the x86_64, arm, arm64, xtensa, s390, and riscv architectures, and tag-based KASAN modes are supported only for arm64. Usage
@@ -39,9 +53,6 @@ For software modes, you also need to cho CONFIG_KASAN_INLINE. Outline and inline are compiler instrumentation types. The former produces smaller binary while the latter is 1.1 - 2 times faster. -Both software KASAN modes work with both SLUB and SLAB memory allocators, -while the hardware tag-based KASAN currently only support SLUB. - For better error reports that include stack traces, enable CONFIG_STACKTRACE. To augment reports with last allocation and freeing stack of the physical page,
_
Andrew Morton
maintainerPATCH 138/178 kasan: docs: update usage section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update usage section
Update the "Usage" section in KASAN documentation:
- Add inline code snippet markers.
- Reword the part about stack traces for clarity.
- Other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-usage-section +11 -12
@@ -41,22 +41,21 @@ and riscv architectures, and tag-based K Usage ----- -To enable KASAN configure kernel with:: +To enable KASAN, configure the kernel with:: - CONFIG_KASAN = y + CONFIG_KASAN=y -and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN), -CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN), and -CONFIG_KASAN_HW_TAGS (to enable hardware tag-based KASAN). +and choose between ``CONFIG_KASAN_GENERIC`` (to enable generic KASAN), +``CONFIG_KASAN_SW_TAGS`` (to enable software tag-based KASAN), and +``CONFIG_KASAN_HW_TAGS`` (to enable hardware tag-based KASAN). -For software modes, you also need to choose between CONFIG_KASAN_OUTLINE and -CONFIG_KASAN_INLINE. Outline and inline are compiler instrumentation types. -The former produces smaller binary while the latter is 1.1 - 2 times faster. +For software modes, also choose between ``CONFIG_KASAN_OUTLINE`` and +``CONFIG_KASAN_INLINE``. Outline and inline are compiler instrumentation types. +The former produces a smaller binary while the latter is 1.1-2 times faster. -For better error reports that include stack traces, enable CONFIG_STACKTRACE. - -To augment reports with last allocation and freeing stack of the physical page, -it is recommended to enable also CONFIG_PAGE_OWNER and boot with page_owner=on. +To include alloc and free stack traces of affected slab objects into reports, +enable ``CONFIG_STACKTRACE``. To include alloc and free stack traces of affected +physical pages, enable ``CONFIG_PAGE_OWNER`` and boot with ``page_owner=on``. Error reports ~~~~~~~~~~~~~
_
Andrew Morton
maintainerPATCH 139/178 kasan: docs: update error reports section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update error reports section
Update the "Error reports" section in KASAN documentation:
- Mention that bug titles are best-effort.
- Move and reword the part about auxiliary stacks from "Implementation details".
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 46 +++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-error-reports-section +26 -20
@@ -60,7 +60,7 @@ physical pages, enable ``CONFIG_PAGE_OWN Error reports ~~~~~~~~~~~~~ -A typical out-of-bounds access generic KASAN report looks like this:: +A typical KASAN report looks like this:: ================================================================== BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan]
@@ -133,33 +133,43 @@ A typical out-of-bounds access generic K ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc ================================================================== -The header of the report provides a short summary of what kind of bug happened -and what kind of access caused it. It's followed by a stack trace of the bad -access, a stack trace of where the accessed memory was allocated (in case bad -access happens on a slab object), and a stack trace of where the object was -freed (in case of a use-after-free bug report). Next comes a description of -the accessed slab object and information about the accessed memory page. +The report header summarizes what kind of bug happened and what kind of access +caused it. It is followed by a stack trace of the bad access, a stack trace of +where the accessed memory was allocated (in case a slab object was accessed), +and a stack trace of where the object was freed (in case of a use-after-free +bug report). Next comes a description of the accessed slab object and the +information about the accessed memory page. -In the last section the report shows memory state around the accessed address. -Internally KASAN tracks memory state separately for each memory granule, which +In the end, the report shows the memory state around the accessed address. +Internally, KASAN tracks memory state separately for each memory granule, which is either 8 or 16 aligned bytes depending on KASAN mode. Each number in the memory state section of the report shows the state of one of the memory granules that surround the accessed address. -For generic KASAN the size of each memory granule is 8. The state of each +For generic KASAN, the size of each memory granule is 8. The state of each granule is encoded in one shadow byte. Those 8 bytes can be accessible, -partially accessible, freed or be a part of a redzone. KASAN uses the following -encoding for each shadow byte: 0 means that all 8 bytes of the corresponding +partially accessible, freed, or be a part of a redzone. KASAN uses the following +encoding for each shadow byte: 00 means that all 8 bytes of the corresponding memory region are accessible; number N (1 <= N <= 7) means that the first N bytes are accessible, and other (8 - N) bytes are not; any negative value indicates that the entire 8-byte word is inaccessible. KASAN uses different negative values to distinguish between different kinds of inaccessible memory like redzones or freed memory (see mm/kasan/kasan.h). -In the report above the arrows point to the shadow byte 03, which means that -the accessed address is partially accessible. For tag-based KASAN modes this -last report section shows the memory tags around the accessed address -(see the `Implementation details`_ section). +In the report above, the arrow points to the shadow byte ``03``, which means +that the accessed address is partially accessible. + +For tag-based KASAN modes, this last report section shows the memory tags around +the accessed address (see the `Implementation details`_ section). + +Note that KASAN bug titles (like ``slab-out-of-bounds`` or ``use-after-free``) +are best-effort: KASAN prints the most probable bug type based on the limited +information it has. The actual type of the bug might be different. + +Generic KASAN also reports up to two auxiliary call stack traces. These stack +traces point to places in code that interacted with the object but that are not +directly present in the bad access stack trace. Currently, this includes +call_rcu() and workqueue queuing. Boot parameters ~~~~~~~~~~~~~~~
@@ -223,10 +233,6 @@ function calls GCC directly inserts the This option significantly enlarges kernel but it gives x1.1-x2 performance boost over outline instrumented kernel. -Generic KASAN also reports the last 2 call stacks to creation of work that -potentially has access to an object. Call stacks for the following are shown: -call_rcu() and workqueue queuing. - Generic KASAN is the only mode that delays the reuse of freed object via quarantine (see mm/kasan/quarantine.c for implementation).
_
Andrew Morton
maintainerPATCH 140/178 kasan: docs: update boot parameters section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update boot parameters section
Update the "Boot parameters" section in KASAN documentation:
- Mention panic_on_warn.
- Mention kasan_multi_shot and its interaction with panic_on_warn.
- Clarify kasan.fault=panic interaction with panic_on_warn.
- A readability clean-up.
---
Documentation/dev-tools/kasan.rst | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-boot-parameters-section +10 -4
@@ -174,10 +174,16 @@ call_rcu() and workqueue queuing. Boot parameters ~~~~~~~~~~~~~~~ +KASAN is affected by the generic ``panic_on_warn`` command line parameter. +When it is enabled, KASAN panics the kernel after printing a bug report. + +By default, KASAN prints a bug report only for the first invalid memory access. +With ``kasan_multi_shot``, KASAN prints a report on every invalid access. This +effectively disables ``panic_on_warn`` for KASAN reports. + Hardware tag-based KASAN mode (see the section about various modes below) is intended for use in production as a security mitigation. Therefore, it supports -boot parameters that allow to disable KASAN competely or otherwise control -particular KASAN features. +boot parameters that allow disabling KASAN or controlling its features. - ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``).
@@ -194,8 +200,8 @@ particular KASAN features. traces collection (default: ``on``). - ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN - report or also panic the kernel (default: ``report``). Note, that tag - checking gets disabled after the first reported bug. + report or also panic the kernel (default: ``report``). The panic happens even + if ``kasan_multi_shot`` is enabled. Implementation details ----------------------
_
Andrew Morton
maintainerPATCH 141/178 kasan: docs: update GENERIC implementation details section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update GENERIC implementation details section
Update the "Implementation details" section for generic KASAN:
- Don't mention kmemcheck, it's not present in the kernel anymore.
- Don't mention GCC as the only supported compiler.
- Update kasan_mem_to_shadow() definition to match actual code.
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 29 +++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-generic-implementation-details-section +14 -15
@@ -209,12 +209,11 @@ Implementation details Generic KASAN ~~~~~~~~~~~~~ -From a high level perspective, KASAN's approach to memory error detection is -similar to that of kmemcheck: use shadow memory to record whether each byte of -memory is safe to access, and use compile-time instrumentation to insert checks -of shadow memory on each memory access. +Software KASAN modes use shadow memory to record whether each byte of memory is +safe to access and use compile-time instrumentation to insert shadow memory +checks before each memory access. -Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (e.g. 16TB +Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (16TB to cover 128TB on x86_64) and uses direct mapping with a scale and offset to translate a memory address to its corresponding shadow address.
@@ -223,23 +222,23 @@ address:: static inline void *kasan_mem_to_shadow(const void *addr) { - return ((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) + return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET; } where ``KASAN_SHADOW_SCALE_SHIFT = 3``. Compile-time instrumentation is used to insert memory access checks. Compiler -inserts function calls (__asan_load*(addr), __asan_store*(addr)) before each -memory access of size 1, 2, 4, 8 or 16. These functions check whether memory -access is valid or not by checking corresponding shadow memory. - -GCC 5.0 has possibility to perform inline instrumentation. Instead of making -function calls GCC directly inserts the code to check the shadow memory. -This option significantly enlarges kernel but it gives x1.1-x2 performance -boost over outline instrumented kernel. +inserts function calls (``__asan_load*(addr)``, ``__asan_store*(addr)``) before +each memory access of size 1, 2, 4, 8, or 16. These functions check whether +memory accesses are valid or not by checking corresponding shadow memory. + +With inline instrumentation, instead of making function calls, the compiler +directly inserts the code to check shadow memory. This option significantly +enlarges the kernel, but it gives an x1.1-x2 performance boost over the +outline-instrumented kernel. -Generic KASAN is the only mode that delays the reuse of freed object via +Generic KASAN is the only mode that delays the reuse of freed objects via quarantine (see mm/kasan/quarantine.c for implementation). Software tag-based KASAN
_
Andrew Morton
maintainerPATCH 142/178 kasan: docs: update SW_TAGS implementation details section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update SW_TAGS implementation details section
Update the "Implementation details" section for SW_TAGS KASAN:
- Clarify the introduction sentence.
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 39 +++++++++++++--------------- 1 file changed, 19 insertions(+), 20 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-sw_tags-implementation-details-section +19 -20
@@ -244,38 +244,37 @@ quarantine (see mm/kasan/quarantine.c fo Software tag-based KASAN ~~~~~~~~~~~~~~~~~~~~~~~~ -Software tag-based KASAN requires software memory tagging support in the form -of HWASan-like compiler instrumentation (see HWASan documentation for details). - -Software tag-based KASAN is currently only implemented for arm64 architecture. +Software tag-based KASAN uses a software memory tagging approach to checking +access validity. It is currently only implemented for the arm64 architecture. Software tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs -to store a pointer tag in the top byte of kernel pointers. Like generic KASAN -it uses shadow memory to store memory tags associated with each 16-byte memory -cell (therefore it dedicates 1/16th of the kernel memory for shadow memory). +to store a pointer tag in the top byte of kernel pointers. It uses shadow memory +to store memory tags associated with each 16-byte memory cell (therefore, it +dedicates 1/16th of the kernel memory for shadow memory). -On each memory allocation software tag-based KASAN generates a random tag, tags -the allocated memory with this tag, and embeds this tag into the returned +On each memory allocation, software tag-based KASAN generates a random tag, tags +the allocated memory with this tag, and embeds the same tag into the returned pointer. Software tag-based KASAN uses compile-time instrumentation to insert checks -before each memory access. These checks make sure that tag of the memory that -is being accessed is equal to tag of the pointer that is used to access this -memory. In case of a tag mismatch software tag-based KASAN prints a bug report. +before each memory access. These checks make sure that the tag of the memory +that is being accessed is equal to the tag of the pointer that is used to access +this memory. In case of a tag mismatch, software tag-based KASAN prints a bug +report. -Software tag-based KASAN also has two instrumentation modes (outline, that -emits callbacks to check memory accesses; and inline, that performs the shadow +Software tag-based KASAN also has two instrumentation modes (outline, which +emits callbacks to check memory accesses; and inline, which performs the shadow memory checks inline). With outline instrumentation mode, a bug report is -simply printed from the function that performs the access check. With inline -instrumentation a brk instruction is emitted by the compiler, and a dedicated -brk handler is used to print bug reports. +printed from the function that performs the access check. With inline +instrumentation, a ``brk`` instruction is emitted by the compiler, and a +dedicated ``brk`` handler is used to print bug reports. Software tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through -pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently +pointers with the 0xFF pointer tag are not checked). The value 0xFE is currently reserved to tag freed memory regions. -Software tag-based KASAN currently only supports tagging of -kmem_cache_alloc/kmalloc and page_alloc memory. +Software tag-based KASAN currently only supports tagging of slab and page_alloc +memory. Hardware tag-based KASAN ~~~~~~~~~~~~~~~~~~~~~~~~
_
Andrew Morton
maintainerPATCH 143/178 kasan: docs: update HW_TAGS implementation details section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update HW_TAGS implementation details section
Update the "Implementation details" section for HW_TAGS KASAN:
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-hw_tags-implementation-details-section +13 -13
@@ -279,35 +279,35 @@ memory. Hardware tag-based KASAN ~~~~~~~~~~~~~~~~~~~~~~~~ -Hardware tag-based KASAN is similar to the software mode in concept, but uses +Hardware tag-based KASAN is similar to the software mode in concept but uses hardware memory tagging support instead of compiler instrumentation and shadow memory. Hardware tag-based KASAN is currently only implemented for arm64 architecture and based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5 -Instruction Set Architecture, and Top Byte Ignore (TBI). +Instruction Set Architecture and Top Byte Ignore (TBI). Special arm64 instructions are used to assign memory tags for each allocation. Same tags are assigned to pointers to those allocations. On every memory -access, hardware makes sure that tag of the memory that is being accessed is -equal to tag of the pointer that is used to access this memory. In case of a -tag mismatch a fault is generated and a report is printed. +access, hardware makes sure that the tag of the memory that is being accessed is +equal to the tag of the pointer that is used to access this memory. In case of a +tag mismatch, a fault is generated, and a report is printed. Hardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through -pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently +pointers with the 0xFF pointer tag are not checked). The value 0xFE is currently reserved to tag freed memory regions. -Hardware tag-based KASAN currently only supports tagging of -kmem_cache_alloc/kmalloc and page_alloc memory. +Hardware tag-based KASAN currently only supports tagging of slab and page_alloc +memory. -If the hardware doesn't support MTE (pre ARMv8.5), hardware tag-based KASAN -won't be enabled. In this case all boot parameters are ignored. +If the hardware does not support MTE (pre ARMv8.5), hardware tag-based KASAN +will not be enabled. In this case, all KASAN boot parameters are ignored. -Note, that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being -enabled. Even when kasan.mode=off is provided, or when the hardware doesn't +Note that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being +enabled. Even when ``kasan.mode=off`` is provided or when the hardware does not support MTE (but supports TBI). -Hardware tag-based KASAN only reports the first found bug. After that MTE tag +Hardware tag-based KASAN only reports the first found bug. After that, MTE tag checking gets disabled. Shadow memory
_
Andrew Morton
maintainerPATCH 144/178 kasan: docs: update shadow memory section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update shadow memory section
Update the "Shadow memory" section in KASAN documentation:
- Rearrange the introduction paragraph do it doesn't give a "KASAN has an issue" impression.
- Update the list of architectures with vmalloc support.
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 31 ++++++++++++---------------- 1 file changed, 14 insertions(+), 17 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-shadow-memory-section +14 -17
@@ -313,14 +313,11 @@ checking gets disabled. Shadow memory ------------- -The kernel maps memory in a number of different parts of the address -space. This poses something of a problem for KASAN, which requires -that all addresses accessed by instrumented code have a valid shadow -region. - -The range of kernel virtual addresses is large: there is not enough -real memory to support a real shadow region for every address that -could be accessed by the kernel. +The kernel maps memory in several different parts of the address space. +The range of kernel virtual addresses is large: there is not enough real +memory to support a real shadow region for every address that could be +accessed by the kernel. Therefore, KASAN only maps real shadow for certain +parts of the address space. Default behaviour ~~~~~~~~~~~~~~~~~
@@ -332,10 +329,9 @@ page is mapped over the shadow area. Thi declares all memory accesses as permitted. This presents a problem for modules: they do not live in the linear -mapping, but in a dedicated module space. By hooking in to the module -allocator, KASAN can temporarily map real shadow memory to cover -them. This allows detection of invalid accesses to module globals, for -example. +mapping but in a dedicated module space. By hooking into the module +allocator, KASAN temporarily maps real shadow memory to cover them. +This allows detection of invalid accesses to module globals, for example. This also creates an incompatibility with ``VMAP_STACK``: if the stack lives in vmalloc space, it will be shadowed by the read-only page, and
@@ -346,9 +342,10 @@ CONFIG_KASAN_VMALLOC ~~~~~~~~~~~~~~~~~~~~ With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the -cost of greater memory usage. Currently this is only supported on x86. +cost of greater memory usage. Currently, this is supported on x86, +riscv, s390, and powerpc. -This works by hooking into vmalloc and vmap, and dynamically +This works by hooking into vmalloc and vmap and dynamically allocating real shadow memory to back the mappings. Most mappings in vmalloc space are small, requiring less than a full
@@ -367,10 +364,10 @@ memory. To avoid the difficulties around swapping mappings around, KASAN expects that the part of the shadow region that covers the vmalloc space will -not be covered by the early shadow page, but will be left -unmapped. This will require changes in arch-specific code. +not be covered by the early shadow page but will be left unmapped. +This will require changes in arch-specific code. -This allows ``VMAP_STACK`` support on x86, and can simplify support of +This allows ``VMAP_STACK`` support on x86 and can simplify support of architectures that do not have a fixed module region. For developers
_
Andrew Morton
maintainerPATCH 145/178 kasan: docs: update ignoring accesses section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update ignoring accesses section
Update the "Ignoring accesses" section in KASAN documentation:
- Mention __no_sanitize_address/noinstr.
- Mention kasan_disable/enable_current().
- Mention kasan_reset_tag()/page_kasan_tag_reset().
- Readability and punctuation clean-ups.
---
Documentation/dev-tools/kasan.rst | 34 ++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-ignoring-accesses-section +30 -4
@@ -377,12 +377,18 @@ Ignoring accesses ~~~~~~~~~~~~~~~~~ Software KASAN modes use compiler instrumentation to insert validity checks. -Such instrumentation might be incompatible with some part of the kernel, and -therefore needs to be disabled. To disable instrumentation for specific files -or directories, add a line similar to the following to the respective kernel +Such instrumentation might be incompatible with some parts of the kernel, and +therefore needs to be disabled. + +Other parts of the kernel might access metadata for allocated objects. +Normally, KASAN detects and reports such accesses, but in some cases (e.g., +in memory allocators), these accesses are valid. + +For software KASAN modes, to disable instrumentation for a specific file or +directory, add a ``KASAN_SANITIZE`` annotation to the respective kernel Makefile: -- For a single file (e.g. main.o):: +- For a single file (e.g., main.o):: KASAN_SANITIZE_main.o := n
@@ -390,6 +396,26 @@ Makefile: KASAN_SANITIZE := n +For software KASAN modes, to disable instrumentation on a per-function basis, +use the KASAN-specific ``__no_sanitize_address`` function attribute or the +generic ``noinstr`` one. + +Note that disabling compiler instrumentation (either on a per-file or a +per-function basis) makes KASAN ignore the accesses that happen directly in +that code for software KASAN modes. It does not help when the accesses happen +indirectly (through calls to instrumented functions) or with the hardware +tag-based mode that does not use compiler instrumentation. + +For software KASAN modes, to disable KASAN reports in a part of the kernel code +for the current task, annotate this part of the code with a +``kasan_disable_current()``/``kasan_enable_current()`` section. This also +disables the reports for indirect accesses that happen through function calls. + +For tag-based KASAN modes (include the hardware one), to disable access +checking, use ``kasan_reset_tag()`` or ``page_kasan_tag_reset()``. Note that +temporarily disabling access checking via ``page_kasan_tag_reset()`` requires +saving and restoring the per-page KASAN tag via +``page_kasan_tag``/``page_kasan_tag_set``. Tests ~~~~~
_
Andrew Morton
maintainerPATCH 146/178 kasan: docs: update tests section
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: docs: update tests section
Update the "Tests" section in KASAN documentation:
- Add an introductory sentence.
- Add proper indentation for the list of ways to run KUnit tests.
- Punctuation, readability, and other minor clean-ups.
---
Documentation/dev-tools/kasan.rst | 32 +++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-)
Documentation/dev-tools/kasan.rst renamed from Documentation/dev-tools/kasan.rst~kasan-docs-update-tests-section +15 -17
@@ -420,19 +420,20 @@ saving and restoring the per-page KASAN Tests ~~~~~ -KASAN tests consist of two parts: +There are KASAN tests that allow verifying that KASAN works and can detect +certain types of memory corruptions. The tests consist of two parts: 1. Tests that are integrated with the KUnit Test Framework. Enabled with ``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified -automatically in a few different ways, see the instructions below. +automatically in a few different ways; see the instructions below. 2. Tests that are currently incompatible with KUnit. Enabled with ``CONFIG_KASAN_MODULE_TEST`` and can only be run as a module. These tests can -only be verified manually, by loading the kernel module and inspecting the +only be verified manually by loading the kernel module and inspecting the kernel log for KASAN reports. -Each KUnit-compatible KASAN test prints a KASAN report if an error is detected. -Then the test prints its number and status. +Each KUnit-compatible KASAN test prints one of multiple KASAN reports if an +error is detected. Then the test prints its number and status. When a test passes::
@@ -460,27 +461,24 @@ Or, if one of the tests failed:: not ok 1 - kasan - There are a few ways to run KUnit-compatible KASAN tests. 1. Loadable module -With ``CONFIG_KUNIT`` enabled, ``CONFIG_KASAN_KUNIT_TEST`` can be built as -a loadable module and run on any architecture that supports KASAN by loading -the module with insmod or modprobe. The module is called ``test_kasan``. + With ``CONFIG_KUNIT`` enabled, KASAN-KUnit tests can be built as a loadable + module and run by loading ``test_kasan.ko`` with ``insmod`` or ``modprobe``. 2. Built-In -With ``CONFIG_KUNIT`` built-in, ``CONFIG_KASAN_KUNIT_TEST`` can be built-in -on any architecure that supports KASAN. These and any other KUnit tests enabled -will run and print the results at boot as a late-init call. + With ``CONFIG_KUNIT`` built-in, KASAN-KUnit tests can be built-in as well. + In this case, the tests will run at boot as a late-init call. 3. Using kunit_tool -With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it's also -possible use ``kunit_tool`` to see the results of these and other KUnit tests -in a more readable way. This will not print the KASAN reports of the tests that -passed. Use `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ -for more up-to-date information on ``kunit_tool``. + With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it is also + possible to use ``kunit_tool`` to see the results of KUnit tests in a more + readable way. This will not print the KASAN reports of the tests that passed. + See `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ + for more up-to-date information on ``kunit_tool``. .. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html
_
Andrew Morton
maintainerPATCH 147/178 kasan: record task_work_add() call stack
In reply to Andrew MortonFrom: Walter Wu <[email protected]>
Subject: kasan: record task_work_add() call stack
Why record task_work_add() call stack? Syzbot reports many use-after-free issues for task_work, see [1]. After seeing the free stack and the current auxiliary stack, we think they are useless, we don't know where the work was registered. This work may be the free call stack, so we miss the root cause and don't solve the use-after-free.
Add the task_work_add() call stack into the KASAN auxiliary stack in order to improve KASAN reports. It helps programmers solve use-after-free issues.
[1]: https://groups.google.com/g/syzkaller-bugs/search?q=kasan%20use-after-free%20task_work_run
---
kernel/task_work.c | 3 +++ mm/kasan/kasan.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-)
kernel/task_work.c renamed from kernel/task_work.c~task_work-kasan-record-task_work_add-call-stack +3 -0
@@ -34,6 +34,9 @@ int task_work_add(struct task_struct *ta {struct callback_head *head; +/* record the work call stack in order to print it in KASAN reports */ +kasan_record_aux_stack (work); + do { head = READ_ONCE(task->task_works); if (unlikely(head == &work_exited))
mm/kasan/kasan.h renamed from mm/kasan/kasan.h~task_work-kasan-record-task_work_add-call-stack +1 -1
@@ -163,7 +163,7 @@ struct kasan_alloc_meta {struct kasan_track alloc_track;#ifdef CONFIG_KASAN_GENERIC /* -* call_rcu() call stack is stored into struct kasan_alloc_meta. +* The auxiliary stack is stored into struct kasan_alloc_meta. * The free stack is stored into struct kasan_free_meta. */ depot_stack_handle_t aux_stack[2 ];
_
Andrew Morton
maintainerPATCH 148/178 kasan: detect false-positives in tests
In reply to Andrew MortonFrom: Andrey Konovalov <[email protected]>
Subject: kasan: detect false-positives in tests
Currently, KASAN-KUnit tests can check that a particular annotated part of code causes a KASAN report. However, they do not check that no unwanted reports happen between the annotated parts.
This patch implements these checks.
It is done by setting report_data.report_found to false in kasan_test_init() and at the end of KUNIT_EXPECT_KASAN_FAIL() and then checking that it remains false at the beginning of KUNIT_EXPECT_KASAN_FAIL() and in kasan_test_exit().
kunit_add_named_resource() call is moved to kasan_test_init(), and the value of fail_data.report_expected is kept as false in between KUNIT_EXPECT_KASAN_FAIL() annotations for consistency.
---
lib/test_kasan.c | 55 +++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 26 deletions(-)
lib/test_kasan.c renamed from lib/test_kasan.c~kasan-detect-false-positives-in-tests +29 -26
@@ -54,6 +54,10 @@ static int kasan_test_init(struct kunit multishot =kasan_save_enable_multi_shot ();kasan_set_tagging_report_once (false); + fail_data.report_found = false; + fail_data.report_expected = false; +kunit_add_named_resource (test,NULL ,NULL , &resource, +"kasan_data" , &fail_data);return 0 ; }
@@ -61,6 +65,7 @@ static void kasan_test_exit(struct kunit {kasan_set_tagging_report_once (true);kasan_restore_multi_shot (multishot); +KUNIT_EXPECT_FALSE (test, fail_data.report_found); }/**
@@ -78,33 +83,31 @@ static void kasan_test_exit(struct kunit* fields, it can reorder or optimize away the accesses to those fields. * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the * expression to prevent that. +* +* In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as +* false. This allows detecting KASAN reports that happen outside of the checks +* by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL +* and in kasan_test_exit. */ -#define KUNIT_EXPECT_KASAN_FAIL (test, expression) do { \ - if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \ - !kasan_async_mode_enabled()) \ - migrate_disable(); \ - WRITE_ONCE(fail_data.report_expected, true); \ - WRITE_ONCE(fail_data.report_found, false); \ - kunit_add_named_resource(test, \ - NULL, \ - NULL, \ - &resource, \ - "kasan_data", &fail_data); \ - barrier(); \ - expression; \ - barrier(); \ - if (kasan_async_mode_enabled()) \ - kasan_force_async_fault(); \ - barrier(); \ - KUNIT_EXPECT_EQ(test, \ - READ_ONCE(fail_data.report_expected), \ - READ_ONCE(fail_data.report_found)); \ - if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \ - !kasan_async_mode_enabled()) { \ - if (READ_ONCE(fail_data.report_found)) \ - kasan_enable_tagging_sync(); \ - migrate_enable(); \ - } \ +#define KUNIT_EXPECT_KASAN_FAIL (test, expression) do { \ + if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \ + !kasan_async_mode_enabled()) \ + migrate_disable(); \ + KUNIT_EXPECT_FALSE(test, READ_ONCE(fail_data.report_found)); \ + WRITE_ONCE(fail_data.report_expected, true); \ + barrier(); \ + expression; \ + barrier(); \ + KUNIT_EXPECT_EQ(test, \ + READ_ONCE(fail_data.report_expected), \ + READ_ONCE(fail_data.report_found)); \ + if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \ + if (READ_ONCE(fail_data.report_found)) \ + kasan_enable_tagging(); \ + migrate_enable(); \ + } \ + WRITE_ONCE(fail_data.report_found, false); \ + WRITE_ONCE(fail_data.report_expected, false); \ } while (0)#define KASAN_TEST_NEEDS_CONFIG_ON (test, config) do { \
_
Re: [patch 148/178] kasan: detect false-positives in tests
Quoted 9 lines · Andrew Morton
From: Andrey Konovalov <[email protected]>
Subject: kasan: detect false-positives in testsCurrently, KASAN-KUnit tests can check that a particular annotated part of code causes a KASAN report. However, they do not check that no unwanted reports happen between the annotated parts.
This patch implements these checks.
No.
This patch does no such thing.
This patch is untested garbage that doesn't even compile.
lib/test_kasan.c static void kasan_test_exit(struct kunit · line 107
Quoted 4 lines
lib/test_kasan.c renamed from lib/test_kasan.c~kasan-detect-false-positives-in-tests +4
-0+if (IS_ENABLED (CONFIG_KASAN_HW_TAGS )) { \ +if (READ_ONCE (fail_data.report_found)) \ +kasan_enable_tagging (); \ +migrate_enable (); \
kasan_enable_tagging() was renamed to kasan_enable_tagging_sync() in commit
2603f8a78dfb ("kasan: Add KASAN mode kernel parameter")
and this patch was clearly rebased onto current -git without any testing at all.
You can even see the effects of the rename in the patch itself, in that the code it removes has the new name:
- if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \
- !kasan_async_mode_enabled()) { \
- if (READ_ONCE(fail_data.report_found)) \
- kasan_enable_tagging_sync(); \but then the patch re-introduces the old name in the new version of that code.
Andrew, you really need to stop using your garbage workflow of randomly rebasing patches at the last minute.
In the cover letter, you clearly say:
"178 patches, based on 8ca5297e7e38f2dc8c753d33a5092e7be181fff0."
where that commit was basically my top commit as of yesterday afternoon - and that base very much has that rename in place.
So you rebased your patch series - apparently without any testing at all - since that afternoon commit.
This *HAS* to stop.
If you use your old legacy "patch series" model, then dammit, stop sending those patches on top of random commits. Pick a base, and *STICK* to it. Make sure your patch series is actually *tested*.
Because I'd much rather handle conflicts and blame myself if I screw something up during the merge, than get something from you that is untested and may be subtly - or as in this case, very much not-subtly
- broken.
Right now, the way you work, all the work that Stephen Rothwell has done to merge your patch series and have it tested in linux-next is basically made almost entirely worthless, because you end up changing your patches and rebasing it all the last minute, and all the testing it got earlier is now thrown right out the window.
What used to be a perfectly cromulent patch is now garbage that doesn't even compile.
I'm going to fix this one patch up by hand, but I really need you to stop doing this. Use the previous release as a base (ie in this case 5.12), and don't do any last-minute rebasing. If you have patches in your patch-series that depend on stuff that comes in this merge window, just drop them - or at least don't send them to me.
Or better yet, base it on something even older than the release that opens the merge window, so that you don't have to rebase anything *during* the merge window, and so that your patch series actually gets tested as-is in the real format in linux-next even before the merge window opens. If you had created this series two weeks ago, marked it as "ready for the 5.13 merge window", and it had been tested as such in linux-next, we would have known about the conflict from linux-next, and I'd have seen it as something that was trivial to fix when I did the merge.
And that would catch all the cases that I can't catch, because linux-next actually does a whole lot more build-testing than I do. Different architectures, lots of different configs etc.
Instead, I applied a series of 178 patches (ok, 175 after I had dropped a couple), saw nothing wrong, and did my (very basic and minimal) build testing, and saw that the patches you sent to me didn't even compile.
I bet a fixed workflow will make it easier for Stephen too, to not continually have to rebase your series, and have a separate queue for patch-series that get randomly updated.
I'm perfectly happy with you not using git - I apply patches from others too. But I don't want to see this broken "rebase with no testing" model. A patch queue that has its base changed has all the same issues that a blind "git rebase" has, and is wrong for all the same reasons - all whether you use git or not.
Linus
Andrew Morton
maintainerRe: [patch 148/178] kasan: detect false-positives in tests
Quoted 1 line · On Fri, 30 Apr 2021 11:32:43 -0700 Linus Torvalds…
This patch is untested garbage that doesn't even compile.
Sorry, I fat-fingered a thing and used gcc-9, which disables CONFIG_KASAN.
Quoted 1 line
Use the previous release as a base (ie in this case 5.12)
Not a problem for the first batch of patches, but what base do I use for the second and succeeding batches?
Quoted 3 lines
If you have patches in
your patch-series that depend on stuff that comes in this merge window, just drop them - or at least don't send them to me.
Maybe 10% of the patches I carry are based on changes which are in linux-next. Things like
- tree-wide renames which need to catch other developers adding instances of the old name. They'll pop up in third-party testing and I do a last-minute grep to find these.
- changes which I've worked with other developers to stage things in that order.
- removal of duplicated includes for which I need to do a last-minute check that nobody has gone and removed the other include.
- new syscalls which would generate a ton of conflicts with new syscalls in other trees.
- right now I'm carrying a pile of little spelling fixes. I stage these after linux-next to find out whether other tree maintainers have independently merged the same patches, or partially similar ones from others.
- etc.
I send this sort of material to you very late in the merge window, after the prerequisites have been merged up.
Re: [patch 148/178] kasan: detect false-positives in tests
Quoted 2 lines · Andrew Morton
Sorry, I fat-fingered a thing and used gcc-9, which disables
CONFIG_KASAN.
My point is that linux-next would have caught it.
And that commit _was_ in linux-next.
But it was rebased literally just hours before being sent to me, and thus all the testing that it got was thrown away.
Quoted 4 lines
Quoted 1 line
Use the previous release as a base (ie in this case 5.12)
Not a problem for the first batch of patches, but what base do I use for the second and succeeding batches?
Well, the first batch is usually the biggest and most core one, and in many ways the most important that it would have been a branch of its own, and be something that has actually been tested as-is in linux-next.
Ad to succeeding batches.. Optimally if they don't have any dependencies on other trees or the first batch, they'd actually entirely independent of the first batch, and just a separate patch queue entirely, and tested as such in linux-next (and again on top of some previous base).
But that kind of workflow would require you literally have multiple separate patch-queues that you test independently. That does sound like a good idea, but it also sounds very much like a "git topic branch" model, and I think quilt basically is a "single series" tool, not able to handle multiple independent series?
I don't know quilt, and a quick google of "multiple independent quilt patch series" ended up at a web page by Andreas Grünbacher that was so old that it was in Latin1 and doesn't even display properly on any modern browser.
Which is a statement in itself, but whatever.
I'd actually be perfectly ok with being told that subsequent patches be based on top of your previous patch series: I already create a branch called "akpm" for applying all your patches, and while right now it's a temporary branch that gets removed after I merge it, I could easily just keep it around - and then apply your next series on top of it.
So the only issues would be the things that actually end up being dependent on other branches in linux-next:
Quoted 2 lines
Maybe 10% of the patches I carry are based on changes which are in linux-next.
These are the ones that you'd have to keep separate, in order to not rebase the patches that _aren't_ based on linux-next changes..
Again, I don't know how to do that with quilt (and iirc, you aren't actually using quilt, you're using your own extended version?)
The quilt man-page does have some signs that there can be multiple series of patches (wording like "current series" vs "another series", and "Different series files can be used.."), but the actual quilt commands don't really show much sign of switching between different patch series. So I get the feeling that it's more of a "theoretically possible" thing rather than something that is actually supported by the tooling natively.
Linus
Andrew Morton
maintainerRe: [patch 148/178] kasan: detect false-positives in tests
Quoted 10 lines · On Sun, 2 May 2021 12:08:17 -0700 Linus Torvalds…
Quoted 4 lines
Quoted 1 line
Use the previous release as a base (ie in this case 5.12)
Not a problem for the first batch of patches, but what base do I use for the second and succeeding batches?
Well, the first batch is usually the biggest and most core one, and in many ways the most important that it would have been a branch of its own, and be something that has actually been tested as-is in linux-next.
Quoted 5 lines
Ad to succeeding batches.. Optimally if they don't have any dependencies on other trees or the first batch, they'd actually entirely independent of the first batch, and just a separate patch queue entirely, and tested as such in linux-next (and again on top of some previous base).
The mm/ patches tend to be significantly interdependent. I try to separate things into mm/whatever subparts, but things are often quite smeary, both textually and conceptually.
I don't send the whole mm/ queue in a single hit because it tends to be rather large. I could do so.
Quoted 10 lines
But that kind of workflow would require you literally have multiple separate patch-queues that you test independently. That does sound like a good idea, but it also sounds very much like a "git topic branch" model, and I think quilt basically is a "single series" tool, not able to handle multiple independent series?
I don't know quilt, and a quick google of "multiple independent quilt patch series" ended up at a web page by Andreas Grünbacher that was so old that it was in Latin1 and doesn't even display properly on any modern browser.
I haven't found a need to do this. I presently identify 353 subsystems in the -mm patch queue. Most have zero patches in them most of the time. But apart from mm/ being all tangled up internally, I basically never see any overlap between these subsystems. So I stack 'em all up in the same series.
Quoted 6 lines
I'd actually be perfectly ok with being told that subsequent patches be based on top of your previous patch series: I already create a branch called "akpm" for applying all your patches, and while right now it's a temporary branch that gets removed after I merge it, I could easily just keep it around - and then apply your next series on top of it.
OK, I'll do that this week and shall have a bit of a think.
Quoted 11 lines
So the only issues would be the things that actually end up being dependent on other branches in linux-next:
Quoted 2 lines
Maybe 10% of the patches I carry are based on changes which are in linux-next.
These are the ones that you'd have to keep separate, in order to not rebase the patches that _aren't_ based on linux-next changes..
Again, I don't know how to do that with quilt (and iirc, you aren't actually using quilt, you're using your own extended version?)
Yes, my patch-scripts is quilt's grandfather and I continue to use and evolve it.
Quoted 7 lines
The quilt man-page does have some signs that there can be multiple series of patches (wording like "current series" vs "another series", and "Different series files can be used.."), but the actual quilt commands don't really show much sign of switching between different patch series. So I get the feeling that it's more of a "theoretically possible" thing rather than something that is actually supported by the tooling natively.
Easy. You'd have a bunch of different series files and then do
ln -s series-ocfs2 series
ln -s series-procfs series
etc to work on a particular series. Then for assembling an entire tree, have a master series-of-series file which contains the names of the various sub-series files, to define the stacking order:
series-ocfs2
series-procfs
...
Then
cat $(cat series-of-series) > series
and go for it. But as said, I haven't seen the need because they're almost always all orthogonal.
A fairly recent series file is at
https://ozlabs.org/~akpm/mmotm/series. It defines the applying order, the various subsystems, tags for which bits are to be included in linux-next, lots of notes-to-self, etc.
Andrew Morton
maintainerPATCH 149/178 irq_work: record irq_work_queue() call stack
In reply to Andrew MortonFrom: Zqiang <[email protected]>
Subject: irq_work: record irq_work_queue() call stack
Add the irq_work_queue() call stack into the KASAN auxiliary stack in order to improve KASAN reports. this will let us know where the irq work be queued.
---
kernel/irq_work.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/irq_work.c renamed from kernel/irq_work.c~irq_work-record-irq_work_queue-call-stack +6 -1
@@ -19,7 +19,7 @@#include <linux/notifier.h> #include <linux/smp.h> #include <asm/processor.h> - +#include <linux/kasan.h> static DEFINE_PER_CPU(struct llist_head, raised_list); static DEFINE_PER_CPU(struct llist_head, lazy_list);
@@ -70,6 +70,9 @@ bool irq_work_queue(struct irq_work *worif (!irq_work_claim (work))return false; +/*record irq_work call stack in order to print it in KASAN reports*/ +kasan_record_aux_stack (work); +/* Queue the entry and raise the IPI if needed. */ preempt_disable ();__irq_work_queue_local (work);
@@ -98,6 +101,8 @@ bool irq_work_queue_on(struct irq_work *if (!irq_work_claim (work))return false; +kasan_record_aux_stack (work); +preempt_disable ();if (cpu !=smp_processor_id ()) {/* Arch remote IPI send/receive backend aren't NMI safe */
_
Andrew Morton
maintainerPATCH 150/178 mm: move mem_init_print_info() into mm_init()
In reply to Andrew MortonFrom: Kefeng Wang <[email protected]>
Subject: mm: move mem_init_print_info() into mm_init()
mem_init_print_info() is called in mem_init() on each architecture, and pass NULL argument, so using void argument and move it into mm_init().
---
arch/alpha/mm/init.c | 1 - arch/arc/mm/init.c | 1 - arch/arm/mm/init.c | 2 -- arch/arm64/mm/init.c | 2 -- arch/csky/mm/init.c | 1 - arch/h8300/mm/init.c | 2 -- arch/hexagon/mm/init.c | 1 - arch/ia64/mm/init.c | 1 - arch/m68k/mm/init.c | 1 - arch/microblaze/mm/init.c | 1 - arch/mips/loongson64/numa.c | 1 - arch/mips/mm/init.c | 1 - arch/mips/sgi-ip27/ip27-memory.c | 1 - arch/nds32/mm/init.c | 1 - arch/nios2/mm/init.c | 1 - arch/openrisc/mm/init.c | 2 -- arch/parisc/mm/init.c | 2 -- arch/powerpc/mm/mem.c | 1 - arch/riscv/mm/init.c | 1 - arch/s390/mm/init.c | 2 -- arch/sh/mm/init.c | 1 - arch/sparc/mm/init_32.c | 2 -- arch/sparc/mm/init_64.c | 1 - arch/um/kernel/mem.c | 1 - arch/x86/mm/init_32.c | 2 -- arch/x86/mm/init_64.c | 2 -- arch/xtensa/mm/init.c | 1 - include/linux/mm.h | 2 +- init/main.c | 1 + mm/page_alloc.c | 10 +++++----- 30 files changed, 7 insertions(+), 42 deletions(-)
arch/alpha/mm/init.c renamed from arch/alpha/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -282,5 +282,4 @@ mem_init(void)set_max_mapnr (max_low_pfn); high_memory = (void *)__va (max_low_pfn *PAGE_SIZE );memblock_free_all (); -mem_init_print_info (NULL ); }
arch/arc/mm/init.c renamed from arch/arc/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -194,7 +194,6 @@ void __init mem_init(void) {memblock_free_all ();highmem_init (); -mem_init_print_info (NULL ); }#ifdef CONFIG_HIGHMEM
arch/arm64/mm/init.c renamed from arch/arm64/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -491,8 +491,6 @@ void __init mem_init(void)/* this will put all unused low memory onto the freelists */ memblock_free_all (); -mem_init_print_info (NULL ); -/* * Check boundaries twice: Some fundamental inconsistencies can be * detected at build time already.
arch/arm/mm/init.c renamed from arch/arm/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -316,8 +316,6 @@ void __init mem_init(void)free_highpages (); -mem_init_print_info (NULL ); -/* * Check boundaries twice: Some fundamental inconsistencies can * be detected at build time already.
arch/csky/mm/init.c renamed from arch/csky/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -107,7 +107,6 @@ void __init mem_init(void) free_highmem_page(page); }#endif -mem_init_print_info (NULL ); }void free_initmem (void )
arch/h8300/mm/init.c renamed from arch/h8300/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -98,6 +98,4 @@ void __init mem_init(void)/* this will put all low memory onto the freelists */ memblock_free_all (); - -mem_init_print_info (NULL ); }
arch/hexagon/mm/init.c renamed from arch/hexagon/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -55,7 +55,6 @@ void __init mem_init(void) {/* No idea where this is actually declared. Seems to evade LXR. */ memblock_free_all (); -mem_init_print_info (NULL );/* * To-Do: someone somewhere should wipe out the bootmem map
arch/ia64/mm/init.c renamed from arch/ia64/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -449,7 +449,6 @@ mem_init (void)set_max_mapnr (max_low_pfn); high_memory =__va (max_low_pfn *PAGE_SIZE );memblock_free_all (); -mem_init_print_info (NULL );/* * For fsyscall entrpoints with no light-weight handler, use the ordinary
arch/m68k/mm/init.c renamed from arch/m68k/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -153,5 +153,4 @@ void __init mem_init(void)/* this will put all memory onto the freelists */ memblock_free_all ();init_pointer_tables (); -mem_init_print_info (NULL ); }
arch/microblaze/mm/init.c renamed from arch/microblaze/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -131,7 +131,6 @@ void __init mem_init(void)highmem_setup ();#endif -mem_init_print_info (NULL ); mem_init_done =1 ; }
arch/mips/loongson64/numa.c renamed from arch/mips/loongson64/numa.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -178,7 +178,6 @@ void __init mem_init(void) high_memory = (void *)__va (get_num_physpages () <<PAGE_SHIFT );memblock_free_all ();setup_zero_pages ();/* This comes from node 0 */ -mem_init_print_info (NULL ); }/* All PCI device belongs to logical Node-0 */
arch/mips/mm/init.c renamed from arch/mips/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -467,7 +467,6 @@ void __init mem_init(void)memblock_free_all ();setup_zero_pages ();/* Setup zeroed pages. */ mem_init_free_highmem (); -mem_init_print_info (NULL );#ifdef CONFIG_64BIT if ((unsigned long ) &_text > (unsigned long )CKSEG0 )
arch/mips/sgi-ip27/ip27-memory.c renamed from arch/mips/sgi-ip27/ip27-memory.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -420,5 +420,4 @@ void __init mem_init(void) high_memory = (void *)__va (get_num_physpages () <<PAGE_SHIFT );memblock_free_all ();setup_zero_pages ();/* This comes from node 0 */ -mem_init_print_info (NULL ); }
arch/nds32/mm/init.c renamed from arch/nds32/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -191,7 +191,6 @@ void __init mem_init(void)/* this will put all low memory onto the freelists */ memblock_free_all (); -mem_init_print_info (NULL );pr_info ("virtual kernel memory layout:\n" " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
arch/nios2/mm/init.c renamed from arch/nios2/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -71,7 +71,6 @@ void __init mem_init(void)/* this will put all memory onto the freelists */ memblock_free_all (); -mem_init_print_info (NULL ); }void __init mmu_init (void )
arch/openrisc/mm/init.c renamed from arch/openrisc/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -211,8 +211,6 @@ void __init mem_init(void)/* this will put all low memory onto the freelists */ memblock_free_all (); -mem_init_print_info (NULL ); -printk ("mem_init_done ...........................................\n" ); mem_init_done =1 ;return ;
arch/parisc/mm/init.c renamed from arch/parisc/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -573,8 +573,6 @@ void __init mem_init(void)#endif parisc_vmalloc_start =SET_MAP_OFFSET (MAP_START ); -mem_init_print_info (NULL ); -#if 0 /* * Do not expose the virtual kernel memory layout to userspace.
arch/powerpc/mm/mem.c renamed from arch/powerpc/mm/mem.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -312,7 +312,6 @@ void __init mem_init(void) (mfspr (SPRN_TLB1CFG ) & TLBnCFG_N_ENTRY) -1 ;#endif -mem_init_print_info (NULL );#ifdef CONFIG_PPC32 pr_info ("Kernel virtual memory layout:\n" );#ifdef CONFIG_KASAN
arch/riscv/mm/init.c renamed from arch/riscv/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -102,7 +102,6 @@ void __init mem_init(void) high_memory = (void *)(__va (PFN_PHYS (max_low_pfn)));memblock_free_all (); -mem_init_print_info (NULL );print_vm_layout (); }
arch/s390/mm/init.c renamed from arch/s390/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -209,8 +209,6 @@ void __init mem_init(void)setup_zero_pages ();/* Setup zeroed pages. */ cmma_init_nodat (); - -mem_init_print_info (NULL ); }void free_initmem (void )
arch/sh/mm/init.c renamed from arch/sh/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -359,7 +359,6 @@ void __init mem_init(void)vsyscall_init (); -mem_init_print_info (NULL );pr_info ("virtual kernel memory layout:\n" " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
arch/sparc/mm/init_32.c renamed from arch/sparc/mm/init_32.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -292,8 +292,6 @@ void __init mem_init(void)map_high_region (start_pfn, end_pfn); } - -mem_init_print_info (NULL ); }void sparc_flush_page_to_ram (struct page *page)
arch/sparc/mm/init_64.c renamed from arch/sparc/mm/init_64.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -2520,7 +2520,6 @@ void __init mem_init(void) }mark_page_reserved (mem_map_zero); -mem_init_print_info (NULL );if (tlb_type == cheetah || tlb_type == cheetah_plus)cheetah_ecache_flush_init ();
arch/um/kernel/mem.c renamed from arch/um/kernel/mem.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -54,7 +54,6 @@ void __init mem_init(void)memblock_free_all (); max_low_pfn =totalram_pages (); max_pfn = max_low_pfn; -mem_init_print_info (NULL ); kmalloc_ok =1 ; }
arch/x86/mm/init_32.c renamed from arch/x86/mm/init_32.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -755,8 +755,6 @@ void __init mem_init(void) after_bootmem =1 ; x86_init.hyper.init_after_bootmem (); -mem_init_print_info (NULL ); -/* * Check boundaries twice: Some fundamental inconsistencies can * be detected at build time already.
arch/x86/mm/init_64.c renamed from arch/x86/mm/init_64.c~mm-move-mem_init_print_info-into-mm_init +0 -2
@@ -1351,8 +1351,6 @@ void __init mem_init(void)kclist_add (&kcore_vsyscall, (void *)VSYSCALL_ADDR ,PAGE_SIZE ,KCORE_USER );preallocate_vmalloc_pages (); - -mem_init_print_info (NULL ); }#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
arch/xtensa/mm/init.c renamed from arch/xtensa/mm/init.c~mm-move-mem_init_print_info-into-mm_init +0 -1
@@ -119,7 +119,6 @@ void __init mem_init(void)memblock_free_all (); -mem_init_print_info (NULL );pr_info ("virtual kernel memory layout:\n" #ifdef CONFIG_KASAN " kasan : 0x%08lx - 0x%08lx (%5lu MB)\n"
include/linux/mm.h renamed from include/linux/mm.h~mm-move-mem_init_print_info-into-mm_init +1 -1
@@ -2360,7 +2360,7 @@ extern unsigned long free_reserved_area( int poison, const char *s);extern void adjust_managed_page_count (struct page *page,long count); -extern void mem_init_print_info (const char *str); +extern void mem_init_print_info (void );extern void reserve_bootmem_region (phys_addr_t start,phys_addr_t end);
init/main.c renamed from init/main.c~mm-move-mem_init_print_info-into-mm_init +1 -0
@@ -830,6 +830,7 @@ static void __init mm_init(void)report_meminit ();stack_depot_init ();mem_init (); +mem_init_print_info ();/* page_owner must be initialized after buddy is ready */ page_ext_init_flatmem_late ();kmem_cache_init ();
mm/page_alloc.c renamed from mm/page_alloc.c~mm-move-mem_init_print_info-into-mm_init +5 -5
@@ -7734,7 +7734,7 @@ unsigned long free_reserved_area(void *sreturn pages; } -void __init mem_init_print_info (const char *str) +void __init mem_init_print_info (void ) {unsigned long physpages, codesize, datasize, rosize, bss_size;unsigned long init_code_size, init_data_size;
@@ -7773,17 +7773,17 @@ void __init mem_init_print_info(const ch#ifdef CONFIG_HIGHMEM ", %luK highmem" #endif -"%s%s)\n" , +")\n" ,nr_free_pages () << (PAGE_SHIFT -10 ), physpages << (PAGE_SHIFT -10 ), codesize >>10 , datasize >>10 , rosize >>10 , (init_data_size + init_code_size) >>10 , bss_size >>10 , (physpages -totalram_pages () - totalcma_pages) << (PAGE_SHIFT -10 ), - totalcma_pages << (PAGE_SHIFT -10 ), + totalcma_pages << (PAGE_SHIFT -10 ) #ifdef CONFIG_HIGHMEM -totalhigh_pages () << (PAGE_SHIFT -10 ), + ,totalhigh_pages () << (PAGE_SHIFT -10 ) #endif - str ? ", " : "", str ? str : ""); + ); }/**
_
Andrew Morton
maintainerPATCH 151/178 mm/page_alloc: drop pr_info_ratelimited() in alloc_contig_range()
In reply to Andrew MortonFrom: David Hildenbrand <[email protected]>
Subject: mm/page_alloc: drop pr_info_ratelimited() in alloc_contig_range()
The information that some PFNs are busy is:
a) not helpful for ordinary users: we don't even know *who* called alloc_contig_range(). This is certainly not worth a pr_info.*().
b) not really helpful for debugging: we don't have any details *why* these PFNs are busy, and that is what we usually care about.
c) not complete: there are other cases where we fail alloc_contig_range() using different paths that are not getting recorded.
For example, we reach this path once we succeeded in isolating pageblocks, but failed to migrate some pages - which can happen easily on ZONE_NORMAL (i.e., has_unmovable_pages() is racy) but also on ZONE_MOVABLE i.e., we would have to retry longer to migrate).
For example via virtio-mem when unplugging memory, we can create quite some noise (especially with ZONE_NORMAL) that is not of interest to users
- it's expected that some allocations may fail as memory is busy.
Let's just drop that pr_info_ratelimit() and rather implement a dynamic debugging mechanism in the future that can give us a better reason why alloc_contig_range() failed on specific pages.
---
mm/page_alloc.c | 2 -- 1 file changed, 2 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-drop-pr_info_ratelimited-in-alloc_contig_range +0 -2
@@ -8676,8 +8676,6 @@ int alloc_contig_range(unsigned long sta/* Make sure the range is really isolated. */ if (test_pages_isolated (outer_start, end,0 )) { -pr_info_ratelimited ("%s: [%lx, %lx) PFNs busy\n" , - __func__, outer_start, end); ret = -EBUSY ; goto done; }
_
Andrew Morton
maintainerPATCH 152/178 mm: remove lru_add_drain_all in alloc_contig_range
In reply to Andrew MortonFrom: Minchan Kim <[email protected]>
Subject: mm: remove lru_add_drain_all in alloc_contig_range
__alloc_contig_migrate_range already has lru_add_drain_all call via migrate_prep. It's necessary to move LRU taget pages into LRU list to be able to isolated. However, lru_add_drain_all call after __alloc_contig_migrate_range is pointless since it has changed source page freeing from putback_lru_pages to put_page[1].
This patch removes it.
[1] c6c919eb90e0, ("mm: use put_page() to free page instead of putback_lru_page()"
---
mm/page_alloc.c | 2 -- 1 file changed, 2 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-remove-lru_add_drain_all-in-alloc_contig_range +0 -2
@@ -8649,8 +8649,6 @@ int alloc_contig_range(unsigned long sta* isolated thus they won't get removed from buddy. */ -lru_add_drain_all (); - order =0 ; outer_start = start;while (!PageBuddy (pfn_to_page (outer_start))) {
_
Andrew Morton
maintainerPATCH 153/178 include/linux/page-flags-layout.h: correctly determine LAST_CPUPID_WIDTH
In reply to Andrew MortonFrom: Yu Zhao <[email protected]>
Subject: include/linux/page-flags-layout.h: correctly determine LAST_CPUPID_WIDTH
The naming convention used in include/linux/page-flags-layout.h:
*_SHIFT: the number of bits trying to allocate *_WIDTH: the number of bits successfully allocated
So when it comes to LAST_CPUPID_WIDTH, we need to check whether all previous *_WIDTH and LAST_CPUPID_SHIFT can fit into page flags. This means we need to use NODES_WIDTH, not NODES_SHIFT.
---
include/linux/page-flags-layout.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/page-flags-layout.h renamed from include/linux/page-flags-layout.h~mm-correctly-determine-last_cpupid_width +1 -1
@@ -83,7 +83,7 @@#define KASAN_TAG_WIDTH 0#endif -#if SECTIONS_WIDTH +ZONES_WIDTH +NODES_SHIFT +LAST_CPUPID_SHIFT +KASAN_TAG_WIDTH \ +#if SECTIONS_WIDTH +ZONES_WIDTH +NODES_WIDTH +LAST_CPUPID_SHIFT +KASAN_TAG_WIDTH \ <=BITS_PER_LONG -NR_PAGEFLAGS #define LAST_CPUPID_WIDTH LAST_CPUPID_SHIFT#else
_
Andrew Morton
maintainerPATCH 154/178 include/linux/page-flags-layout.h: cleanups
In reply to Andrew MortonFrom: Yu Zhao <[email protected]>
Subject: include/linux/page-flags-layout.h: cleanups
Tidy things up and delete comments stating the obvious with typos or making no sense.
---
include/linux/page-flags-layout.h | 62 +++++++++++++--------------- mm/mm_init.c | 4 - 2 files changed, 29 insertions(+), 37 deletions(-)
include/linux/page-flags-layout.h renamed from include/linux/page-flags-layout.h~mm-clean-up-include-linux-page-flags-layouth +29 -33
@@ -21,16 +21,17 @@#elif MAX_NR_ZONES <=8 #define ZONES_SHIFT 3#else -#error ZONES_SHIFT -- too many zones configured adjust calculation +#error ZONES_SHIFT "Too many zones configured"#endif +#define ZONES_WIDTH ZONES_SHIFT +#ifdef CONFIG_SPARSEMEM #include <asm/sparsemem.h> - -/* SECTION_SHIFT #bits space required to store a section # */ #define SECTIONS_SHIFT (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS) - -#endif /* CONFIG_SPARSEMEM */ +#else +#define SECTIONS_SHIFT 0 +#endif #ifndef BUILD_VDSO32_64 /*
@@ -54,17 +55,28 @@#define SECTIONS_WIDTH 0#endif -#define ZONES_WIDTH ZONES_SHIFT - -#if SECTIONS_WIDTH +ZONES_WIDTH +NODES_SHIFT <=BITS_PER_LONG -NR_PAGEFLAGS +#if ZONES_WIDTH +SECTIONS_WIDTH +NODES_SHIFT <=BITS_PER_LONG -NR_PAGEFLAGS #define NODES_WIDTH NODES_SHIFT -#else -#ifdef CONFIG_SPARSEMEM_VMEMMAP +#elif defined(CONFIG_SPARSEMEM_VMEMMAP )#error "Vmemmap: No space for nodes field in page flags" -#endif +#else #define NODES_WIDTH 0#endif +/* +* Note that this #define MUST have a value so that it can be tested with +* the IS_ENABLED() macro. +*/ +#if NODES_SHIFT !=0 &&NODES_WIDTH ==0 +#define NODE_NOT_IN_PAGE_FLAGS 1 +#endif + +#if defined(CONFIG_KASAN_SW_TAGS ) || defined(CONFIG_KASAN_HW_TAGS ) +#define KASAN_TAG_WIDTH 8 +#else +#define KASAN_TAG_WIDTH 0 +#endif +#ifdef CONFIG_NUMA_BALANCING #define LAST__PID_SHIFT 8#define LAST__PID_MASK ((1 << LAST__PID_SHIFT)-1)
@@ -77,36 +89,20 @@#define LAST_CPUPID_SHIFT 0#endif -#if defined(CONFIG_KASAN_SW_TAGS ) || defined(CONFIG_KASAN_HW_TAGS ) -#define KASAN_TAG_WIDTH 8 -#else -#define KASAN_TAG_WIDTH 0 -#endif - -#if SECTIONS_WIDTH +ZONES_WIDTH +NODES_WIDTH +LAST_CPUPID_SHIFT +KASAN_TAG_WIDTH \ +#if ZONES_WIDTH +SECTIONS_WIDTH +NODES_WIDTH +KASAN_TAG_WIDTH +LAST_CPUPID_SHIFT \ <=BITS_PER_LONG -NR_PAGEFLAGS #define LAST_CPUPID_WIDTH LAST_CPUPID_SHIFT#else #define LAST_CPUPID_WIDTH 0#endif -#if SECTIONS_WIDTH +NODES_WIDTH +ZONES_WIDTH +LAST_CPUPID_WIDTH +KASAN_TAG_WIDTH \ - >BITS_PER_LONG -NR_PAGEFLAGS -#error "Not enough bits in page flags" -#endif - -/* -* We are going to use the flags for the page to node mapping if its in -* there. This includes the case where there is no node, so it is implicit. -* Note that this #define MUST have a value so that it can be tested with -* the IS_ENABLED() macro. -*/ -#if !(NODES_WIDTH >0 ||NODES_SHIFT ==0 ) -#define NODE_NOT_IN_PAGE_FLAGS 1 +#if LAST_CPUPID_SHIFT !=0 &&LAST_CPUPID_WIDTH ==0 +#define LAST_CPUPID_NOT_IN_PAGE_FLAGS #endif -#if defined(CONFIG_NUMA_BALANCING ) &&LAST_CPUPID_WIDTH ==0 -#define LAST_CPUPID_NOT_IN_PAGE_FLAGS +#if ZONES_WIDTH +SECTIONS_WIDTH +NODES_WIDTH +KASAN_TAG_WIDTH +LAST_CPUPID_WIDTH \ + >BITS_PER_LONG -NR_PAGEFLAGS +#error "Not enough bits in page flags"#endif #endif
mm/mm_init.c renamed from mm/mm_init.c~mm-clean-up-include-linux-page-flags-layouth +0 -4
@@ -19,10 +19,6 @@#ifdef CONFIG_DEBUG_MEMORY_INIT int __meminitdata mminit_loglevel; -#ifndef SECTIONS_SHIFT -#define SECTIONS_SHIFT 0 -#endif -/* The zonelists are simply reported, validation is manual. */ void __init mminit_verify_zonelist (void ) {
_
Andrew Morton
maintainerPATCH 155/178 mm/page_alloc: rename alloc_mask to alloc_gfp
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/page_alloc: rename alloc_mask to alloc_gfp
Patch series "Rationalise __alloc_pages wrappers", v3.
I was poking around the __alloc_pages variants trying to understand why they each exist, and couldn't really find a good justification for keeping __alloc_pages and __alloc_pages_nodemask as separate functions. That led to getting rid of alloc_pages_current() and then I noticed the documentation was bad, and then I noticed the mempolicy documentation wasn't included.
Anyway, this is all cleanups & doc fixes.
This patch (of 7):
We have two masks involved -- the nodemask and the gfp mask, so alloc_mask is an unclear name.
---
mm/page_alloc.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-rename-alloc_mask-to-alloc_gfp +10 -9
@@ -4966,7 +4966,7 @@ got_pg:static inline bool prepare_alloc_pages (gfp_t gfp_mask,unsigned int order,int preferred_nid,nodemask_t *nodemask, -struct alloc_context *ac,gfp_t *alloc_mask, +struct alloc_context *ac,gfp_t *alloc_gfp,unsigned int *alloc_flags) { ac->highest_zoneidx =gfp_zone (gfp_mask);
@@ -4975,7 +4975,7 @@ static inline bool prepare_alloc_pages(g ac->migratetype =gfp_migratetype (gfp_mask);if (cpusets_enabled ()) { - *alloc_mask |= __GFP_HARDWALL; + *alloc_gfp |= __GFP_HARDWALL;/* * When we are in the interrupt context, it is irrelevant * to the current task context. It means that any node ok.
@@ -5019,7 +5019,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u {struct page *page;unsigned int alloc_flags =ALLOC_WMARK_LOW ; -gfp_t alloc_mask;/* The gfp_t that was actually used for allocation */ +gfp_t alloc_gfp;/* The gfp_t that was actually used for allocation */ struct alloc_context ac = { };/*
@@ -5032,8 +5032,9 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u } gfp_mask &= gfp_allowed_mask; - alloc_mask = gfp_mask; -if (!prepare_alloc_pages (gfp_mask, order, preferred_nid, nodemask, &ac, &alloc_mask, &alloc_flags)) + alloc_gfp = gfp_mask; +if (!prepare_alloc_pages (gfp_mask, order, preferred_nid, nodemask, &ac, + &alloc_gfp, &alloc_flags))return NULL ;/*
@@ -5043,7 +5044,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u alloc_flags |=alloc_flags_nofragment (ac.preferred_zoneref->zone, gfp_mask);/* First allocation attempt */ - page =get_page_from_freelist (alloc_mask, order, alloc_flags, &ac); + page =get_page_from_freelist (alloc_gfp, order, alloc_flags, &ac);if (likely (page)) goto out;
@@ -5053,7 +5054,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u* from a particular context which has been marked by * memalloc_no{fs,io}_{save,restore}. */ - alloc_mask =current_gfp_context (gfp_mask); + alloc_gfp =current_gfp_context (gfp_mask); ac.spread_dirty_pages = false;/*
@@ -5062,7 +5063,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u*/ ac.nodemask = nodemask; - page =__alloc_pages_slowpath (alloc_mask, order, &ac); + page =__alloc_pages_slowpath (alloc_gfp, order, &ac); out: if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page &&
@@ -5071,7 +5072,7 @@ out: page =NULL ; } -trace_mm_page_alloc (page, order, alloc_mask, ac.migratetype); +trace_mm_page_alloc (page, order, alloc_gfp, ac.migratetype);return page; }
_
Andrew Morton
maintainerPATCH 156/178 mm/page_alloc: rename gfp_mask to gfp
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/page_alloc: rename gfp_mask to gfp
Shorten some overly-long lines by renaming this identifier.
---
mm/page_alloc.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-rename-gfp_mask-to-gfp +9 -9
@@ -5014,7 +5014,7 @@ static inline bool prepare_alloc_pages(g* This is the 'heart' of the zoned buddy allocator. */ struct page * -__alloc_pages_nodemask (gfp_t gfp_mask,unsigned int order,int preferred_nid, +__alloc_pages_nodemask (gfp_t gfp,unsigned int order,int preferred_nid,nodemask_t *nodemask) {struct page *page;
@@ -5027,13 +5027,13 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u* so bail out early if the request is out of bound. */ if (unlikely (order >=MAX_ORDER )) { -WARN_ON_ONCE (!(gfp_mask & __GFP_NOWARN)); +WARN_ON_ONCE (!(gfp & __GFP_NOWARN));return NULL ; } - gfp_mask &= gfp_allowed_mask; - alloc_gfp = gfp_mask; -if (!prepare_alloc_pages (gfp_mask, order, preferred_nid, nodemask, &ac, + gfp &= gfp_allowed_mask; + alloc_gfp = gfp; +if (!prepare_alloc_pages (gfp, order, preferred_nid, nodemask, &ac, &alloc_gfp, &alloc_flags))return NULL ;
@@ -5041,7 +5041,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u* Forbid the first pass from falling back to types that fragment * memory until all local zones are considered. */ - alloc_flags |=alloc_flags_nofragment (ac.preferred_zoneref->zone, gfp_mask); + alloc_flags |=alloc_flags_nofragment (ac.preferred_zoneref->zone, gfp);/* First allocation attempt */ page =get_page_from_freelist (alloc_gfp, order, alloc_flags, &ac);
@@ -5054,7 +5054,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u* from a particular context which has been marked by * memalloc_no{fs,io}_{save,restore}. */ - alloc_gfp =current_gfp_context (gfp_mask); + alloc_gfp =current_gfp_context (gfp); ac.spread_dirty_pages = false;/*
@@ -5066,8 +5066,8 @@ __alloc_pages_nodemask(gfp_t gfp_mask, u page =__alloc_pages_slowpath (alloc_gfp, order, &ac); out: -if (memcg_kmem_enabled () && (gfp_mask & __GFP_ACCOUNT) && page && -unlikely (__memcg_kmem_charge_page (page, gfp_mask, order) !=0 )) { +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && page && +unlikely (__memcg_kmem_charge_page (page, gfp, order) !=0 )) {__free_pages (page, order); page =NULL ; }
_
Andrew Morton
maintainerPATCH 157/178 mm/page_alloc: combine __alloc_pages and __alloc_pages_nodemask
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/page_alloc: combine __alloc_pages and __alloc_pages_nodemask
There are only two callers of __alloc_pages() so prune the thicket of alloc_page variants by combining the two functions together. Current callers of __alloc_pages() simply add an extra 'NULL' parameter and current callers of __alloc_pages_nodemask() call __alloc_pages() instead.
---
Documentation/admin-guide/mm/transhuge.rst | 2 +- include/linux/gfp.h | 13 +++---------- mm/hugetlb.c | 2 +- mm/internal.h | 4 ++-- mm/mempolicy.c | 6 +++--- mm/migrate.c | 2 +- mm/page_alloc.c | 5 ++--- 7 files changed, 13 insertions(+), 21 deletions(-)
Documentation/admin-guide/mm/transhuge.rst renamed from Documentation/admin-guide/mm/transhuge.rst~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +1 -1
@@ -402,7 +402,7 @@ compact_fail but failed. It is possible to establish how long the stalls were using the function -tracer to record how long was spent in __alloc_pages_nodemask and +tracer to record how long was spent in __alloc_pages() and using the mm_page_alloc tracepoint to identify which allocations were for huge pages.
include/linux/gfp.h renamed from include/linux/gfp.h~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +3 -10
@@ -515,15 +515,8 @@ static inline int arch_make_page_accessi }#endif -struct page * -__alloc_pages_nodemask (gfp_t gfp_mask,unsigned int order,int preferred_nid, -nodemask_t *nodemask); - -static inline struct page * -__alloc_pages (gfp_t gfp_mask,unsigned int order,int preferred_nid) -{ -return __alloc_pages_nodemask (gfp_mask, order, preferred_nid,NULL ); -} +struct page *__alloc_pages (gfp_t gfp,unsigned int order,int preferred_nid, +nodemask_t *nodemask);/* * Allocate pages, preferring the node given as nid. The node must be valid and
@@ -535,7 +528,7 @@ __alloc_pages_node(int nid, gfp_t gfp_maVM_BUG_ON (nid <0 || nid >=MAX_NUMNODES );VM_WARN_ON ((gfp_mask & __GFP_THISNODE) && !node_online (nid)); -return __alloc_pages (gfp_mask, order, nid); +return __alloc_pages (gfp_mask, order, nid,NULL ); }/*
mm/hugetlb.c renamed from mm/hugetlb.c~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +1 -1
@@ -1616,7 +1616,7 @@ static struct page *alloc_buddy_huge_pag gfp_mask |= __GFP_RETRY_MAYFAIL;if (nid ==NUMA_NO_NODE ) nid =numa_mem_id (); - page =__alloc_pages_nodemask (gfp_mask, order, nid, nmask); + page =__alloc_pages (gfp_mask, order, nid, nmask);if (page)__count_vm_event (HTLB_BUDDY_PGALLOC );else
mm/internal.h renamed from mm/internal.h~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +2 -2
@@ -145,10 +145,10 @@ extern pmd_t *mm_find_pmd(struct mm_stru* family of functions. * * nodemask, migratetype and highest_zoneidx are initialized only once in -* __alloc_pages_nodemask() and then never change. +* __alloc_pages() and then never change. * * zonelist, preferred_zone and highest_zoneidx are set first in -* __alloc_pages_nodemask() for the fast path, and might be later changed +* __alloc_pages() for the fast path, and might be later changed * in __alloc_pages_slowpath(). All other functions pass the whole structure * by a const pointer. */
mm/mempolicy.c renamed from mm/mempolicy.c~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +3 -3
@@ -2140,7 +2140,7 @@ static struct page *alloc_page_interleav {struct page *page; - page =__alloc_pages (gfp, order, nid); + page =__alloc_pages (gfp, order, nid,NULL );/* skip NUMA_INTERLEAVE_HIT counter update if numa stats is disabled */ if (!static_branch_likely (&vm_numa_stat_key))return page;
@@ -2237,7 +2237,7 @@ alloc_pages_vma(gfp_t gfp, int order, st nmask =policy_nodemask (gfp, pol); preferred_nid =policy_node (gfp, pol, node); - page =__alloc_pages_nodemask (gfp, order, preferred_nid, nmask); + page =__alloc_pages (gfp, order, preferred_nid, nmask);mpol_cond_put (pol); out:return page;
@@ -2274,7 +2274,7 @@ struct page *alloc_pages_current(gfp_t gif (pol->mode ==MPOL_INTERLEAVE ) page =alloc_page_interleave (gfp, order,interleave_nodes (pol));else - page =__alloc_pages_nodemask (gfp, order, + page =__alloc_pages (gfp, order,policy_node (gfp, pol,numa_node_id ()),policy_nodemask (gfp, pol));
mm/migrate.c renamed from mm/migrate.c~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +1 -1
@@ -1617,7 +1617,7 @@ struct page *alloc_migration_target(struif (is_highmem_idx (zidx) || zidx ==ZONE_MOVABLE ) gfp_mask |= __GFP_HIGHMEM; - new_page =__alloc_pages_nodemask (gfp_mask, order, nid, mtc->nmask); + new_page =__alloc_pages (gfp_mask, order, nid, mtc->nmask);if (new_page &&PageTransHuge (new_page))prep_transhuge_page (new_page);
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-combine-__alloc_pages-and-__alloc_pages_nodemask +2 -3
@@ -5013,8 +5013,7 @@ static inline bool prepare_alloc_pages(g/* * This is the 'heart' of the zoned buddy allocator. */ -struct page * -__alloc_pages_nodemask (gfp_t gfp,unsigned int order,int preferred_nid, +struct page *__alloc_pages (gfp_t gfp,unsigned int order,int preferred_nid,nodemask_t *nodemask) {struct page *page;
@@ -5076,7 +5075,7 @@ out:return page; } -EXPORT_SYMBOL (__alloc_pages_nodemask); +EXPORT_SYMBOL (__alloc_pages);/* * Common helper functions. Never use with __GFP_HIGHMEM because the returned
_
Andrew Morton
maintainerPATCH 158/178 mm/mempolicy: rename alloc_pages_current to alloc_pages
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/mempolicy: rename alloc_pages_current to alloc_pages
When CONFIG_NUMA is enabled, alloc_pages() is a wrapper around alloc_pages_current(). This is pointless, just implement alloc_pages() directly.
---
include/linux/gfp.h | 8 +------- mm/mempolicy.c | 6 +++--- 2 files changed, 4 insertions(+), 10 deletions(-)
include/linux/gfp.h renamed from include/linux/gfp.h~mm-mempolicy-rename-alloc_pages_current-to-alloc_pages +1 -7
@@ -546,13 +546,7 @@ static inline struct page *alloc_pages_n }#ifdef CONFIG_NUMA -extern struct page *alloc_pages_current (gfp_t gfp_mask,unsigned order); - -static inline struct page * -alloc_pages (gfp_t gfp_mask,unsigned int order) -{ -return alloc_pages_current (gfp_mask, order); -} +struct page *alloc_pages (gfp_t gfp,unsigned int order);extern struct page *alloc_pages_vma (gfp_t gfp_mask,int order,struct vm_area_struct *vma,unsigned long addr,int node,bool hugepage);
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-rename-alloc_pages_current-to-alloc_pages +3 -3
@@ -2245,7 +2245,7 @@ out:EXPORT_SYMBOL (alloc_pages_vma);/** -* alloc_pages_current - Allocate pages. +* alloc_pages - Allocate pages. * * @gfp: * %GFP_USER user allocation,
@@ -2259,7 +2259,7 @@ EXPORT_SYMBOL(alloc_pages_vma);* interrupt context and apply the current process NUMA policy. * Returns NULL when no page can be allocated. */ -struct page *alloc_pages_current (gfp_t gfp,unsigned order) +struct page *alloc_pages (gfp_t gfp,unsigned order) {struct mempolicy *pol = &default_policy;struct page *page;
@@ -2280,7 +2280,7 @@ struct page *alloc_pages_current(gfp_t greturn page; } -EXPORT_SYMBOL (alloc_pages_current); +EXPORT_SYMBOL (alloc_pages);int vma_dup_policy (struct vm_area_struct *src,struct vm_area_struct *dst) {
_
Andrew Morton
maintainerPATCH 159/178 mm/mempolicy: rewrite alloc_pages documentation
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/mempolicy: rewrite alloc_pages documentation
Document alloc_pages() for both NUMA and non-NUMA cases as kernel-doc doesn't care.
---
mm/mempolicy.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-rewrite-alloc_pages-documentation +10 -11
@@ -2245,19 +2245,18 @@ out:EXPORT_SYMBOL (alloc_pages_vma);/** -* alloc_pages - Allocate pages. +* alloc_pages - Allocate pages. +* @gfp: GFP flags. +* @order: Power of two of number of pages to allocate. * -* @gfp: -* %GFP_USER user allocation, -* %GFP_KERNEL kernel allocation, -* %GFP_HIGHMEM highmem allocation, -* %GFP_FS don't call back into a file system. -* %GFP_ATOMIC don't sleep. -* @order: Power of two of allocation size in pages. 0 is a single page. +* Allocate 1 << @order contiguous pages. The physical address of the +* first page is naturally aligned (eg an order-3 allocation will be aligned +* to a multiple of 8 * PAGE_SIZE bytes). The NUMA policy of the current +* process is honoured when in process context. * -* Allocate a page from the kernel page pool. When not in -* interrupt context and apply the current process NUMA policy. -* Returns NULL when no page can be allocated. +* Context: Can be called from any context, providing the appropriate GFP +* flags are used. +* Return: The page on success or NULL if allocation fails. */ struct page *alloc_pages (gfp_t gfp,unsigned order) {
_
Andrew Morton
maintainerPATCH 160/178 mm/mempolicy: rewrite alloc_pages_vma documentation
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/mempolicy: rewrite alloc_pages_vma documentation
The current formatting doesn't quite work with kernel-doc.
---
mm/mempolicy.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-)
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-rewrite-alloc_pages_vma-documentation +13 -21
@@ -2153,30 +2153,22 @@ static struct page *alloc_page_interleav }/** -* alloc_pages_vma - Allocate a page for a VMA. +* alloc_pages_vma - Allocate a page for a VMA. +* @gfp: GFP flags. +* @order: Order of the GFP allocation. +* @vma: Pointer to VMA or NULL if not available. +* @addr: Virtual address of the allocation. Must be inside @vma. +* @node: Which node to prefer for allocation (modulo policy). +* @hugepage: For hugepages try only the preferred node if possible. * -* @gfp: -* %GFP_USER user allocation. -* %GFP_KERNEL kernel allocations, -* %GFP_HIGHMEM highmem/user allocations, -* %GFP_FS allocation should not call back into a file system. -* %GFP_ATOMIC don't sleep. +* Allocate a page for a specific address in @vma, using the appropriate +* NUMA policy. When @vma is not NULL the caller must hold the mmap_lock +* of the mm_struct of the VMA to prevent it from going away. Should be +* used for all allocations for pages that will be mapped into user space. * -* @order:Order of the GFP allocation. -* @vma: Pointer to VMA or NULL if not available. -* @addr: Virtual Address of the allocation. Must be inside the VMA. -* @node: Which node to prefer for allocation (modulo policy). -* @hugepage: for hugepages try only the preferred node if possible -* -* This function allocates a page from the kernel page pool and applies -* a NUMA policy associated with the VMA or the current process. -* When VMA is not NULL caller must read-lock the mmap_lock of the -* mm_struct of the VMA to prevent it from going away. Should be used for -* all allocations for pages that will be mapped into user space. Returns -* NULL when no page can be allocated. +* Return: The page on success or NULL if allocation fails. */ -struct page * -alloc_pages_vma (gfp_t gfp,int order,struct vm_area_struct *vma, +struct page *alloc_pages_vma (gfp_t gfp,int order,struct vm_area_struct *vma,unsigned long addr,int node,bool hugepage) {struct mempolicy *pol;
_
Andrew Morton
maintainerPATCH 161/178 mm/mempolicy: fix mpol_misplaced kernel-doc
In reply to Andrew MortonFrom: "Matthew Wilcox (Oracle)" <[email protected]>
Subject: mm/mempolicy: fix mpol_misplaced kernel-doc
Sphinx interprets the Return section as a list and complains about it. Turn it into a sentence and move it to the end of the kernel-doc to fit the kernel-doc style.
---
Documentation/core-api/mm-api.rst | 1 + mm/mempolicy.c | 11 ++++------- 2 files changed, 5 insertions(+), 7 deletions(-)
Documentation/core-api/mm-api.rst renamed from Documentation/core-api/mm-api.rst~mm-mempolicy-fix-mpol_misplaced-kernel-doc +1 -0
@@ -92,6 +92,7 @@ More Memory Management Functions :export: .. kernel-doc:: mm/page_alloc.c +.. kernel-doc:: mm/mempolicy.c .. kernel-doc:: include/linux/mm_types.h :internal: .. kernel-doc:: include/linux/mm.h
mm/mempolicy.c renamed from mm/mempolicy.c~mm-mempolicy-fix-mpol_misplaced-kernel-doc +4 -7
@@ -2448,14 +2448,11 @@ static void sp_free(struct sp_node *n)* @addr: virtual address where page mapped * * Lookup current policy node id for vma,addr and "compare to" page's -* node id. -* -* Returns: -* -1 - not misplaced, page is in the right node -* node - node id where the page should be -* -* Policy determination "mimics" alloc_page_vma(). +* node id. Policy determination "mimics" alloc_page_vma(). * Called from fault path where we know the vma and faulting address. +* +* Return: -1 if the page is in a node that is valid for this policy, or a +* suitable node ID to allocate a replacement page from. */ int mpol_misplaced (struct page *page,struct vm_area_struct *vma,unsigned long addr) {
_
Andrew Morton
maintainerPATCH 162/178 mm: page_alloc: dump migrate-failed pages
In reply to Andrew MortonFrom: Minchan Kim <[email protected]>
Subject: mm: page_alloc: dump migrate-failed pages
Currently, debugging CMA allocation failures is quite limited. The most common source of these failures seems to be page migration which doesn't provide any useful information on the reason of the failure by itself. alloc_contig_range can report those failures as it holds a list of migrate-failed pages.
The information logged by dump_page() has already proven helpful for debugging allocation issues, like identifying long-term pinnings on ZONE_MOVABLE or MIGRATE_CMA.
Let's use the dynamic debugging infrastructure, such that we avoid flooding the logs and creating a lot of noise on frequent alloc_contig_range() calls. This information is helpful for debugging only.
There are two ifdefery conditions to support common dyndbg options:
- CONFIG_DYNAMIC_DEBUG_CORE && DYNAMIC_DEBUG_MODULE
It aims for supporting the feature with only specific file with adding ccflags.
- CONFIG_DYNAMIC_DEBUG
It aims for supporting the feature with system wide globally.
A simple example to enable the feature:
Admin could enable the dump like this(by default, disabled)
echo "func alloc_contig_dump_pages +p" > control
Admin could disable it.
echo "func alloc_contig_dump_pages =_" > control
Detail goes Documentation/admin-guide/dynamic-debug-howto.rst
A concern is utility functions in dump_page use inconsistent loglevels. In the future, we might want to make the loglevels used inside dump_page() consistent and eventually rework the way we log the information here. See [1].
[1] https://lore.kernel.org/linux-mm/[email protected]/
---
mm/page_alloc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-dump-migrate-failed-pages +22 -0
@@ -8497,6 +8497,27 @@ static unsigned long pfn_max_align_up(un pageblock_nr_pages)); } +#if defined(CONFIG_DYNAMIC_DEBUG ) || \ + (defined(CONFIG_DYNAMIC_DEBUG_CORE ) && defined(DYNAMIC_DEBUG_MODULE )) +/* Usage: See admin-guide/dynamic-debug-howto.rst */ +static void alloc_contig_dump_pages (struct list_head *page_list) +{ +DEFINE_DYNAMIC_DEBUG_METADATA (descriptor,"migrate failure" ); + +if (DYNAMIC_DEBUG_BRANCH (descriptor)) { +struct page *page; + +dump_stack (); +list_for_each_entry (page, page_list, lru) +dump_page (page,"migration failure" ); + } +} +#else +static inline void alloc_contig_dump_pages (struct list_head *page_list) +{ +} +#endif +/* [start, end) must belong to a single zone. */ static int __alloc_contig_migrate_range (struct compact_control *cc,unsigned long start,unsigned long end)
@@ -8540,6 +8561,7 @@ static int __alloc_contig_migrate_range( NULL, (unsigned long)&mtc, cc->mode, MR_CONTIG_RANGE); }if (ret <0 ) { +alloc_contig_dump_pages (&cc->migratepages);putback_movable_pages (&cc->migratepages);return ret; }
_
Re: [patch 162/178] mm: page_alloc: dump migrate-failed pages
Quoted 36 lines · Andrew Morton
From: Minchan Kim <[email protected]>
Subject: mm:page_alloc: dump migrate-failed pagesCurrently, debugging CMA allocation failures is quite limited. The most common source of these failures seems to be page migration which doesn't provide any useful information on the reason of the failure by itself.
alloc_contig_rangecan report those failures as it holds a list of migrate-failed pages.The information logged by
dump_page()has already proven helpful for debugging allocation issues, like identifying long-term pinnings onZONE_MOVABLEorMIGRATE_CMA.Let's use the dynamic debugging infrastructure, such that we avoid flooding the logs and creating a lot of noise on frequent
alloc_contig_range()calls. This information is helpful for debugging only.There are two ifdefery conditions to support common dyndbg options:
-
CONFIG_DYNAMIC_DEBUG_CORE&&DYNAMIC_DEBUG_MODULE
It aims for supporting the feature with only specific file with adding ccflags.-
CONFIG_DYNAMIC_DEBUG
It aims for supporting the feature with system wide globally.A simple example to enable the feature:
Admin could enable the dump like
this(by default, disabled)echo "func
alloc_contig_dump_pages+p" > controlAdmin could disable it.
Quoted 9 lines
echo "func
alloc_contig_dump_pages=_" > controlDetail goes
Documentation/admin-guide/dynamic-debug-howto.rstA concern is utility functions in
dump_pageuse inconsistent loglevels. In the future, we might want to make the loglevels used insidedump_page()consistent and eventually rework the way we log the information here. See [1].
FWIW, the "format ^$prefix" specifier allows selection of "classes" of pr_debugs
examples:
"^drm:kms:" etc
"^drm:" too, for example to set +fmlt across entire subsystem "gvt:"
"^Lo", "^MID", "^HI"
"^1:", "^2:", "^3:"
all hierarchy / set-creation is applied by authors
Andrew Morton
maintainerPATCH 163/178 mm/Kconfig: remove default DISCONTIGMEM_MANUAL
In reply to Andrew MortonFrom: Geert Uytterhoeven <[email protected]>
Subject: mm/Kconfig: remove default DISCONTIGMEM_MANUAL
Commit 214496cb18700fd7 ("ia64: make SPARSEMEM default and disable DISCONTIGMEM") removed the last enabler of ARCH_DISCONTIGMEM_DEFAULT, hence the memory model can no longer default to DISCONTIGMEM_MANUAL.
---
mm/Kconfig | 1 - 1 file changed, 1 deletion(-)
mm/Kconfig renamed from mm/Kconfig~mm-remove-default-discontigmem_manual +0 -1
@@ -9,7 +9,6 @@ config SELECT_MEMORY_MODEL choice prompt "Memory model" depends on SELECT_MEMORY_MODEL - default DISCONTIGMEM_MANUAL if ARCH_DISCONTIGMEM_DEFAULT default SPARSEMEM_MANUAL if ARCH_SPARSEMEM_DEFAULT default FLATMEM_MANUAL help
_
Andrew Morton
maintainerPATCH 164/178 mm, page_alloc: avoid page_to_pfn() in move_freepages()
In reply to Andrew MortonFrom: Kefeng Wang <[email protected]>
Subject: mm, page_alloc: avoid page_to_pfn() in move_freepages()
The start_pfn and end_pfn are already available in move_freepages_block(), there is no need to go back and forth between page and pfn in move_freepages and move_freepages_block, and pfn_valid_within() should validate pfn first before touching the page.
---
mm/page_alloc.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-avoid-page_to_pfn-in-move_freepages +13 -15
@@ -2431,19 +2431,21 @@ static inline struct page *__rmqueue_cma* boundary. If alignment is required, use move_freepages_block() */ static int move_freepages (struct zone *zone, -struct page *start_page,struct page *end_page, +unsigned long start_pfn,unsigned long end_pfn,int migratetype,int *num_movable) {struct page *page; +unsigned long pfn;unsigned int order;int pages_moved =0 ; -for (page = start_page; page <= end_page;) { -if (!pfn_valid_within (page_to_pfn (page))) { - page++; +for (pfn = start_pfn; pfn <= end_pfn;) { +if (!pfn_valid_within (pfn)) { + pfn++;continue ; } + page =pfn_to_page (pfn);if (!PageBuddy (page)) {/* * We assume that pages that could be isolated for
@@ -2453,8 +2455,7 @@ static int move_freepages(struct zone *zif (num_movable && (PageLRU (page) ||__PageMovable (page))) (*num_movable)++; - - page++; + pfn++;continue ; }
@@ -2464,7 +2465,7 @@ static int move_freepages(struct zone *z order =buddy_order (page);move_to_free_list (page, zone, order, migratetype); - page +=1 << order; + pfn +=1 << order; pages_moved +=1 << order; }
@@ -2474,25 +2475,22 @@ static int move_freepages(struct zone *zint move_freepages_block (struct zone *zone,struct page *page,int migratetype,int *num_movable) { -unsigned long start_pfn, end_pfn; -struct page *start_page, *end_page; +unsigned long start_pfn, end_pfn, pfn;if (num_movable) *num_movable =0 ; - start_pfn =page_to_pfn (page); - start_pfn = start_pfn & ~(pageblock_nr_pages-1 ); - start_page =pfn_to_page (start_pfn); - end_page = start_page + pageblock_nr_pages -1 ; + pfn =page_to_pfn (page); + start_pfn = pfn & ~(pageblock_nr_pages -1 ); end_pfn = start_pfn + pageblock_nr_pages -1 ;/* Do not cross zone boundaries */ if (!zone_spans_pfn (zone, start_pfn)) - start_page = page; + start_pfn = pfn;if (!zone_spans_pfn (zone, end_pfn))return 0 ; -return move_freepages (zone, start_page, end_page, migratetype, +return move_freepages (zone, start_pfn, end_pfn, migratetype, num_movable); }
_
Andrew Morton
maintainerPATCH 165/178 mm/page_alloc: duplicate include linux/vmalloc.h
In reply to Andrew MortonFrom: zhouchuangao <[email protected]>
Subject: mm/page_alloc: duplicate include linux/vmalloc.h
linux/vmalloc.h is repeatedly in the file page_alloc.c
---
mm/page_alloc.c | 2 -- 1 file changed, 2 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-duplicate-include-linux-vmalloch +0 -2
@@ -72,8 +72,6 @@#include <linux/padata.h> #include <linux/khugepaged.h> #include <linux/buffer_head.h> -#include <linux/vmalloc.h> -#include <asm/sections.h> #include <asm/tlbflush.h> #include <asm/div64.h>
_
Andrew Morton
maintainerPATCH 166/178 mm/page_alloc: rename alloced to allocated
In reply to Andrew MortonFrom: Mel Gorman <[email protected]>
Subject: mm/page_alloc: rename alloced to allocated
Patch series "Introduce a bulk order-0 page allocator with two in-tree users", v6.
This series introduces a bulk order-0 page allocator with sunrpc and the network page pool being the first users. The implementation is not efficient as semantics needed to be ironed out first. If no other semantic changes are needed, it can be made more efficient. Despite that, this is a performance-related for users that require multiple pages for an operation without multiple round-trips to the page allocator. Quoting the last patch for the high-speed networking use-case
Kernel XDP stats CPU pps Delta
Baseline XDP-RX CPU total 3,771,046 n/a
List XDP-RX CPU total 3,940,242 +4.49%
Array XDP-RX CPU total 4,249,224 +12.68%Via the SUNRPC traces of svc_alloc_arg()
Single page: 25.007 us per call over 532,571 calls Bulk list: 6.258 us per call over 517,034 calls Bulk array: 4.590 us per call over 517,442 calls
Both potential users in this series are corner cases (NFS and high-speed networks) so it is unlikely that most users will see any benefit in the short term. Other potential other users are batch allocations for page cache readahead, fault around and SLUB allocations when high-order pages are unavailable. It's unknown how much benefit would be seen by converting multiple page allocation calls to a single batch or what difference it may make to headline performance.
Light testing of my own running dbench over NFS passed. Chuck and Jesper conducted their own tests and details are included in the changelogs.
Patch 1 renames a variable name that is particularly unpopular
Patch 2 adds a bulk page allocator
Patch 3 adds an array-based version of the bulk allocator
Patches 4-5 adds micro-optimisations to the implementation
Patches 6-7 SUNRPC user
Patches 8-9 Network page_pool user
This patch (of 9):
Review feedback of the bulk allocator twice found problems with "alloced" being a counter for pages allocated. The naming was based on the API name "alloc" and was based on the idea that verbal communication about malloc tends to use the fake word "malloced" instead of the fake word mallocated. To be consistent, this preparation patch renames alloced to allocated in rmqueue_bulk so the bulk allocator and per-cpu allocator use similar names when the bulk allocator is introduced.
---
mm/page_alloc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-rename-alloced-to-allocated +4 -4
@@ -2949,7 +2949,7 @@ static int rmqueue_bulk(struct zone *zon unsigned long count, struct list_head *list, int migratetype, unsigned int alloc_flags) { - int i, alloced = 0; + int i, allocated = 0;spin_lock (&zone->lock);for (i =0 ; i < count; ++i) {
@@ -2972,7 +2972,7 @@ static int rmqueue_bulk(struct zone *zon* pages are ordered properly. */ list_add_tail (&page->lru, list); - alloced++; + allocated++;if (is_migrate_cma (get_pcppage_migratetype (page)))__mod_zone_page_state (zone,NR_FREE_CMA_PAGES , -(1 << order));
@@ -2981,12 +2981,12 @@ static int rmqueue_bulk(struct zone *zon/* * i pages were removed from the buddy list even if some leak due * to check_pcp_refill failing so adjust NR_FREE_PAGES based -* on i. Do not confuse with 'alloced' which is the number of +* on i. Do not confuse with 'allocated' which is the number of * pages added to the pcp list. */ __mod_zone_page_state (zone,NR_FREE_PAGES , -(i << order));spin_unlock (&zone->lock); -return alloced; +return allocated; }#ifdef CONFIG_NUMA
_
Andrew Morton
maintainerPATCH 167/178 mm/page_alloc: add a bulk page allocator
In reply to Andrew MortonFrom: Mel Gorman <[email protected]>
Subject: mm/page_alloc: add a bulk page allocator
This patch adds a new page allocator interface via alloc_pages_bulk, and __alloc_pages_bulk_nodemask. A caller requests a number of pages to be allocated and added to a list.
The API is not guaranteed to return the requested number of pages and may fail if the preferred allocation zone has limited free memory, the cpuset changes during the allocation or page debugging decides to fail an allocation. It's up to the caller to request more pages in batch if necessary.
Note that this implementation is not very efficient and could be improved but it would require refactoring. The intent is to make it available early to determine what semantics are required by different callers. Once the full semantics are nailed down, it can be refactored.
---
include/linux/gfp.h | 11 +++ mm/page_alloc.c | 118 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+)
include/linux/gfp.h renamed from include/linux/gfp.h~mm-page_alloc-add-a-bulk-page-allocator +11 -0
@@ -518,6 +518,17 @@ static inline int arch_make_page_accessistruct page *__alloc_pages (gfp_t gfp,unsigned int order,int preferred_nid,nodemask_t *nodemask); +unsigned long __alloc_pages_bulk (gfp_t gfp,int preferred_nid, +nodemask_t *nodemask,int nr_pages, +struct list_head *list); + +/* Bulk allocate order-0 pages */ +static inline unsigned long +alloc_pages_bulk (gfp_t gfp,unsigned long nr_pages,struct list_head *list) +{ +return __alloc_pages_bulk (gfp,numa_mem_id (),NULL , nr_pages, list); +} +/* * Allocate pages, preferring the node given as nid. The node must be valid and * online. For more general interface, see alloc_pages_node().
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-add-a-bulk-page-allocator +118 -0
124 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 168/178 mm/page_alloc: add an array-based interface to the bulk page allocator
In reply to Andrew MortonFrom: Mel Gorman <[email protected]>
Subject: mm/page_alloc: add an array-based interface to the bulk page allocator
The proposed callers for the bulk allocator store pages from the bulk allocator in an array. This patch adds an array-based interface to the API to avoid multiple list iterations. The page list interface is preserved to avoid requiring all users of the bulk API to allocate and manage enough storage to store the pages.
---
include/linux/gfp.h | 13 +++++++-- mm/page_alloc.c | 60 ++++++++++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 19 deletions(-)
include/linux/gfp.h renamed from include/linux/gfp.h~mm-page_alloc-add-an-array-based-interface-to-the-bulk-page-allocator +10 -3
@@ -520,13 +520,20 @@ struct page *__alloc_pages(gfp_t gfp, ununsigned long __alloc_pages_bulk (gfp_t gfp,int preferred_nid,nodemask_t *nodemask,int nr_pages, -struct list_head *list); +struct list_head *page_list, +struct page **page_array);/* Bulk allocate order-0 pages */ static inline unsigned long -alloc_pages_bulk (gfp_t gfp,unsigned long nr_pages,struct list_head *list) +alloc_pages_bulk_list (gfp_t gfp,unsigned long nr_pages,struct list_head *list) { -return __alloc_pages_bulk (gfp,numa_mem_id (),NULL , nr_pages, list); +return __alloc_pages_bulk (gfp,numa_mem_id (),NULL , nr_pages, list,NULL ); +} + +static inline unsigned long +alloc_pages_bulk_array (gfp_t gfp,unsigned long nr_pages,struct page **page_array) +{ +return __alloc_pages_bulk (gfp,numa_mem_id (),NULL , nr_pages,NULL , page_array); }/*
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-add-an-array-based-interface-to-the-bulk-page-allocator +44 -16
114 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 169/178 mm/page_alloc: optimize code layout for __alloc_pages_bulk
In reply to Andrew MortonFrom: Jesper Dangaard Brouer <[email protected]>
Subject: mm/page_alloc: optimize code layout for __alloc_pages_bulk
Looking at perf-report and ASM-code for __alloc_pages_bulk() it is clear that the code activated is suboptimal. The compiler guesses wrong and places unlikely code at the beginning. Due to the use of WARN_ON_ONCE() macro the UD2 asm instruction is added to the code, which confuse the I-cache prefetcher in the CPU.
---
mm/page_alloc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-optimize-code-layout-for-__alloc_pages_bulk +3 -3
@@ -5042,7 +5042,7 @@ unsigned long __alloc_pages_bulk(gfp_t gunsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 ; -if (WARN_ON_ONCE (nr_pages <=0 )) +if (unlikely (nr_pages <=0 ))return 0 ;/*
@@ -5089,7 +5089,7 @@ unsigned long __alloc_pages_bulk(gfp_t g* If there are no allowed local zones that meets the watermarks then * try to allocate a single page and reclaim if necessary. */ -if (!zone) +if (unlikely (!zone)) goto failed;/* Attempt the batch allocation */
@@ -5107,7 +5107,7 @@ unsigned long __alloc_pages_bulk(gfp_t g page =__rmqueue_pcplist (zone, ac.migratetype, alloc_flags, pcp, pcp_list); -if (!page) { +if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated) goto failed_irq;
_
Andrew Morton
maintainerPATCH 170/178 mm/page_alloc: inline __rmqueue_pcplist
In reply to Andrew MortonFrom: Jesper Dangaard Brouer <[email protected]>
Subject: mm/page_alloc: inline __rmqueue_pcplist
When __alloc_pages_bulk() got introduced two callers of __rmqueue_pcplist exist and the compiler chooses to not inline this function.
./scripts/bloat-o-meter vmlinux-before vmlinux-inline__rmqueue_pcplist
add/remove: 0/1 grow/shrink: 2/0 up/down: 164/-125 (39)
Function old new delta
rmqueue 2197 2296 +99 __alloc_pages_bulk 1921 1986 +65
__rmqueue_pcplist 125 - -125
Total: Before=19374127, After=19374166, chg +0.00%
modprobe page_bench04_bulk loops=$((10**7))
Type:time_bulk_page_alloc_free_array
- Per elem: 106 cycles(tsc) 29.595 ns (step:64)
- (measurement period time:0.295955434 sec time_interval:295955434)
- (invoke count:10000000 tsc_interval:1065447105)
Before:
- Per elem: 110 cycles(tsc) 30.633 ns (step:64)
---
mm/page_alloc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-inline-__rmqueue_pcplist +2 -1
@@ -3456,7 +3456,8 @@ static inline void zone_statistics(struc }/* Remove page from the per-cpu list, caller must protect the list */ -static struct page *__rmqueue_pcplist (struct zone *zone,int migratetype, +static inline +struct page *__rmqueue_pcplist (struct zone *zone,int migratetype,unsigned int alloc_flags,struct per_cpu_pages *pcp,struct list_head *list)
_
Andrew Morton
maintainerPATCH 171/178 SUNRPC: set rq_page_end differently
In reply to Andrew MortonFrom: Chuck Lever <[email protected]>
Subject: SUNRPC: set rq_page_end differently
Patch series "SUNRPC consumer for the bulk page allocator"
This patch set and the measurements below are based on yesterday's bulk allocator series:
git://git.kernel.org/pub/scm/linux/kernel/git/mel/linux.git mm-bulk-rebase-v5r9
The patches change SUNRPC to invoke the array-based bulk allocator instead of alloc_page().
The micro-benchmark results are promising. I ran a mixture of 256KB reads and writes over NFSv3. The server's kernel is built with KASAN enabled, so the comparison is exaggerated but I believe it is still valid.
I instrumented svc_recv() to measure the latency of each call to svc_alloc_arg() and report it via a trace point. The following results are averages across the trace events.
Single page: 25.007 us per call over 532,571 calls Bulk list: 6.258 us per call over 517,034 calls Bulk array: 4.590 us per call over 517,442 calls
This patch (of 2)
Refactor:
I'm about to use the loop variable @i for something else.
As far as the "i++" is concerned, that is a post-increment. The value of @i is not used subsequently, so the increment operator is unnecessary and can be removed.
Also note that nfsd_read_actor() was renamed nfsd_splice_actor() by commit cf8208d0eabd ("sendfile: convert nfsd to splice_direct_to_actor()").
---
net/sunrpc/svc_xprt.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
net/sunrpc/svc_xprt.c renamed from net/sunrpc/svc_xprt.c~sunrpc-set-rq_page_end-differently +3 -4
@@ -661,7 +661,7 @@ static void svc_check_conn_limits(structstatic int svc_alloc_arg (struct svc_rqst *rqstp) {struct svc_serv *serv = rqstp->rq_server; -struct xdr_buf *arg; +struct xdr_buf *arg = &rqstp->rq_arg;int pages;int i;
@@ -686,11 +686,10 @@ static int svc_alloc_arg(struct svc_rqst } rqstp->rq_pages[i] = p; } - rqstp->rq_page_end = &rqstp->rq_pages[i]; - rqstp->rq_pages[i++] =NULL ;/* this might be seen in nfs_read_actor */ + rqstp->rq_page_end = &rqstp->rq_pages[pages]; + rqstp->rq_pages[pages] =NULL ;/* this might be seen in nfsd_splice_actor() */ /* Make arg->head point to first page and arg->pages point to rest */ - arg = &rqstp->rq_arg; arg->head[0 ].iov_base =page_address (rqstp->rq_pages[0 ]); arg->head[0 ].iov_len =PAGE_SIZE ; arg->pages = rqstp->rq_pages +1 ;
_
Andrew Morton
maintainerPATCH 172/178 SUNRPC: refresh rq_pages using a bulk page allocator
In reply to Andrew MortonFrom: Chuck Lever <[email protected]>
Subject: SUNRPC: refresh rq_pages using a bulk page allocator
Reduce the rate at which nfsd threads hammer on the page allocator. This improves throughput scalability by enabling the threads to run more independently of each other.
---
net/sunrpc/svc_xprt.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
net/sunrpc/svc_xprt.c renamed from net/sunrpc/svc_xprt.c~sunrpc-refresh-rq_pages-using-a-bulk-page-allocator +15 -16
@@ -662,30 +662,29 @@ static int svc_alloc_arg(struct svc_rqst {struct svc_serv *serv = rqstp->rq_server;struct xdr_buf *arg = &rqstp->rq_arg; -int pages; -int i; +unsigned long pages, filled; -/* now allocate needed pages. If we get a failure, sleep briefly */ pages = (serv->sv_max_mesg +2 *PAGE_SIZE ) >>PAGE_SHIFT ;if (pages >RPCSVC_MAXPAGES ) { -pr_warn_once ("svc: warning: pages=%u > RPCSVC_MAXPAGES=%lu\n" , +pr_warn_once ("svc: warning: pages=%lu > RPCSVC_MAXPAGES=%lu\n" , pages,RPCSVC_MAXPAGES );/* use as many pages as possible */ pages =RPCSVC_MAXPAGES ; } -for (i =0 ; i < pages ; i++) -while (rqstp->rq_pages[i] ==NULL ) { -struct page *p =alloc_page (GFP_KERNEL ); -if (!p) { -set_current_state (TASK_INTERRUPTIBLE ); -if (signalled () ||kthread_should_stop ()) { -set_current_state (TASK_RUNNING ); -return -EINTR ; - } -schedule_timeout (msecs_to_jiffies (500 )); - } - rqstp->rq_pages[i] = p; + +for (;;) { + filled =alloc_pages_bulk_array (GFP_KERNEL , pages, + rqstp->rq_pages); +if (filled == pages) +break ; + +set_current_state (TASK_INTERRUPTIBLE ); +if (signalled () ||kthread_should_stop ()) { +set_current_state (TASK_RUNNING ); +return -EINTR ; } +schedule_timeout (msecs_to_jiffies (500 )); + } rqstp->rq_page_end = &rqstp->rq_pages[pages]; rqstp->rq_pages[pages] =NULL ;/* this might be seen in nfsd_splice_actor() */
_
Andrew Morton
maintainerPATCH 173/178 net: page_pool: refactor dma_map into own function page_pool_dma_map
In reply to Andrew MortonFrom: Jesper Dangaard Brouer <[email protected]>
Subject: net: page_pool: refactor dma_map into own function page_pool_dma_map
In preparation for next patch, move the dma mapping into its own function, as this will make it easier to follow the changes.
---
net/core/page_pool.c | 45 +++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-)
net/core/page_pool.c renamed from net/core/page_pool.c~net-page_pool-refactor-dma_map-into-own-function-page_pool_dma_map +26 -19
@@ -180,14 +180,37 @@ static void page_pool_dma_sync_for_devic pool->p.dma_dir); } +static bool page_pool_dma_map (struct page_pool *pool,struct page *page) +{ +dma_addr_t dma; + +/* Setup DMA mapping: use 'struct page' area for storing DMA-addr +* since dma_addr_t can be either 32 or 64 bits and does not always fit +* into page private data (i.e 32bit cpu with 64bit DMA caps) +* This mapping is kept for lifetime of page, until leaving pool. +*/ + dma =dma_map_page_attrs (pool->p.dev, page,0 , + (PAGE_SIZE << pool->p.order), + pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); +if (dma_mapping_error (pool->p.dev, dma)) +return false; + + page->dma_addr = dma; + +if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) +page_pool_dma_sync_for_device (pool, page, pool->p.max_len); + +return true; +} +/* slow path */ noinline static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,gfp_t _gfp) { +unsigned int pp_flags = pool->p.flags;struct page *page;gfp_t gfp = _gfp; -dma_addr_t dma;/* We could always set __GFP_COMP, and avoid this branch, as * prep_new_page() can handle order-0 with __GFP_COMP.
@@ -211,30 +234,14 @@ static struct page *__page_pool_alloc_paif (!page)return NULL ; -if (!(pool->p.flags &PP_FLAG_DMA_MAP )) - goto skip_dma_map; - -/* Setup DMA mapping: use 'struct page' area for storing DMA-addr -* since dma_addr_t can be either 32 or 64 bits and does not always fit -* into page private data (i.e 32bit cpu with 64bit DMA caps) -* This mapping is kept for lifetime of page, until leaving pool. -*/ - dma =dma_map_page_attrs (pool->p.dev, page,0 , - (PAGE_SIZE << pool->p.order), - pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); -if (dma_mapping_error (pool->p.dev, dma)) { +if ((pp_flags &PP_FLAG_DMA_MAP ) && +unlikely (!page_pool_dma_map (pool, page))) {put_page (page);return NULL ; } - page->dma_addr = dma; -if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) -page_pool_dma_sync_for_device (pool, page, pool->p.max_len); - -skip_dma_map:/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; -trace_page_pool_state_hold (pool, page, pool->pages_state_hold_cnt);/* When page just alloc'ed is should/must have refcnt 1. */
_
Andrew Morton
maintainerPATCH 174/178 net: page_pool: use alloc_pages_bulk in refill code path
In reply to Andrew MortonFrom: Jesper Dangaard Brouer <[email protected]>
Subject: net: page_pool: use alloc_pages_bulk in refill code path
There are cases where the page_pool need to refill with pages from the page allocator. Some workloads cause the page_pool to release pages instead of recycling these pages.
For these workload it can improve performance to bulk alloc pages from the page-allocator to refill the alloc cache.
For XDP-redirect workload with 100G mlx5 driver (that use page_pool) redirecting xdp_frame packets into a veth, that does XDP_PASS to create an SKB from the xdp_frame, which then cannot return the page to the page_pool.
Performance results under GitHub xdp-project[1]:
[1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
Mel: The patch "net: page_pool: convert to use alloc_pages_bulk_array variant" was squashed with this patch. From the test page, the array variant was superior with one of the test results as follows.
Kernel XDP stats CPU pps Delta Baseline XDP-RX CPU total 3,771,046 n/a List XDP-RX CPU total 3,940,242 +4.49% Array XDP-RX CPU total 4,249,224 +12.68%
---
include/net/page_pool.h | 2 net/core/page_pool.c | 82 +++++++++++++++++++++++++------------- 2 files changed, 57 insertions(+), 27 deletions(-)
include/net/page_pool.h renamed from include/net/page_pool.h~net-page_pool-use-alloc_pages_bulk-in-refill-code-path +1 -1
@@ -65,7 +65,7 @@#define PP_ALLOC_CACHE_REFILL 64struct pp_alloc_cache {u32 count; -void *cache[PP_ALLOC_CACHE_SIZE ]; +struct page *cache[PP_ALLOC_CACHE_SIZE ]; };struct page_pool_params {
net/core/page_pool.c renamed from net/core/page_pool.c~net-page_pool-use-alloc_pages_bulk-in-refill-code-path +56 -26
100 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 175/178 mm: page_alloc: ignore init_on_free=1 for debug_pagealloc=1
In reply to Andrew MortonFrom: Sergei Trofimovich <[email protected]>
Subject: mm: page_alloc: ignore init_on_free=1 for debug_pagealloc=1
On !ARCH_SUPPORTS_DEBUG_PAGEALLOC (like ia64) debug_pagealloc=1 implies page_poison=on:
if (page_poisoning_enabled() ||
(!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
debug_pagealloc_enabled()))
static_branch_enable(&_page_poisoning_enabled);page_poison=on needs to override init_on_free=1.
Before the change it did not work as expected for the following case:
- have PAGE_POISONING=y
- have page_poison unset
- have !ARCH_SUPPORTS_DEBUG_PAGEALLOC arch (like ia64)
- have init_on_free=1
- have debug_pagealloc=1
That way we get both keys enabled:
- static_branch_enable(&init_on_free);
- static_branch_enable(&_page_poisoning_enabled);
which leads to poisoned pages returned for __GFP_ZERO pages.
After the change we execute only:
- static_branch_enable(&_page_poisoning_enabled);
and ignore init_on_free=1.
---
mm/page_alloc.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-ignore-init_on_free=1-for-debug_pagealloc=1 +17 -13
@@ -786,32 +786,36 @@ static inline void clear_page_guard(stru*/ void init_mem_debugging_and_hardening (void ) { +bool page_poisoning_requested = false; + +#ifdef CONFIG_PAGE_POISONING +/* +* Page poisoning is debug page alloc for some arches. If +* either of those options are enabled, enable poisoning. +*/ +if (page_poisoning_enabled () || + (!IS_ENABLED (CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC ) && +debug_pagealloc_enabled ())) { +static_branch_enable (&_page_poisoning_enabled); + page_poisoning_requested = true; + } +#endif +if (_init_on_alloc_enabled_early) { -if (page_poisoning_enabled ()) +if (page_poisoning_requested)pr_info ("mem auto-init: CONFIG_PAGE_POISONING is on, " "will take precedence over init_on_alloc\n" );else static_branch_enable (&init_on_alloc); }if (_init_on_free_enabled_early) { -if (page_poisoning_enabled ()) +if (page_poisoning_requested)pr_info ("mem auto-init: CONFIG_PAGE_POISONING is on, " "will take precedence over init_on_free\n" );else static_branch_enable (&init_on_free); } -#ifdef CONFIG_PAGE_POISONING -/* -* Page poisoning is debug page alloc for some arches. If -* either of those options are enabled, enable poisoning. -*/ -if (page_poisoning_enabled () || - (!IS_ENABLED (CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC ) && -debug_pagealloc_enabled ())) -static_branch_enable (&_page_poisoning_enabled); -#endif -#ifdef CONFIG_DEBUG_PAGEALLOC if (!debug_pagealloc_enabled ())return ;
_
Andrew Morton
maintainerPATCH 176/178 mm/page_alloc: redundant definition variables of pfn in for loop
In reply to Andrew MortonFrom: huxiang <[email protected]>
Subject: mm/page_alloc: redundant definition variables of pfn in for loop
This variable pfn is defined repeatedly, so it can be deleted.
---
mm/page_alloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c renamed from mm/page_alloc.c~mm-page_alloc-redundant-definition-variables-of-pfn-in-for-loop +1 -1
@@ -3322,7 +3322,7 @@ void free_unref_page_list(struct list_helocal_irq_save (flags);list_for_each_entry_safe (page, next, list, lru) { -unsigned long pfn =page_private (page); + pfn =page_private (page);set_page_private (page,0 );trace_mm_page_free_batched (page);
_
Re: [patch 176/178] mm/page_alloc: redundant definition variables of pfn in for loop
Quoted 2 lines · Andrew Morton
This variable pfn is defined repeatedly, so it can be deleted.
I'd actually much prefer this patch to be done exactly the other way:
avoid the variable name shadowing not by removing the second declaration, but by actually making *both* declarations local to the loops.
The lifetime of those 'pfn' variables really is just the inner body of the loop, not the whole function.
Now, a good compiler will notice that the uses are entirely disjoint, and not care about how the programmer used the same variable for two different cases, but it's good practice to just minimize the scope of a variable.
So I'm dropping this, but I would apply a patch that did something like this instead
void free_unref_page_list(struct list_head *list)
{
struct page *page, *next;
- unsigned long flags, pfn;
+ unsigned long flags;
int batch_count = 0;
/* Prepare pages for freeing */
list_for_each_entry_safe(page, next, list, lru) {
- pfn = page_to_pfn(page);
+ unsigned long pfn = page_to_pfn(page);
if (!free_unref_page_prepare(page, pfn))
list_del(&page->lru);
set_page_private(page, pfn);(UNTESTED, and intentionally whitespace-damaged, you get the idea).
Linus
Andrew Morton
maintainerPATCH 177/178 mm/mmzone.h: fix existing kernel-doc comments and link them to core-api
In reply to Andrew MortonFrom: Mike Rapoport <[email protected]>
Subject: mm/mmzone.h: fix existing kernel-doc comments and link them to core-api
There are a couple of kernel-doc comments in include/linux/mmzone.h but they have minor formatting issues that would cause kernel-doc warnings.
Fix the formatting of those comments, add missing Return: descriptions and link include/linux/mmzone.h to Documentation/core-api/mm-api.rst
---
Documentation/core-api/mm-api.rst | 1 include/linux/mmzone.h | 43 +++++++++++++++------------- 2 files changed, 25 insertions(+), 19 deletions(-)
Documentation/core-api/mm-api.rst renamed from Documentation/core-api/mm-api.rst~mm-mmzoneh-fix-existing-kernel-doc-comments-and-link-them-to-core-api +1 -0
@@ -97,3 +97,4 @@ More Memory Management Functions :internal: .. kernel-doc:: include/linux/mm.h :internal: +.. kernel-doc:: include/linux/mmzone.h
include/linux/mmzone.h renamed from include/linux/mmzone.h~mm-mmzoneh-fix-existing-kernel-doc-comments-and-link-them-to-core-api +24 -19
97 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 178/178 mm/memory-failure: unnecessary amount of unmapping
In reply to Andrew MortonFrom: Jane Chu <[email protected]>
Subject: mm/memory-failure: unnecessary amount of unmapping
It appears that unmap_mapping_range() actually takes a 'size' as its third argument rather than a location, the current calling fashion causes unnecessary amount of unmapping to occur.
---
mm/memory-failure.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-unecessary-amount-of-unmapping +1 -1
@@ -1368,7 +1368,7 @@ static int memory_failure_dev_pagemap(un* communicated in siginfo, see kill_proc() */ start = (page->index <<PAGE_SHIFT ) & ~(size -1 ); -unmap_mapping_range (page->mapping, start, start + size,0 ); +unmap_mapping_range (page->mapping, start, size,0 ); }kill_procs (&tokill, flags &MF_MUST_KILL , !unmap_success, pfn, flags); rc =0 ;
_