From patchwork Wed Jan 17 20:20:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthew Wilcox (Oracle)" X-Patchwork-Id: 10171773 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C195660386 for ; Wed, 17 Jan 2018 21:07:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B1E4E23B23 for ; Wed, 17 Jan 2018 21:07:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A5E1023B32; Wed, 17 Jan 2018 21:07:31 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 3A0AA23B23 for ; Wed, 17 Jan 2018 21:07:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754375AbeAQVGd (ORCPT ); Wed, 17 Jan 2018 16:06:33 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:37363 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753522AbeAQUWe (ORCPT ); Wed, 17 Jan 2018 15:22:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=kxMDFVvJxazhbqx48cDE1jvnCN9jWvyDKrSQLHcLun4=; b=Nted86jAFb7CV8xrVgaT3ribU xjmbwrMwn2i3RaszinUbHmBVZnXNLv4mxYIpxjMqABJlfnzrVbdthmc83/34N+lo7GQABzTF4+2D8 NF0zlCgBARCOWBB8inrmxqc4Uqi/HcMciCyh9Yl0mf1Z2iRfQJLsimcIVfQFC3r6aMtSFUXR1tXVz zOMiqSWFWf4BTvIQL/mSIkCjxLII0qsZd8K12ICvPrSrf0Ed5zgX56aFEYQw6EZjtunQGMx4P1Lw+ PtLU8bH9MXtJyQjYNaI9GbWukripbuO/K0kINJimRKsieQQoH3pDuCG8qe0zUWkZ2LdOH5cUQHRrf EqoSPRAOQ==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1ebuED-0005lz-Cg; Wed, 17 Jan 2018 20:22:33 +0000 From: Matthew Wilcox To: linux-kernel@vger.kernel.org Cc: Matthew Wilcox , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-nilfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-usb@vger.kernel.org, Bjorn Andersson , Stefano Stabellini , iommu@lists.linux-foundation.org, linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org, intel-gfx@lists.freedesktop.org, cgroups@vger.kernel.org, linux-sh@vger.kernel.org, David Howells Subject: [PATCH v6 21/99] xarray: Add xa_reserve and xa_release Date: Wed, 17 Jan 2018 12:20:45 -0800 Message-Id: <20180117202203.19756-22-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180117202203.19756-1-willy@infradead.org> References: <20180117202203.19756-1-willy@infradead.org> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Matthew Wilcox This function simply creates a slot in the XArray for users which need to acquire multiple locks before storing their entry in the tree and so cannot use a plain xa_store(). Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 14 ++++++++++ lib/xarray.c | 51 ++++++++++++++++++++++++++++++++++ tools/testing/radix-tree/xarray-test.c | 25 +++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 6f59f1f60205..c3f7405c5517 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -259,6 +259,7 @@ void *xa_load(struct xarray *, unsigned long index); void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t); void *xa_cmpxchg(struct xarray *, unsigned long index, void *old, void *entry, gfp_t); +int xa_reserve(struct xarray *, unsigned long index, gfp_t); bool xa_get_tag(struct xarray *, unsigned long index, xa_tag_t); void xa_set_tag(struct xarray *, unsigned long index, xa_tag_t); void xa_clear_tag(struct xarray *, unsigned long index, xa_tag_t); @@ -373,6 +374,19 @@ static inline int xa_insert(struct xarray *xa, unsigned long index, return -EEXIST; } +/** + * xa_release() - Release a reserved entry. + * @xa: XArray. + * @index: Index of entry. + * + * After calling xa_reserve(), you can call this function to release the + * reservation. It is harmless to call this function if the entry was used. + */ +static inline void xa_release(struct xarray *xa, unsigned long index) +{ + xa_cmpxchg(xa, index, NULL, NULL, 0); +} + #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock) #define xa_lock(xa) spin_lock(&(xa)->xa_lock) #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock) diff --git a/lib/xarray.c b/lib/xarray.c index ace309cc9253..b4dec8e2d202 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1275,6 +1275,8 @@ void *xa_cmpxchg(struct xarray *xa, unsigned long index, do { xas_lock(&xas); curr = xas_load(&xas); + if (curr == XA_ZERO_ENTRY) + curr = NULL; if (curr == old) xas_store(&xas, entry); xas_unlock(&xas); @@ -1310,6 +1312,8 @@ void *__xa_cmpxchg(struct xarray *xa, unsigned long index, do { curr = xas_load(&xas); + if (curr == XA_ZERO_ENTRY) + curr = NULL; if (curr == old) xas_store(&xas, entry); } while (__xas_nomem(&xas, gfp)); @@ -1318,6 +1322,53 @@ void *__xa_cmpxchg(struct xarray *xa, unsigned long index, } EXPORT_SYMBOL(__xa_cmpxchg); +/** + * xa_reserve() - Reserve this index in the XArray. + * @xa: XArray. + * @index: Index into array. + * @gfp: Memory allocation flags. + * + * Ensures there is somewhere to store an entry at @index in the array. + * If there is already something stored at @index, this function does + * nothing. If there was nothing there, the entry is marked as reserved. + * Loads from @index will continue to see a %NULL pointer until a + * subsequent store to @index. + * + * If you do not use the entry that you have reserved, call xa_release() + * or xa_erase() to free any unnecessary memory. + * + * Return: 0 if the reservation succeeded or -ENOMEM if it failed. + */ +int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp) +{ + XA_STATE(xas, xa, index); + unsigned int lock_type = xa_lock_type(xa); + void *curr; + + do { + if (lock_type == XA_LOCK_IRQ) + xas_lock_irq(&xas); + else if (lock_type == XA_LOCK_BH) + xas_lock_bh(&xas); + else + xas_lock(&xas); + + curr = xas_create(&xas); + if (!curr) + xas_store(&xas, XA_ZERO_ENTRY); + + if (lock_type == XA_LOCK_IRQ) + xas_unlock_irq(&xas); + else if (lock_type == XA_LOCK_BH) + xas_unlock_bh(&xas); + else + xas_unlock(&xas); + } while (xas_nomem(&xas, gfp)); + + return xas_error(&xas); +} +EXPORT_SYMBOL(xa_reserve); + /** * __xa_set_tag() - Set this tag on this entry while locked. * @xa: XArray. diff --git a/tools/testing/radix-tree/xarray-test.c b/tools/testing/radix-tree/xarray-test.c index 4d3541ac31e9..fe38b53df2ab 100644 --- a/tools/testing/radix-tree/xarray-test.c +++ b/tools/testing/radix-tree/xarray-test.c @@ -502,6 +502,29 @@ void check_move(struct xarray *xa) } while (i < (1 << 16)); } +void check_reserve(struct xarray *xa) +{ + assert(xa_empty(xa)); + xa_reserve(xa, 12345678, GFP_KERNEL); + assert(!xa_empty(xa)); + assert(!xa_load(xa, 12345678)); + xa_release(xa, 12345678); + assert(xa_empty(xa)); + + xa_reserve(xa, 12345678, GFP_KERNEL); + assert(!xa_store(xa, 12345678, xa_mk_value(12345678), GFP_NOWAIT)); + xa_release(xa, 12345678); + assert(xa_erase(xa, 12345678) == xa_mk_value(12345678)); + assert(xa_empty(xa)); + + xa_reserve(xa, 12345678, GFP_KERNEL); + assert(!xa_cmpxchg(xa, 12345678, NULL, xa_mk_value(12345678), + GFP_NOWAIT)); + xa_release(xa, 12345678); + assert(xa_erase(xa, 12345678) == xa_mk_value(12345678)); + assert(xa_empty(xa)); +} + void xarray_checks(void) { DEFINE_XARRAY(array); @@ -548,6 +571,8 @@ void xarray_checks(void) check_move(&array); item_kill_tree(&array); + + check_reserve(&array); } int __weak main(void)