Merge tag 'mm-stable-2024-05-22-17-22' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Pull more mm updates from Andrew Morton:
"A series from Dave Chinner which cleans up and fixes the handling of
nested allocations within stackdepot and page-owner"

* tag 'mm-stable-2024-05-22-17-22' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm:
mm/page-owner: use gfp_nested_mask() instead of open coded masking
stackdepot: use gfp_nested_mask() instead of open coded masking
mm: lift gfp_kmemleak_mask() to gfp.h

+32 -23
+25
include/linux/gfp.h
··· 157 } 158 159 /* 160 * We get the zone list from the current node and the gfp_mask. 161 * This zone list contains a maximum of MAX_NUMNODES*MAX_NR_ZONES zones. 162 * There are two zonelists per node, one for all zones with memory and
··· 157 } 158 159 /* 160 + * gfp flag masking for nested internal allocations. 161 + * 162 + * For code that needs to do allocations inside the public allocation API (e.g. 163 + * memory allocation tracking code) the allocations need to obey the caller 164 + * allocation context constrains to prevent allocation context mismatches (e.g. 165 + * GFP_KERNEL allocations in GFP_NOFS contexts) from potential deadlock 166 + * situations. 167 + * 168 + * It is also assumed that these nested allocations are for internal kernel 169 + * object storage purposes only and are not going to be used for DMA, etc. Hence 170 + * we strip out all the zone information and leave just the context information 171 + * intact. 172 + * 173 + * Further, internal allocations must fail before the higher level allocation 174 + * can fail, so we must make them fail faster and fail silently. We also don't 175 + * want them to deplete emergency reserves. Hence nested allocations must be 176 + * prepared for these allocations to fail. 177 + */ 178 + static inline gfp_t gfp_nested_mask(gfp_t flags) 179 + { 180 + return ((flags & (GFP_KERNEL | GFP_ATOMIC | __GFP_NOLOCKDEP)) | 181 + (__GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)); 182 + } 183 + 184 + /* 185 * We get the zone list from the current node and the gfp_mask. 186 * This zone list contains a maximum of MAX_NUMNODES*MAX_NR_ZONES zones. 187 * There are two zonelists per node, one for all zones with memory and
+2 -9
lib/stackdepot.c
··· 624 * we won't be able to do that under the lock. 625 */ 626 if (unlikely(can_alloc && !READ_ONCE(new_pool))) { 627 - /* 628 - * Zero out zone modifiers, as we don't have specific zone 629 - * requirements. Keep the flags related to allocation in atomic 630 - * contexts, I/O, nolockdep. 631 - */ 632 - alloc_flags &= ~GFP_ZONEMASK; 633 - alloc_flags &= (GFP_ATOMIC | GFP_KERNEL | __GFP_NOLOCKDEP); 634 - alloc_flags |= __GFP_NOWARN; 635 - page = alloc_pages(alloc_flags, DEPOT_POOL_ORDER); 636 if (page) 637 prealloc = page_address(page); 638 }
··· 624 * we won't be able to do that under the lock. 625 */ 626 if (unlikely(can_alloc && !READ_ONCE(new_pool))) { 627 + page = alloc_pages(gfp_nested_mask(alloc_flags), 628 + DEPOT_POOL_ORDER); 629 if (page) 630 prealloc = page_address(page); 631 }
+4 -8
mm/kmemleak.c
··· 114 115 #define BYTES_PER_POINTER sizeof(void *) 116 117 - /* GFP bitmask for kmemleak internal allocations */ 118 - #define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC | \ 119 - __GFP_NOLOCKDEP)) | \ 120 - __GFP_NORETRY | __GFP_NOMEMALLOC | \ 121 - __GFP_NOWARN) 122 - 123 /* scanning area inside a memory block */ 124 struct kmemleak_scan_area { 125 struct hlist_node node; ··· 457 458 /* try the slab allocator first */ 459 if (object_cache) { 460 - object = kmem_cache_alloc_noprof(object_cache, gfp_kmemleak_mask(gfp)); 461 if (object) 462 return object; 463 } ··· 942 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer); 943 944 if (scan_area_cache) 945 - area = kmem_cache_alloc_noprof(scan_area_cache, gfp_kmemleak_mask(gfp)); 946 947 raw_spin_lock_irqsave(&object->lock, flags); 948 if (!area) {
··· 114 115 #define BYTES_PER_POINTER sizeof(void *) 116 117 /* scanning area inside a memory block */ 118 struct kmemleak_scan_area { 119 struct hlist_node node; ··· 463 464 /* try the slab allocator first */ 465 if (object_cache) { 466 + object = kmem_cache_alloc_noprof(object_cache, 467 + gfp_nested_mask(gfp)); 468 if (object) 469 return object; 470 } ··· 947 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer); 948 949 if (scan_area_cache) 950 + area = kmem_cache_alloc_noprof(scan_area_cache, 951 + gfp_nested_mask(gfp)); 952 953 raw_spin_lock_irqsave(&object->lock, flags); 954 if (!area) {
+1 -6
mm/page_owner.c
··· 168 unsigned long flags; 169 struct stack *stack; 170 171 - /* Filter gfp_mask the same way stackdepot does, for consistency */ 172 - gfp_mask &= ~GFP_ZONEMASK; 173 - gfp_mask &= (GFP_ATOMIC | GFP_KERNEL | __GFP_NOLOCKDEP); 174 - gfp_mask |= __GFP_NOWARN; 175 - 176 set_current_in_page_owner(); 177 - stack = kmalloc(sizeof(*stack), gfp_mask); 178 if (!stack) { 179 unset_current_in_page_owner(); 180 return;
··· 168 unsigned long flags; 169 struct stack *stack; 170 171 set_current_in_page_owner(); 172 + stack = kmalloc(sizeof(*stack), gfp_nested_mask(gfp_mask)); 173 if (!stack) { 174 unset_current_in_page_owner(); 175 return;