[openib-general] [PATCH] SDP: Use linux/list.h in sdp_buff.[ch]

Tom Duffy thomas.duffy.99 at alumni.brown.edu
Tue Aug 30 13:11:38 PDT 2005


On Tue, 2005-08-30 at 09:10 +0300, Michael S. Tsirkin wrote: 
> Quoting r. Tom Duffy <thomas.duffy.99 at alumni.brown.edu>:
> > I just got back from vacation and I am still waiting for a machine so  
> > I can setup a rudimentary IB network at home to test my code.  I have  
> > a patch that converts sdp_buff.[ch] to use linux/list.h (glad you  
> > didn't decide to work on that), but I want to test it before  
> > submitting to the list (it compiles!).
> 
> I actually need that ASAP so that I can finally use
> list_for_each and such instead of the stupid wrappers in
> sdp_buff for my zcopy code.
> Tom, could you please post the patch?

Note: This patch is *UNTESTED*.  Please verify before committing.

Signed-off-by: Tom Duffy <thomas.duffy.99 at alumni.brown.edu>

Index: drivers/infiniband/ulp/sdp/sdp_buff.c
===================================================================
--- drivers/infiniband/ulp/sdp/sdp_buff.c	(revision 3240)
+++ drivers/infiniband/ulp/sdp/sdp_buff.c	(working copy)
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright (c) 2005 Tom Duffy (thomas.duffy.99 at alumni.brown.edu)
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -52,31 +53,18 @@ static inline struct sdpc_buff *do_buff_
 {
 	struct sdpc_buff *buff;
 
-	if (!pool->head)
+	if (list_empty(&pool->head))
 		return NULL;
 
 	if (fifo)
-		buff = pool->head;
+		buff = list_entry(pool->head.next, struct sdpc_buff, list);
 	else
-		buff = pool->head->prev;
+		buff = list_entry(pool->head.prev, struct sdpc_buff, list);
 
 	if (!test_func || !test_func(buff, usr_arg)) {
-		if (buff->next == buff && buff->prev == buff)
-			pool->head = NULL;
-		else {
-			buff->next->prev = buff->prev;
-			buff->prev->next = buff->next;
-
-			pool->head = buff->next;
-		}
-
+		list_del(&buff->list);
 		pool->size--;
-
-		buff->next = NULL;
-		buff->prev = NULL;
-		buff->pool = NULL;
-	}
-	else
+	} else
 		buff = NULL;
 
 	return buff;
@@ -91,20 +79,10 @@ static inline void do_buff_q_put(struct 
 	/* fifo: false == tail, true == head */
 	BUG_ON(buff->pool);
 
-	if (!pool->head) {
-		buff->next = buff;
-		buff->prev = buff;
-		pool->head = buff;
-	} else {
-		buff->next = pool->head;
-		buff->prev = pool->head->prev;
-
-		buff->next->prev = buff;
-		buff->prev->next = buff;
-
-		if (fifo)
-			pool->head = buff;
-	}
+	if (fifo)
+		list_add(&buff->list, &pool->head);
+	else
+		list_add_tail(&buff->list, &pool->head);
 
 	pool->size++;
 	buff->pool = pool;
@@ -116,10 +94,12 @@ static inline void do_buff_q_put(struct 
 static inline struct sdpc_buff *sdp_buff_q_look(struct sdpc_buff_q *pool,
 						int fifo)
 {
-	if (!pool->head || fifo)
-		return pool->head;
+	if (list_empty(&pool->head))
+		return NULL;
+	if (fifo)
+		return list_entry(pool->head.next, struct sdpc_buff, list);
 	else
-		return pool->head->prev;
+		return list_entry(pool->head.prev, struct sdpc_buff, list);
 }
 
 /*
@@ -128,28 +108,11 @@ static inline struct sdpc_buff *sdp_buff
 static inline void do_buff_q_remove(struct sdpc_buff_q *pool,
 				    struct sdpc_buff *buff)
 {
-	struct sdpc_buff *prev;
-	struct sdpc_buff *next;
-
 	BUG_ON(pool != buff->pool);
 
-	if (buff->next == buff && buff->prev == buff)
-		pool->head = NULL;
-	else {
-		next = buff->next;
-		prev = buff->prev;
-		next->prev = prev;
-		prev->next = next;
-
-		if (pool->head == buff)
-			pool->head = next;
-	}
-
+	list_del(&buff->list);
 	pool->size--;
-
 	buff->pool = NULL;
-	buff->next = NULL;
-	buff->prev = NULL;
 }
 
 /*
@@ -157,7 +120,7 @@ static inline void do_buff_q_remove(stru
  */
 void sdp_buff_q_init(struct sdpc_buff_q *pool)
 {
-	pool->head = NULL;
+	INIT_LIST_HEAD(&pool->head);
 	pool->size = 0;
 }
 
@@ -201,28 +164,24 @@ struct sdpc_buff *sdp_buff_q_fetch(struc
 					       void *arg),
 				   void *usr_arg)
 {
-	struct sdpc_buff *buff;
+	struct sdpc_buff *buff, *tmp;
 	int result = 0;
-	int counter;
 
 	/*
 	 * check to see if there is anything to traverse.
 	 */
-	if (pool->head)
+	list_for_each_entry_safe(buff, tmp, &pool->head, list) {
 		/*
-		 * lock to prevent corruption of table
+		 * XXX lock to prevent corruption of table
 		 */
-		for (counter = 0, buff = pool->head;
-		     counter < pool->size; counter++, buff = buff->next) {
-			result = test(buff, usr_arg);
-			if (result > 0) {
-				do_buff_q_remove(pool, buff);
-				return buff;
-			}
-
-			if (result < 0)
-				break;
+		result = test(buff, usr_arg);
+		if (result > 0) {
+			do_buff_q_remove(pool, buff);
+			return buff;
 		}
+		if (result < 0)
+			break;
+	}
 
 	return NULL;
 }
@@ -237,22 +196,18 @@ int sdp_buff_q_trav_head(struct sdpc_buf
 {
 	struct sdpc_buff *buff;
 	int result = 0;
-	int counter;
 
 	/*
 	 * check to see if there is anything to traverse.
 	 */
-	if (pool->head)
+	list_for_each_entry(buff, &pool->head, list) {
 		/*
-		 * lock to prevent corruption of table
+		 * XXX lock to prevent corruption of table
 		 */
-		for (counter = 0, buff = pool->head;
-		     counter < pool->size; counter++, buff = buff->next) {
-
-			result = trav_func(buff, usr_arg);
-			if (result < 0)
-				break;
-		}
+		result = trav_func(buff, usr_arg);
+		if (result < 0)
+			break;
+	}
 
 	return result;
 }
@@ -398,7 +353,7 @@ static int sdp_buff_pool_alloc(struct sd
 		m_pool->buff_cur++;
 	}
 
-	if (!main_pool->pool.head) {
+	if (list_empty(&main_pool->pool.head)) {
 		sdp_warn("Failed to allocate any buffers. <%d:%d:%d>",
 			 total, m_pool->buff_cur, m_pool->alloc_inc);
 
@@ -545,7 +500,7 @@ struct sdpc_buff *sdp_buff_pool_get(void
 	 */
 	spin_lock_irqsave(&main_pool->lock, flags);
 
-	if (!main_pool->pool.head) {
+	if (list_empty(&main_pool->pool.head)) {
 		result = sdp_buff_pool_alloc(main_pool);
 		if (result < 0) {
 			sdp_warn("Error <%d> allocating buffers.", result);
@@ -554,23 +509,12 @@ struct sdpc_buff *sdp_buff_pool_get(void
 		}
 	}
 
-	buff = main_pool->pool.head;
-
-	if (buff->next == buff)
-		main_pool->pool.head = NULL;
-	else {
-		buff->next->prev = buff->prev;
-		buff->prev->next = buff->next;
-
-		main_pool->pool.head = buff->next;
-	}
-
+	buff = list_entry(main_pool->pool.head.next, struct sdpc_buff, list);
+	list_del(&buff->list);
 	main_pool->pool.size--;
 
 	spin_unlock_irqrestore(&main_pool->lock, flags);
 
-	buff->next = NULL;
-	buff->prev = NULL;
 	buff->pool = NULL;
 	/*
 	 * main pool specific reset
@@ -596,7 +540,6 @@ void sdp_buff_pool_put(struct sdpc_buff 
 		return;
 
 	BUG_ON(buff->pool);
-	BUG_ON(buff->next || buff->prev);
 	/*
 	 * reset pointers
 	 */
@@ -606,17 +549,7 @@ void sdp_buff_pool_put(struct sdpc_buff 
 
 	spin_lock_irqsave(&main_pool->lock, flags);
 
-	if (!main_pool->pool.head) {
-		buff->next = buff;
-		buff->prev = buff;
-		main_pool->pool.head = buff;
-	} else {
-		buff->next = main_pool->pool.head;
-		buff->prev = main_pool->pool.head->prev;
-
-		buff->next->prev = buff;
-		buff->prev->next = buff;
-	}
+	list_add(&buff->list, &main_pool->pool.head);
 
 	main_pool->pool.size++;
 
@@ -634,16 +567,8 @@ void sdp_buff_pool_chain_link(struct sdp
 	buff->tail = buff->head;
 	buff->pool = &main_pool->pool;
 
-	if (!head) {
-		buff->next = buff;
-		buff->prev = buff;
-	} else {
-		buff->next = head;
-		buff->prev = head->prev;
-
-		buff->next->prev = buff;
-		buff->prev->next = buff;
-	}
+	if (head)
+		__list_splice(&buff->list, &head->list);
 }
 
 /*
@@ -652,8 +577,6 @@ void sdp_buff_pool_chain_link(struct sdp
 void sdp_buff_pool_chain_put(struct sdpc_buff *buff, u32 count)
 {
 	unsigned long flags;
-	struct sdpc_buff *next;
-	struct sdpc_buff *prev;
 	/*
 	 * return an entire Link of buffers to the queue, this save on
 	 * lock contention for the buffer pool, for code paths where
@@ -665,18 +588,7 @@ void sdp_buff_pool_chain_put(struct sdpc
 
 	spin_lock_irqsave(&main_pool->lock, flags);
 
-	if (!main_pool->pool.head)
-		main_pool->pool.head = buff;
-	else {
-		prev = buff->prev;
-		next = main_pool->pool.head->next;
-
-		buff->prev = main_pool->pool.head;
-		main_pool->pool.head->next = buff;
-
-		prev->next = next;
-		next->prev = prev;
-	}
+	list_add(&main_pool->pool.head, &buff->list);
 
 	main_pool->pool.size += count;
 
Index: drivers/infiniband/ulp/sdp/sdp_buff.h
===================================================================
--- drivers/infiniband/ulp/sdp/sdp_buff.h	(revision 3240)
+++ drivers/infiniband/ulp/sdp/sdp_buff.h	(working copy)
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright (c) 2005 Tom Duffy (thomas.duffy.99 at alumni.brown.edu)
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -36,18 +37,19 @@
 #ifndef _SDP_BUFF_H
 #define _SDP_BUFF_H
 
+#include <linux/list.h>
+
 #include "sdp_queue.h"
 /*
  * structures
  */
 struct sdpc_buff_q {
-	struct sdpc_buff *head; /* double linked list of buffers */
+	struct list_head head; /* double linked list of buffers */
 	u32 size;               /* number of buffers in the pool */
 };
 
 struct sdpc_buff {
-	struct sdpc_buff   *next;
-	struct sdpc_buff   *prev;
+	struct list_head    list;
 	u32                 type; /* element type. (for generic queue) */
 	struct sdpc_buff_q *pool; /* pool currently holding this buffer. */
 	void (*release)(struct sdpc_buff *buff); /* release the object */







More information about the general mailing list