From patchwork Sun Jun 17 01:59:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 10468267 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 52C8E603B4 for ; Sun, 17 Jun 2018 02:02:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 420AD28C6A for ; Sun, 17 Jun 2018 02:02:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 34A6828C75; Sun, 17 Jun 2018 02:02:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A362328C6A for ; Sun, 17 Jun 2018 02:02:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74CE06B0281; Sat, 16 Jun 2018 22:01:12 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6AD056B0282; Sat, 16 Jun 2018 22:01:12 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 503616B0283; Sat, 16 Jun 2018 22:01:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f198.google.com (mail-pf0-f198.google.com [209.85.192.198]) by kanga.kvack.org (Postfix) with ESMTP id E52676B027F for ; Sat, 16 Jun 2018 22:01:11 -0400 (EDT) Received: by mail-pf0-f198.google.com with SMTP id p16-v6so6649514pfn.7 for ; Sat, 16 Jun 2018 19:01:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=6CtH22NpoaemDSFVaBuH/UKCnAIo12i5SDA7CsPpPB8=; b=t2/ji6zJ+Z0I5a9sq+ZD/11Zbiv9R+N9sxRNtA1wMQwvPgXg7F16ucwqxYsLFSBOSE ETqnqAIM2kl8yEDruYH6wo2GFoLQUHv6hByOVKeOjg+2HNRj9SX5X4LSJWp8O5dgGbpF wGioynbKDjlDrC27oQvBn3WZVBFChzNi3uzCCncntsw6VVsKJ3QXuNL26UBdwgulw597 dwAr0d2SV1VF8jUqCgtdjjLsKbzbVhqqrroB6MbXz4AvMHjF87s/v0gyMiRMxjLZm9ia h6Ra5ye2sqLG4bmXoy2vcyLts/n1d31eRC/ZldgPVGnD9YQHCLXEsXd7Ibiy/Lz1UVtk zXjw== X-Gm-Message-State: APt69E2Nzh7t7DFFqLhwN4VJkaDvyvadf1HSqu7AHPCkgfDWxIJPepr8 qLozmz0VOn9n7EIon6ULF5mlgT8S3w3tIfjKeIG9tGunLAqbei33M+lwFa+meAAs35+qkBdnh2Q rzvmV446BfLclZG/OY4NcZDDQGfTVVVNqzgNgtr/HxI47gQkwlc8+N4sT1acKqUxoKQ== X-Received: by 2002:a17:902:ab95:: with SMTP id f21-v6mr8227680plr.264.1529200871626; Sat, 16 Jun 2018 19:01:11 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJfD/axPyKe42GZjBGmkux7ZFBHw8ilY/y0TvrkdJVe5swC67vxRHAAaGm7oLAOoAJ7VQFK X-Received: by 2002:a17:902:ab95:: with SMTP id f21-v6mr8227635plr.264.1529200870730; Sat, 16 Jun 2018 19:01:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529200870; cv=none; d=google.com; s=arc-20160816; b=YRS+13sh/jEiG6gWZRkNnZIIwrdpMjW2kcxRFlIC5iShE/ODm4TnOYvNMNm421FF7X U3UC1KXlJx+TQVZwZXO38B/WpIrqWtKZhBxGV1LA4U0ky7l3XgtAj3nqtY0Ea2TE7daV FPhylEzPuaDUjCwWW2GUTcmAfSNcS/ZCJRRyoZp3P9ReREPAn2j0CcFGUb3K5nZln810 ZovRo8pfIzquAW1aSM2iy94VHyjFG1Nzl7iI8kErhwjkz0a7oowZdk4+Wj4rMv9dRCVi HHCBYOhbDwgTVhO/rgJZHvVqGQJtkl4NWmhQL0xVPAI2pwOEBMBn8tr4GqBRJsHi0x2d 6idw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=6CtH22NpoaemDSFVaBuH/UKCnAIo12i5SDA7CsPpPB8=; b=kDXJPwsUbHsVNYC0FmbM58QScq+8XVmXWsCGvASY0CL7P/ymKgKmSm/pIOicd4eZVE P3qepgmDpWOb1NFstFW8e0A+UVllw5D9xIHTbNDtptdtvlxtbmtPj9CxEvf0Q40UIAR8 0H4N1+5WIsr0tcWQh+BCRVz6PxrUitcI4nSNlvzmRT1eHQ7h2vyTRXt0cTvCE9stOiQT TKhd/AfzURe4roW/3NmC/G6xGo1MAL3F2oN87BShGg+HEcx9aWYgiJ1oHciSMTepdmpf UMp77TCG3G6Img06ZWdw2lqojyf32Qr5toJgm0IcxKBR2dgDAXGy0ZAbIS4iYVHRgsd7 5FfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20170209 header.b=enZlbfI9; spf=pass (google.com: best guess record for domain of willy@infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom=willy@infradead.org Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id x3-v6si11659436pfi.230.2018.06.16.19.01.10 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 16 Jun 2018 19:01:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of willy@infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20170209 header.b=enZlbfI9; spf=pass (google.com: best guess record for domain of willy@infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom=willy@infradead.org 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=6CtH22NpoaemDSFVaBuH/UKCnAIo12i5SDA7CsPpPB8=; b=enZlbfI9wXXz6+EBEkFiy9m/k kt+C1t9v+DOjsgRjSlROOtXSExB0JfbGElKzRRlkpDixFmAsj7NqoYl2vkaNRaAV9e2qsBLbawMd7 2V+NPrpcpZr6Tz9vH6txcgm1gsZQEthoYzoWVpuIbZN9iKx8Nczisl6Kf7ODKpnWM5U4nNqJUzKQU k2dxk7v2a30hWRXlDFVIobiPkbwfGy5H5+5On4MFGEIG4gDqydppCgcT8oHofRynWzWHq3ynIWJKE yI4DbKG1dLpTYowlYpBvwZBWXNe7JCH1ajp+0EW+pTITfO03ovzN3TENi+EQ8pMTkW4/YaYTdvDrE 75myFGVhg==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1fUMzw-0001GS-Lp; Sun, 17 Jun 2018 02:00:56 +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 v14 14/74] xarray: Extract entries from an XArray Date: Sat, 16 Jun 2018 18:59:52 -0700 Message-Id: <20180617020052.4759-15-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180617020052.4759-1-willy@infradead.org> References: <20180617020052.4759-1-willy@infradead.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The xa_extract 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 0790b3b098f0..6f398732b664 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 8756ca22363e..1e15ead40d78 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1413,6 +1413,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) {