Received: by 10.213.65.68 with SMTP id h4csp361143imn; Tue, 13 Mar 2018 06:47:24 -0700 (PDT) X-Google-Smtp-Source: AG47ELtKd9txb1FeoA1GT08Nu6OZimByNRcT03mdMmK6almUVX0UszjtqN/wVU+lTiu8wIeVsXYm X-Received: by 10.101.69.134 with SMTP id o6mr579197pgq.340.1520948844583; Tue, 13 Mar 2018 06:47:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520948844; cv=none; d=google.com; s=arc-20160816; b=qcT0hVXVA/L6WccVRUcN2KWiTjZRh/PSyM5VToCMf/fd7ajZd1i7g2WmN0i+g0qgyP c52AEPp6+1OR8v84jDLUYM5gdNx/OcjvMQyLbPVTRpb6YU9q/j4WaKyyFj2KeJLGC0HF 8CdGlDvu+h/BgIVFuCr4QeyhTM1vqP0vaM98zy4OnKX2RYc09W04QYMJfDsn3WMX6ZO6 h4I5jTRrZb22Lh7N5Z0RcTf0YDAvq94IFaqO464MK1QJXb8bWKO0RcGuzT1NEWZi4/or c9lNgE2XGcacd7tdJG0vxGoEmkTMf+nONf6Ewh3N3RLiPkp05UGoP+Ix7rBsf/Go0jgf 42Vg== 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=xGQQGmjllep0kxYKulB0Lo0kYYvyd9LburzqxePTOck=; b=YG0T16kQnDhvYVlGM95sIKBceSqZunwUKLdk+OlQswHxkjxzRbpT9fUGgl3HCWkWMj G0Jhf1VxfkuassnMA9ZdMQNbl7q3mJWDQeUrYCBJTWkgpo2kh0k1O2ck5YdkV0tjEtIv cq8xZd7uEFydmLuvPGqNdyWrs0Swso7tkwR/MH4ejmjdbpkk+ku3etQcOEtAEAO4r1xj 0kPJ7bsb9aswC/ndv4hzlcnLg4jfY7JxOM3+DCfnacAjPkqM51leHK9fyGo+WfeBADru KhTci/DJOpJ/708fEX+zR2JqGkCdDLaKjvNhcZKer9O+/9dXSbFu2HMZWq9DTGHsAuFE mmRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=a/VVtCw4; 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 u24si133451pfh.326.2018.03.13.06.47.10; Tue, 13 Mar 2018 06:47:24 -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=a/VVtCw4; 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 S1752857AbeCMNqS (ORCPT + 99 others); Tue, 13 Mar 2018 09:46:18 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:35132 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751852AbeCMN0u (ORCPT ); Tue, 13 Mar 2018 09:26:50 -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=xGQQGmjllep0kxYKulB0Lo0kYYvyd9LburzqxePTOck=; b=a/VVtCw4b7Z6m+NvTHslwRwq7 pEEF1N1LSd1jVYTarR8W7Y4ApuLK7w3XzHlDCHMGjthCNz/DffFjYSPO7F65uTDnXB2zd7yES+Oj5 KDoKgvjrOBFH7DRHmaWxa9aJZCIfkoYsV1W2rxEia4iIDk8TxJBLFtYmR834V0zKokt/GoDbDa2Sh c/vQJShoRFpabF5RZ6zR9yEozV4KLcU2nlqdWOfZ5K4idw4nJRvD+rLO1c47kRVqtYOpKRPwMrg5r HZyfgEUXnFW5u29sSiK61H4025nX1qrpo5A8IpOzjY0wSrowGiIncxs5sB5O+MPtkth6VIinQqiWz 9ObneiUHA==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1evjx2-0004cM-Nw; Tue, 13 Mar 2018 13:26:48 +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 19/61] xarray: Add xa_extract Date: Tue, 13 Mar 2018 06:25:57 -0700 Message-Id: <20180313132639.17387-20-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 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 cf7966bfdd3e..85dd909586f0 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 267510e98a57..124bbfec66ae 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1388,6 +1388,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_tag(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.16.1