Skip to content

Commit 451769e

Browse files
Michal Hockotorvalds
authored andcommitted
mm/vmalloc: alloc GFP_NO{FS,IO} for vmalloc
Patch series "extend vmalloc support for constrained allocations", v2. Based on a recent discussion with Dave and Neil [1] I have tried to implement NOFS, NOIO, NOFAIL support for the vmalloc to make life of kvmalloc users easier. A requirement for NOFAIL support for kvmalloc was new to me but this seems to be really needed by the xfs code. NOFS/NOIO was a known and a long term problem which was hoped to be handled by the scope API. Those scope should have been used at the reclaim recursion boundaries both to document them and also to remove the necessity of NOFS/NOIO constrains for all allocations within that scope. Instead workarounds were developed to wrap a single allocation instead (like ceph_kvmalloc). First patch implements NOFS/NOIO support for vmalloc. The second one adds NOFAIL support and the third one bundles all together into kvmalloc and drops ceph_kvmalloc which can use kvmalloc directly now. [1] http://lkml.kernel.org/r/163184741778.29351.16920832234899124642.stgit@noble.brown This patch (of 4): vmalloc historically hasn't supported GFP_NO{FS,IO} requests because page table allocations do not support externally provided gfp mask and performed GFP_KERNEL like allocations. Since few years we have scope (memalloc_no{fs,io}_{save,restore}) APIs to enforce NOFS and NOIO constrains implicitly to all allocators within the scope. There was a hope that those scopes would be defined on a higher level when the reclaim recursion boundary starts/stops (e.g. when a lock required during the memory reclaim is required etc.). It seems that not all NOFS/NOIO users have adopted this approach and instead they have taken a workaround approach to wrap a single [k]vmalloc allocation by a scope API. These workarounds do not serve the purpose of a better reclaim recursion documentation and reduction of explicit GFP_NO{FS,IO} usege so let's just provide them with the semantic they are asking for without a need for workarounds. Add support for GFP_NOFS and GFP_NOIO to vmalloc directly. All internal allocations already comply with the given gfp_mask. The only current exception is vmap_pages_range which maps kernel page tables. Infer the proper scope API based on the given gfp mask. [sfr@canb.auug.org.au: mm/vmalloc.c needs linux/sched/mm.h] Link: https://lkml.kernel.org/r/20211217232641.0148710c@canb.auug.org.au Link: https://lkml.kernel.org/r/20211122153233.9924-1-mhocko@kernel.org Link: https://lkml.kernel.org/r/20211122153233.9924-2-mhocko@kernel.org Signed-off-by: Michal Hocko <mhocko@suse.com> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Neil Brown <neilb@suse.de> Cc: Christoph Hellwig <hch@lst.de> Cc: Ilya Dryomov <idryomov@gmail.com> Cc: Jeff Layton <jlayton@kernel.org> Cc: Dave Chinner <dchinner@redhat.com> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cc6266f commit 451769e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

mm/vmalloc.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <linux/pgtable.h>
4040
#include <linux/uaccess.h>
4141
#include <linux/hugetlb.h>
42+
#include <linux/sched/mm.h>
4243
#include <asm/tlbflush.h>
4344
#include <asm/shmparam.h>
4445

@@ -2928,6 +2929,8 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
29282929
unsigned long array_size;
29292930
unsigned int nr_small_pages = size >> PAGE_SHIFT;
29302931
unsigned int page_order;
2932+
unsigned int flags;
2933+
int ret;
29312934

29322935
array_size = (unsigned long)nr_small_pages * sizeof(struct page *);
29332936
gfp_mask |= __GFP_NOWARN;
@@ -2976,8 +2979,24 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
29762979
goto fail;
29772980
}
29782981

2979-
if (vmap_pages_range(addr, addr + size, prot, area->pages,
2980-
page_shift) < 0) {
2982+
/*
2983+
* page tables allocations ignore external gfp mask, enforce it
2984+
* by the scope API
2985+
*/
2986+
if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
2987+
flags = memalloc_nofs_save();
2988+
else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
2989+
flags = memalloc_noio_save();
2990+
2991+
ret = vmap_pages_range(addr, addr + size, prot, area->pages,
2992+
page_shift);
2993+
2994+
if ((gfp_mask & (__GFP_FS | __GFP_IO)) == __GFP_IO)
2995+
memalloc_nofs_restore(flags);
2996+
else if ((gfp_mask & (__GFP_FS | __GFP_IO)) == 0)
2997+
memalloc_noio_restore(flags);
2998+
2999+
if (ret < 0) {
29813000
warn_alloc(orig_gfp_mask, NULL,
29823001
"vmalloc error: size %lu, failed to map pages",
29833002
area->nr_pages * PAGE_SIZE);

0 commit comments

Comments
 (0)