Skip to content

Commit

Permalink
some kmalloc/memset ->kzalloc (tree wide)
Browse files Browse the repository at this point in the history
Transform some calls to kmalloc/memset to a single kzalloc (or kcalloc).

Here is a short excerpt of the semantic patch performing
this transformation:

@@
type T2;
expression x;
identifier f,fld;
expression E;
expression E1,E2;
expression e1,e2,e3,y;
statement S;
@@

 x =
- kmalloc
+ kzalloc
  (E1,E2)
  ...  when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
- memset((T2)x,0,E1);

@@
expression E1,E2,E3;
@@

- kzalloc(E1 * E2,E3)
+ kcalloc(E1,E2,E3)

[[email protected]: get kcalloc args the right way around]
Signed-off-by: Yoann Padioleau <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Ivan Kokshaysky <[email protected]>
Acked-by: Russell King <[email protected]>
Cc: Bryan Wu <[email protected]>
Acked-by: Jiri Slaby <[email protected]>
Cc: Dave Airlie <[email protected]>
Acked-by: Roland Dreier <[email protected]>
Cc: Jiri Kosina <[email protected]>
Acked-by: Dmitry Torokhov <[email protected]>
Cc: Benjamin Herrenschmidt <[email protected]>
Acked-by: Mauro Carvalho Chehab <[email protected]>
Acked-by: Pierre Ossman <[email protected]>
Cc: Jeff Garzik <[email protected]>
Cc: "David S. Miller" <[email protected]>
Acked-by: Greg KH <[email protected]>
Cc: James Bottomley <[email protected]>
Cc: "Antonino A. Daplas" <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Yoann Padioleau authored and Linus Torvalds committed Jul 19, 2007
1 parent 3b5ad07 commit dd00cc4
Show file tree
Hide file tree
Showing 136 changed files with 157 additions and 330 deletions.
3 changes: 1 addition & 2 deletions Documentation/connector/cn_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ static void cn_test_timer_func(unsigned long __data)
struct cn_msg *m;
char data[32];

m = kmalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
if (m) {
memset(m, 0, sizeof(*m) + sizeof(data));

memcpy(&m->id, &cn_test_id, sizeof(m->id));
m->seq = cn_test_timer_counter;
Expand Down
6 changes: 2 additions & 4 deletions Documentation/filesystems/configfs/configfs_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,10 @@ static struct config_item *simple_children_make_item(struct config_group *group,
{
struct simple_child *simple_child;

simple_child = kmalloc(sizeof(struct simple_child), GFP_KERNEL);
simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
if (!simple_child)
return NULL;

memset(simple_child, 0, sizeof(struct simple_child));

config_item_init_type_name(&simple_child->item, name,
&simple_child_type);
Expand Down Expand Up @@ -364,12 +363,11 @@ static struct config_group *group_children_make_group(struct config_group *group
{
struct simple_children *simple_children;

simple_children = kmalloc(sizeof(struct simple_children),
simple_children = kzalloc(sizeof(struct simple_children),
GFP_KERNEL);
if (!simple_children)
return NULL;

memset(simple_children, 0, sizeof(struct simple_children));

config_group_init_type_name(&simple_children->group, name,
&simple_children_type);
Expand Down
3 changes: 1 addition & 2 deletions arch/alpha/kernel/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs,
}

nsyms = symtab->sh_size / sizeof(Elf64_Sym);
chains = kmalloc(nsyms * sizeof(struct got_entry), GFP_KERNEL);
memset(chains, 0, nsyms * sizeof(struct got_entry));
chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL);

got->sh_size = 0;
got->sh_addralign = 8;
Expand Down
3 changes: 1 addition & 2 deletions arch/arm/mach-iop13xx/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,11 +1002,10 @@ int iop13xx_pci_setup(int nr, struct pci_sys_data *sys)
if (nr > 1)
return 0;

res = kmalloc(sizeof(struct resource) * 2, GFP_KERNEL);
res = kcalloc(2, sizeof(struct resource), GFP_KERNEL);
if (!res)
panic("PCI: unable to alloc resources");

memset(res, 0, sizeof(struct resource) * 2);

/* 'nr' assumptions:
* ATUX is always 0
Expand Down
3 changes: 1 addition & 2 deletions arch/blackfin/mm/blackfin_sram.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,9 @@ void *sram_alloc_with_lsl(size_t size, unsigned long flags)
struct sram_list_struct *lsl = NULL;
struct mm_struct *mm = current->mm;

lsl = kmalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
if (!lsl)
return NULL;
memset(lsl, 0, sizeof(*lsl));

if (flags & L1_INST_SRAM)
addr = l1_inst_sram_alloc(size);
Expand Down
6 changes: 2 additions & 4 deletions arch/cris/arch-v32/drivers/pci/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
if (!mem_base)
goto out;

dev->dma_mem = kmalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
if (!dev->dma_mem)
goto out;
memset(dev->dma_mem, 0, sizeof(struct dma_coherent_mem));
dev->dma_mem->bitmap = kmalloc(bitmap_size, GFP_KERNEL);
dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
if (!dev->dma_mem->bitmap)
goto free1_out;
memset(dev->dma_mem->bitmap, 0, bitmap_size);

dev->dma_mem->virt_base = mem_base;
dev->dma_mem->device_base = device_addr;
Expand Down
3 changes: 1 addition & 2 deletions arch/powerpc/kernel/lparcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static void parse_system_parameter_string(struct seq_file *m)
} else {
int splpar_strlen;
int idx, w_idx;
char *workbuffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
if (!workbuffer) {
printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
__FILE__, __FUNCTION__, __LINE__);
Expand All @@ -261,7 +261,6 @@ static void parse_system_parameter_string(struct seq_file *m)
splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
local_buffer += 2; /* step over strlen value */

memset(workbuffer, 0, SPLPAR_MAXLENGTH);
w_idx = 0;
idx = 0;
while ((*local_buffer) && (idx < splpar_strlen)) {
Expand Down
3 changes: 1 addition & 2 deletions arch/powerpc/kernel/of_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ struct of_device* of_platform_device_create(struct device_node *np,
{
struct of_device *dev;

dev = kmalloc(sizeof(*dev), GFP_KERNEL);
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return NULL;
memset(dev, 0, sizeof(*dev));

dev->node = of_node_get(np);
dev->dma_mask = 0xffffffffUL;
Expand Down
3 changes: 1 addition & 2 deletions block/scsi_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,10 @@ int sg_scsi_ioctl(struct file *file, struct request_queue *q,

bytes = max(in_len, out_len);
if (bytes) {
buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
if (!buffer)
return -ENOMEM;

memset(buffer, 0, bytes);
}

rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
Expand Down
3 changes: 1 addition & 2 deletions drivers/block/sx8.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,15 +1608,14 @@ static int carm_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
}
#endif

host = kmalloc(sizeof(*host), GFP_KERNEL);
host = kzalloc(sizeof(*host), GFP_KERNEL);
if (!host) {
printk(KERN_ERR DRV_NAME "(%s): memory alloc failure\n",
pci_name(pdev));
rc = -ENOMEM;
goto err_out_regions;
}

memset(host, 0, sizeof(*host));
host->pdev = pdev;
host->flags = pci_dac ? FL_DAC : 0;
spin_lock_init(&host->lock);
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/amiserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -1721,12 +1721,11 @@ static int get_async_struct(int line, struct async_struct **ret_info)
*ret_info = sstate->info;
return 0;
}
info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
if (!info) {
sstate->count--;
return -ENOMEM;
}
memset(info, 0, sizeof(struct async_struct));
#ifdef DECLARE_WAITQUEUE
init_waitqueue_head(&info->open_wait);
init_waitqueue_head(&info->close_wait);
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/drm/via_dmablit.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,9 @@ via_alloc_desc_pages(drm_via_sg_info_t *vsg)
vsg->num_desc_pages = (vsg->num_desc + vsg->descriptors_per_page - 1) /
vsg->descriptors_per_page;

if (NULL == (vsg->desc_pages = kmalloc(sizeof(void *) * vsg->num_desc_pages, GFP_KERNEL)))
if (NULL == (vsg->desc_pages = kcalloc(vsg->num_desc_pages, sizeof(void *), GFP_KERNEL)))
return DRM_ERR(ENOMEM);

memset(vsg->desc_pages, 0, sizeof(void *) * vsg->num_desc_pages);
vsg->state = dr_via_desc_pages_alloc;
for (i=0; i<vsg->num_desc_pages; ++i) {
if (NULL == (vsg->desc_pages[i] =
Expand Down
6 changes: 2 additions & 4 deletions drivers/char/esp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2459,7 +2459,7 @@ static int __init espserial_init(void)
return 1;
}

info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);

if (!info)
{
Expand All @@ -2469,7 +2469,6 @@ static int __init espserial_init(void)
return 1;
}

memset((void *)info, 0, sizeof(struct esp_struct));
spin_lock_init(&info->lock);
/* rx_trigger, tx_trigger are needed by autoconfig */
info->config.rx_trigger = rx_trigger;
Expand Down Expand Up @@ -2527,7 +2526,7 @@ static int __init espserial_init(void)
if (!dma)
info->stat_flags |= ESP_STAT_NEVER_DMA;

info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
if (!info)
{
printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
Expand All @@ -2536,7 +2535,6 @@ static int __init espserial_init(void)
return 0;
}

memset((void *)info, 0, sizeof(struct esp_struct));
/* rx_trigger, tx_trigger are needed by autoconfig */
info->config.rx_trigger = rx_trigger;
info->config.tx_trigger = tx_trigger;
Expand Down
4 changes: 1 addition & 3 deletions drivers/char/hvcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,10 @@ static int __devinit hvcs_probe(
return -EFAULT;
}

hvcsd = kmalloc(sizeof(*hvcsd), GFP_KERNEL);
hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
if (!hvcsd)
return -ENODEV;

/* hvcsd->tty is zeroed out with the memset */
memset(hvcsd, 0x00, sizeof(*hvcsd));

spin_lock_init(&hvcsd->lock);
/* Automatically incs the refcount the first time */
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/ipmi/ipmi_msghandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2639,10 +2639,9 @@ int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
return -ENODEV;
}

intf = kmalloc(sizeof(*intf), GFP_KERNEL);
intf = kzalloc(sizeof(*intf), GFP_KERNEL);
if (!intf)
return -ENOMEM;
memset(intf, 0, sizeof(*intf));

intf->ipmi_version_major = ipmi_version_major(device_id);
intf->ipmi_version_minor = ipmi_version_minor(device_id);
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/pcmcia/synclink_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,12 @@ static int mgslpc_probe(struct pcmcia_device *link)
if (debug_level >= DEBUG_LEVEL_INFO)
printk("mgslpc_attach\n");

info = kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
info = kzalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
if (!info) {
printk("Error can't allocate device instance data\n");
return -ENOMEM;
}

memset(info, 0, sizeof(MGSLPC_INFO));
info->magic = MGSLPC_MAGIC;
INIT_WORK(&info->task, bh_handler);
info->max_frame_size = 4096;
Expand Down
4 changes: 1 addition & 3 deletions drivers/char/rio/rio_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,7 @@ static void *ckmalloc(int size)
{
void *p;

p = kmalloc(size, GFP_KERNEL);
if (p)
memset(p, 0, size);
p = kzalloc(size, GFP_KERNEL);
return p;
}

Expand Down
4 changes: 1 addition & 3 deletions drivers/char/rio/riocmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,7 @@ struct CmdBlk *RIOGetCmdBlk(void)
{
struct CmdBlk *CmdBlkP;

CmdBlkP = kmalloc(sizeof(struct CmdBlk), GFP_ATOMIC);
if (CmdBlkP)
memset(CmdBlkP, 0, sizeof(struct CmdBlk));
CmdBlkP = kzalloc(sizeof(struct CmdBlk), GFP_ATOMIC);
return CmdBlkP;
}

Expand Down
3 changes: 1 addition & 2 deletions drivers/char/rio/riotable.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,7 @@ int RIOReMapPorts(struct rio_info *p, struct Host *HostP, struct Map *HostMapP)
if (PortP->TxRingBuffer)
memset(PortP->TxRingBuffer, 0, p->RIOBufferSize);
else if (p->RIOBufferSize) {
PortP->TxRingBuffer = kmalloc(p->RIOBufferSize, GFP_KERNEL);
memset(PortP->TxRingBuffer, 0, p->RIOBufferSize);
PortP->TxRingBuffer = kzalloc(p->RIOBufferSize, GFP_KERNEL);
}
PortP->TxBufferOut = 0;
PortP->TxBufferIn = 0;
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/rocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,11 @@ static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
ctlp = sCtlNumToCtlPtr(board);

/* Get a r_port struct for the port, fill it in and save it globally, indexed by line number */
info = kmalloc(sizeof (struct r_port), GFP_KERNEL);
info = kzalloc(sizeof (struct r_port), GFP_KERNEL);
if (!info) {
printk(KERN_INFO "Couldn't allocate info struct for line #%d\n", line);
return;
}
memset(info, 0, sizeof (struct r_port));

info->magic = RPORT_MAGIC;
info->line = line;
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/synclink.c
Original file line number Diff line number Diff line change
Expand Up @@ -4324,13 +4324,12 @@ static struct mgsl_struct* mgsl_allocate_device(void)
{
struct mgsl_struct *info;

info = kmalloc(sizeof(struct mgsl_struct),
info = kzalloc(sizeof(struct mgsl_struct),
GFP_KERNEL);

if (!info) {
printk("Error can't allocate device instance data\n");
} else {
memset(info, 0, sizeof(struct mgsl_struct));
info->magic = MGSL_MAGIC;
INIT_WORK(&info->task, mgsl_bh_handler);
info->max_frame_size = 4096;
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/synclink_gt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3414,13 +3414,12 @@ static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev
{
struct slgt_info *info;

info = kmalloc(sizeof(struct slgt_info), GFP_KERNEL);
info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);

if (!info) {
DBGERR(("%s device alloc failed adapter=%d port=%d\n",
driver_name, adapter_num, port_num));
} else {
memset(info, 0, sizeof(struct slgt_info));
info->magic = MGSL_MAGIC;
INIT_WORK(&info->task, bh_handler);
info->max_frame_size = 4096;
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/synclinkmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3786,14 +3786,13 @@ static SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
{
SLMP_INFO *info;

info = kmalloc(sizeof(SLMP_INFO),
info = kzalloc(sizeof(SLMP_INFO),
GFP_KERNEL);

if (!info) {
printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
__FILE__,__LINE__, adapter_num, port_num);
} else {
memset(info, 0, sizeof(SLMP_INFO));
info->magic = MGSL_MAGIC;
INIT_WORK(&info->task, bh_handler);
info->max_frame_size = 4096;
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/watchdog/mpcore_wdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,11 @@ static int __devinit mpcore_wdt_probe(struct platform_device *dev)
goto err_out;
}

wdt = kmalloc(sizeof(struct mpcore_wdt), GFP_KERNEL);
wdt = kzalloc(sizeof(struct mpcore_wdt), GFP_KERNEL);
if (!wdt) {
ret = -ENOMEM;
goto err_out;
}
memset(wdt, 0, sizeof(struct mpcore_wdt));

wdt->dev = &dev->dev;
wdt->irq = platform_get_irq(dev, 0);
Expand Down
3 changes: 1 addition & 2 deletions drivers/char/watchdog/pcwd_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,11 @@ static int usb_pcwd_probe(struct usb_interface *interface, const struct usb_devi
maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));

/* allocate memory for our device and initialize it */
usb_pcwd = kmalloc (sizeof(struct usb_pcwd_private), GFP_KERNEL);
usb_pcwd = kzalloc (sizeof(struct usb_pcwd_private), GFP_KERNEL);
if (usb_pcwd == NULL) {
printk(KERN_ERR PFX "Out of memory\n");
goto error;
}
memset (usb_pcwd, 0x00, sizeof (*usb_pcwd));

usb_pcwd_device = usb_pcwd;

Expand Down
3 changes: 1 addition & 2 deletions drivers/ide/mips/swarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,11 @@ static int __devinit swarm_ide_init_module(void)
goto out;
}

if (!(pldev = kmalloc(sizeof (*pldev), GFP_KERNEL))) {
if (!(pldev = kzalloc(sizeof (*pldev), GFP_KERNEL))) {
err = -ENOMEM;
goto out_unregister_driver;
}

memset (pldev, 0, sizeof (*pldev));
pldev->name = swarm_ide_string;
pldev->id = 0;
pldev->dev.release = swarm_ide_platform_release;
Expand Down
3 changes: 1 addition & 2 deletions drivers/infiniband/core/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,9 @@ int rdma_resolve_ip(struct rdma_addr_client *client,
struct addr_req *req;
int ret = 0;

req = kmalloc(sizeof *req, GFP_KERNEL);
req = kzalloc(sizeof *req, GFP_KERNEL);
if (!req)
return -ENOMEM;
memset(req, 0, sizeof *req);

if (src_addr)
memcpy(&req->src_addr, src_addr, ip_addr_size(src_addr));
Expand Down
Loading

0 comments on commit dd00cc4

Please sign in to comment.