Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4255282imm; Mon, 11 Jun 2018 09:18:55 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLbw3NYXTumw/FPWRiA9tDboVofeluQT0ApHHNq1GF10sKxolf/r39ev7sQeDRQTWcckvP8 X-Received: by 2002:a63:6642:: with SMTP id a63-v6mr536727pgc.408.1528733935825; Mon, 11 Jun 2018 09:18:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528733935; cv=none; d=google.com; s=arc-20160816; b=alv2eIsVdeWVOelqAixFSQ+a435ApSbJcwW6mTPdOv5XMvZj3C5aUbIZlnMfqCQD2/ wQ4CIZfUf3kIlwle9MF6fb63OJXyb1EWq94B0fhIY0naeTlrvHUh1oQzmEfTdsragELg CIqbaMpncyJ6AKD7zT3S2HI+IX49GiWcoHQSrWTYPovFBrgEFWlBfSfnTtYAbZnbvfy8 PPWTMA56rLdeyQ6oqYA9H0rDjDRy3/DM7DhowBoHwA/Jd2ldbAzz5vhcb7+JNh2x6f1o v1JbT9PPvGHuXditz070IwykpcgRKuTdg3Zp5d7CQ7a5eBEThq5sn5C+7FFyYXsc3N2v dgfw== 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=QHopodgOckH0JnTMC74cz/L5jvYaTcy0lLTV0wcZbXM=; b=yluqMVaOfJIu+KbKWPRMMn7POXHSAZy6Y4qdkx+8u7XPP/F5IXW24y0q3uBfJYgx7m bdIg99F+HxhcdPLMqm0JgKpbpTWV2itWjUNyc2+IJHpJMzW1bIYkiw2eDsWQOIhctPO2 3KgxHmRQ0L/q5HzhRbpT8NlWq8R6l9El/2n84hGxRSRNlS0X4586I8KIOnYUGW6HykK5 j573wY8wXpwYER0PVfd+wZbn1D8YTR60qgsMGdvFBKiLK2+6cdQMY2Cud4pAgjK+sfcN IqblHo+lXfukscOWUmxF2h0lGKODzkytL1dJkdDFRNYT0yTIu0H503VTZapF4eQlfWlH oWQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=GecsTcI+; 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 i9-v6si22923457pgv.109.2018.06.11.09.18.41; Mon, 11 Jun 2018 09:18:55 -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=GecsTcI+; 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 S934322AbeFKOWK (ORCPT + 99 others); Mon, 11 Jun 2018 10:22:10 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:44956 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933467AbeFKOGp (ORCPT ); Mon, 11 Jun 2018 10:06:45 -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=QHopodgOckH0JnTMC74cz/L5jvYaTcy0lLTV0wcZbXM=; b=GecsTcI+M6rjNPEH8IVnZO/7U csPI9XxMj9/MVftlaHS2asCmBAsHsk6ok6my/n0U4albS3QAbSEnCvvR1BEMX9uLORgrBOFyDq0Rn 8gf9gYBzcXqmTRTJwYYJUck6kiM1xYgOmsEBpHUIRe+yqAX0v3C702kcj3+eWFYYgDgxoruulWJhv RS68PiGzLZu29vPOVlzjfZzVVs3ILwQikbFoZSk0ZppAyCG5lHSvoV1wDcHeVZ93msMf81zyYlHDY leddNtMmGcvh07C8cSmooDQngcilxEVbh5pyUk8mBTY+GejmCFx2g/VguyMcsUuItsMXm2tRG/lXz muC85uX9g==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1fSNT2-0004Zn-Pf; Mon, 11 Jun 2018 14:06:44 +0000 From: Matthew Wilcox To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Jan Kara , Jeff Layton , Lukas Czerner , Ross Zwisler , Christoph Hellwig , Goldwyn Rodrigues , Nicholas Piggin , Ryusuke Konishi , linux-nilfs@vger.kernel.org, Jaegeuk Kim , Chao Yu , linux-f2fs-devel@lists.sourceforge.net Subject: [PATCH v13 13/72] xarray: Add xa_extract Date: Mon, 11 Jun 2018 07:05:40 -0700 Message-Id: <20180611140639.17215-14-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180611140639.17215-1-willy@infradead.org> References: <20180611140639.17215-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 This function combines the functionality of radix_tree_gang_lookup() and radix_tree_gang_lookup_tagged(). It extracts entries matching the specified filter into a normal array. Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 2 ++ lib/xarray.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 89f37d841d18..3764f4b4c1a8 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -227,6 +227,8 @@ void *xa_find(struct xarray *xa, unsigned long *index, unsigned long max, xa_tag_t) __attribute__((nonnull(2))); void *xa_find_after(struct xarray *xa, unsigned long *index, unsigned long max, xa_tag_t) __attribute__((nonnull(2))); +unsigned int xa_extract(struct xarray *, void **dst, unsigned long start, + unsigned long max, unsigned int n, xa_tag_t); /** * xa_init() - Initialise an empty XArray. diff --git a/lib/xarray.c b/lib/xarray.c index c6b7adcd6131..c3a53f09aa88 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1427,6 +1427,86 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp, } EXPORT_SYMBOL(xa_find_after); +static unsigned int xas_extract_present(struct xa_state *xas, void **dst, + unsigned long max, unsigned int n) +{ + void *entry; + unsigned int i = 0; + + rcu_read_lock(); + xas_for_each(xas, entry, max) { + if (xas_retry(xas, entry)) + continue; + dst[i++] = entry; + if (i == n) + break; + } + rcu_read_unlock(); + + return i; +} + +static unsigned int xas_extract_tag(struct xa_state *xas, void **dst, + unsigned long max, unsigned int n, xa_tag_t tag) +{ + void *entry; + unsigned int i = 0; + + rcu_read_lock(); + xas_for_each_tagged(xas, entry, max, tag) { + if (xas_retry(xas, entry)) + continue; + dst[i++] = entry; + if (i == n) + break; + } + rcu_read_unlock(); + + return i; +} + +/** + * xa_extract() - Copy selected entries from the XArray into a normal array. + * @xa: The source XArray to copy from. + * @dst: The buffer to copy entries into. + * @start: The first index in the XArray eligible to be selected. + * @max: The last index in the XArray eligible to be selected. + * @n: The maximum number of entries to copy. + * @filter: Selection criterion. + * + * Copies up to @n entries that match @filter from the XArray. The + * copied entries will have indices between @start and @max, inclusive. + * + * The @filter may be an XArray tag value, in which case entries which are + * tagged with that tag will be copied. It may also be %XA_PRESENT, in + * which case non-NULL entries will be copied. + * + * The entries returned may not represent a snapshot of the XArray at a + * moment in time. For example, if another thread stores to index 5, then + * index 10, calling xa_extract() may return the old contents of index 5 + * and the new contents of index 10. Indices not modified while this + * function is running will not be skipped. + * + * If you need stronger guarantees, holding the xa_lock across calls to this + * function will prevent concurrent modification. + * + * Context: Any context. Takes and releases the RCU lock. + * Return: The number of entries copied. + */ +unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start, + unsigned long max, unsigned int n, xa_tag_t filter) +{ + XA_STATE(xas, xa, start); + + if (!n) + return 0; + + if ((__force unsigned int)filter < XA_MAX_TAGS) + return xas_extract_tag(&xas, dst, max, n, filter); + return xas_extract_present(&xas, dst, max, n); +} +EXPORT_SYMBOL(xa_extract); + #ifdef XA_DEBUG void xa_dump_node(const struct xa_node *node) { -- 2.17.1