ocfs2-devel.oss.oracle.com archive mirror
 help / color / mirror / Atom feed
From: Julia Lawall <julia@diku.dk>
To: Joel Becker <Joel.Becker@oracle.com>
Cc: Mark Fasheh <mfasheh@suse.com>,
	ocfs2-devel@oss.oracle.com, linux-kernel@vger.kernel.org,
	kernel-janitors@vger.kernel.org,
	Sunil Mushran <sunil.mushran@oracle.com>
Subject: [Ocfs2-devel] [PATCH 1/2] fs/ocfs2/dlm: Eliminate update of list_for_each_entry loop cursor
Date: Thu, 12 Aug 2010 07:46:13 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.64.1008120735200.16027@ask.diku.dk> (raw)
In-Reply-To: <20100812000355.GA7195@mail.oracle.com>

On Wed, 11 Aug 2010, Joel Becker wrote:

> On Sat, Aug 07, 2010 at 11:09:13AM +0200, Julia Lawall wrote:
> > From: Julia Lawall <julia@diku.dk>
> > 
> > list_for_each_entry uses its first argument to move from one element to the
> > next, so modifying it can break the iteration.
> 
> 	Thanks for catching the bug.  It was introduced by 800deef3
> [ocfs2: use list_for_each_entry where benefical].  I blame Christoph.
> 
> > diff --git a/fs/ocfs2/dlm/dlmrecovery.c b/fs/ocfs2/dlm/dlmrecovery.c
> > index 9dfaac7..7084a11 100644
> > --- a/fs/ocfs2/dlm/dlmrecovery.c
> > +++ b/fs/ocfs2/dlm/dlmrecovery.c
> > @@ -1792,10 +1792,10 @@ static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
> >  			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
> >  				tmpq = dlm_list_idx_to_ptr(res, j);
> >  				list_for_each_entry(lock, tmpq, list) {
> > -					if (lock->ml.cookie != ml->cookie)
> > +					if (lock->ml.cookie != ml->cookie) {
> >  						lock = NULL;
> > -					else
> >  						break;
> > +					}
> >  				}
> >  				if (lock)
> >  					break;
> 
> 	However, this is not the correct solution.  The goal of the
> original code, which used to use list_for_each(), was to leave lock
> non-NULL if the cookie was found.  Your version merely exits the loop on
> the first non-matching entry, always leaving lock==NULL if there is a
> non-matching entry.
> 	One possible solution is to return the original code:
> 
> --8<-----------------------------------------------------------------
> @@ -1747,7 +1747,7 @@ static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
>  				     struct dlm_migratable_lockres *mres)
>  {
>  	struct dlm_migratable_lock *ml;
> -	struct list_head *queue;
> +	struct list_head *queue, *iter;
>  	struct list_head *tmpq = NULL;
>  	struct dlm_lock *newlock = NULL;
>  	struct dlm_lockstatus *lksb = NULL;
> @@ -1791,11 +1791,12 @@ static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
>  			spin_lock(&res->spinlock);
>  			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
>  				tmpq = dlm_list_idx_to_ptr(res, j);
> -				list_for_each_entry(lock, tmpq, list) {
> -					if (lock->ml.cookie != ml->cookie)
> -						lock = NULL;
> -					else
> +				list_for_each(iter, tmpq) {
> +					lock = list_entry(iter, struct dlm_lock, list);
> +
> +					if (lock->ml.cookie == ml->cookie)
>  						break;
> +					lock = NULL;
>  				}
>  				if (lock)
>  					break;
> -->8-----------------------------------------------------------------
> 
> 	Another approach would be to keep list_for_each_entry() around,
> but use a better check for entry existence:
> 
> --8<-----------------------------------------------------------------
> @@ -1792,13 +1792,12 @@ static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
>  			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
>  				tmpq = dlm_list_idx_to_ptr(res, j);
>  				list_for_each_entry(lock, tmpq, list) {
> -					if (lock->ml.cookie != ml->cookie)
> -						lock = NULL;
> -					else
> +					if (lock->ml.cookie == ml->cookie)
>  						break;
>  				}
> -				if (lock)
> +				if (&lock->list != tmpq)
>  					break;
> +				lock = NULL;
>  			}

This seems a bit ugly to me, since it exposes the implementation of the 
list abstraction.  What about the following:

lock = NULL;
list_for_each_entry(x, tmpq, list) {
	if (x->ml.cookie == ml->cookie) {
	   	lock = x;
		break;
	}
}

julia

  reply	other threads:[~2010-08-12  5:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-07  9:09 [Ocfs2-devel] [PATCH 1/2] fs/ocfs2/dlm: Eliminate update of list_for_each_entry loop cursor Julia Lawall
2010-08-12  0:03 ` Joel Becker
2010-08-12  5:46   ` Julia Lawall [this message]
2010-08-12  7:14     ` Dan Carpenter
2010-08-12  9:31     ` Joel Becker
     [not found]   ` <20111102073928.GA23750@mwanda>
2011-11-02 17:05     ` Sunil Mushran
2011-11-17  8:43       ` Joel Becker

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=Pine.LNX.4.64.1008120735200.16027@ask.diku.dk \
    --to=julia@diku.dk \
    --cc=Joel.Becker@oracle.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfasheh@suse.com \
    --cc=ocfs2-devel@oss.oracle.com \
    --cc=sunil.mushran@oracle.com \
    /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 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).