Received: by 10.223.185.116 with SMTP id b49csp4150149wrg; Mon, 19 Feb 2018 12:04:32 -0800 (PST) X-Google-Smtp-Source: AH8x2266z3F6t29FSv03u4Ly6wjIaBHibogL0HZtxR9X4J2LQVVRZ5qk8QTR3gSlwUKIOquFJjFC X-Received: by 2002:a17:902:8c86:: with SMTP id t6-v6mr15504197plo.400.1519070672428; Mon, 19 Feb 2018 12:04:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519070672; cv=none; d=google.com; s=arc-20160816; b=lqA3XtUAl5Gby5d6sVCq2g6c87LQxvRqsN/HLN+9zPjQtE5wLCTHK4Kb1rhqrchV5w oz2ZtRF6yL5tjbh/YmGDwA7A8R1nNgCt42tnT5CkQAXz70sX/4RAt5lqMDTvPGF7Cegw macITG8la/jo6/S18XFoagXNlSI6Iydq0kfbLEEZMXMdTqqTes7/Dylo+6XOwxDzt1iR LUDKn+wBEV615Lyim9fypNwahURy9kG0+p4qntHiDXUOXwD0E3xRRRE4ORbacaBSO8B2 DVQiRHJn+MsL+1b1zvaJc2lHr6BAdqxk/lO+ymTDPWJ3TukXnHAwgddZHXOWnDSdthAB QHiQ== 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=ETbmWvKLc/XbTavNw/xs4F1bFZEDdVCEMwUI1jg5MsbzUot7fEEtmnSqGriByumfit uTTkRnLd5KPXllcgjbD0e3NarBaED5zvo15RuBIXh/o+BCn4WTAs0A8D8nCXnbVjLNgR Bs6PjVLaX57yPsvBUmXe8Hvjx73vHHNcrSvfiHh5K+ij/1yA0FHU44Ikt7aQCkLlT37z UFwhuevvzF7kNJA8fUUdCmEMLfuNl3gT029OZQg5eyk5Td2Zu3NDx7rhJuxoB83iU7Lt dyagbvYqGnoTcFekbzS0co+6ZVpwRonTkwTY/g9kVDGawlurHmKeIMaB3kE+2jdVAFre BgSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=JTN/Pfxj; 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 z62si5366383pgd.688.2018.02.19.12.04.18; Mon, 19 Feb 2018 12:04:32 -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=JTN/Pfxj; 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 S1753900AbeBSUBX (ORCPT + 99 others); Mon, 19 Feb 2018 15:01:23 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:44656 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753693AbeBSTqN (ORCPT ); Mon, 19 Feb 2018 14:46:13 -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=xGQQGmjllep0kxYKulB0Lo0kYYvyd9LburzqxePTOck=; b=JTN/PfxjYsP01TfylJDEldPaq hnahh0ljemJMAjWhNwc9/g621BbHoHo9+m1PBrWmNhkbWeG2E9iGYLRgZtMA3kp1K8r2iVAfvpDQZ RU8bguWTM0i2YSEIi4tEUJz8mpNXU82YVd+2bpvkZ3qeq9z9v5/lDcMmLEUyJQZougvJQG8JMohIC sk7ZbMbxEZ4CGwV61Myy7O6AxeVj2p59VFF3i2+4x/qros+zM69cP+EmevCyaQF4PGSNdS4bq3wqY pn3Xn7uLkFsjfOSlJYZvbWfq/5D2k0ZSyDyFk4lYDDZNpO3QtwurRFyi/trnq5sl7Ha6Mzf22U5T8 7Jl2H3BUw==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1enrO7-0001mk-Jl; Mon, 19 Feb 2018 19:46:11 +0000 From: Matthew Wilcox To: Andrew Morton Cc: Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v7 20/61] xarray: Add xa_extract Date: Mon, 19 Feb 2018 11:45:15 -0800 Message-Id: <20180219194556.6575-21-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180219194556.6575-1-willy@infradead.org> References: <20180219194556.6575-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