Skip to content

Commit 752ade6

Browse files
Michal Hockotorvalds
authored andcommitted
treewide: use kv[mz]alloc* rather than opencoded variants
There are many code paths opencoding kvmalloc. Let's use the helper instead. The main difference to kvmalloc is that those users are usually not considering all the aspects of the memory allocator. E.g. allocation requests <= 32kB (with 4kB pages) are basically never failing and invoke OOM killer to satisfy the allocation. This sounds too disruptive for something that has a reasonable fallback - the vmalloc. On the other hand those requests might fallback to vmalloc even when the memory allocator would succeed after several more reclaim/compaction attempts previously. There is no guarantee something like that happens though. This patch converts many of those places to kv[mz]alloc* helpers because they are more conservative. Link: http://lkml.kernel.org/r/20170306103327.2766-2-mhocko@kernel.org Signed-off-by: Michal Hocko <mhocko@suse.com> Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com> # Xen bits Acked-by: Kees Cook <keescook@chromium.org> Acked-by: Vlastimil Babka <vbabka@suse.cz> Acked-by: Andreas Dilger <andreas.dilger@intel.com> # Lustre Acked-by: Christian Borntraeger <borntraeger@de.ibm.com> # KVM/s390 Acked-by: Dan Williams <dan.j.williams@intel.com> # nvdim Acked-by: David Sterba <dsterba@suse.com> # btrfs Acked-by: Ilya Dryomov <idryomov@gmail.com> # Ceph Acked-by: Tariq Toukan <tariqt@mellanox.com> # mlx4 Acked-by: Leon Romanovsky <leonro@mellanox.com> # mlx5 Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: Anton Vorontsov <anton@enomsg.org> Cc: Colin Cross <ccross@android.com> Cc: Tony Luck <tony.luck@intel.com> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net> Cc: Ben Skeggs <bskeggs@redhat.com> Cc: Kent Overstreet <kent.overstreet@gmail.com> Cc: Santosh Raspatur <santosh@chelsio.com> Cc: Hariprasad S <hariprasad@chelsio.com> Cc: Yishai Hadas <yishaih@mellanox.com> Cc: Oleg Drokin <oleg.drokin@intel.com> Cc: "Yan, Zheng" <zyan@redhat.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Eric Dumazet <eric.dumazet@gmail.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>
1 parent 81be3de commit 752ade6

File tree

44 files changed

+128
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+128
-350
lines changed

arch/s390/kvm/kvm-s390.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,7 @@ static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
11661166
if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
11671167
return -EINVAL;
11681168

1169-
keys = kmalloc_array(args->count, sizeof(uint8_t),
1170-
GFP_KERNEL | __GFP_NOWARN);
1171-
if (!keys)
1172-
keys = vmalloc(sizeof(uint8_t) * args->count);
1169+
keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
11731170
if (!keys)
11741171
return -ENOMEM;
11751172

@@ -1211,10 +1208,7 @@ static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
12111208
if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
12121209
return -EINVAL;
12131210

1214-
keys = kmalloc_array(args->count, sizeof(uint8_t),
1215-
GFP_KERNEL | __GFP_NOWARN);
1216-
if (!keys)
1217-
keys = vmalloc(sizeof(uint8_t) * args->count);
1211+
keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
12181212
if (!keys)
12191213
return -ENOMEM;
12201214

crypto/lzo.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ static void *lzo_alloc_ctx(struct crypto_scomp *tfm)
3232
{
3333
void *ctx;
3434

35-
ctx = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN);
36-
if (!ctx)
37-
ctx = vmalloc(LZO1X_MEM_COMPRESS);
35+
ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
3836
if (!ctx)
3937
return ERR_PTR(-ENOMEM);
4038

drivers/acpi/apei/erst.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ static int __erst_record_id_cache_add_one(void)
513513
if (i < erst_record_id_cache.len)
514514
goto retry;
515515
if (erst_record_id_cache.len >= erst_record_id_cache.size) {
516-
int new_size, alloc_size;
516+
int new_size;
517517
u64 *new_entries;
518518

519519
new_size = erst_record_id_cache.size * 2;
@@ -524,11 +524,7 @@ static int __erst_record_id_cache_add_one(void)
524524
pr_warn(FW_WARN "too many record IDs!\n");
525525
return 0;
526526
}
527-
alloc_size = new_size * sizeof(entries[0]);
528-
if (alloc_size < PAGE_SIZE)
529-
new_entries = kmalloc(alloc_size, GFP_KERNEL);
530-
else
531-
new_entries = vmalloc(alloc_size);
527+
new_entries = kvmalloc(new_size * sizeof(entries[0]), GFP_KERNEL);
532528
if (!new_entries)
533529
return -ENOMEM;
534530
memcpy(new_entries, entries,

drivers/char/agp/generic.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ static int agp_get_key(void)
8888

8989
void agp_alloc_page_array(size_t size, struct agp_memory *mem)
9090
{
91-
mem->pages = NULL;
92-
93-
if (size <= 2*PAGE_SIZE)
94-
mem->pages = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
95-
if (mem->pages == NULL) {
96-
mem->pages = vmalloc(size);
97-
}
91+
mem->pages = kvmalloc(size, GFP_KERNEL);
9892
}
9993
EXPORT_SYMBOL(agp_alloc_page_array);
10094

drivers/gpu/drm/nouveau/nouveau_gem.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,7 @@ u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
568568

569569
size *= nmemb;
570570

571-
mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
572-
if (!mem)
573-
mem = vmalloc(size);
571+
mem = kvmalloc(size, GFP_KERNEL);
574572
if (!mem)
575573
return ERR_PTR(-ENOMEM);
576574

drivers/md/bcache/util.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ struct closure;
4343
(heap)->used = 0; \
4444
(heap)->size = (_size); \
4545
_bytes = (heap)->size * sizeof(*(heap)->data); \
46-
(heap)->data = NULL; \
47-
if (_bytes < KMALLOC_MAX_SIZE) \
48-
(heap)->data = kmalloc(_bytes, (gfp)); \
49-
if ((!(heap)->data) && ((gfp) & GFP_KERNEL)) \
50-
(heap)->data = vmalloc(_bytes); \
46+
(heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
5147
(heap)->data; \
5248
})
5349

@@ -136,12 +132,8 @@ do { \
136132
\
137133
(fifo)->mask = _allocated_size - 1; \
138134
(fifo)->front = (fifo)->back = 0; \
139-
(fifo)->data = NULL; \
140135
\
141-
if (_bytes < KMALLOC_MAX_SIZE) \
142-
(fifo)->data = kmalloc(_bytes, (gfp)); \
143-
if ((!(fifo)->data) && ((gfp) & GFP_KERNEL)) \
144-
(fifo)->data = vmalloc(_bytes); \
136+
(fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
145137
(fifo)->data; \
146138
})
147139

drivers/net/ethernet/chelsio/cxgb3/cxgb3_defs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141

4242
#define VALIDATE_TID 1
4343

44-
void *cxgb_alloc_mem(unsigned long size);
45-
void cxgb_free_mem(void *addr);
46-
4744
/*
4845
* Map an ATID or STID to their entries in the corresponding TID tables.
4946
*/

drivers/net/ethernet/chelsio/cxgb3/cxgb3_offload.c

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,27 +1151,6 @@ static void cxgb_redirect(struct dst_entry *old, struct dst_entry *new,
11511151
l2t_release(tdev, e);
11521152
}
11531153

1154-
/*
1155-
* Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1156-
* The allocated memory is cleared.
1157-
*/
1158-
void *cxgb_alloc_mem(unsigned long size)
1159-
{
1160-
void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1161-
1162-
if (!p)
1163-
p = vzalloc(size);
1164-
return p;
1165-
}
1166-
1167-
/*
1168-
* Free memory allocated through t3_alloc_mem().
1169-
*/
1170-
void cxgb_free_mem(void *addr)
1171-
{
1172-
kvfree(addr);
1173-
}
1174-
11751154
/*
11761155
* Allocate and initialize the TID tables. Returns 0 on success.
11771156
*/
@@ -1182,7 +1161,7 @@ static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
11821161
unsigned long size = ntids * sizeof(*t->tid_tab) +
11831162
natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
11841163

1185-
t->tid_tab = cxgb_alloc_mem(size);
1164+
t->tid_tab = kvzalloc(size, GFP_KERNEL);
11861165
if (!t->tid_tab)
11871166
return -ENOMEM;
11881167

@@ -1218,7 +1197,7 @@ static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
12181197

12191198
static void free_tid_maps(struct tid_info *t)
12201199
{
1221-
cxgb_free_mem(t->tid_tab);
1200+
kvfree(t->tid_tab);
12221201
}
12231202

12241203
static inline void add_adapter(struct adapter *adap)
@@ -1293,7 +1272,7 @@ int cxgb3_offload_activate(struct adapter *adapter)
12931272
return 0;
12941273

12951274
out_free_l2t:
1296-
t3_free_l2t(l2td);
1275+
kvfree(l2td);
12971276
out_free:
12981277
kfree(t);
12991278
return err;
@@ -1302,7 +1281,7 @@ int cxgb3_offload_activate(struct adapter *adapter)
13021281
static void clean_l2_data(struct rcu_head *head)
13031282
{
13041283
struct l2t_data *d = container_of(head, struct l2t_data, rcu_head);
1305-
t3_free_l2t(d);
1284+
kvfree(d);
13061285
}
13071286

13081287

drivers/net/ethernet/chelsio/cxgb3/l2t.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
444444
struct l2t_data *d;
445445
int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
446446

447-
d = cxgb_alloc_mem(size);
447+
d = kvzalloc(size, GFP_KERNEL);
448448
if (!d)
449449
return NULL;
450450

@@ -462,9 +462,3 @@ struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
462462
}
463463
return d;
464464
}
465-
466-
void t3_free_l2t(struct l2t_data *d)
467-
{
468-
cxgb_free_mem(d);
469-
}
470-

drivers/net/ethernet/chelsio/cxgb3/l2t.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
115115
struct l2t_entry *e);
116116
void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e);
117117
struct l2t_data *t3_init_l2t(unsigned int l2t_capacity);
118-
void t3_free_l2t(struct l2t_data *d);
119118

120119
int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb);
121120

drivers/net/ethernet/chelsio/cxgb4/clip_tbl.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
290290
if (clipt_size < CLIPT_MIN_HASH_BUCKETS)
291291
return NULL;
292292

293-
ctbl = t4_alloc_mem(sizeof(*ctbl) +
294-
clipt_size*sizeof(struct list_head));
293+
ctbl = kvzalloc(sizeof(*ctbl) +
294+
clipt_size*sizeof(struct list_head), GFP_KERNEL);
295295
if (!ctbl)
296296
return NULL;
297297

@@ -305,9 +305,9 @@ struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
305305
for (i = 0; i < ctbl->clipt_size; ++i)
306306
INIT_LIST_HEAD(&ctbl->hash_list[i]);
307307

308-
cl_list = t4_alloc_mem(clipt_size*sizeof(struct clip_entry));
308+
cl_list = kvzalloc(clipt_size*sizeof(struct clip_entry), GFP_KERNEL);
309309
if (!cl_list) {
310-
t4_free_mem(ctbl);
310+
kvfree(ctbl);
311311
return NULL;
312312
}
313313
ctbl->cl_list = (void *)cl_list;
@@ -326,8 +326,8 @@ void t4_cleanup_clip_tbl(struct adapter *adap)
326326

327327
if (ctbl) {
328328
if (ctbl->cl_list)
329-
t4_free_mem(ctbl->cl_list);
330-
t4_free_mem(ctbl);
329+
kvfree(ctbl->cl_list);
330+
kvfree(ctbl);
331331
}
332332
}
333333
EXPORT_SYMBOL(t4_cleanup_clip_tbl);

drivers/net/ethernet/chelsio/cxgb4/cxgb4.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,6 @@ extern const char cxgb4_driver_version[];
11841184
void t4_os_portmod_changed(const struct adapter *adap, int port_id);
11851185
void t4_os_link_changed(struct adapter *adap, int port_id, int link_stat);
11861186

1187-
void *t4_alloc_mem(size_t size);
1188-
11891187
void t4_free_sge_resources(struct adapter *adap);
11901188
void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q);
11911189
irq_handler_t t4_intr_handler(struct adapter *adap);
@@ -1557,7 +1555,6 @@ int t4_sched_params(struct adapter *adapter, int type, int level, int mode,
15571555
int rateunit, int ratemode, int channel, int class,
15581556
int minrate, int maxrate, int weight, int pktsize);
15591557
void t4_sge_decode_idma_state(struct adapter *adapter, int state);
1560-
void t4_free_mem(void *addr);
15611558
void t4_idma_monitor_init(struct adapter *adapter,
15621559
struct sge_idma_monitor_state *idma);
15631560
void t4_idma_monitor(struct adapter *adapter,

drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,20 +2634,20 @@ static ssize_t mem_read(struct file *file, char __user *buf, size_t count,
26342634
if (count > avail - pos)
26352635
count = avail - pos;
26362636

2637-
data = t4_alloc_mem(count);
2637+
data = kvzalloc(count, GFP_KERNEL);
26382638
if (!data)
26392639
return -ENOMEM;
26402640

26412641
spin_lock(&adap->win0_lock);
26422642
ret = t4_memory_rw(adap, 0, mem, pos, count, data, T4_MEMORY_READ);
26432643
spin_unlock(&adap->win0_lock);
26442644
if (ret) {
2645-
t4_free_mem(data);
2645+
kvfree(data);
26462646
return ret;
26472647
}
26482648
ret = copy_to_user(buf, data, count);
26492649

2650-
t4_free_mem(data);
2650+
kvfree(data);
26512651
if (ret)
26522652
return -EFAULT;
26532653

@@ -2753,7 +2753,7 @@ static ssize_t blocked_fl_read(struct file *filp, char __user *ubuf,
27532753
adap->sge.egr_sz, adap->sge.blocked_fl);
27542754
len += sprintf(buf + len, "\n");
27552755
size = simple_read_from_buffer(ubuf, count, ppos, buf, len);
2756-
t4_free_mem(buf);
2756+
kvfree(buf);
27572757
return size;
27582758
}
27592759

@@ -2773,7 +2773,7 @@ static ssize_t blocked_fl_write(struct file *filp, const char __user *ubuf,
27732773
return err;
27742774

27752775
bitmap_copy(adap->sge.blocked_fl, t, adap->sge.egr_sz);
2776-
t4_free_mem(t);
2776+
kvfree(t);
27772777
return count;
27782778
}
27792779

drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
969969
{
970970
int i, err = 0;
971971
struct adapter *adapter = netdev2adap(dev);
972-
u8 *buf = t4_alloc_mem(EEPROMSIZE);
972+
u8 *buf = kvzalloc(EEPROMSIZE, GFP_KERNEL);
973973

974974
if (!buf)
975975
return -ENOMEM;
@@ -980,7 +980,7 @@ static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
980980

981981
if (!err)
982982
memcpy(data, buf + e->offset, e->len);
983-
t4_free_mem(buf);
983+
kvfree(buf);
984984
return err;
985985
}
986986

@@ -1009,7 +1009,7 @@ static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
10091009
if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
10101010
/* RMW possibly needed for first or last words.
10111011
*/
1012-
buf = t4_alloc_mem(aligned_len);
1012+
buf = kvzalloc(aligned_len, GFP_KERNEL);
10131013
if (!buf)
10141014
return -ENOMEM;
10151015
err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
@@ -1037,7 +1037,7 @@ static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
10371037
err = t4_seeprom_wp(adapter, true);
10381038
out:
10391039
if (buf != data)
1040-
t4_free_mem(buf);
1040+
kvfree(buf);
10411041
return err;
10421042
}
10431043

0 commit comments

Comments
 (0)