Received: by 10.223.164.202 with SMTP id h10csp667393wrb; Wed, 22 Nov 2017 13:21:57 -0800 (PST) X-Google-Smtp-Source: AGs4zMZSCUxLw+lCF+aLZh1h+kRHaI2cvQlzOZkHXRGxMZAH0x7ShbmZ1uRtNDKRnb885o4BTX2a X-Received: by 10.84.179.165 with SMTP id b34mr23177344plc.17.1511385717869; Wed, 22 Nov 2017 13:21:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511385717; cv=none; d=google.com; s=arc-20160816; b=pjAQniT2nkVsjU46mBatbrg+YJk5UNF29HGyyOneQL4xuXDGo6iQtCWxK55M47sS5D eDKs7jCYGRC5xVAZCmr0Q6l3331edahUyUJcj35DIdIxct36OLTBcZe4Ajv3rsxj0grB nZfvHIRe6bRKf1aiQC9H/4k/Mu4kj/v4JX+OZWcLzJK+d2ZUWNycu4abjElj+mVzNDtB 5NPA3rEu7JgSBT8CF6AmCKwTbnWxGm8fMQj+ed1ln77GAqUqNmjYUqJJR1TYSn1gbI6C lIzb71gIkM1GiaobcWYIhpCziCwqKZsK5jQ8dnU9paNUoca7/x68duUaFzeFrncpmzAG 7nHw== 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=r8r9rTID0TmSmSrmypLD+kYqB7aLq5lvQixGoFPpaNQ=; b=qG2IQ83/w6I0h0MJERKiGS6/vrs4M6K0J+V6M7nwVcJPluXnsGMwpoWy+oFOSn2iDl XahsY6EW0E7Qv6OcntX7V6JkVrxs1cBgH6yMnvMzIRuUlbkWKNJsJuF5mfrJUvpWli5D ugx6uHSpFAn4xcsbFuUuOo4+Q9ZwBoXDCwHm4NCehAB0ydg6i98daCxwmBAqh+Pa2syU xuc1aky2fEiS5cU4Q7xdD8MdnpzrYH+ASSn6oh0Uv8vZGnXgg9K4z4x758PMHAG1foNa QHd8PFXPQo7fbllWQQbmy+aEN4reX4lYMSujavzjR2SA/HHLu+4+tCCAFiWF9/y9z6YC TebA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=LLpeuNou; 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 f9si14337772plo.763.2017.11.22.13.21.46; Wed, 22 Nov 2017 13:21:57 -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=LLpeuNou; 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 S1752151AbdKVVUe (ORCPT + 77 others); Wed, 22 Nov 2017 16:20:34 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:48632 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751759AbdKVVIS (ORCPT ); Wed, 22 Nov 2017 16:08:18 -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=r8r9rTID0TmSmSrmypLD+kYqB7aLq5lvQixGoFPpaNQ=; b=LLpeuNouD3fACqZa68V+lpjSR CUg83u0DNFkfORSfwfkHtklWKM+oZtEAq39ZSZnDwyoDexyJ7Jn6cO8xI36kXvBcNVik6G6kuRXmb TX0EUk8hzN9zlsAxk6kkqiWeIOxECgVk8DNny8aNhGbb6IhWEoNQbz7eggrSuz8CSYl07/1d/jTYd YcHMwLVIMOX/ajFq3xsAHhLfiPGdBV24P/DkV//7UZVHW/EXXTtLv1xgRMCAQqzeGj68rQIsW6b1/ gHigKWePd6VjMzCuk47vciH9hgLtd/CSt8VBvyVs+W67OCOgWNG9Efg5LFMVueMdlBAdZ/yxVBbUV Tu7PaNVHQ==; Received: from willy by bombadil.infradead.org with local (Exim 4.87 #1 (Red Hat Linux)) id 1eHcFl-0007uK-OG; Wed, 22 Nov 2017 21:08:17 +0000 From: Matthew Wilcox To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Matthew Wilcox Subject: [PATCH 26/62] xarray: Add xas_for_each_tag Date: Wed, 22 Nov 2017 13:07:03 -0800 Message-Id: <20171122210739.29916-27-willy@infradead.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171122210739.29916-1-willy@infradead.org> References: <20171122210739.29916-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 iterator operates across each tagged entry in the specified range. We do not yet have a user for an xa_for_each_tag iterator, but it would be straight-forward to add one if needed. This commit also includes xas_find_tag() and xas_next_tag(). Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 70 +++++++++++++++++++++++++++++++++++++++++++ lib/xarray.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 19a3974fdc4f..427c792ddb2a 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -465,6 +465,8 @@ void *xas_find(struct xarray *, struct xa_state *, unsigned long max); bool xas_get_tag(const struct xarray *, const struct xa_state *, xa_tag_t); void xas_set_tag(struct xarray *, const struct xa_state *, xa_tag_t); void xas_clear_tag(struct xarray *, const struct xa_state *, xa_tag_t); +void *xas_find_tag(struct xarray *, struct xa_state *, unsigned long max, + xa_tag_t); void xas_init_tags(struct xarray *, const struct xa_state *); void xas_destroy(struct xa_state *); @@ -566,6 +568,55 @@ static inline void *xas_next(struct xarray *xa, struct xa_state *xas, return entry; } +static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, + xa_tag_t tag) +{ + unsigned long *addr = xas->xa_node->tags[(__force unsigned)tag]; + unsigned int offset = xas->xa_offset; + + if (advance) + offset++; + if (XA_CHUNK_SIZE == BITS_PER_LONG) { + unsigned long data = *addr & (~0UL << offset); + if (data) + return __ffs(data); + return XA_CHUNK_SIZE; + } + + return find_next_bit(addr, XA_CHUNK_SIZE, offset); +} + +/** + * xas_next_tag() - Advance iterator to next tagged entry. + * @xa: XArray. + * @xas: XArray operation state. + * @max: Highest index to return. + * @tag: Tag to search for. + * + * xas_next_tag() is an inline function to optimise xarray traversal for + * speed. It is equivalent to calling xas_find_tag(), and will call + * xas_find_tag() for all the hard cases. + * + * Return: The next tagged entry after the one currently referred to by @xas. + */ +static inline void *xas_next_tag(struct xarray *xa, struct xa_state *xas, + unsigned long max, xa_tag_t tag) +{ + struct xa_node *node = xas->xa_node; + unsigned int offset; + + if (unlikely(xas_not_node(node) || node->shift)) + return xas_find_tag(xa, xas, max, tag); + offset = xas_find_chunk(xas, true, tag); + xas->xa_offset = offset; + xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset; + if (xas->xa_index > max) + return NULL; + if (offset == XA_CHUNK_SIZE) + return xas_find_tag(xa, xas, max, tag); + return xa_entry(xa, node, offset); +} + /** * xas_for_each() - Iterate over a range of an XArray * @xa: XArray. @@ -584,4 +635,23 @@ static inline void *xas_next(struct xarray *xa, struct xa_state *xas, for (entry = xas_find(xa, xas, max); entry; \ entry = xas_next(xa, xas, max)) +/** + * xas_for_each_tag() - Iterate over a range of an XArray + * @xa: XArray. + * @xas: XArray operation state. + * @entry: Entry retrieved from array. + * @max: Maximum index to retrieve from array. + * @tag: Tag to search for. + * + * The loop body will be executed for each tagged entry in the xarray + * between the current xas position and @max. @entry will be set to + * the entry retrieved from the xarray. It is safe to delete entries + * from the array in the loop body. You should hold either the RCU lock + * or the xa_lock while iterating. If you need to drop the lock, call + * xas_pause() first. + */ +#define xas_for_each_tag(xa, xas, entry, max, tag) \ + for (entry = xas_find_tag(xa, xas, max, tag); entry; \ + entry = xas_next_tag(xa, xas, max, tag)) + #endif /* _LINUX_XARRAY_H */ diff --git a/lib/xarray.c b/lib/xarray.c index 59f45c07988f..ea2dbd343380 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -868,6 +868,86 @@ void *xas_find(struct xarray *xa, struct xa_state *xas, unsigned long max) } EXPORT_SYMBOL_GPL(xas_find); +/** + * xas_find_tag() - Find the next tagged entry in the XArray. + * @xa: XArray. + * @xas: XArray operation state. + * @max: Highest index to return. + * @tag: Tag number to search for. + * + * If the xas has not yet been walked to an entry, return the tagged entry + * which has an index >= xas.xa_index. If it has been walked, the entry + * currently being pointed at has been processed, and so we move to the + * next tagged entry. + * + * If no tagged entry is found and the array is smaller than @max, @xas is + * set to the restart state and xas->xa_index is set to the smallest index + * not yet in the array. This allows @xas to be immediately passed to + * xas_create(). + * + * Return: The entry, if found, otherwise NULL. + */ +void *xas_find_tag(struct xarray *xa, struct xa_state *xas, unsigned long max, + xa_tag_t tag) +{ + bool advance = true; + unsigned int offset; + void *entry; + + if (xas_error(xas)) + return NULL; + + if (xas->xa_node == NULL) { + xas->xa_index = 1; + goto out; + } else if (xas->xa_node == XAS_RESTART) { + advance = false; + entry = xa_head(xa); + if (xas->xa_index > max_index(entry)) + goto out; + if (!xa_is_node(entry)) { + if (xa_tagged(xa, tag)) { + xas->xa_node = NULL; + return entry; + } + xas->xa_index = 1; + goto out; + } + xas->xa_node = xa_to_node(entry); + xas->xa_offset = xas->xa_index >> xas->xa_node->shift; + } + + while (xas->xa_index <= max) { + if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) { + xas->xa_offset = xas->xa_node->offset + 1; + xas->xa_node = xa_parent(xa, xas->xa_node); + if (!xas->xa_node) + break; + advance = false; + continue; + } + + offset = xas_find_chunk(xas, advance, tag); + xas_add(xas, offset - xas->xa_offset); + if (offset == XA_CHUNK_SIZE) { + advance = false; + continue; + } + + entry = xa_entry(xa, xas->xa_node, xas->xa_offset); + if (!xa_is_node(entry)) + return entry; + xas->xa_node = xa_to_node(entry); + xas->xa_offset = get_offset(xas->xa_index, xas->xa_node); + } + + out: + if (!xas->xa_node) + xas->xa_node = XAS_RESTART; + return NULL; +} +EXPORT_SYMBOL_GPL(xas_find_tag); + /** * __xa_init() - Initialise an empty XArray * @xa: XArray. -- 2.15.0 From 1584808283853980133@xxx Wed Nov 22 22:49:16 +0000 2017 X-GM-THRID: 1584808283853980133 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread