Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1448988 > unrolled thread

[PATCH] list: Expand list_first_entry_or_null()

Started byChris Wilson <chris@chris-wilson.co.uk>
First post2016-07-23 20:30 +0200
Last post2016-07-25 00:50 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] list: Expand list_first_entry_or_null() Chris Wilson <chris@chris-wilson.co.uk> - 2016-07-23 20:30 +0200
    Re: [PATCH] list: Expand list_first_entry_or_null() "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2016-07-25 00:50 +0200

#1448988 — [PATCH] list: Expand list_first_entry_or_null()

FromChris Wilson <chris@chris-wilson.co.uk>
Date2016-07-23 20:30 +0200
Subject[PATCH] list: Expand list_first_entry_or_null()
Message-ID<rY9Qu-65M-5@gated-at.bofh.it>
Due to the use of READ_ONCE() in list_empty() the compiler cannot
optimise !list_empty() ? list_first_entry() : NULL very well. By
manually expanding list_first_entry_or_null() we can take advantage of
the READ_ONCE() to avoid the list element changing under the test while
the compiler can generate smaller code.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Josef Bacik <jbacik@fb.com>
Cc: linux-kernel@vger.kernel.org
---
 include/linux/list.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/include/linux/list.h b/include/linux/list.h
index 5356f4d661a7..7f8b08492cb3 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -381,8 +381,11 @@ static inline void list_splice_tail_init(struct list_head *list,
  *
  * Note that if the list is empty, it returns NULL.
  */
-#define list_first_entry_or_null(ptr, type, member) \
-	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
+#define list_first_entry_or_null(ptr, type, member) ({ \
+	struct list_head *head__ = (ptr); \
+	struct list_head *pos__ = READ_ONCE(head__->next); \
+	pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
+})
 
 /**
  * list_next_entry - get the next element in list
-- 
2.8.1

[toc] | [next] | [standalone]


#1449220

From"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Date2016-07-25 00:50 +0200
Message-ID<rYAnD-57C-15@gated-at.bofh.it>
In reply to#1448988
On Sat, Jul 23, 2016 at 07:27:50PM +0100, Chris Wilson wrote:
> Due to the use of READ_ONCE() in list_empty() the compiler cannot
> optimise !list_empty() ? list_first_entry() : NULL very well. By
> manually expanding list_first_entry_or_null() we can take advantage of
> the READ_ONCE() to avoid the list element changing under the test while
> the compiler can generate smaller code.
> 
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Dan Williams <dan.j.williams@intel.com>
> Cc: Jan Kara <jack@suse.cz>
> Cc: Josef Bacik <jbacik@fb.com>
> Cc: linux-kernel@vger.kernel.org

Queued for review and testing, thank you!

							Thanx, Paul

> ---
>  include/linux/list.h | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/list.h b/include/linux/list.h
> index 5356f4d661a7..7f8b08492cb3 100644
> --- a/include/linux/list.h
> +++ b/include/linux/list.h
> @@ -381,8 +381,11 @@ static inline void list_splice_tail_init(struct list_head *list,
>   *
>   * Note that if the list is empty, it returns NULL.
>   */
> -#define list_first_entry_or_null(ptr, type, member) \
> -	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
> +#define list_first_entry_or_null(ptr, type, member) ({ \
> +	struct list_head *head__ = (ptr); \
> +	struct list_head *pos__ = READ_ONCE(head__->next); \
> +	pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
> +})
> 
>  /**
>   * list_next_entry - get the next element in list
> -- 
> 2.8.1
> 

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web