linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] [SCSI] libsrp: replace kfifo_init with kfifo_alloc
@ 2012-11-19  8:58 Yuanhan Liu
  2012-11-19  8:58 ` [PATCH 2/2] [SCSI] libiscsi: " Yuanhan Liu
  0 siblings, 1 reply; 2+ messages in thread
From: Yuanhan Liu @ 2012-11-19  8:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-scsi, Yuanhan Liu, James E.J. Bottomley, Stefani Seibold

kfifo_init will use a pre-allocated buffer as fifo buffer; the buffer
size is determinted at caller side. While, kfifo will maintain a real
kfifo buffer size(power of 2 aligned). So, the two size may not be
equal.

So, to be safe. We should use kfifo_alloc instead. git grep 'kfifo_init\>'
shows that this is one user of kfifo_init(2 in total).

Replace it with kfifo_alloc, as I propose to remove kfifo_init API
later.

Cc: James E.J. Bottomley <JBottomley@parallels.com>
Cc: Stefani Seibold <stefani@seibold.net>
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
---
 drivers/scsi/libsrp.c |   22 ++++++++++------------
 include/scsi/libsrp.h |    1 -
 2 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/scsi/libsrp.c b/drivers/scsi/libsrp.c
index 0707ecd..63db792 100644
--- a/drivers/scsi/libsrp.c
+++ b/drivers/scsi/libsrp.c
@@ -50,34 +50,32 @@ static int srp_iu_pool_alloc(struct srp_queue *q, size_t max,
 			     struct srp_buf **ring)
 {
 	int i;
+	int ret;
 	struct iu_entry *iue;
 
-	q->pool = kcalloc(max, sizeof(struct iu_entry *), GFP_KERNEL);
+	q->pool = kcalloc(max, sizeof(struct iu_entry), GFP_KERNEL);
 	if (!q->pool)
 		return -ENOMEM;
-	q->items = kcalloc(max, sizeof(struct iu_entry), GFP_KERNEL);
-	if (!q->items)
-		goto free_pool;
+
+	ret = kfifo_alloc(&q->queue, max * sizeof(void *), GFP_KERNEL);
+	if (ret < 0) {
+		kfree(q->pool);
+		return ret;
+	}
 
 	spin_lock_init(&q->lock);
-	kfifo_init(&q->queue, (void *) q->pool, max * sizeof(void *));
 
-	for (i = 0, iue = q->items; i < max; i++) {
+	for (i = 0, iue = q->pool; i < max; i++) {
 		kfifo_in(&q->queue, (void *) &iue, sizeof(void *));
 		iue->sbuf = ring[i];
 		iue++;
 	}
 	return 0;
-
-	kfree(q->items);
-free_pool:
-	kfree(q->pool);
-	return -ENOMEM;
 }
 
 static void srp_iu_pool_free(struct srp_queue *q)
 {
-	kfree(q->items);
+	kfifo_free(&q->queue);
 	kfree(q->pool);
 }
 
diff --git a/include/scsi/libsrp.h b/include/scsi/libsrp.h
index f4105c9..999b1e7 100644
--- a/include/scsi/libsrp.h
+++ b/include/scsi/libsrp.h
@@ -21,7 +21,6 @@ struct srp_buf {
 
 struct srp_queue {
 	void *pool;
-	void *items;
 	struct kfifo queue;
 	spinlock_t lock;
 };
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] [SCSI] libiscsi: replace kfifo_init with kfifo_alloc
  2012-11-19  8:58 [PATCH 1/2] [SCSI] libsrp: replace kfifo_init with kfifo_alloc Yuanhan Liu
@ 2012-11-19  8:58 ` Yuanhan Liu
  0 siblings, 0 replies; 2+ messages in thread
From: Yuanhan Liu @ 2012-11-19  8:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-scsi, Yuanhan Liu, Mike Christie, James E.J. Bottomley,
	iscsi list, Stefani Seibold

kfifo_init will use a pre-allocated buffer as fifo buffer; the buffer
size is determinted at caller side. While, kfifo will maintain a real
kfifo buffer size(power of 2 aligned). So, the two size may not be
equal.

So, to be safe. We should use kfifo_alloc instead. git grep 'kfifo_init\>'
shows that this is one user of kfifo_init(2 in total).

Replace it with kfifo_alloc, as I propose to remove kfifo_init API
later.

Besides that, I refactored the code a bit with the style used in
libsrp.c: allocated items once by kcalloc.

Cc: Mike Christie <michaelc@cs.wisc.edu>
Cc: James E.J. Bottomley <JBottomley@parallels.com>
Cc: iscsi list <open-iscsi@googlegroups.com>
Cc: Stefani Seibold <stefani@seibold.net>
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
---
 drivers/scsi/libiscsi.c |   59 ++++++++++++++++++++++++-----------------------
 include/scsi/libiscsi.h |    4 +-
 2 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
index 82c3fd4..8e60f1f 100644
--- a/drivers/scsi/libiscsi.c
+++ b/drivers/scsi/libiscsi.c
@@ -2485,51 +2485,52 @@ EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
 int
 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
 {
-	int i, num_arrays = 1;
-
-	memset(q, 0, sizeof(*q));
+	int i;
+	int ret;
+	void *item;
 
-	q->max = max;
+	ret = -ENOMEM;
+	q->pool = kcalloc(max, item_size, GFP_KERNEL);
+	if (!q->pool)
+		goto err_out;
 
-	/* If the user passed an items pointer, he wants a copy of
-	 * the array. */
-	if (items)
-		num_arrays++;
-	q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
-	if (q->pool == NULL)
-		return -ENOMEM;
+	ret = kfifo_alloc(&q->queue, max * sizeof(void *), GFP_KERNEL);
+	if (ret < 0)
+		goto free_pool;
 
-	kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
-
-	for (i = 0; i < max; i++) {
-		q->pool[i] = kzalloc(item_size, GFP_KERNEL);
-		if (q->pool[i] == NULL) {
-			q->max = i;
-			goto enomem;
+	q->items_ptr = NULL;
+	/* If the user passed an items pointer, he wants a copy of the array */
+	if (items) {
+		q->items_ptr = kcalloc(max, sizeof(void *), GFP_KERNEL);
+		if (!q->items_ptr) {
+			ret = -ENOMEM;
+			goto free_fifo;
 		}
-		kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
+		*items = q->items_ptr;
 	}
 
-	if (items) {
-		*items = q->pool + max;
-		memcpy(*items, q->pool, max * sizeof(void *));
+	for (i = 0, item = q->pool; i < max; i++, item += item_size) {
+		kfifo_in(&q->queue, &item, sizeof(void *));
+		if (q->items_ptr)
+			q->items_ptr[i] = item;
 	}
 
 	return 0;
 
-enomem:
-	iscsi_pool_free(q);
-	return -ENOMEM;
+free_fifo:
+	kfifo_free(&q->queue);
+free_pool:
+	kfree(q->pool);
+err_out:
+	return ret;
 }
 EXPORT_SYMBOL_GPL(iscsi_pool_init);
 
 void iscsi_pool_free(struct iscsi_pool *q)
 {
-	int i;
-
-	for (i = 0; i < q->max; i++)
-		kfree(q->pool[i]);
 	kfree(q->pool);
+	kfree(q->items_ptr);
+	kfifo_free(&q->queue);
 }
 EXPORT_SYMBOL_GPL(iscsi_pool_free);
 
diff --git a/include/scsi/libiscsi.h b/include/scsi/libiscsi.h
index 6e33386..c4262c6 100644
--- a/include/scsi/libiscsi.h
+++ b/include/scsi/libiscsi.h
@@ -231,8 +231,8 @@ struct iscsi_conn {
 
 struct iscsi_pool {
 	struct kfifo		queue;		/* FIFO Queue */
-	void			**pool;		/* Pool of elements */
-	int			max;		/* Max number of elements */
+	void			*pool;		/* Pool of elements */
+	void			**items_ptr;	/* element pointers */
 };
 
 /* Session's states */
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-11-19  8:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-19  8:58 [PATCH 1/2] [SCSI] libsrp: replace kfifo_init with kfifo_alloc Yuanhan Liu
2012-11-19  8:58 ` [PATCH 2/2] [SCSI] libiscsi: " Yuanhan Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).