All of lore.kernel.org
 help / color / mirror / Atom feed
From: NeilBrown <neilb@suse.com>
To: lustre-devel@lists.lustre.org
Subject: [lustre-devel] [PATCH 03/21] lustre: obdclass: use list_sort() to sort a list.
Date: Mon, 11 Feb 2019 12:05:16 +1100	[thread overview]
Message-ID: <87ef8fgmnn.fsf@notabene.neil.brown.name> (raw)
In-Reply-To: <alpine.LFD.2.21.1902110037220.24975@casper.infradead.org>

On Mon, Feb 11 2019, James Simmons wrote:

>> Rather than a bespoke bubble-sort, use list_sort() to
>> sort this linked list.
>
> Reviewed-by: James Simmons <jsimmons@infradead.org>
>  
>> Signed-off-by: NeilBrown <neilb@suse.com>
>> ---
>>  drivers/staging/lustre/lustre/obdclass/cl_io.c |   51 +++++-------------------
>>  1 file changed, 10 insertions(+), 41 deletions(-)
>> 
>> diff --git a/drivers/staging/lustre/lustre/obdclass/cl_io.c b/drivers/staging/lustre/lustre/obdclass/cl_io.c
>> index beac7e8bc92a..7bf02350f19d 100644
>> --- a/drivers/staging/lustre/lustre/obdclass/cl_io.c
>> +++ b/drivers/staging/lustre/lustre/obdclass/cl_io.c
>> @@ -42,6 +42,7 @@
>>  #include <obd_support.h>
>>  #include <lustre_fid.h>
>>  #include <linux/list.h>
>> +#include <linux/list_sort.h>
>>  #include <linux/sched.h>
>>  #include <cl_object.h>
>>  #include "cl_internal.h"
>> @@ -213,9 +214,15 @@ int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
>>  }
>>  EXPORT_SYMBOL(cl_io_rw_init);
>>  
>> -static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
>> -			      const struct cl_lock_descr *d1)
>> +static int cl_lock_descr_cmp(void *priv,
>> +			     struct list_head *a, struct list_head *b)
>>  {
>> +	const struct cl_io_lock_link *l0 = list_entry(a, struct cl_io_lock_link, cill_linkage);
>> +	const struct cl_io_lock_link *l1 = list_entry(b, struct cl_io_lock_link, cill_linkage);
>> +
>> +	const struct cl_lock_descr *d0 = &l0->cill_descr;
>> +	const struct cl_lock_descr *d1 = &l1->cill_descr;
>> +
>>  	return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
>>  			  lu_object_fid(&d1->cld_obj->co_lu));
>>  }
>> @@ -225,45 +232,7 @@ static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
>>   */
>>  static void cl_io_locks_sort(struct cl_io *io)
>>  {
>> -	int done = 0;
>> -
>> -	/* hidden treasure: bubble sort for now. */
>> -	do {
>> -		struct cl_io_lock_link *curr;
>> -		struct cl_io_lock_link *prev;
>> -		struct cl_io_lock_link *temp;
>> -
>> -		done = 1;
>> -		prev = NULL;
>> -
>> -		list_for_each_entry_safe(curr, temp,
>> -					 &io->ci_lockset.cls_todo,
>> -					 cill_linkage) {
>> -			if (prev) {
>> -				switch (cl_lock_descr_sort(&prev->cill_descr,
>> -							   &curr->cill_descr)) {
>> -				case 0:
>> -					/*
>> -					 * IMPOSSIBLE: Identical locks are
>> -					 *	     already removed at
>> -					 *	     this point.
>> -					 */
>> -				default:
>> -					LBUG();
>> -				case 1:
>> -					list_move_tail(&curr->cill_linkage,
>> -						       &prev->cill_linkage);
>> -					done = 0;
>> -					continue; /* don't change prev: it's
>> -						   * still "previous"
>> -						   */
>> -				case -1: /* already in order */
>> -					break;
>> -				}
>> -			}
>> -			prev = curr;
>> -		}
>> -	} while (!done);
>> +	list_sort(NULL, &io->ci_lockset.cls_todo, cl_lock_descr_cmp);
>>  }
>
> While this fine cl_io_locks_sort() is discussed in one comment in 
> cl_object.h and used once in cl_io.c. We could remove this one line
> function completely and update the comment in cl_object.h.
>

Excellent idea.  New patch is below.
Thanks,
NeilBrown

From: NeilBrown <neilb@suse.com>
Subject: [PATCH] lustre: obdclass: use list_sort() to sort a list.

Rather than a bespoke bubble-sort, use list_sort() to
sort this linked list.

As this would become a 1-line function that is only called once,
call list_sort() directly from the one call site.

Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Signed-off-by: NeilBrown <neilb@suse.com>
---
 drivers/staging/lustre/lustre/obdclass/cl_io.c | 63 ++++++--------------------
 1 file changed, 14 insertions(+), 49 deletions(-)

diff --git a/drivers/staging/lustre/lustre/obdclass/cl_io.c b/drivers/staging/lustre/lustre/obdclass/cl_io.c
index 5191fba8bbc0..c8a99b61ecd2 100644
--- a/drivers/staging/lustre/lustre/obdclass/cl_io.c
+++ b/drivers/staging/lustre/lustre/obdclass/cl_io.c
@@ -42,6 +42,7 @@
 #include <obd_support.h>
 #include <lustre_fid.h>
 #include <linux/list.h>
+#include <linux/list_sort.h>
 #include <linux/sched.h>
 #include <cl_object.h>
 #include "cl_internal.h"
@@ -213,57 +214,17 @@ int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
 }
 EXPORT_SYMBOL(cl_io_rw_init);
 
-static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
-			      const struct cl_lock_descr *d1)
+static int cl_lock_descr_cmp(void *priv,
+			     struct list_head *a, struct list_head *b)
 {
-	return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
-			  lu_object_fid(&d1->cld_obj->co_lu));
-}
+	const struct cl_io_lock_link *l0 = list_entry(a, struct cl_io_lock_link, cill_linkage);
+	const struct cl_io_lock_link *l1 = list_entry(b, struct cl_io_lock_link, cill_linkage);
 
-/*
- * Sort locks in lexicographical order of their (fid, start-offset) pairs.
- */
-static void cl_io_locks_sort(struct cl_io *io)
-{
-	int done = 0;
+	const struct cl_lock_descr *d0 = &l0->cill_descr;
+	const struct cl_lock_descr *d1 = &l1->cill_descr;
 
-	/* hidden treasure: bubble sort for now. */
-	do {
-		struct cl_io_lock_link *curr;
-		struct cl_io_lock_link *prev;
-		struct cl_io_lock_link *temp;
-
-		done = 1;
-		prev = NULL;
-
-		list_for_each_entry_safe(curr, temp,
-					 &io->ci_lockset.cls_todo,
-					 cill_linkage) {
-			if (prev) {
-				switch (cl_lock_descr_sort(&prev->cill_descr,
-							   &curr->cill_descr)) {
-				case 0:
-					/*
-					 * IMPOSSIBLE: Identical locks are
-					 *	     already removed at
-					 *	     this point.
-					 */
-				default:
-					LBUG();
-				case 1:
-					list_move_tail(&curr->cill_linkage,
-						       &prev->cill_linkage);
-					done = 0;
-					continue; /* don't change prev: it's
-						   * still "previous"
-						   */
-				case -1: /* already in order */
-					break;
-				}
-			}
-			prev = curr;
-		}
-	} while (!done);
+	return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
+			  lu_object_fid(&d1->cld_obj->co_lu));
 }
 
 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
@@ -347,7 +308,11 @@ int cl_io_lock(const struct lu_env *env, struct cl_io *io)
 	}
 
 	if (result == 0) {
-		cl_io_locks_sort(io);
+		/*
+		 * Sort locks in lexicographical order of their (fid,
+		 * start-offset) pairs to avoid deadlocks.
+		 */
+		list_sort(NULL, &io->ci_lockset.cls_todo, cl_lock_descr_cmp);
 		result = cl_lockset_lock(env, io, &io->ci_lockset);
 	}
 	if (result != 0)
-- 
2.14.0.rc0.dirty

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20190211/b7a2ccca/attachment-0001.sig>

  reply	other threads:[~2019-02-11  1:05 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-07  0:03 [lustre-devel] [PATCH 00/21] lustre: Assorted cleanups for obdclass NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 04/21] lustre: use list*entry macros in place of container_of() NeilBrown
2019-02-08  0:25   ` Andreas Dilger
2019-02-11  1:32   ` James Simmons
2019-02-11  3:14     ` NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 05/21] lustre: use list_first_entry() in lustre subdirectory NeilBrown
2019-02-08  0:31   ` Andreas Dilger
2019-02-11  0:13     ` NeilBrown
2019-02-11  1:45   ` James Simmons
2019-02-11  3:08     ` NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 03/21] lustre: obdclass: use list_sort() to sort a list NeilBrown
2019-02-08  0:13   ` Andreas Dilger
2019-02-11  0:39   ` James Simmons
2019-02-11  1:05     ` NeilBrown [this message]
2019-02-07  0:03 ` [lustre-devel] [PATCH 02/21] lustre: obd_class: remove csi_barrier from struct cl_sync_io NeilBrown
2019-02-08  0:09   ` Andreas Dilger
2019-02-11  0:24     ` NeilBrown
2019-02-11  0:34   ` James Simmons
2019-02-11  1:09     ` NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 06/21] lustre: use list_first_entry() in lnet/lnet subdirectory NeilBrown
2019-02-08  0:44   ` Andreas Dilger
2019-02-11  1:46   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 01/21] lustre: obdclass: discard csi_end_io NeilBrown
2019-02-07  0:20   ` Andreas Dilger
2019-02-11  0:19   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 10/21] lustre: obdclass: use cl_object_for_each where appropriate NeilBrown
2019-02-08  1:10   ` Andreas Dilger
2019-02-11  0:42     ` NeilBrown
2019-02-11  4:19       ` James Simmons
2019-02-15 18:15       ` Andreas Dilger
2019-02-11  1:57   ` James Simmons
2019-02-11  3:19     ` NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 18/21] lustre: move debug.c from obdclass to obdecho NeilBrown
2019-02-08  6:02   ` Andreas Dilger
2019-02-11  4:17   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 11/21] lustre: cl_object: remove vestigial debugging NeilBrown
2019-02-08  1:31   ` Andreas Dilger
2019-02-11  0:48     ` NeilBrown
2019-02-11  2:04   ` James Simmons
2019-02-11  3:25     ` NeilBrown
2019-02-12  5:19       ` James Simmons
2019-02-12 13:56         ` Patrick Farrell
2019-02-12 22:12         ` NeilBrown
2019-02-13  0:19           ` James Simmons
2019-02-13  0:29             ` NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 09/21] lustre: use list_last_entry() throughout NeilBrown
2019-02-08  1:07   ` Andreas Dilger
2019-02-11  1:48   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 21/21] lustre: make exp_refcount in obd_export a refcount_t NeilBrown
2019-02-08  7:07   ` Andreas Dilger
2019-02-11  4:18   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 12/21] lustre: cl_page.c: remove PINVRNT() NeilBrown
2019-02-08  5:43   ` Andreas Dilger
2019-02-11  4:01   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 15/21] lustre: obdclass: char obd_ioctl_getdata type NeilBrown
2019-02-08  5:56   ` Andreas Dilger
2019-02-11  0:52     ` NeilBrown
2019-02-11  4:03   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 16/21] lustre: obdclass: normalize a switch statement NeilBrown
2019-02-08  5:57   ` Andreas Dilger
2019-02-11  4:03   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 07/21] lustre: use list_first_entry() in lnet/klnds subdirectory NeilBrown
2019-02-08  0:59   ` Andreas Dilger
2019-02-11  0:34     ` NeilBrown
2019-02-11  1:47   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 20/21] lustre: obdclass: fix module load locking NeilBrown
2019-02-13  1:53   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 14/21] lustre: make ccc_users in cl_client_cache a refcount_t NeilBrown
2019-02-08  5:46   ` Andreas Dilger
2019-02-11  4:01   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 19/21] lustre: obdclass: avoid races in class_register_type() NeilBrown
2019-02-08  6:41   ` Andreas Dilger
2019-02-11  0:58     ` NeilBrown
2019-02-12  5:03   ` James Simmons
2019-02-14  3:43     ` NeilBrown
2019-02-07  0:03 ` [lustre-devel] [PATCH 08/21] lustre: use list_first_entry() throughout NeilBrown
2019-02-08  1:06   ` Andreas Dilger
2019-02-11  1:48   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 13/21] lustre: make cp_ref in cl_page a refcount_t NeilBrown
2019-02-08  5:45   ` Andreas Dilger
2019-02-11  4:00   ` James Simmons
2019-02-07  0:03 ` [lustre-devel] [PATCH 17/21] lustre: obdclass: result of try_module_get() should not be ignored NeilBrown
2019-02-08  5:58   ` Andreas Dilger
2019-02-11  4:22   ` James Simmons
2019-02-11  5:01     ` NeilBrown
2019-02-11  5:09       ` [lustre-devel] [PATCH] lustre: don't manage module refs in obd_class_open/close NeilBrown
2019-02-12  4:17         ` James Simmons

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ef8fgmnn.fsf@notabene.neil.brown.name \
    --to=neilb@suse.com \
    --cc=lustre-devel@lists.lustre.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.