2002-10-03 23:31:50

by Mark Peloquin

[permalink] [raw]
Subject: [PATCH] add safe version of list_for_each_entry() to list.h

Please consider adding the following patch to list.h.

The following patch adds list_for_each_entry_safe() and
list_member() to list.h.

- List_for_each_entry_safe adds a removal-safe version of this macro.
- List_member indicates if the container object is currently in a list.

Thanks.
Mark

diff -Naur old/include/linux/list.h new/include/linux/list.h
--- old/include/linux/list.h Thu Oct 3 18:06:42 2002
+++ new/include/linux/list.h Thu Oct 3 18:10:46 2002
@@ -137,6 +137,15 @@
return head->next == head;
}

+/**
+ * list_member - tests whether a list member is currently on a list
+ * @member: member to evaulate
+ */
+static inline int list_member(struct list_head *member)
+{
+ return ((!member->next || !member->prev) ? 0 : 1);
+}
+
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
@@ -241,6 +250,20 @@
pos = list_entry(pos->member.next, typeof(*pos), member), \
prefetch(pos->member.next))

+/**
+ * list_for_each_entry_safe - iterate over list safe against removal of list entry
+ * @pos: the type * to use as a loop counter.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, \
+ n = list_entry(pos->member.next, typeof(*pos), member))
+
#endif /* __KERNEL__ || _LVM_H_INCLUDE */

#endif



2002-10-03 23:41:48

by Greg KH

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

On Thu, Oct 03, 2002 at 06:42:09PM -0500, Mark Peloquin wrote:
> Please consider adding the following patch to list.h.

This patch had the tabs mangled and would not apply.

Yeah, Notes sucks for sending patches...

thanks,

greg k-h

2002-10-04 01:05:44

by Kevin Corry

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

On Thursday 03 October 2002 18:44, Greg KH wrote:
> On Thu, Oct 03, 2002 at 06:42:09PM -0500, Mark Peloquin wrote:
> > Please consider adding the following patch to list.h.
>
> This patch had the tabs mangled and would not apply.
>
> Yeah, Notes sucks for sending patches...

It does indeed. Avoid Notes at all costs. :)

Here is the patch again. Should apply cleanly this time.

-Kevin

=========================================================
diff -Naur linux-2.5.40a/include/linux/list.h linux-2.5.40b/include/linux/list.h
--- linux-2.5.40a/include/linux/list.h Tue Oct 1 02:05:48 2002
+++ linux-2.5.40b/include/linux/list.h Thu Oct 3 19:17:27 2002
@@ -137,6 +137,15 @@
return head->next == head;
}

+/**
+ * list_member - tests whether a list member is currently on a list
+ * @member: member to evaulate
+ */
+static inline int list_member(struct list_head *member)
+{
+ return ((!member->next || !member->prev) ? 0 : 1);
+}
+
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
@@ -240,6 +249,20 @@
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member), \
prefetch(pos->member.next))
+
+/**
+ * list_for_each_entry_safe - iterate over list safe against removal of list entry
+ * @pos: the type * to use as a loop counter.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, \
+ n = list_entry(pos->member.next, typeof(*pos), member))

#endif /* __KERNEL__ || _LVM_H_INCLUDE */

2002-10-04 14:18:36

by Kevin Corry

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

On Friday 04 October 2002 08:58, Christoph Hellwig wrote:
> > + */
> > +#define list_for_each_entry_safe(pos, n, head, member) \
> > + for (pos = list_entry((head)->next, typeof(*pos), member), \
> > + n = list_entry(pos->member.next, typeof(*pos), member); \
> > + &pos->member != (head); \
> > + pos = n, \
> > + n = list_entry(pos->member.next, typeof(*pos), member))
>
> Identation looks a little strange..

Dammit. Forgot to insert the patch. Here goes again.

--
Kevin Corry
[email protected]
http://evms.sourceforge.net/

======================================================
diff -Naur linux-2.5.40a/include/linux/list.h linux-2.5.40b/include/linux/list.h
--- linux-2.5.40a/include/linux/list.h Fri Oct 4 08:45:54 2002
+++ linux-2.5.40b/include/linux/list.h Fri Oct 4 08:45:31 2002
@@ -137,6 +137,15 @@
return head->next == head;
}

+/**
+ * list_member - tests whether a list member is currently on a list
+ * @member: member to evaulate
+ */
+static inline int list_member(struct list_head *member)
+{
+ return member->next && member->prev;
+}
+
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
@@ -240,6 +249,20 @@
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member), \
prefetch(pos->member.next))
+
+/**
+ * list_for_each_entry_safe - iterate over list safe against removal of list entry
+ * @pos: the type * to use as a loop counter.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, \
+ n = list_entry(pos->member.next, typeof(*pos), member))

#endif /* __KERNEL__ || _LVM_H_INCLUDE */

2002-10-04 13:53:29

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

> =========================================================
> diff -Naur linux-2.5.40a/include/linux/list.h linux-2.5.40b/include/linux/list.h
> --- linux-2.5.40a/include/linux/list.h Tue Oct 1 02:05:48 2002
> +++ linux-2.5.40b/include/linux/list.h Thu Oct 3 19:17:27 2002
> @@ -137,6 +137,15 @@
> return head->next == head;
> }
>
> +/**
> + * list_member - tests whether a list member is currently on a list
> + * @member: member to evaulate
> + */
> +static inline int list_member(struct list_head *member)
> +{
> + return ((!member->next || !member->prev) ? 0 : 1);

Wouldn't return (member->next && member->prev); be simpler?

> + */
> +#define list_for_each_entry_safe(pos, n, head, member) \
> + for (pos = list_entry((head)->next, typeof(*pos), member), \
> + n = list_entry(pos->member.next, typeof(*pos), member); \
> + &pos->member != (head); \
> + pos = n, \
> + n = list_entry(pos->member.next, typeof(*pos), member))
>

Identation looks a little strange..

2002-10-04 14:16:34

by Kevin Corry

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

On Friday 04 October 2002 08:58, Christoph Hellwig wrote:
> > +/**
> > + * list_member - tests whether a list member is currently on a list
> > + * @member: member to evaulate
> > + */
> > +static inline int list_member(struct list_head *member)
> > +{
> > + return ((!member->next || !member->prev) ? 0 : 1);
>
> Wouldn't return (member->next && member->prev); be simpler?

Sure. New patch below with new list_member() function.

> > + */
> > +#define list_for_each_entry_safe(pos, n, head, member) \
> > + for (pos = list_entry((head)->next, typeof(*pos), member), \
> > + n = list_entry(pos->member.next, typeof(*pos), member); \
> > + &pos->member != (head); \
> > + pos = n, \
> > + n = list_entry(pos->member.next, typeof(*pos), member))
>
> Identation looks a little strange..

Perhaps. But there are plenty of places in list.h that have some strange
indenting. If you'd like it another way, please post a patch with your
preferred version.

--
Kevin Corry
[email protected]
http://evms.sourceforge.net/

2002-10-04 15:00:58

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h


Your list_member macro:

+static inline int list_member(struct list_head *member)
+{
+ return ((!member->next || !member->prev) ? 0 : 1);
+}

seems wrong to me. A list head which has been removed from its list using
list_del() still points to its old prev & next entries. If removed using
list_del_init(), those pointers are reinitialised to point at itself.
ie you only need list_empty(). Are you abusing list.h somehow?

--
Revolutions do not require corporate support.

2002-10-04 15:54:49

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

On Fri, Oct 04, 2002 at 10:48:33AM -0500, Mark Peloquin wrote:
> list_empty() can be used on check list heads *or*
> to check if a list element is currently in a list,
> assuming the coder uses list_del_init(). However,
> if the coder chooses to use list_del() [which sets
> the prev and next fields to 0] instead, there is no
> corresponding function to indicate if that element
> is currently on a list. This function does that.

That behaviour for list_del is new and, IMNSHO, bogus. There's now _zero_
gain in using list_del instead of list_del_init. akpm changed it about
5 months ago with a comment that says:

"list_head debugging"

so i think it's pretty safe to assume that this behaviour will not
remain into 2.6. if you think you want list_member, use list_del_init
and list_empty() instead.

--
Revolutions do not require corporate support.

2002-10-04 16:28:10

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h

Matthew Wilcox wrote:
>
> On Fri, Oct 04, 2002 at 10:48:33AM -0500, Mark Peloquin wrote:
> > list_empty() can be used on check list heads *or*
> > to check if a list element is currently in a list,
> > assuming the coder uses list_del_init(). However,
> > if the coder chooses to use list_del() [which sets
> > the prev and next fields to 0] instead, there is no
> > corresponding function to indicate if that element
> > is currently on a list. This function does that.
>
> That behaviour for list_del is new and, IMNSHO, bogus. There's now _zero_
> gain in using list_del instead of list_del_init. akpm changed it about
> 5 months ago with a comment that says:
>
> "list_head debugging"
>

It doesn't seem to have caught anyone out, so I guess we can
put it back now. I'll do a patch.

2002-10-04 15:37:19

by Mark Peloquin

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h


On 10/04/2002 at 10:05 AM, Matthew Wilcox wrote:
> Your list_member macro:

> +static inline int list_member(struct list_head *member)
> +{
> + return ((!member->next || !member->prev) ? 0 : 1);
> +}

> seems wrong to me. A list head which has been removed from its list
using
> list_del() still points to its old prev & next entries. If removed using
> list_del_init(), those pointers are reinitialised to point at itself.
> ie you only need list_empty(). Are you abusing list.h somehow?

list_empty() can be used on check list heads *or*
to check if a list element is currently in a list,
assuming the coder uses list_del_init(). However,
if the coder chooses to use list_del() [which sets
the prev and next fields to 0] instead, there is no
corresponding function to indicate if that element
is currently on a list. This function does that.

Mark


2002-10-04 16:29:38

by Mark Peloquin

[permalink] [raw]
Subject: Re: [PATCH] add safe version of list_for_each_entry() to list.h


On 10/04/2002 at 11:00 AM, Matthew Wilcox wrote:

> That behaviour for list_del is new and, IMNSHO, bogus. There's now
_zero_
> gain in using list_del instead of list_del_init.

The only gain I've noticed is when the container
object is memset it gives implicit initialization
if one uses list_del.

> akpm changed it about
> 5 months ago with a comment that says:

> "list_head debugging"

> so i think it's pretty safe to assume that this behaviour will not
> remain into 2.6. if you think you want list_member, use list_del_init
> and list_empty() instead.

I wasn't aware this was somewhat recently added item
for debug and will switch to list_del_init().

Thanks for bring this to my attention!