From patchwork Tue Jul 30 01:17:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11064689 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9EC5513B1 for ; Tue, 30 Jul 2019 01:18:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E68D286DB for ; Tue, 30 Jul 2019 01:18:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8290C28779; Tue, 30 Jul 2019 01:18:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 30304286F9 for ; Tue, 30 Jul 2019 01:18:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729295AbfG3BSF (ORCPT ); Mon, 29 Jul 2019 21:18:05 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:32954 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725878AbfG3BSE (ORCPT ); Mon, 29 Jul 2019 21:18:04 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x6U18aVI095483; Tue, 30 Jul 2019 01:17:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=g9c2RncZ7+p19tL7qo1Ya21k4WOi7/ov7/QwLqXzV6A=; b=yTYIwKoR5mIzYeuT4zpkrEvCb9//jDDnTafQopVWPtbkccVsmJ9m8fk/pBT0YlKXVZ2Z 7NAbX/t7zvcRYEArm1MoS1SpO2fk5hAZh8F2SKT1e5TYgK4xD6rXsasa+pZCLVHivYsl O6X0odK6zslYED4e30QHGNBVNMUSP65t+ap3WsqjAbgZUk4v8i+bVGAQZs4IqtS7pcAG GihsLjdKogp+U6+t5l7OZm0D/dXfB7H3zBxy50+2A55scxlZ3h53ZJMBVLgtHup1w8Ve EYfDCh2uM4VNa7AW0gNZTEaKSoAi+fYLUqbFz48L1dKWGrIt74z0eijHA+gjqq2/HAfj RQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 2u0ejpb0eu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Jul 2019 01:17:50 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x6U1D9dl033949; Tue, 30 Jul 2019 01:17:49 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3020.oracle.com with ESMTP id 2u0dxqmrfc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Jul 2019 01:17:49 +0000 Received: from abhmp0022.oracle.com (abhmp0022.oracle.com [141.146.116.28]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x6U1HlbI011963; Tue, 30 Jul 2019 01:17:47 GMT Received: from localhost (/10.159.132.41) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 29 Jul 2019 18:17:46 -0700 Subject: [PATCH 1/6] list.h: add list_pop and list_pop_entry helpers From: "Darrick J. Wong" To: hch@infradead.org, darrick.wong@oracle.com Cc: Damien.LeMoal@wdc.com, agruenba@redhat.com, "Matthew Wilcox \(Oracle\)" , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, Christoph Hellwig Date: Mon, 29 Jul 2019 18:17:46 -0700 Message-ID: <156444946635.2682261.14776328020249327506.stgit@magnolia> In-Reply-To: <156444945993.2682261.3926017251626679029.stgit@magnolia> References: <156444945993.2682261.3926017251626679029.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9333 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1906280000 definitions=main-1907300010 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9333 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1906280000 definitions=main-1907300010 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Christoph Hellwig We have a very common pattern where we want to delete the first entry from a list and return it as the properly typed container structure. Add two helpers to implement this behavior. Signed-off-by: Christoph Hellwig Reviewed-by: Matthew Wilcox (Oracle) Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong --- include/linux/list.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/linux/list.h b/include/linux/list.h index 85c92555e31f..d3b00267446a 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -514,6 +514,39 @@ static inline void list_splice_tail_init(struct list_head *list, pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ }) +/** + * list_pop - delete the first entry from a list and return it + * @list: the list to take the element from. + * + * Return the list entry after @list. If @list is empty return NULL. + */ +static inline struct list_head *list_pop(struct list_head *list) +{ + struct list_head *pos = READ_ONCE(list->next); + + if (pos == list) + return NULL; + list_del(pos); + return pos; +} + +/** + * list_pop_entry - delete the first entry from a list and return the + * containing structure + * @list: the list to take the element from. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_head within the struct. + * + * Return the containing structure for the list entry after @list. If @list + * is empty return NULL. + */ +#define list_pop_entry(list, type, member) \ +({ \ + struct list_head *pos__ = list_pop(list); \ + \ + pos__ ? list_entry(pos__, type, member) : NULL; \ +}) + /** * list_next_entry - get the next element in list * @pos: the type * to cursor