From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752628AbcGXWnM (ORCPT ); Sun, 24 Jul 2016 18:43:12 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:44946 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752225AbcGXWnK (ORCPT ); Sun, 24 Jul 2016 18:43:10 -0400 X-IBM-Helo: d01dlp01.pok.ibm.com X-IBM-MailFrom: paulmck@linux.vnet.ibm.com Date: Sun, 24 Jul 2016 15:43:33 -0700 From: "Paul E. McKenney" To: Chris Wilson Cc: linux-kernel@vger.kernel.org, Andrew Morton , Dan Williams , Jan Kara , Josef Bacik Subject: Re: [PATCH] list: Expand list_first_entry_or_null() Reply-To: paulmck@linux.vnet.ibm.com References: <1469298470-27470-1-git-send-email-chris@chris-wilson.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1469298470-27470-1-git-send-email-chris@chris-wilson.co.uk> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16072422-0056-0000-0000-000000DE6130 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16072422-0057-0000-0000-000004F889BF Message-Id: <20160724224333.GW7094@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-07-24_19:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1607240281 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > Cc: "Paul E. McKenney" > Cc: Andrew Morton > Cc: Dan Williams > Cc: Jan Kara > Cc: Josef Bacik > 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 >