Received: by 10.213.65.68 with SMTP id h4csp361444imn; Tue, 13 Mar 2018 06:47:57 -0700 (PDT) X-Google-Smtp-Source: AG47ELswD2Z3AYelSQd8VflMZfAayJoYvEopjznCfLl33pJlDyvjeJB1Q7hJGe8pGHqkG2f+APUg X-Received: by 10.98.166.200 with SMTP id r69mr662602pfl.205.1520948877216; Tue, 13 Mar 2018 06:47:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520948877; cv=none; d=google.com; s=arc-20160816; b=a2JYMXzIlEH1kDM6u8271Dq6I4T7r93A6qM+L3ZNQDsEf6L54GJP4AkHIohpWzG4MR RYxCoOwL56TYA6DXNsk+P0YqWC3S06rCpFIYFf/WkmKhWu4avJGfsJaUNaPrvkzyaLV3 3eXVqB9FwYj+q8M7xEUj3tGNgJtrDcVAu8LiCbFbm2Jq0BEG7V3yNIy6QUne5OG9ggq6 nOxgRp1qsfeSWsWZ25S13OWy0fjzdEkyI/G/9YQ9gQ2NvyGK93CwBVupHLU5UbLJFAUz CP2T2dzSEW9qb+elaFyYZONeEdN97G9QwwFqgWB2SRr/YaH2bW3/kzKDcUPCXL91EeTb CqJw== 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=u8eE0e2Cr+kqYKm5naJWT7LX6bMK3ikwkwgPBby8s10=; b=tXTJD3G1lgEINFhxW50gRAKgxh9w90CX5UOn77p70B5CdUalhXOat8Jxr8f9shNPLz UawySMhaXOdTu/n0qW0xyWOGqq5T74/1KLdQvYdstPvJE1RRzXVuWG0wqQC5D8kQBrkn ikXXZNq4vr3J9T3m9S+yvlmi5ue6u15FMQRPHA4DkXxdsZMDq5zS6h6xP4GXLGglASl6 NOcUoLn32MZm4u0K+bfqaZ7jn8PO/pfnFAxO9jF250b7F6UgSZIZ3x/VtjiSm2rxfVyr 6951QXfNz9ziZ3zgCH7CM8CH9YXatsZnGYxLNcG/L5qrSmU6bsta8LsQnpWzfoFMEC9P Vg4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=kRRKy+eA; 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 q9si110818pgc.623.2018.03.13.06.47.42; Tue, 13 Mar 2018 06:47:57 -0700 (PDT) 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=kRRKy+eA; 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 S932557AbeCMNqj (ORCPT + 99 others); Tue, 13 Mar 2018 09:46:39 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:35112 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752681AbeCMN0t (ORCPT ); Tue, 13 Mar 2018 09:26:49 -0400 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=u8eE0e2Cr+kqYKm5naJWT7LX6bMK3ikwkwgPBby8s10=; b=kRRKy+eA3FY5/54r43BHH74nG grey3k65BUlt63dWKaT8ozOE9li3DRLRaGFPDYklSGLt6zE9axn+cRpXJMYYAnMFs4guugoEd+l4D 1zxecE28PLLFWcvQ7DGBGVkrIwaab+tiMyO1XBIj+xeT5iCeCkJUe2WCjb81NoB7VGSYGCegcpUWk apc6sxAym3RO0s76RT/Hn+npXBHumDhf5t8X3f6U71hL1J1fAtzDu89/VFmkUZEUbJi1HV04tGp1+ H4G0BuNHbZJDGlFXOJin/Zr+bPHoVrM+3WIholtGGC8O8kfqr/020m/l0FUhhtNqvwvOKmEH7DJog 1EHkXT1bA==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1evjx1-0004bu-Sm; Tue, 13 Mar 2018 13:26:47 +0000 From: Matthew Wilcox To: Andrew Morton Cc: Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Ryusuke Konishi , linux-nilfs@vger.kernel.org Subject: [PATCH v9 17/61] xarray: Add xa_cmpxchg and xa_insert Date: Tue, 13 Mar 2018 06:25:55 -0700 Message-Id: <20180313132639.17387-18-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180313132639.17387-1-willy@infradead.org> References: <20180313132639.17387-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 | 60 ++++++++++++++++++++++++++++ lib/xarray.c | 71 ++++++++++++++++++++++++++++++++++ tools/testing/radix-tree/xarray-test.c | 10 +++++ 3 files changed, 141 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 38e290df2ff0..e95ebe2488f9 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -218,6 +218,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); @@ -277,6 +279,34 @@ 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. + * + * Context: Process context. Takes and releases the xa_lock. + * May sleep if the @gfp flags permit. + * Return: 0 if the store succeeded. -EEXIST if another entry was present. + * -ENOMEM if memory could not be allocated. + */ +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) @@ -296,9 +326,39 @@ 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. + * + * Context: Any context. Expects xa_lock to be held on entry. May + * release and reacquire xa_lock if the @gfp flags permit. + * Return: 0 if the store succeeded. -EEXIST if another entry was present. + * -ENOMEM if memory could not be allocated. + */ +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 9e50804f168c..a231699d894a 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -937,6 +937,77 @@ 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. + * + * Context: Process context. Takes and releases the xa_lock. May sleep + * if the @gfp flags permit. + * 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. + * + * Context: Any context. Expects xa_lock to be held on entry. May + * release and reacquire xa_lock if @gfp flags permit. + * 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 5744f0180f5a..0012e80d3fff 100644 --- a/tools/testing/radix-tree/xarray-test.c +++ b/tools/testing/radix-tree/xarray-test.c @@ -77,6 +77,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; @@ -142,6 +151,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.16.1