From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752237AbdKVVVg (ORCPT ); Wed, 22 Nov 2017 16:21:36 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:48825 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751753AbdKVVIR (ORCPT ); Wed, 22 Nov 2017 16:08:17 -0500 From: Matthew Wilcox To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox Subject: [PATCH 23/62] xarray: Add xa_cmpxchg Date: Wed, 22 Nov 2017 13:07:00 -0800 Message-Id: <20171122210739.29916-24-willy@infradead.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171122210739.29916-1-willy@infradead.org> References: <20171122210739.29916-1-willy@infradead.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox This works like doing cmpxchg() on an array entry. Code which wants the radix_tree_insert() semantic of not overwriting an existing entry can cmpxchg() with NULL and get the action it wants. Plus, instead of having an error returned, they get the value currently stored in the array, which often saves them a subsequent lookup. Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 2 ++ lib/xarray.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 5e975c512018..274dd7530e40 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -83,6 +83,8 @@ struct xarray { 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); /** * xa_empty() - Determine if an array has any present entries diff --git a/lib/xarray.c b/lib/xarray.c index a3f4f4ab673f..82f39d86fc76 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -818,6 +818,44 @@ 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: 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 ERR_PTR() if an error happened. + */ +void *xa_cmpxchg(struct xarray *xa, unsigned long index, + void *old, void *entry, gfp_t gfp) +{ + XA_STATE(xas, index); + unsigned long flags; + void *curr; + + if (WARN_ON_ONCE(xa_is_internal(entry))) + return ERR_PTR(-EINVAL); + + do { + xa_lock_irqsave(xa, flags); + curr = xas_create(xa, &xas); + if (curr == old) + xas_store(xa, &xas, entry); + xa_unlock_irqrestore(xa, flags); + } while (xas_nomem(&xas, gfp)); + xas_destroy(&xas); + + if (xas_error(&xas)) + curr = ERR_PTR(xas_error(&xas)); + return curr; +} +EXPORT_SYMBOL(xa_cmpxchg); + /** * __xa_set_tag() - Set this tag on this entry. * @xa: XArray. -- 2.15.0