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


Groups > linux.kernel > #1448988

[PATCH] list: Expand list_first_entry_or_null()

Path csiph.com!aioe.org!gothmog.csi.it!bofh.it!news.nic.it!robomod
From Chris Wilson <chris@chris-wilson.co.uk>
Newsgroups linux.kernel
Subject [PATCH] list: Expand list_first_entry_or_null()
Date Sat, 23 Jul 2016 20:30:02 +0200
Message-ID <rY9Qu-65M-5@gated-at.bofh.it> (permalink)
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id; bh=5y+9irGbMzP3SeXT8Jmw0GMXBchj2zrfb0kPEJUln3Q=; b=GqDX/rRAdZPlSFzP/vr7OnsaT6RilsXNCpbgVz/wxYPNt1MWci5VN7IE/jwp2RYOKc I7q7V0sDOAOCpbPXJwvC4VW9v9b+3Sxko0o95Z7BHpUFcI6UFDOs86wkW+a1fPI+9JL1 Nt7oOh+hcH5oLkEjS36IBHT/mvaEbcFDv3yK1Qds0QNhptpKnk/bupoT19cQGfzDnJIr xNwCM+L2M/1TpeJ6RSN2R4GGoAHbl/0zuzGUHZ2aO/wTcRjTP7axBT8z5g4wJQp5In0L izR+KKSziuGa4UokHXKykga2qZPaDMIOQ1TK0nKFyEVtiHPD6diCLBwAOxk0n4DAVCxg z+eA==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=5y+9irGbMzP3SeXT8Jmw0GMXBchj2zrfb0kPEJUln3Q=; b=edN1ILm4+3aX3tBzVhMFxLx1Ug0rNSGqOBFysvnwa49shjHjYw6oqOXf5RSogmdIlD DFTQ7kGq5q5q8vZSZ+wpxPYFst2/gRww0vAcWP396Wk0MfmYqjNfHjIEAuBLiDPv3Mzw nfQxLGjiC1QZsdEBmN3IgJtlhdqi1jLWrSgOJJG89IWKmKFlWmGBKx9A5aD5qaH/NEzv eaREax4Bg5IBZdwOcaSHSj3/zEo7EidEt3r3xKm8hejNlAyLQBcMPRl6e+mCJNibqYic 7e7n9JGFU2Xgg3lN+Xn2+sOsfgVVDeJwHeE2ZDINoT8UAdDh7DIUsVfwirTorxGmQFbo AKAQ==
X-Gm-Message-State AEkoouuWortPE+aUXk6xMHHeLXlROv33tuIdyiRcGHG2bYJZ1L/Dm3KC91tn8ZkiLfh5hg==
X-Received by 10.194.110.102 with SMTP id hz6mr7738110wjb.5.1469298478995; Sat, 23 Jul 2016 11:27:58 -0700 (PDT)
X-Mailer git-send-email 2.8.1
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 37
Organization linux.* mail to news gateway
X-Original-Cc Chris Wilson <chris@chris-wilson.co.uk>, "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>, Andrew Morton <akpm@linux-foundation.org>, Dan Williams <dan.j.williams@intel.com>, Jan Kara <jack@suse.cz>, Josef Bacik <jbacik@fb.com>
X-Original-Date Sat, 23 Jul 2016 19:27:50 +0100
X-Original-Message-ID <1469298470-27470-1-git-send-email-chris@chris-wilson.co.uk>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1448988

Show key headers only | View raw


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

Back to linux.kernel | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

[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

csiph-web