Received: by 10.223.148.5 with SMTP id 5csp6403882wrq; Wed, 17 Jan 2018 13:13:42 -0800 (PST) X-Google-Smtp-Source: ACJfBosRIPsZV2ZN/gY/KBkd6wmykltGw1WvvTj6h4lj1oCP4835UPRQPXa82ipL4ECsRC7uvX5X X-Received: by 10.84.253.148 with SMTP id a20mr42862962plm.216.1516223622425; Wed, 17 Jan 2018 13:13:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516223622; cv=none; d=google.com; s=arc-20160816; b=WWJwDtQV1+oKcW0ftFLo4wb5Ly6KmCN4eNyY8NsSnBr2oFIRaj9Gl8JsZxgBUjvHxJ eVy4yoR3pXWR/v55sqHG7XxZBgDNAoO4HofkclzuuU/9TGRvzvbY6XPOLA/NBnVpQILy Bd0eijYyvxtEobeY02a8vGR3DGlXU4yXs9qMZkvFlN7f1vXgd9JW+o3wI6/Zc5F/c9TI RTBgBDEWUCzbQBeWv8WFdH/F3foIYVz51fc21dWD6VCec+ZRvPlj/apzheY85klaiPeu tZjt9kHf8n1Tmke2BsTfGEiLVkhFhM0In+tmHH0IvA3GXBQVu8iqaZbkiOv77nJQ8Kqf tz3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=fIcnEhWMSWIXtCwnsaFsIdvWWdqhuNU88/3DgBlNLP8=; b=RILGDRZLsPNVheAMPNZnnCEq8wb862NH1vnUL/5NBTS5QZZGkw+77sL4SS4Iv5QMeL BFWiAV2Ruig0NfLLGYolLg2QmrRHlGhpkM0sUtKK7xWQj6Lkt8IPIzjqsF/5WjIA5qhZ c5jddiiTc/P7zuqEL6wxKKCZPG3NeU21yQCxna6vusFTXxO9SB32oPMaxoDr8b3Axpgs SyqlYE0PW6reS4FoCgyC1CqSiwOHEhCzEj1iRecRNMgGR2evH7G69uRmOptsbHickYDp qvT//OdC+4LtuhQv/d59pWo7mRnMI28RGFiosngMvVpubQMml+gXN39Spti6dS3U1b1w h0LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=JQAIiqDe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 99si5178549plc.368.2018.01.17.13.13.28; Wed, 17 Jan 2018 13:13:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=JQAIiqDe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932265AbeAQVMb (ORCPT + 99 others); Wed, 17 Jan 2018 16:12:31 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:38559 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753151AbeAQUWa (ORCPT ); Wed, 17 Jan 2018 15:22:30 -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=fIcnEhWMSWIXtCwnsaFsIdvWWdqhuNU88/3DgBlNLP8=; b=JQAIiqDeXjeKXxUJhHojuv1kX VAMnf+csPeERUpJceAZoOnDnmmDpcVmpxpx/HIPn/kjYQWjYaSSF0pQQIjg9YBq8iXAiuExDhNjtL 3pGaNUUCpOnSjkICkU2ccKLnkQCzuioNQwcm/kdIDSM00BTSjofcRp6mZG1xvHnQsfps4mk/lI/RO Ca8zILCJJMu+lklwxd8negAOFSEfLjh1NMl5pOmV0E+IgGQ0mSZGo885/El5MTwtqfZwQ087+JzrJ y2gIY/LsZa7mDWZgesQFCb0n1DmYW68TcyZ9krDdhEYcrUnYtY1DaOKquNRebVJ8wKDEw9YvH7khC /qaA/S87g==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1ebuE9-0005jV-4F; Wed, 17 Jan 2018 20:22:29 +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 11/99] xarray: Add xa_cmpxchg and xa_insert Date: Wed, 17 Jan 2018 12:20:35 -0800 Message-Id: <20180117202203.19756-12-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox Like cmpxchg(), xa_cmpxchg will only store to the index if the current entry matches the old entry. It returns the current entry, which is usually more useful than the errno returned by radix_tree_insert(). For the users who really only want the errno, the xa_insert() wrapper provides a more convenient calling convention. Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 56 ++++++++++++++++++++++++++++ lib/xarray.c | 68 ++++++++++++++++++++++++++++++++++ tools/testing/radix-tree/xarray-test.c | 10 +++++ 3 files changed, 134 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 139b1c1fd022..fc9ab3b13e60 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -210,6 +210,8 @@ struct xarray { void xa_init_flags(struct xarray *, gfp_t flags); 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); 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); @@ -264,6 +266,32 @@ static inline bool xa_tagged(const struct xarray *xa, xa_tag_t tag) return xa->xa_flags & XA_FLAGS_TAG(tag); } +/** + * xa_insert() - Store this entry in the XArray unless another entry is + * already present. + * @xa: XArray. + * @index: Index into array. + * @entry: New entry. + * @gfp: Memory allocation flags. + * + * If you would rather see the existing entry in the array, use xa_cmpxchg(). + * This function is for users who don't care what the entry is, only that + * one is present. + * + * Return: -EEXIST if another entry was present, 0 if the store succeeded, + * or another negative errno if a different error happened (eg -ENOMEM). + */ +static inline int xa_insert(struct xarray *xa, unsigned long index, + void *entry, gfp_t gfp) +{ + void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp); + if (!curr) + return 0; + if (xa_is_err(curr)) + return xa_err(curr); + return -EEXIST; +} + #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) @@ -283,9 +311,37 @@ static inline bool xa_tagged(const struct xarray *xa, xa_tag_t tag) */ void *__xa_erase(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); void __xa_set_tag(struct xarray *, unsigned long index, xa_tag_t); void __xa_clear_tag(struct xarray *, unsigned long index, xa_tag_t); +/** + * __xa_insert() - Store this entry in the XArray unless another entry is + * already present. + * @xa: XArray. + * @index: Index into array. + * @entry: New entry. + * @gfp: Memory allocation flags. + * + * If you would rather see the existing entry in the array, use __xa_cmpxchg(). + * This function is for users who don't care what the entry is, only that + * one is present. + * + * Return: -EEXIST if another entry was present, 0 if the store succeeded, + * or another negative errno if a different error happened (eg -ENOMEM). + */ +static inline int __xa_insert(struct xarray *xa, unsigned long index, + void *entry, gfp_t gfp) +{ + void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp); + if (!curr) + return 0; + if (xa_is_err(curr)) + return xa_err(curr); + return -EEXIST; +} + /* Everything below here is the Advanced API. Proceed with caution. */ /* diff --git a/lib/xarray.c b/lib/xarray.c index 45b70e622bf1..d925a98fb9b8 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -928,6 +928,74 @@ void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) } EXPORT_SYMBOL(__xa_store); +/** + * xa_cmpxchg() - Conditionally replace an entry in the XArray. + * @xa: XArray. + * @index: Index into array. + * @old: Old value to test against. + * @entry: New value to place in array. + * @gfp: Memory allocation flags. + * + * If the entry at @index is the same as @old, replace it with @entry. + * If the return value is equal to @old, then the exchange was successful. + * + * Return: The old value at this index or xa_err() if an error happened. + */ +void *xa_cmpxchg(struct xarray *xa, unsigned long index, + void *old, void *entry, gfp_t gfp) +{ + XA_STATE(xas, xa, index); + void *curr; + + if (WARN_ON_ONCE(xa_is_internal(entry))) + return XA_ERROR(-EINVAL); + + do { + xas_lock(&xas); + curr = xas_load(&xas); + if (curr == old) + xas_store(&xas, entry); + xas_unlock(&xas); + } while (xas_nomem(&xas, gfp)); + + return xas_result(&xas, curr); +} +EXPORT_SYMBOL(xa_cmpxchg); + +/** + * __xa_cmpxchg() - Store this entry in the XArray. + * @xa: XArray. + * @index: Index into array. + * @old: Old value to test against. + * @entry: New entry. + * @gfp: Memory allocation flags. + * + * You must already be holding the xa_lock when calling this function. + * It will drop the lock if needed to allocate memory, and then reacquire + * it afterwards. + * + * + * Return: The old entry at this index or xa_err() if an error happened. + */ +void *__xa_cmpxchg(struct xarray *xa, unsigned long index, + void *old, void *entry, gfp_t gfp) +{ + XA_STATE(xas, xa, index); + void *curr; + + if (WARN_ON_ONCE(xa_is_internal(entry))) + return XA_ERROR(-EINVAL); + + do { + curr = xas_load(&xas); + if (curr == old) + xas_store(&xas, entry); + } while (__xas_nomem(&xas, gfp)); + + return xas_result(&xas, curr); +} +EXPORT_SYMBOL(__xa_cmpxchg); + /** * __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 5defd0b9f85c..d6a969d999d9 100644 --- a/tools/testing/radix-tree/xarray-test.c +++ b/tools/testing/radix-tree/xarray-test.c @@ -84,6 +84,15 @@ void check_xa_shrink(struct xarray *xa) assert(xa_load(xa, 0) == xa_mk_value(0)); } +void check_cmpxchg(struct xarray *xa) +{ + assert(xa_empty(xa)); + assert(!xa_store(xa, 12345678, xa_mk_value(12345678), GFP_KERNEL)); + assert(!xa_cmpxchg(xa, 5, xa_mk_value(5), NULL, GFP_KERNEL)); + assert(xa_erase(xa, 12345678) == xa_mk_value(12345678)); + assert(xa_empty(xa)); +} + void check_multi_store(struct xarray *xa) { unsigned long i, j, k; @@ -149,6 +158,7 @@ void xarray_checks(void) check_xa_shrink(&array); item_kill_tree(&array); + check_cmpxchg(&array); check_multi_store(&array); item_kill_tree(&array); } -- 2.15.1