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 X-Patchwork-Id: 10172723 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 37A9F60230 for ; Thu, 18 Jan 2018 10:05:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 273FC22380 for ; Thu, 18 Jan 2018 10:05:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 198B12624A; Thu, 18 Jan 2018 10:05:13 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 78EC222380 for ; Thu, 18 Jan 2018 10:05:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DFE916E51F; Thu, 18 Jan 2018 09:42:36 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) by gabe.freedesktop.org (Postfix) with ESMTPS id D92216E1C4 for ; Wed, 17 Jan 2018 20:22:45 +0000 (UTC) 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 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> Cc: linux-s390@vger.kernel.org, David Howells , linux-nilfs@vger.kernel.org, Matthew Wilcox , linux-sh@vger.kernel.org, intel-gfx@lists.freedesktop.org, linux-usb@vger.kernel.org, linux-remoteproc@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-xfs@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, Stefano Stabellini , linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, Bjorn Andersson , linux-btrfs@vger.kernel.org Subject: [Intel-gfx] [PATCH v6 21/99] xarray: Add xa_reserve and xa_release X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" 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)