From patchwork Mon Jun 11 14:05:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 10457865 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 580786020F for ; Mon, 11 Jun 2018 14:07:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4538C28451 for ; Mon, 11 Jun 2018 14:07:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 39A1B2846F; Mon, 11 Jun 2018 14:07:18 +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 B2ACF28451 for ; Mon, 11 Jun 2018 14:07:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7B856B026E; Mon, 11 Jun 2018 10:06:47 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BB5706B026D; Mon, 11 Jun 2018 10:06:47 -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 80B396B000A; Mon, 11 Jun 2018 10:06:47 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f200.google.com (mail-pf0-f200.google.com [209.85.192.200]) by kanga.kvack.org (Postfix) with ESMTP id 0E5ED6B026D for ; Mon, 11 Jun 2018 10:06:47 -0400 (EDT) Received: by mail-pf0-f200.google.com with SMTP id a12-v6so10267376pfn.12 for ; Mon, 11 Jun 2018 07:06:47 -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=QHopodgOckH0JnTMC74cz/L5jvYaTcy0lLTV0wcZbXM=; b=KG3kBaSityg5HnrzRwV6nto8x+vbBJOlZJip/Xqw7B9IYhx4vo33Bn9jcrfYYpR7lQ 9yhw2thUB7qS1EWJgg85GpuBVG3QDyS7mD3CIjOAKJQC4ewWoHs8Ml3hMtDiQZX7lv1N j5khSIjKhX3vUMk0CVW/p4uPXqFG652YIiKSnYLhVFnwFZfVz2qxAhRvuykaXaY4FMRa xnGD/Reie5diC7By0Njo6F4paeRFZeHy2exvuO0Fh4tA9+/zX6feyClq/ZI0PYg8IDRN 2HCXvd42J6JooXROcXcNwyJxryi4nsfq3MEfQfHiX7sBdsQitJHHqH7nvBcOgR7ZgItj XTfQ== X-Gm-Message-State: APt69E1bf9mpnvZbyDj+byPiV4Cq6ZdoVp4a0lwervIKoJnqPIafEj/P Z59nRmtumt9XeWoAvt6VIu//97o16+Zqv1OoKX2gYYiXU84+Ny9pCMGuRHaOaL7fyFv9+CsFPv9 LbTnSaKoSvAoSsKMnN0wHbXYSoclqEK1fHlA2SdGN2nDqky/LEQ6G5OWUSKcBeuFysw== X-Received: by 2002:a65:6397:: with SMTP id h23-v6mr15018362pgv.380.1528726006740; Mon, 11 Jun 2018 07:06:46 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJr/V5KLG+nGYay+B97QPyLzCKtEM+niC0J6n4oyAnZq3wcKBkX0l2Hs7rdK996QHJN1UtA X-Received: by 2002:a65:6397:: with SMTP id h23-v6mr15018309pgv.380.1528726005761; Mon, 11 Jun 2018 07:06:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528726005; cv=none; d=google.com; s=arc-20160816; b=tQNZMM17uulnEgWWvwAs1w1YGirqEXM7JRH3+Vh7mZ5PYlVE1Sgo7hQ0NMZ7LaFYzc Gy0F6/qPhrDtWlrK2jSU/uAL19XNb+Nfu1kb9pcpQCvo6fkQQoXJvHWSb0dDrd1ka7bC ITzYmQeQ6p8m3jmHtDSsIyaGIQwEjNbIhpybzWnLa/ZgcM00IK5uaLo75JgARq7fBJL5 gDjVnOj64XFfjvETSIAsaLm2+3Dgz+943E/QkW/i9VHgrsqLv5QqcW3ngwlq/RYQ9i1T ClYSlPlJjB7pFvmrRBmDdFs6zIWdLCG0QW7em/I8pHS6ToBewF093/eA7wzIAq2lMYOV zPMA== 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=QHopodgOckH0JnTMC74cz/L5jvYaTcy0lLTV0wcZbXM=; b=YPSSQo67l+Dj7BZbqU/JcQYR0CaylloPvKS7cUNF6S44rDrAiTDCbEq9LzcwoPyzIM Z21g7aCYx1qP8szdIAc3Lg9dgpX/y2zGE/A6QiWbpiE8wmEOJCncwb0l71L8NS/GZNRO VFlBeLewB/sj4Cr6X7gCA2epLEad4nRKeKly0aouat10lL1V8PzlFZ5yajplRZWH9WwY E/pEBY1shYemU1EvCK6T466Ko/bbCrJTDtHLwWhaCqQ1GFpq/4uyR2Iapvrt3+1CGMdm xHS3WCfbIkJZcmsiEehOHSX3dtkMgshnHpTU0lDppWXICi16el05lN7D3zglAA9blVH7 47ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20170209 header.b=GecsTcI+; 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 g5-v6si25301565pgp.91.2018.06.11.07.06.45 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 11 Jun 2018 07:06:45 -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=GecsTcI+; 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=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> 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 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) {