Skip to contentSkip to conversation

xen-blkfront: Use kcalloc instead of kzalloc to allocate array

PATCH In review Thomas Meyer 2 participants 3 messages Nov 29, 2011 – Dec 14, 2011

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-blkfront: Use kcalloc instead of kzalloc to allocate array

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The semantic patch that makes this change is available in https://lkml.org/lkml/2011/11/25/107

---

drivers/block/cciss_scsi.c 2011-11-28 19:49:24.922716381 +0100 renamed from drivers/block/cciss_scsi.c 2011-11-28 19:36:47.343430551 +0100 +5 -5
diff -u -p a/drivers/block/cciss_scsi.c b/drivers/block/cciss_scsi.c
@@ -534,10 +534,10 @@ adjust_cciss_scsi_table(ctlr_info_t *h,
 	int nadded, nremoved;
 	struct Scsi_Host *sh = NULL;
 
-	added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
-			GFP_KERNEL);
-	removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
+	added = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA, sizeof(*added),
 			GFP_KERNEL);
+	removed = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA, sizeof(*removed),
+			  GFP_KERNEL);
 
 	if (!added || !removed) {
 		dev_warn(&h->pdev->dev,
@@ -1191,8 +1191,8 @@ cciss_update_non_disk_devices(ctlr_info_
 
 	ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
 	inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
-	currentsd = kzalloc(sizeof(*currentsd) *
-			(CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
+	currentsd = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA + 1,
+			    sizeof(*currentsd), GFP_KERNEL);
 	if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
 		printk(KERN_ERR "cciss: out of memory\n");
 		goto out;
drivers/block/xen-blkfront.c 2011-11-28 19:49:29.109460410 +0100 renamed from drivers/block/xen-blkfront.c 2011-11-13 11:07:22.680095573 +0100 +1 -1
diff -u -p a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
@@ -156,7 +156,7 @@ static int xlbd_reserve_minors(unsigned
 	if (end > nr_minors) {
 		unsigned long *bitmap, *old;
 
-		bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
+		bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
 				 GFP_KERNEL);
 		if (bitmap == NULL)
 			return -ENOMEM;
#1 lore raw
Quoted 15 lines · Thomas Meyer

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The semantic patch that makes this change is available in https://lkml.org/lkml/2011/11/25/107

---

diff -u -p a/drivers/block/cciss_scsi.c b/drivers/block/cciss_scsi.c

drivers/block/cciss_scsi.c 2011-11-28 19:49:24.922716381 +0100 renamed from drivers/block/cciss_scsi.c 2011-11-28 19:36:47.343430551 +0100 +0 -0
@@ -534,10 +534,10 @@ adjust_cciss_scsi_table(ctlr_info_t *h,
 	int nadded, nremoved;

drivers/block/cciss_scsi.c 2011-11-28 19:49:24.922716381 +0100 adjust_cciss_scsi_table(ctlr_info_t *h, · line 541

Quoted 10 lines
drivers/block/cciss_scsi.c 2011-11-28 19:49:24.922716381 +0100 renamed from drivers/block/cciss_scsi.c 2011-11-28 19:36:47.343430551 +0100 +3 -3
 	struct Scsi_Host *sh = NULL;
 
-	added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
-			GFP_KERNEL);
-	removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
+	added = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA, sizeof(*added),
 			GFP_KERNEL);
+	removed = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA, sizeof(*removed),
+			  GFP_KERNEL);
 

It looks like you mixed two patches together.

Quoted 17 lines
drivers/block/cciss_scsi.c 2011-11-28 19:49:24.922716381 +0100 renamed from drivers/block/cciss_scsi.c 2011-11-28 19:36:47.343430551 +0100 +2 -2
 	if (!added || !removed) {
 		dev_warn(&h->pdev->dev,
@@ -1191,8 +1191,8 @@ cciss_update_non_disk_devices(ctlr_info_
 
 	ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
 	inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
-	currentsd = kzalloc(sizeof(*currentsd) *
-			(CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
+	currentsd = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA + 1,
+			    sizeof(*currentsd), GFP_KERNEL);
 	if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
 		printk(KERN_ERR "cciss: out of memory\n");
 		goto out;

diff -u -p a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c

drivers/block/xen-blkfront.c 2011-11-28 19:49:29.109460410 +0100 static int xlbd_reserve_minors(unsigned · line 160

Quoted 6 lines
drivers/block/xen-blkfront.c 2011-11-28 19:49:29.109460410 +0100 renamed from drivers/block/xen-blkfront.c 2011-11-13 11:07:22.680095573 +0100 +1 -1
@@ -156,7 +156,7 @@ static int xlbd_reserve_minors(unsigned
 	if (end > nr_minors) {
 		unsigned long *bitmap, *old;
 
-		bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
+		bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
 				 GFP_KERNEL);

But this parts looks good.

Can you respin it with just that part please?

Quoted 7 lines
drivers/block/xen-blkfront.c 2011-11-28 19:49:29.109460410 +0100 renamed from drivers/block/xen-blkfront.c 2011-11-13 11:07:22.680095573 +0100 +0 -1
 		if (bitmap == NULL)
 			return -ENOMEM;
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw
In reply to Thomas Meyer
Quoted 7 lines · Thomas Meyer

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

The semantic patch that makes this change is available in https://lkml.org/lkml/2011/11/25/107

Thomas,

I put the xen-blkfront part of the patch in my tree and dropped the cciss_scsi one.

Quoted 48 lines

---

diff -u -p a/drivers/block/cciss_scsi.c b/drivers/block/cciss_scsi.c

drivers/block/cciss_scsi.c 2011-11-28 19:49:24.922716381 +0100 renamed from drivers/block/cciss_scsi.c 2011-11-28 19:36:47.343430551 +0100 +5 -5
@@ -534,10 +534,10 @@ adjust_cciss_scsi_table(ctlr_info_t *h,
 	int nadded, nremoved;
 	struct Scsi_Host *sh = NULL;
 
-	added = kzalloc(sizeof(*added) * CCISS_MAX_SCSI_DEVS_PER_HBA,
-			GFP_KERNEL);
-	removed = kzalloc(sizeof(*removed) * CCISS_MAX_SCSI_DEVS_PER_HBA,
+	added = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA, sizeof(*added),
 			GFP_KERNEL);
+	removed = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA, sizeof(*removed),
+			  GFP_KERNEL);
 
 	if (!added || !removed) {
 		dev_warn(&h->pdev->dev,
@@ -1191,8 +1191,8 @@ cciss_update_non_disk_devices(ctlr_info_
 
 	ld_buff = kzalloc(reportlunsize, GFP_KERNEL);
 	inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
-	currentsd = kzalloc(sizeof(*currentsd) *
-			(CCISS_MAX_SCSI_DEVS_PER_HBA+1), GFP_KERNEL);
+	currentsd = kcalloc(CCISS_MAX_SCSI_DEVS_PER_HBA + 1,
+			    sizeof(*currentsd), GFP_KERNEL);
 	if (ld_buff == NULL || inq_buff == NULL || currentsd == NULL) {
 		printk(KERN_ERR "cciss: out of memory\n");
 		goto out;

diff -u -p a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c

drivers/block/xen-blkfront.c 2011-11-28 19:49:29.109460410 +0100 renamed from drivers/block/xen-blkfront.c 2011-11-13 11:07:22.680095573 +0100 +1 -2
@@ -156,7 +156,7 @@ static int xlbd_reserve_minors(unsigned
 	if (end > nr_minors) {
 		unsigned long *bitmap, *old;
 
-		bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
+		bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
 				 GFP_KERNEL);
 		if (bitmap == NULL)
 			return -ENOMEM;
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/