diff options
author | Stefani Seibold <stefani@seibold.net> | 2009-12-21 17:37:26 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-22 17:17:55 -0500 |
commit | 45465487897a1c6d508b14b904dc5777f7ec7e04 (patch) | |
tree | 935c8dae68dc793ff2f795d57cf027531475cd53 /drivers/scsi | |
parent | 2ec91eec47f713e3d158ba5b28a24a85a2cf3650 (diff) |
kfifo: move struct kfifo in place
This is a new generic kernel FIFO implementation.
The current kernel fifo API is not very widely used, because it has to
many constrains. Only 17 files in the current 2.6.31-rc5 used it.
FIFO's are like list's a very basic thing and a kfifo API which handles
the most use case would save a lot of development time and memory
resources.
I think this are the reasons why kfifo is not in use:
- The API is to simple, important functions are missing
- A fifo can be only allocated dynamically
- There is a requirement of a spinlock whether you need it or not
- There is no support for data records inside a fifo
So I decided to extend the kfifo in a more generic way without blowing up
the API to much. The new API has the following benefits:
- Generic usage: For kernel internal use and/or device driver.
- Provide an API for the most use case.
- Slim API: The whole API provides 25 functions.
- Linux style habit.
- DECLARE_KFIFO, DEFINE_KFIFO and INIT_KFIFO Macros
- Direct copy_to_user from the fifo and copy_from_user into the fifo.
- The kfifo itself is an in place member of the using data structure, this save an
indirection access and does not waste the kernel allocator.
- Lockless access: if only one reader and one writer is active on the fifo,
which is the common use case, no additional locking is necessary.
- Remove spinlock - give the user the freedom of choice what kind of locking to use if
one is required.
- Ability to handle records. Three type of records are supported:
- Variable length records between 0-255 bytes, with a record size
field of 1 bytes.
- Variable length records between 0-65535 bytes, with a record size
field of 2 bytes.
- Fixed size records, which no record size field.
- Preserve memory resource.
- Performance!
- Easy to use!
This patch:
Since most users want to have the kfifo as part of another object,
reorganize the code to allow including struct kfifo in another data
structure. This requires changing the kfifo_alloc and kfifo_init
prototypes so that we pass an existing kfifo pointer into them. This
patch changes the implementation and all existing users.
[akpm@linux-foundation.org: fix warning]
Signed-off-by: Stefani Seibold <stefani@seibold.net>
Acked-by: Greg Kroah-Hartman <gregkh@suse.de>
Acked-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/scsi')
-rw-r--r-- | drivers/scsi/libiscsi.c | 22 | ||||
-rw-r--r-- | drivers/scsi/libiscsi_tcp.c | 29 | ||||
-rw-r--r-- | drivers/scsi/libsrp.c | 13 |
3 files changed, 27 insertions, 37 deletions
diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c index b7689f3d05f5..cf0aa7e90be9 100644 --- a/drivers/scsi/libiscsi.c +++ b/drivers/scsi/libiscsi.c | |||
@@ -517,7 +517,7 @@ static void iscsi_free_task(struct iscsi_task *task) | |||
517 | if (conn->login_task == task) | 517 | if (conn->login_task == task) |
518 | return; | 518 | return; |
519 | 519 | ||
520 | __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*)); | 520 | __kfifo_put(&session->cmdpool.queue, (void*)&task, sizeof(void*)); |
521 | 521 | ||
522 | if (sc) { | 522 | if (sc) { |
523 | task->sc = NULL; | 523 | task->sc = NULL; |
@@ -737,7 +737,7 @@ __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, | |||
737 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); | 737 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); |
738 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); | 738 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); |
739 | 739 | ||
740 | if (!__kfifo_get(session->cmdpool.queue, | 740 | if (!__kfifo_get(&session->cmdpool.queue, |
741 | (void*)&task, sizeof(void*))) | 741 | (void*)&task, sizeof(void*))) |
742 | return NULL; | 742 | return NULL; |
743 | } | 743 | } |
@@ -1567,7 +1567,7 @@ static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, | |||
1567 | { | 1567 | { |
1568 | struct iscsi_task *task; | 1568 | struct iscsi_task *task; |
1569 | 1569 | ||
1570 | if (!__kfifo_get(conn->session->cmdpool.queue, | 1570 | if (!__kfifo_get(&conn->session->cmdpool.queue, |
1571 | (void *) &task, sizeof(void *))) | 1571 | (void *) &task, sizeof(void *))) |
1572 | return NULL; | 1572 | return NULL; |
1573 | 1573 | ||
@@ -2461,12 +2461,7 @@ iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) | |||
2461 | if (q->pool == NULL) | 2461 | if (q->pool == NULL) |
2462 | return -ENOMEM; | 2462 | return -ENOMEM; |
2463 | 2463 | ||
2464 | q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), | 2464 | kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*), NULL); |
2465 | GFP_KERNEL, NULL); | ||
2466 | if (IS_ERR(q->queue)) { | ||
2467 | q->queue = NULL; | ||
2468 | goto enomem; | ||
2469 | } | ||
2470 | 2465 | ||
2471 | for (i = 0; i < max; i++) { | 2466 | for (i = 0; i < max; i++) { |
2472 | q->pool[i] = kzalloc(item_size, GFP_KERNEL); | 2467 | q->pool[i] = kzalloc(item_size, GFP_KERNEL); |
@@ -2474,7 +2469,7 @@ iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) | |||
2474 | q->max = i; | 2469 | q->max = i; |
2475 | goto enomem; | 2470 | goto enomem; |
2476 | } | 2471 | } |
2477 | __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); | 2472 | __kfifo_put(&q->queue, (void*)&q->pool[i], sizeof(void*)); |
2478 | } | 2473 | } |
2479 | 2474 | ||
2480 | if (items) { | 2475 | if (items) { |
@@ -2497,7 +2492,6 @@ void iscsi_pool_free(struct iscsi_pool *q) | |||
2497 | for (i = 0; i < q->max; i++) | 2492 | for (i = 0; i < q->max; i++) |
2498 | kfree(q->pool[i]); | 2493 | kfree(q->pool[i]); |
2499 | kfree(q->pool); | 2494 | kfree(q->pool); |
2500 | kfree(q->queue); | ||
2501 | } | 2495 | } |
2502 | EXPORT_SYMBOL_GPL(iscsi_pool_free); | 2496 | EXPORT_SYMBOL_GPL(iscsi_pool_free); |
2503 | 2497 | ||
@@ -2825,7 +2819,7 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, | |||
2825 | 2819 | ||
2826 | /* allocate login_task used for the login/text sequences */ | 2820 | /* allocate login_task used for the login/text sequences */ |
2827 | spin_lock_bh(&session->lock); | 2821 | spin_lock_bh(&session->lock); |
2828 | if (!__kfifo_get(session->cmdpool.queue, | 2822 | if (!__kfifo_get(&session->cmdpool.queue, |
2829 | (void*)&conn->login_task, | 2823 | (void*)&conn->login_task, |
2830 | sizeof(void*))) { | 2824 | sizeof(void*))) { |
2831 | spin_unlock_bh(&session->lock); | 2825 | spin_unlock_bh(&session->lock); |
@@ -2845,7 +2839,7 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, | |||
2845 | return cls_conn; | 2839 | return cls_conn; |
2846 | 2840 | ||
2847 | login_task_data_alloc_fail: | 2841 | login_task_data_alloc_fail: |
2848 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, | 2842 | __kfifo_put(&session->cmdpool.queue, (void*)&conn->login_task, |
2849 | sizeof(void*)); | 2843 | sizeof(void*)); |
2850 | login_task_alloc_fail: | 2844 | login_task_alloc_fail: |
2851 | iscsi_destroy_conn(cls_conn); | 2845 | iscsi_destroy_conn(cls_conn); |
@@ -2908,7 +2902,7 @@ void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) | |||
2908 | free_pages((unsigned long) conn->data, | 2902 | free_pages((unsigned long) conn->data, |
2909 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); | 2903 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); |
2910 | kfree(conn->persistent_address); | 2904 | kfree(conn->persistent_address); |
2911 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, | 2905 | __kfifo_put(&session->cmdpool.queue, (void*)&conn->login_task, |
2912 | sizeof(void*)); | 2906 | sizeof(void*)); |
2913 | if (session->leadconn == conn) | 2907 | if (session->leadconn == conn) |
2914 | session->leadconn = NULL; | 2908 | session->leadconn = NULL; |
diff --git a/drivers/scsi/libiscsi_tcp.c b/drivers/scsi/libiscsi_tcp.c index ca25ee5190b0..a83ee56a185e 100644 --- a/drivers/scsi/libiscsi_tcp.c +++ b/drivers/scsi/libiscsi_tcp.c | |||
@@ -445,15 +445,15 @@ void iscsi_tcp_cleanup_task(struct iscsi_task *task) | |||
445 | return; | 445 | return; |
446 | 446 | ||
447 | /* flush task's r2t queues */ | 447 | /* flush task's r2t queues */ |
448 | while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) { | 448 | while (__kfifo_get(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) { |
449 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | 449 | __kfifo_put(&tcp_task->r2tpool.queue, (void*)&r2t, |
450 | sizeof(void*)); | 450 | sizeof(void*)); |
451 | ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n"); | 451 | ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n"); |
452 | } | 452 | } |
453 | 453 | ||
454 | r2t = tcp_task->r2t; | 454 | r2t = tcp_task->r2t; |
455 | if (r2t != NULL) { | 455 | if (r2t != NULL) { |
456 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | 456 | __kfifo_put(&tcp_task->r2tpool.queue, (void*)&r2t, |
457 | sizeof(void*)); | 457 | sizeof(void*)); |
458 | tcp_task->r2t = NULL; | 458 | tcp_task->r2t = NULL; |
459 | } | 459 | } |
@@ -541,7 +541,7 @@ static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task) | |||
541 | return 0; | 541 | return 0; |
542 | } | 542 | } |
543 | 543 | ||
544 | rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*)); | 544 | rc = __kfifo_get(&tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*)); |
545 | if (!rc) { | 545 | if (!rc) { |
546 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. " | 546 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. " |
547 | "Target has sent more R2Ts than it " | 547 | "Target has sent more R2Ts than it " |
@@ -554,7 +554,7 @@ static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task) | |||
554 | if (r2t->data_length == 0) { | 554 | if (r2t->data_length == 0) { |
555 | iscsi_conn_printk(KERN_ERR, conn, | 555 | iscsi_conn_printk(KERN_ERR, conn, |
556 | "invalid R2T with zero data len\n"); | 556 | "invalid R2T with zero data len\n"); |
557 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | 557 | __kfifo_put(&tcp_task->r2tpool.queue, (void*)&r2t, |
558 | sizeof(void*)); | 558 | sizeof(void*)); |
559 | return ISCSI_ERR_DATALEN; | 559 | return ISCSI_ERR_DATALEN; |
560 | } | 560 | } |
@@ -570,7 +570,7 @@ static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task) | |||
570 | "invalid R2T with data len %u at offset %u " | 570 | "invalid R2T with data len %u at offset %u " |
571 | "and total length %d\n", r2t->data_length, | 571 | "and total length %d\n", r2t->data_length, |
572 | r2t->data_offset, scsi_out(task->sc)->length); | 572 | r2t->data_offset, scsi_out(task->sc)->length); |
573 | __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t, | 573 | __kfifo_put(&tcp_task->r2tpool.queue, (void*)&r2t, |
574 | sizeof(void*)); | 574 | sizeof(void*)); |
575 | return ISCSI_ERR_DATALEN; | 575 | return ISCSI_ERR_DATALEN; |
576 | } | 576 | } |
@@ -580,7 +580,7 @@ static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task) | |||
580 | r2t->sent = 0; | 580 | r2t->sent = 0; |
581 | 581 | ||
582 | tcp_task->exp_datasn = r2tsn + 1; | 582 | tcp_task->exp_datasn = r2tsn + 1; |
583 | __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*)); | 583 | __kfifo_put(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*)); |
584 | conn->r2t_pdus_cnt++; | 584 | conn->r2t_pdus_cnt++; |
585 | 585 | ||
586 | iscsi_requeue_task(task); | 586 | iscsi_requeue_task(task); |
@@ -951,7 +951,7 @@ int iscsi_tcp_task_init(struct iscsi_task *task) | |||
951 | return conn->session->tt->init_pdu(task, 0, task->data_count); | 951 | return conn->session->tt->init_pdu(task, 0, task->data_count); |
952 | } | 952 | } |
953 | 953 | ||
954 | BUG_ON(__kfifo_len(tcp_task->r2tqueue)); | 954 | BUG_ON(__kfifo_len(&tcp_task->r2tqueue)); |
955 | tcp_task->exp_datasn = 0; | 955 | tcp_task->exp_datasn = 0; |
956 | 956 | ||
957 | /* Prepare PDU, optionally w/ immediate data */ | 957 | /* Prepare PDU, optionally w/ immediate data */ |
@@ -982,7 +982,7 @@ static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task) | |||
982 | if (r2t->data_length <= r2t->sent) { | 982 | if (r2t->data_length <= r2t->sent) { |
983 | ISCSI_DBG_TCP(task->conn, | 983 | ISCSI_DBG_TCP(task->conn, |
984 | " done with r2t %p\n", r2t); | 984 | " done with r2t %p\n", r2t); |
985 | __kfifo_put(tcp_task->r2tpool.queue, | 985 | __kfifo_put(&tcp_task->r2tpool.queue, |
986 | (void *)&tcp_task->r2t, | 986 | (void *)&tcp_task->r2t, |
987 | sizeof(void *)); | 987 | sizeof(void *)); |
988 | tcp_task->r2t = r2t = NULL; | 988 | tcp_task->r2t = r2t = NULL; |
@@ -990,7 +990,7 @@ static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task) | |||
990 | } | 990 | } |
991 | 991 | ||
992 | if (r2t == NULL) { | 992 | if (r2t == NULL) { |
993 | __kfifo_get(tcp_task->r2tqueue, | 993 | __kfifo_get(&tcp_task->r2tqueue, |
994 | (void *)&tcp_task->r2t, sizeof(void *)); | 994 | (void *)&tcp_task->r2t, sizeof(void *)); |
995 | r2t = tcp_task->r2t; | 995 | r2t = tcp_task->r2t; |
996 | } | 996 | } |
@@ -1127,9 +1127,8 @@ int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session) | |||
1127 | } | 1127 | } |
1128 | 1128 | ||
1129 | /* R2T xmit queue */ | 1129 | /* R2T xmit queue */ |
1130 | tcp_task->r2tqueue = kfifo_alloc( | 1130 | if (kfifo_alloc(&tcp_task->r2tqueue, |
1131 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); | 1131 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL)) { |
1132 | if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) { | ||
1133 | iscsi_pool_free(&tcp_task->r2tpool); | 1132 | iscsi_pool_free(&tcp_task->r2tpool); |
1134 | goto r2t_alloc_fail; | 1133 | goto r2t_alloc_fail; |
1135 | } | 1134 | } |
@@ -1142,7 +1141,7 @@ r2t_alloc_fail: | |||
1142 | struct iscsi_task *task = session->cmds[i]; | 1141 | struct iscsi_task *task = session->cmds[i]; |
1143 | struct iscsi_tcp_task *tcp_task = task->dd_data; | 1142 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
1144 | 1143 | ||
1145 | kfifo_free(tcp_task->r2tqueue); | 1144 | kfifo_free(&tcp_task->r2tqueue); |
1146 | iscsi_pool_free(&tcp_task->r2tpool); | 1145 | iscsi_pool_free(&tcp_task->r2tpool); |
1147 | } | 1146 | } |
1148 | return -ENOMEM; | 1147 | return -ENOMEM; |
@@ -1157,7 +1156,7 @@ void iscsi_tcp_r2tpool_free(struct iscsi_session *session) | |||
1157 | struct iscsi_task *task = session->cmds[i]; | 1156 | struct iscsi_task *task = session->cmds[i]; |
1158 | struct iscsi_tcp_task *tcp_task = task->dd_data; | 1157 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
1159 | 1158 | ||
1160 | kfifo_free(tcp_task->r2tqueue); | 1159 | kfifo_free(&tcp_task->r2tqueue); |
1161 | iscsi_pool_free(&tcp_task->r2tpool); | 1160 | iscsi_pool_free(&tcp_task->r2tpool); |
1162 | } | 1161 | } |
1163 | } | 1162 | } |
diff --git a/drivers/scsi/libsrp.c b/drivers/scsi/libsrp.c index 9ad38e81e343..b1b5e51ca8e3 100644 --- a/drivers/scsi/libsrp.c +++ b/drivers/scsi/libsrp.c | |||
@@ -58,19 +58,16 @@ static int srp_iu_pool_alloc(struct srp_queue *q, size_t max, | |||
58 | goto free_pool; | 58 | goto free_pool; |
59 | 59 | ||
60 | spin_lock_init(&q->lock); | 60 | spin_lock_init(&q->lock); |
61 | q->queue = kfifo_init((void *) q->pool, max * sizeof(void *), | 61 | kfifo_init(&q->queue, (void *) q->pool, max * sizeof(void *), |
62 | GFP_KERNEL, &q->lock); | 62 | &q->lock); |
63 | if (IS_ERR(q->queue)) | ||
64 | goto free_item; | ||
65 | 63 | ||
66 | for (i = 0, iue = q->items; i < max; i++) { | 64 | for (i = 0, iue = q->items; i < max; i++) { |
67 | __kfifo_put(q->queue, (void *) &iue, sizeof(void *)); | 65 | __kfifo_put(&q->queue, (void *) &iue, sizeof(void *)); |
68 | iue->sbuf = ring[i]; | 66 | iue->sbuf = ring[i]; |
69 | iue++; | 67 | iue++; |
70 | } | 68 | } |
71 | return 0; | 69 | return 0; |
72 | 70 | ||
73 | free_item: | ||
74 | kfree(q->items); | 71 | kfree(q->items); |
75 | free_pool: | 72 | free_pool: |
76 | kfree(q->pool); | 73 | kfree(q->pool); |
@@ -167,7 +164,7 @@ struct iu_entry *srp_iu_get(struct srp_target *target) | |||
167 | { | 164 | { |
168 | struct iu_entry *iue = NULL; | 165 | struct iu_entry *iue = NULL; |
169 | 166 | ||
170 | kfifo_get(target->iu_queue.queue, (void *) &iue, sizeof(void *)); | 167 | kfifo_get(&target->iu_queue.queue, (void *) &iue, sizeof(void *)); |
171 | if (!iue) | 168 | if (!iue) |
172 | return iue; | 169 | return iue; |
173 | iue->target = target; | 170 | iue->target = target; |
@@ -179,7 +176,7 @@ EXPORT_SYMBOL_GPL(srp_iu_get); | |||
179 | 176 | ||
180 | void srp_iu_put(struct iu_entry *iue) | 177 | void srp_iu_put(struct iu_entry *iue) |
181 | { | 178 | { |
182 | kfifo_put(iue->target->iu_queue.queue, (void *) &iue, sizeof(void *)); | 179 | kfifo_put(&iue->target->iu_queue.queue, (void *) &iue, sizeof(void *)); |
183 | } | 180 | } |
184 | EXPORT_SYMBOL_GPL(srp_iu_put); | 181 | EXPORT_SYMBOL_GPL(srp_iu_put); |
185 | 182 | ||