Skip to content

Commit

Permalink
treewide: use kv[mz]alloc* rather than opencoded variants
Browse files Browse the repository at this point in the history
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/[email protected]
Signed-off-by: Michal Hocko <[email protected]>
Reviewed-by: Boris Ostrovsky <[email protected]> # Xen bits
Acked-by: Kees Cook <[email protected]>
Acked-by: Vlastimil Babka <[email protected]>
Acked-by: Andreas Dilger <[email protected]> # Lustre
Acked-by: Christian Borntraeger <[email protected]> # KVM/s390
Acked-by: Dan Williams <[email protected]> # nvdim
Acked-by: David Sterba <[email protected]> # btrfs
Acked-by: Ilya Dryomov <[email protected]> # Ceph
Acked-by: Tariq Toukan <[email protected]> # mlx4
Acked-by: Leon Romanovsky <[email protected]> # mlx5
Cc: Martin Schwidefsky <[email protected]>
Cc: Heiko Carstens <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Anton Vorontsov <[email protected]>
Cc: Colin Cross <[email protected]>
Cc: Tony Luck <[email protected]>
Cc: "Rafael J. Wysocki" <[email protected]>
Cc: Ben Skeggs <[email protected]>
Cc: Kent Overstreet <[email protected]>
Cc: Santosh Raspatur <[email protected]>
Cc: Hariprasad S <[email protected]>
Cc: Yishai Hadas <[email protected]>
Cc: Oleg Drokin <[email protected]>
Cc: "Yan, Zheng" <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: David Miller <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Michal Hocko authored and torvalds committed May 9, 2017
1 parent 81be3de commit 752ade6
Show file tree
Hide file tree
Showing 44 changed files with 128 additions and 350 deletions.
10 changes: 2 additions & 8 deletions arch/s390/kvm/kvm-s390.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,10 +1166,7 @@ static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
return -EINVAL;

keys = kmalloc_array(args->count, sizeof(uint8_t),
GFP_KERNEL | __GFP_NOWARN);
if (!keys)
keys = vmalloc(sizeof(uint8_t) * args->count);
keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
if (!keys)
return -ENOMEM;

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

keys = kmalloc_array(args->count, sizeof(uint8_t),
GFP_KERNEL | __GFP_NOWARN);
if (!keys)
keys = vmalloc(sizeof(uint8_t) * args->count);
keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
if (!keys)
return -ENOMEM;

Expand Down
4 changes: 1 addition & 3 deletions crypto/lzo.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ static void *lzo_alloc_ctx(struct crypto_scomp *tfm)
{
void *ctx;

ctx = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN);
if (!ctx)
ctx = vmalloc(LZO1X_MEM_COMPRESS);
ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
if (!ctx)
return ERR_PTR(-ENOMEM);

Expand Down
8 changes: 2 additions & 6 deletions drivers/acpi/apei/erst.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ static int __erst_record_id_cache_add_one(void)
if (i < erst_record_id_cache.len)
goto retry;
if (erst_record_id_cache.len >= erst_record_id_cache.size) {
int new_size, alloc_size;
int new_size;
u64 *new_entries;

new_size = erst_record_id_cache.size * 2;
Expand All @@ -524,11 +524,7 @@ static int __erst_record_id_cache_add_one(void)
pr_warn(FW_WARN "too many record IDs!\n");
return 0;
}
alloc_size = new_size * sizeof(entries[0]);
if (alloc_size < PAGE_SIZE)
new_entries = kmalloc(alloc_size, GFP_KERNEL);
else
new_entries = vmalloc(alloc_size);
new_entries = kvmalloc(new_size * sizeof(entries[0]), GFP_KERNEL);
if (!new_entries)
return -ENOMEM;
memcpy(new_entries, entries,
Expand Down
8 changes: 1 addition & 7 deletions drivers/char/agp/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ static int agp_get_key(void)

void agp_alloc_page_array(size_t size, struct agp_memory *mem)
{
mem->pages = NULL;

if (size <= 2*PAGE_SIZE)
mem->pages = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
if (mem->pages == NULL) {
mem->pages = vmalloc(size);
}
mem->pages = kvmalloc(size, GFP_KERNEL);
}
EXPORT_SYMBOL(agp_alloc_page_array);

Expand Down
4 changes: 1 addition & 3 deletions drivers/gpu/drm/nouveau/nouveau_gem.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,7 @@ u_memcpya(uint64_t user, unsigned nmemb, unsigned size)

size *= nmemb;

mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
if (!mem)
mem = vmalloc(size);
mem = kvmalloc(size, GFP_KERNEL);
if (!mem)
return ERR_PTR(-ENOMEM);

Expand Down
12 changes: 2 additions & 10 deletions drivers/md/bcache/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ struct closure;
(heap)->used = 0; \
(heap)->size = (_size); \
_bytes = (heap)->size * sizeof(*(heap)->data); \
(heap)->data = NULL; \
if (_bytes < KMALLOC_MAX_SIZE) \
(heap)->data = kmalloc(_bytes, (gfp)); \
if ((!(heap)->data) && ((gfp) & GFP_KERNEL)) \
(heap)->data = vmalloc(_bytes); \
(heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
(heap)->data; \
})

Expand Down Expand Up @@ -136,12 +132,8 @@ do { \
\
(fifo)->mask = _allocated_size - 1; \
(fifo)->front = (fifo)->back = 0; \
(fifo)->data = NULL; \
\
if (_bytes < KMALLOC_MAX_SIZE) \
(fifo)->data = kmalloc(_bytes, (gfp)); \
if ((!(fifo)->data) && ((gfp) & GFP_KERNEL)) \
(fifo)->data = vmalloc(_bytes); \
(fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
(fifo)->data; \
})

Expand Down
3 changes: 0 additions & 3 deletions drivers/net/ethernet/chelsio/cxgb3/cxgb3_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@

#define VALIDATE_TID 1

void *cxgb_alloc_mem(unsigned long size);
void cxgb_free_mem(void *addr);

/*
* Map an ATID or STID to their entries in the corresponding TID tables.
*/
Expand Down
29 changes: 4 additions & 25 deletions drivers/net/ethernet/chelsio/cxgb3/cxgb3_offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,27 +1151,6 @@ static void cxgb_redirect(struct dst_entry *old, struct dst_entry *new,
l2t_release(tdev, e);
}

/*
* Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
* The allocated memory is cleared.
*/
void *cxgb_alloc_mem(unsigned long size)
{
void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);

if (!p)
p = vzalloc(size);
return p;
}

/*
* Free memory allocated through t3_alloc_mem().
*/
void cxgb_free_mem(void *addr)
{
kvfree(addr);
}

/*
* Allocate and initialize the TID tables. Returns 0 on success.
*/
Expand All @@ -1182,7 +1161,7 @@ static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
unsigned long size = ntids * sizeof(*t->tid_tab) +
natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);

t->tid_tab = cxgb_alloc_mem(size);
t->tid_tab = kvzalloc(size, GFP_KERNEL);
if (!t->tid_tab)
return -ENOMEM;

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

static void free_tid_maps(struct tid_info *t)
{
cxgb_free_mem(t->tid_tab);
kvfree(t->tid_tab);
}

static inline void add_adapter(struct adapter *adap)
Expand Down Expand Up @@ -1293,7 +1272,7 @@ int cxgb3_offload_activate(struct adapter *adapter)
return 0;

out_free_l2t:
t3_free_l2t(l2td);
kvfree(l2td);
out_free:
kfree(t);
return err;
Expand All @@ -1302,7 +1281,7 @@ int cxgb3_offload_activate(struct adapter *adapter)
static void clean_l2_data(struct rcu_head *head)
{
struct l2t_data *d = container_of(head, struct l2t_data, rcu_head);
t3_free_l2t(d);
kvfree(d);
}


Expand Down
8 changes: 1 addition & 7 deletions drivers/net/ethernet/chelsio/cxgb3/l2t.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
struct l2t_data *d;
int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);

d = cxgb_alloc_mem(size);
d = kvzalloc(size, GFP_KERNEL);
if (!d)
return NULL;

Expand All @@ -462,9 +462,3 @@ struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
}
return d;
}

void t3_free_l2t(struct l2t_data *d)
{
cxgb_free_mem(d);
}

1 change: 0 additions & 1 deletion drivers/net/ethernet/chelsio/cxgb3/l2t.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
struct l2t_entry *e);
void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e);
struct l2t_data *t3_init_l2t(unsigned int l2t_capacity);
void t3_free_l2t(struct l2t_data *d);

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

Expand Down
12 changes: 6 additions & 6 deletions drivers/net/ethernet/chelsio/cxgb4/clip_tbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
if (clipt_size < CLIPT_MIN_HASH_BUCKETS)
return NULL;

ctbl = t4_alloc_mem(sizeof(*ctbl) +
clipt_size*sizeof(struct list_head));
ctbl = kvzalloc(sizeof(*ctbl) +
clipt_size*sizeof(struct list_head), GFP_KERNEL);
if (!ctbl)
return NULL;

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

cl_list = t4_alloc_mem(clipt_size*sizeof(struct clip_entry));
cl_list = kvzalloc(clipt_size*sizeof(struct clip_entry), GFP_KERNEL);
if (!cl_list) {
t4_free_mem(ctbl);
kvfree(ctbl);
return NULL;
}
ctbl->cl_list = (void *)cl_list;
Expand All @@ -326,8 +326,8 @@ void t4_cleanup_clip_tbl(struct adapter *adap)

if (ctbl) {
if (ctbl->cl_list)
t4_free_mem(ctbl->cl_list);
t4_free_mem(ctbl);
kvfree(ctbl->cl_list);
kvfree(ctbl);
}
}
EXPORT_SYMBOL(t4_cleanup_clip_tbl);
3 changes: 0 additions & 3 deletions drivers/net/ethernet/chelsio/cxgb4/cxgb4.h
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,6 @@ extern const char cxgb4_driver_version[];
void t4_os_portmod_changed(const struct adapter *adap, int port_id);
void t4_os_link_changed(struct adapter *adap, int port_id, int link_stat);

void *t4_alloc_mem(size_t size);

void t4_free_sge_resources(struct adapter *adap);
void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q);
irq_handler_t t4_intr_handler(struct adapter *adap);
Expand Down Expand Up @@ -1557,7 +1555,6 @@ int t4_sched_params(struct adapter *adapter, int type, int level, int mode,
int rateunit, int ratemode, int channel, int class,
int minrate, int maxrate, int weight, int pktsize);
void t4_sge_decode_idma_state(struct adapter *adapter, int state);
void t4_free_mem(void *addr);
void t4_idma_monitor_init(struct adapter *adapter,
struct sge_idma_monitor_state *idma);
void t4_idma_monitor(struct adapter *adapter,
Expand Down
10 changes: 5 additions & 5 deletions drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2634,20 +2634,20 @@ static ssize_t mem_read(struct file *file, char __user *buf, size_t count,
if (count > avail - pos)
count = avail - pos;

data = t4_alloc_mem(count);
data = kvzalloc(count, GFP_KERNEL);
if (!data)
return -ENOMEM;

spin_lock(&adap->win0_lock);
ret = t4_memory_rw(adap, 0, mem, pos, count, data, T4_MEMORY_READ);
spin_unlock(&adap->win0_lock);
if (ret) {
t4_free_mem(data);
kvfree(data);
return ret;
}
ret = copy_to_user(buf, data, count);

t4_free_mem(data);
kvfree(data);
if (ret)
return -EFAULT;

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

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

bitmap_copy(adap->sge.blocked_fl, t, adap->sge.egr_sz);
t4_free_mem(t);
kvfree(t);
return count;
}

Expand Down
8 changes: 4 additions & 4 deletions drivers/net/ethernet/chelsio/cxgb4/cxgb4_ethtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
{
int i, err = 0;
struct adapter *adapter = netdev2adap(dev);
u8 *buf = t4_alloc_mem(EEPROMSIZE);
u8 *buf = kvzalloc(EEPROMSIZE, GFP_KERNEL);

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

if (!err)
memcpy(data, buf + e->offset, e->len);
t4_free_mem(buf);
kvfree(buf);
return err;
}

Expand Down Expand Up @@ -1009,7 +1009,7 @@ static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
/* RMW possibly needed for first or last words.
*/
buf = t4_alloc_mem(aligned_len);
buf = kvzalloc(aligned_len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
Expand Down Expand Up @@ -1037,7 +1037,7 @@ static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
err = t4_seeprom_wp(adapter, true);
out:
if (buf != data)
t4_free_mem(buf);
kvfree(buf);
return err;
}

Expand Down
Loading

0 comments on commit 752ade6

Please sign in to comment.