aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Hocko <mhocko@suse.com>2017-05-08 18:57:09 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-05-08 20:15:12 -0400
commita7c3e901a46ff54c016d040847eda598a9e3e653 (patch)
treed149d70d420ff19586daa827db47a2e26a5598fe
parent60f3e00d25b44e3aa51846590d1e10f408466a83 (diff)
mm: introduce kv[mz]alloc helpers
Patch series "kvmalloc", v5. There are many open coded kmalloc with vmalloc fallback instances in the tree. Most of them are not careful enough or simply do not care about the underlying semantic of the kmalloc/page allocator which means that a) some vmalloc fallbacks are basically unreachable because the kmalloc part will keep retrying until it succeeds b) the page allocator can invoke a really disruptive steps like the OOM killer to move forward which doesn't sound appropriate when we consider that the vmalloc fallback is available. As it can be seen implementing kvmalloc requires quite an intimate knowledge if the page allocator and the memory reclaim internals which strongly suggests that a helper should be implemented in the memory subsystem proper. Most callers, I could find, have been converted to use the helper instead. This is patch 6. There are some more relying on __GFP_REPEAT in the networking stack which I have converted as well and Eric Dumazet was not opposed [2] to convert them as well. [1] http://lkml.kernel.org/r/20170130094940.13546-1-mhocko@kernel.org [2] http://lkml.kernel.org/r/1485273626.16328.301.camel@edumazet-glaptop3.roam.corp.google.com This patch (of 9): Using kmalloc with the vmalloc fallback for larger allocations is a common pattern in the kernel code. Yet we do not have any common helper for that and so users have invented their own helpers. Some of them are really creative when doing so. Let's just add kv[mz]alloc and make sure it is implemented properly. This implementation makes sure to not make a large memory pressure for > PAGE_SZE requests (__GFP_NORETRY) and also to not warn about allocation failures. This also rules out the OOM killer as the vmalloc is a more approapriate fallback than a disruptive user visible action. This patch also changes some existing users and removes helpers which are specific for them. In some cases this is not possible (e.g. ext4_kvmalloc, libcfs_kvzalloc) because those seems to be broken and require GFP_NO{FS,IO} context which is not vmalloc compatible in general (note that the page table allocation is GFP_KERNEL). Those need to be fixed separately. While we are at it, document that __vmalloc{_node} about unsupported gfp mask because there seems to be a lot of confusion out there. kvmalloc_node will warn about GFP_KERNEL incompatible (which are not superset) flags to catch new abusers. Existing ones would have to die slowly. [sfr@canb.auug.org.au: f2fs fixup] Link: http://lkml.kernel.org/r/20170320163735.332e64b7@canb.auug.org.au Link: http://lkml.kernel.org/r/20170306103032.2540-2-mhocko@kernel.org Signed-off-by: Michal Hocko <mhocko@suse.com> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au> Reviewed-by: Andreas Dilger <adilger@dilger.ca> [ext4 part] Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: John Hubbard <jhubbard@nvidia.com> Cc: David Miller <davem@davemloft.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--arch/x86/kvm/lapic.c4
-rw-r--r--arch/x86/kvm/page_track.c4
-rw-r--r--arch/x86/kvm/x86.c4
-rw-r--r--drivers/md/dm-stats.c7
-rw-r--r--fs/ext4/mballoc.c2
-rw-r--r--fs/ext4/super.c4
-rw-r--r--fs/f2fs/f2fs.h20
-rw-r--r--fs/f2fs/file.c4
-rw-r--r--fs/f2fs/node.c6
-rw-r--r--fs/f2fs/segment.c14
-rw-r--r--fs/seq_file.c16
-rw-r--r--include/linux/kvm_host.h2
-rw-r--r--include/linux/mm.h14
-rw-r--r--include/linux/vmalloc.h1
-rw-r--r--ipc/util.c7
-rw-r--r--mm/nommu.c5
-rw-r--r--mm/util.c45
-rw-r--r--mm/vmalloc.c9
-rw-r--r--security/apparmor/apparmorfs.c2
-rw-r--r--security/apparmor/include/lib.h11
-rw-r--r--security/apparmor/lib.c30
-rw-r--r--security/apparmor/match.c2
-rw-r--r--security/apparmor/policy_unpack.c2
-rw-r--r--virt/kvm/kvm_main.c18
24 files changed, 103 insertions, 130 deletions
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index bad6a25067bc..d2a892fc92bf 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -177,8 +177,8 @@ static void recalculate_apic_map(struct kvm *kvm)
177 if (kvm_apic_present(vcpu)) 177 if (kvm_apic_present(vcpu))
178 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic)); 178 max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
179 179
180 new = kvm_kvzalloc(sizeof(struct kvm_apic_map) + 180 new = kvzalloc(sizeof(struct kvm_apic_map) +
181 sizeof(struct kvm_lapic *) * ((u64)max_id + 1)); 181 sizeof(struct kvm_lapic *) * ((u64)max_id + 1), GFP_KERNEL);
182 182
183 if (!new) 183 if (!new)
184 goto out; 184 goto out;
diff --git a/arch/x86/kvm/page_track.c b/arch/x86/kvm/page_track.c
index 60168cdd0546..ea67dc876316 100644
--- a/arch/x86/kvm/page_track.c
+++ b/arch/x86/kvm/page_track.c
@@ -40,8 +40,8 @@ int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
40 int i; 40 int i;
41 41
42 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) { 42 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
43 slot->arch.gfn_track[i] = kvm_kvzalloc(npages * 43 slot->arch.gfn_track[i] = kvzalloc(npages *
44 sizeof(*slot->arch.gfn_track[i])); 44 sizeof(*slot->arch.gfn_track[i]), GFP_KERNEL);
45 if (!slot->arch.gfn_track[i]) 45 if (!slot->arch.gfn_track[i])
46 goto track_free; 46 goto track_free;
47 } 47 }
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ccbd45ecd41a..ee22226e3807 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8199,13 +8199,13 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
8199 slot->base_gfn, level) + 1; 8199 slot->base_gfn, level) + 1;
8200 8200
8201 slot->arch.rmap[i] = 8201 slot->arch.rmap[i] =
8202 kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i])); 8202 kvzalloc(lpages * sizeof(*slot->arch.rmap[i]), GFP_KERNEL);
8203 if (!slot->arch.rmap[i]) 8203 if (!slot->arch.rmap[i])
8204 goto out_free; 8204 goto out_free;
8205 if (i == 0) 8205 if (i == 0)
8206 continue; 8206 continue;
8207 8207
8208 linfo = kvm_kvzalloc(lpages * sizeof(*linfo)); 8208 linfo = kvzalloc(lpages * sizeof(*linfo), GFP_KERNEL);
8209 if (!linfo) 8209 if (!linfo)
8210 goto out_free; 8210 goto out_free;
8211 8211
diff --git a/drivers/md/dm-stats.c b/drivers/md/dm-stats.c
index 0250e7e521ab..6028d8247f58 100644
--- a/drivers/md/dm-stats.c
+++ b/drivers/md/dm-stats.c
@@ -146,12 +146,7 @@ static void *dm_kvzalloc(size_t alloc_size, int node)
146 if (!claim_shared_memory(alloc_size)) 146 if (!claim_shared_memory(alloc_size))
147 return NULL; 147 return NULL;
148 148
149 if (alloc_size <= KMALLOC_MAX_SIZE) { 149 p = kvzalloc_node(alloc_size, GFP_KERNEL | __GFP_NOMEMALLOC, node);
150 p = kzalloc_node(alloc_size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN, node);
151 if (p)
152 return p;
153 }
154 p = vzalloc_node(alloc_size, node);
155 if (p) 150 if (p)
156 return p; 151 return p;
157 152
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 354dc1a894c2..b60698c104fd 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -2393,7 +2393,7 @@ int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2393 return 0; 2393 return 0;
2394 2394
2395 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size); 2395 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2396 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL); 2396 new_groupinfo = kvzalloc(size, GFP_KERNEL);
2397 if (!new_groupinfo) { 2397 if (!new_groupinfo) {
2398 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group"); 2398 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2399 return -ENOMEM; 2399 return -ENOMEM;
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index a9c72e39a4ee..b2c74644d5de 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -2153,7 +2153,7 @@ int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2153 return 0; 2153 return 0;
2154 2154
2155 size = roundup_pow_of_two(size * sizeof(struct flex_groups)); 2155 size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2156 new_groups = ext4_kvzalloc(size, GFP_KERNEL); 2156 new_groups = kvzalloc(size, GFP_KERNEL);
2157 if (!new_groups) { 2157 if (!new_groups) {
2158 ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups", 2158 ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2159 size / (int) sizeof(struct flex_groups)); 2159 size / (int) sizeof(struct flex_groups));
@@ -3887,7 +3887,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3887 goto failed_mount; 3887 goto failed_mount;
3888 } 3888 }
3889 } 3889 }
3890 sbi->s_group_desc = ext4_kvmalloc(db_count * 3890 sbi->s_group_desc = kvmalloc(db_count *
3891 sizeof(struct buffer_head *), 3891 sizeof(struct buffer_head *),
3892 GFP_KERNEL); 3892 GFP_KERNEL);
3893 if (sbi->s_group_desc == NULL) { 3893 if (sbi->s_group_desc == NULL) {
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 0a6e115562f6..1fc17a1fc5d0 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2005,26 +2005,6 @@ static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2005 return kmalloc(size, flags); 2005 return kmalloc(size, flags);
2006} 2006}
2007 2007
2008static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
2009{
2010 void *ret;
2011
2012 ret = kmalloc(size, flags | __GFP_NOWARN);
2013 if (!ret)
2014 ret = __vmalloc(size, flags, PAGE_KERNEL);
2015 return ret;
2016}
2017
2018static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
2019{
2020 void *ret;
2021
2022 ret = kzalloc(size, flags | __GFP_NOWARN);
2023 if (!ret)
2024 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2025 return ret;
2026}
2027
2028#define get_inode_mode(i) \ 2008#define get_inode_mode(i) \
2029 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \ 2009 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2030 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode)) 2010 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 5f7317875a67..0849af78381f 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1012,11 +1012,11 @@ static int __exchange_data_block(struct inode *src_inode,
1012 while (len) { 1012 while (len) {
1013 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len); 1013 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1014 1014
1015 src_blkaddr = f2fs_kvzalloc(sizeof(block_t) * olen, GFP_KERNEL); 1015 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
1016 if (!src_blkaddr) 1016 if (!src_blkaddr)
1017 return -ENOMEM; 1017 return -ENOMEM;
1018 1018
1019 do_replace = f2fs_kvzalloc(sizeof(int) * olen, GFP_KERNEL); 1019 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL);
1020 if (!do_replace) { 1020 if (!do_replace) {
1021 kvfree(src_blkaddr); 1021 kvfree(src_blkaddr);
1022 return -ENOMEM; 1022 return -ENOMEM;
diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c
index 481aa8dc79f4..0ea1dca8a0e2 100644
--- a/fs/f2fs/node.c
+++ b/fs/f2fs/node.c
@@ -2621,17 +2621,17 @@ static int init_free_nid_cache(struct f2fs_sb_info *sbi)
2621{ 2621{
2622 struct f2fs_nm_info *nm_i = NM_I(sbi); 2622 struct f2fs_nm_info *nm_i = NM_I(sbi);
2623 2623
2624 nm_i->free_nid_bitmap = f2fs_kvzalloc(nm_i->nat_blocks * 2624 nm_i->free_nid_bitmap = kvzalloc(nm_i->nat_blocks *
2625 NAT_ENTRY_BITMAP_SIZE, GFP_KERNEL); 2625 NAT_ENTRY_BITMAP_SIZE, GFP_KERNEL);
2626 if (!nm_i->free_nid_bitmap) 2626 if (!nm_i->free_nid_bitmap)
2627 return -ENOMEM; 2627 return -ENOMEM;
2628 2628
2629 nm_i->nat_block_bitmap = f2fs_kvzalloc(nm_i->nat_blocks / 8, 2629 nm_i->nat_block_bitmap = kvzalloc(nm_i->nat_blocks / 8,
2630 GFP_KERNEL); 2630 GFP_KERNEL);
2631 if (!nm_i->nat_block_bitmap) 2631 if (!nm_i->nat_block_bitmap)
2632 return -ENOMEM; 2632 return -ENOMEM;
2633 2633
2634 nm_i->free_nid_count = f2fs_kvzalloc(nm_i->nat_blocks * 2634 nm_i->free_nid_count = kvzalloc(nm_i->nat_blocks *
2635 sizeof(unsigned short), GFP_KERNEL); 2635 sizeof(unsigned short), GFP_KERNEL);
2636 if (!nm_i->free_nid_count) 2636 if (!nm_i->free_nid_count)
2637 return -ENOMEM; 2637 return -ENOMEM;
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 29ef7088c558..13806f642ab5 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -2501,13 +2501,13 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
2501 2501
2502 SM_I(sbi)->sit_info = sit_i; 2502 SM_I(sbi)->sit_info = sit_i;
2503 2503
2504 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) * 2504 sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) *
2505 sizeof(struct seg_entry), GFP_KERNEL); 2505 sizeof(struct seg_entry), GFP_KERNEL);
2506 if (!sit_i->sentries) 2506 if (!sit_i->sentries)
2507 return -ENOMEM; 2507 return -ENOMEM;
2508 2508
2509 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2509 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2510 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); 2510 sit_i->dirty_sentries_bitmap = kvzalloc(bitmap_size, GFP_KERNEL);
2511 if (!sit_i->dirty_sentries_bitmap) 2511 if (!sit_i->dirty_sentries_bitmap)
2512 return -ENOMEM; 2512 return -ENOMEM;
2513 2513
@@ -2540,7 +2540,7 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
2540 return -ENOMEM; 2540 return -ENOMEM;
2541 2541
2542 if (sbi->segs_per_sec > 1) { 2542 if (sbi->segs_per_sec > 1) {
2543 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) * 2543 sit_i->sec_entries = kvzalloc(MAIN_SECS(sbi) *
2544 sizeof(struct sec_entry), GFP_KERNEL); 2544 sizeof(struct sec_entry), GFP_KERNEL);
2545 if (!sit_i->sec_entries) 2545 if (!sit_i->sec_entries)
2546 return -ENOMEM; 2546 return -ENOMEM;
@@ -2591,12 +2591,12 @@ static int build_free_segmap(struct f2fs_sb_info *sbi)
2591 SM_I(sbi)->free_info = free_i; 2591 SM_I(sbi)->free_info = free_i;
2592 2592
2593 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2593 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2594 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL); 2594 free_i->free_segmap = kvmalloc(bitmap_size, GFP_KERNEL);
2595 if (!free_i->free_segmap) 2595 if (!free_i->free_segmap)
2596 return -ENOMEM; 2596 return -ENOMEM;
2597 2597
2598 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 2598 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2599 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL); 2599 free_i->free_secmap = kvmalloc(sec_bitmap_size, GFP_KERNEL);
2600 if (!free_i->free_secmap) 2600 if (!free_i->free_secmap)
2601 return -ENOMEM; 2601 return -ENOMEM;
2602 2602
@@ -2764,7 +2764,7 @@ static int init_victim_secmap(struct f2fs_sb_info *sbi)
2764 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2764 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2765 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 2765 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2766 2766
2767 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); 2767 dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL);
2768 if (!dirty_i->victim_secmap) 2768 if (!dirty_i->victim_secmap)
2769 return -ENOMEM; 2769 return -ENOMEM;
2770 return 0; 2770 return 0;
@@ -2786,7 +2786,7 @@ static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2786 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2786 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2787 2787
2788 for (i = 0; i < NR_DIRTY_TYPE; i++) { 2788 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2789 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); 2789 dirty_i->dirty_segmap[i] = kvzalloc(bitmap_size, GFP_KERNEL);
2790 if (!dirty_i->dirty_segmap[i]) 2790 if (!dirty_i->dirty_segmap[i])
2791 return -ENOMEM; 2791 return -ENOMEM;
2792 } 2792 }
diff --git a/fs/seq_file.c b/fs/seq_file.c
index ca69fb99e41a..dc7c2be963ed 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -25,21 +25,7 @@ static void seq_set_overflow(struct seq_file *m)
25 25
26static void *seq_buf_alloc(unsigned long size) 26static void *seq_buf_alloc(unsigned long size)
27{ 27{
28 void *buf; 28 return kvmalloc(size, GFP_KERNEL);
29 gfp_t gfp = GFP_KERNEL;
30
31 /*
32 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
33 * it's better to fall back to vmalloc() than to kill things. For small
34 * allocations, just use GFP_KERNEL which will oom kill, thus no need
35 * for vmalloc fallback.
36 */
37 if (size > PAGE_SIZE)
38 gfp |= __GFP_NORETRY | __GFP_NOWARN;
39 buf = kmalloc(size, gfp);
40 if (!buf && size > PAGE_SIZE)
41 buf = vmalloc(size);
42 return buf;
43} 29}
44 30
45/** 31/**
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index d0250744507a..5d9b2a08e553 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -767,8 +767,6 @@ void kvm_arch_check_processor_compat(void *rtn);
767int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); 767int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
768int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); 768int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
769 769
770void *kvm_kvzalloc(unsigned long size);
771
772#ifndef __KVM_HAVE_ARCH_VM_ALLOC 770#ifndef __KVM_HAVE_ARCH_VM_ALLOC
773static inline struct kvm *kvm_arch_alloc_vm(void) 771static inline struct kvm *kvm_arch_alloc_vm(void)
774{ 772{
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 5d22e69f51ea..08e2849d27ca 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -518,6 +518,20 @@ static inline int is_vmalloc_or_module_addr(const void *x)
518} 518}
519#endif 519#endif
520 520
521extern void *kvmalloc_node(size_t size, gfp_t flags, int node);
522static inline void *kvmalloc(size_t size, gfp_t flags)
523{
524 return kvmalloc_node(size, flags, NUMA_NO_NODE);
525}
526static inline void *kvzalloc_node(size_t size, gfp_t flags, int node)
527{
528 return kvmalloc_node(size, flags | __GFP_ZERO, node);
529}
530static inline void *kvzalloc(size_t size, gfp_t flags)
531{
532 return kvmalloc(size, flags | __GFP_ZERO);
533}
534
521extern void kvfree(const void *addr); 535extern void kvfree(const void *addr);
522 536
523static inline atomic_t *compound_mapcount_ptr(struct page *page) 537static inline atomic_t *compound_mapcount_ptr(struct page *page)
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index d68edffbf142..46991ad3ddd5 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -80,6 +80,7 @@ extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
80 unsigned long start, unsigned long end, gfp_t gfp_mask, 80 unsigned long start, unsigned long end, gfp_t gfp_mask,
81 pgprot_t prot, unsigned long vm_flags, int node, 81 pgprot_t prot, unsigned long vm_flags, int node,
82 const void *caller); 82 const void *caller);
83extern void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags);
83 84
84extern void vfree(const void *addr); 85extern void vfree(const void *addr);
85extern void vfree_atomic(const void *addr); 86extern void vfree_atomic(const void *addr);
diff --git a/ipc/util.c b/ipc/util.c
index 3459a16a9df9..caec7b1bfaa3 100644
--- a/ipc/util.c
+++ b/ipc/util.c
@@ -403,12 +403,7 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
403 */ 403 */
404void *ipc_alloc(int size) 404void *ipc_alloc(int size)
405{ 405{
406 void *out; 406 return kvmalloc(size, GFP_KERNEL);
407 if (size > PAGE_SIZE)
408 out = vmalloc(size);
409 else
410 out = kmalloc(size, GFP_KERNEL);
411 return out;
412} 407}
413 408
414/** 409/**
diff --git a/mm/nommu.c b/mm/nommu.c
index 2d131b97a851..a80411d258fc 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -237,6 +237,11 @@ void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
237} 237}
238EXPORT_SYMBOL(__vmalloc); 238EXPORT_SYMBOL(__vmalloc);
239 239
240void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
241{
242 return __vmalloc(size, flags, PAGE_KERNEL);
243}
244
240void *vmalloc_user(unsigned long size) 245void *vmalloc_user(unsigned long size)
241{ 246{
242 void *ret; 247 void *ret;
diff --git a/mm/util.c b/mm/util.c
index 656dc5e37a87..10a14a0ac3c2 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -329,6 +329,51 @@ unsigned long vm_mmap(struct file *file, unsigned long addr,
329} 329}
330EXPORT_SYMBOL(vm_mmap); 330EXPORT_SYMBOL(vm_mmap);
331 331
332/**
333 * kvmalloc_node - attempt to allocate physically contiguous memory, but upon
334 * failure, fall back to non-contiguous (vmalloc) allocation.
335 * @size: size of the request.
336 * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.
337 * @node: numa node to allocate from
338 *
339 * Uses kmalloc to get the memory but if the allocation fails then falls back
340 * to the vmalloc allocator. Use kvfree for freeing the memory.
341 *
342 * Reclaim modifiers - __GFP_NORETRY, __GFP_REPEAT and __GFP_NOFAIL are not supported
343 *
344 * Any use of gfp flags outside of GFP_KERNEL should be consulted with mm people.
345 */
346void *kvmalloc_node(size_t size, gfp_t flags, int node)
347{
348 gfp_t kmalloc_flags = flags;
349 void *ret;
350
351 /*
352 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
353 * so the given set of flags has to be compatible.
354 */
355 WARN_ON_ONCE((flags & GFP_KERNEL) != GFP_KERNEL);
356
357 /*
358 * Make sure that larger requests are not too disruptive - no OOM
359 * killer and no allocation failure warnings as we have a fallback
360 */
361 if (size > PAGE_SIZE)
362 kmalloc_flags |= __GFP_NORETRY | __GFP_NOWARN;
363
364 ret = kmalloc_node(size, kmalloc_flags, node);
365
366 /*
367 * It doesn't really make sense to fallback to vmalloc for sub page
368 * requests
369 */
370 if (ret || size <= PAGE_SIZE)
371 return ret;
372
373 return __vmalloc_node_flags(size, node, flags | __GFP_HIGHMEM);
374}
375EXPORT_SYMBOL(kvmalloc_node);
376
332void kvfree(const void *addr) 377void kvfree(const void *addr)
333{ 378{
334 if (is_vmalloc_addr(addr)) 379 if (is_vmalloc_addr(addr))
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index b52aeed3f58e..33603239560e 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1786,6 +1786,13 @@ fail:
1786 * Allocate enough pages to cover @size from the page level 1786 * Allocate enough pages to cover @size from the page level
1787 * allocator with @gfp_mask flags. Map them into contiguous 1787 * allocator with @gfp_mask flags. Map them into contiguous
1788 * kernel virtual space, using a pagetable protection of @prot. 1788 * kernel virtual space, using a pagetable protection of @prot.
1789 *
1790 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_REPEAT
1791 * and __GFP_NOFAIL are not supported
1792 *
1793 * Any use of gfp flags outside of GFP_KERNEL should be consulted
1794 * with mm people.
1795 *
1789 */ 1796 */
1790static void *__vmalloc_node(unsigned long size, unsigned long align, 1797static void *__vmalloc_node(unsigned long size, unsigned long align,
1791 gfp_t gfp_mask, pgprot_t prot, 1798 gfp_t gfp_mask, pgprot_t prot,
@@ -1802,7 +1809,7 @@ void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1802} 1809}
1803EXPORT_SYMBOL(__vmalloc); 1810EXPORT_SYMBOL(__vmalloc);
1804 1811
1805static inline void *__vmalloc_node_flags(unsigned long size, 1812void *__vmalloc_node_flags(unsigned long size,
1806 int node, gfp_t flags) 1813 int node, gfp_t flags)
1807{ 1814{
1808 return __vmalloc_node(size, 1, flags, PAGE_KERNEL, 1815 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
index 41073f70eb41..be0b49897a67 100644
--- a/security/apparmor/apparmorfs.c
+++ b/security/apparmor/apparmorfs.c
@@ -98,7 +98,7 @@ static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
98 return ERR_PTR(-ESPIPE); 98 return ERR_PTR(-ESPIPE);
99 99
100 /* freed by caller to simple_write_to_buffer */ 100 /* freed by caller to simple_write_to_buffer */
101 data = kvmalloc(sizeof(*data) + alloc_size); 101 data = kvmalloc(sizeof(*data) + alloc_size, GFP_KERNEL);
102 if (data == NULL) 102 if (data == NULL)
103 return ERR_PTR(-ENOMEM); 103 return ERR_PTR(-ENOMEM);
104 kref_init(&data->count); 104 kref_init(&data->count);
diff --git a/security/apparmor/include/lib.h b/security/apparmor/include/lib.h
index 0291ff3902f9..550a700563b4 100644
--- a/security/apparmor/include/lib.h
+++ b/security/apparmor/include/lib.h
@@ -64,17 +64,6 @@ char *aa_split_fqname(char *args, char **ns_name);
64const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, 64const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
65 size_t *ns_len); 65 size_t *ns_len);
66void aa_info_message(const char *str); 66void aa_info_message(const char *str);
67void *__aa_kvmalloc(size_t size, gfp_t flags);
68
69static inline void *kvmalloc(size_t size)
70{
71 return __aa_kvmalloc(size, 0);
72}
73
74static inline void *kvzalloc(size_t size)
75{
76 return __aa_kvmalloc(size, __GFP_ZERO);
77}
78 67
79/** 68/**
80 * aa_strneq - compare null terminated @str to a non null terminated substring 69 * aa_strneq - compare null terminated @str to a non null terminated substring
diff --git a/security/apparmor/lib.c b/security/apparmor/lib.c
index 32cafc12593e..7cd788a9445b 100644
--- a/security/apparmor/lib.c
+++ b/security/apparmor/lib.c
@@ -129,36 +129,6 @@ void aa_info_message(const char *str)
129} 129}
130 130
131/** 131/**
132 * __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
133 * @size: how many bytes of memory are required
134 * @flags: the type of memory to allocate (see kmalloc).
135 *
136 * Return: allocated buffer or NULL if failed
137 *
138 * It is possible that policy being loaded from the user is larger than
139 * what can be allocated by kmalloc, in those cases fall back to vmalloc.
140 */
141void *__aa_kvmalloc(size_t size, gfp_t flags)
142{
143 void *buffer = NULL;
144
145 if (size == 0)
146 return NULL;
147
148 /* do not attempt kmalloc if we need more than 16 pages at once */
149 if (size <= (16*PAGE_SIZE))
150 buffer = kmalloc(size, flags | GFP_KERNEL | __GFP_NORETRY |
151 __GFP_NOWARN);
152 if (!buffer) {
153 if (flags & __GFP_ZERO)
154 buffer = vzalloc(size);
155 else
156 buffer = vmalloc(size);
157 }
158 return buffer;
159}
160
161/**
162 * aa_policy_init - initialize a policy structure 132 * aa_policy_init - initialize a policy structure
163 * @policy: policy to initialize (NOT NULL) 133 * @policy: policy to initialize (NOT NULL)
164 * @prefix: prefix name if any is required. (MAYBE NULL) 134 * @prefix: prefix name if any is required. (MAYBE NULL)
diff --git a/security/apparmor/match.c b/security/apparmor/match.c
index eb0efef746f5..960c913381e2 100644
--- a/security/apparmor/match.c
+++ b/security/apparmor/match.c
@@ -88,7 +88,7 @@ static struct table_header *unpack_table(char *blob, size_t bsize)
88 if (bsize < tsize) 88 if (bsize < tsize)
89 goto out; 89 goto out;
90 90
91 table = kvzalloc(tsize); 91 table = kvzalloc(tsize, GFP_KERNEL);
92 if (table) { 92 if (table) {
93 table->td_id = th.td_id; 93 table->td_id = th.td_id;
94 table->td_flags = th.td_flags; 94 table->td_flags = th.td_flags;
diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c
index 2e37c9c26bbd..f3422a91353c 100644
--- a/security/apparmor/policy_unpack.c
+++ b/security/apparmor/policy_unpack.c
@@ -487,7 +487,7 @@ fail:
487 487
488static void *kvmemdup(const void *src, size_t len) 488static void *kvmemdup(const void *src, size_t len)
489{ 489{
490 void *p = kvmalloc(len); 490 void *p = kvmalloc(len, GFP_KERNEL);
491 491
492 if (p) 492 if (p)
493 memcpy(p, src, len); 493 memcpy(p, src, len);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 88257b311cb5..aca22d36be9c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -504,7 +504,7 @@ static struct kvm_memslots *kvm_alloc_memslots(void)
504 int i; 504 int i;
505 struct kvm_memslots *slots; 505 struct kvm_memslots *slots;
506 506
507 slots = kvm_kvzalloc(sizeof(struct kvm_memslots)); 507 slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
508 if (!slots) 508 if (!slots)
509 return NULL; 509 return NULL;
510 510
@@ -689,18 +689,6 @@ out_err_no_disable:
689 return ERR_PTR(r); 689 return ERR_PTR(r);
690} 690}
691 691
692/*
693 * Avoid using vmalloc for a small buffer.
694 * Should not be used when the size is statically known.
695 */
696void *kvm_kvzalloc(unsigned long size)
697{
698 if (size > PAGE_SIZE)
699 return vzalloc(size);
700 else
701 return kzalloc(size, GFP_KERNEL);
702}
703
704static void kvm_destroy_devices(struct kvm *kvm) 692static void kvm_destroy_devices(struct kvm *kvm)
705{ 693{
706 struct kvm_device *dev, *tmp; 694 struct kvm_device *dev, *tmp;
@@ -782,7 +770,7 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
782{ 770{
783 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); 771 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
784 772
785 memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes); 773 memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL);
786 if (!memslot->dirty_bitmap) 774 if (!memslot->dirty_bitmap)
787 return -ENOMEM; 775 return -ENOMEM;
788 776
@@ -1008,7 +996,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
1008 goto out_free; 996 goto out_free;
1009 } 997 }
1010 998
1011 slots = kvm_kvzalloc(sizeof(struct kvm_memslots)); 999 slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
1012 if (!slots) 1000 if (!slots)
1013 goto out_free; 1001 goto out_free;
1014 memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots)); 1002 memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots));