Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp182002imm; Thu, 31 May 2018 21:50:51 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIIfP5Kfra4OZydpHF5aBryfs4KrOP/m1r6r7YvyipeqlNmtjJ+zOUc7faJu0w4HhzVQFxG X-Received: by 2002:a17:902:6bca:: with SMTP id m10-v6mr9646527plt.6.1527828651539; Thu, 31 May 2018 21:50:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527828651; cv=none; d=google.com; s=arc-20160816; b=bgfUVD6cqQVMc1BmA0wCXx+CCRC/WqFMlDVwwR5tyyUOWpBec0OHPGf35+fwsg5+c+ XkiTcQLqyV76nRmYgw2WACrM3RAy51MXCQNcQX//KPwi3QPRUdBURHjH8sVV6tw/xHrE 7IgcCxPprU5KJ4BBoacX/0hWX4yCRbkG9Gh5wa6bElcdS0xCuVietFFHpRf3q25zSUrA yjFcBCgYeXbiu99lcH2UWsJEbnnQ+DET0kMIpBsF04d6K+zF6iKbq7QykvBNmnC+2qPr Y6rh/EHqYEozD0xzoYiTpGc+iBaIOdybSZL1S4QofZiw3F8m8sPRpYlI39ATcXRcwt20 f7xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:cc:subject:date:to :from:arc-authentication-results; bh=i/EAcflWtOZ4PltRaU142ZOdAsbS1pLzw+d91w04cN0=; b=GltF7O23AzNqatMy9HYbanecgT17ZfgbnwI91qFWDvZfkFTDnnhQnAdAnsVyulCInP ZHm/DTuNDuW6iW1PO9Y4r2Xi6okELfN1H53Q8Y6oX4zYdK1kyzPd4KBeGFsyGTGM7i0L nSp5rxphLBdYWgsZQj8PXucrAC1cRIhWsRRdwuhecp07lIaEbkBL3/q9nPwF/yPxxvra bs6Y5/GWeZn5tKuZ+R743rhdVsQLv/g52Bx9bYY+pGpbELxF2IDDZ2C0VTacsBMZ6nqx PObsFbyMiFFtYnagFjhAmh3ftvFbHEVhptU75+DhjSoo84f6EpF4mfshTkPqo1NRK8TG MNUQ== ARC-Authentication-Results: i=1; mx.google.com; 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 bg8-v6si39010516plb.486.2018.05.31.21.50.07; Thu, 31 May 2018 21:50:51 -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; 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 S1751716AbeFAEqY (ORCPT + 99 others); Fri, 1 Jun 2018 00:46:24 -0400 Received: from mx2.suse.de ([195.135.220.15]:34134 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751678AbeFAEqU (ORCPT ); Fri, 1 Jun 2018 00:46:20 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 0617BADE3; Fri, 1 Jun 2018 04:46:19 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Fri, 01 Jun 2018 14:44:09 +1000 Subject: [PATCH 12/18] rhashtable: add rhashtable_walk_prev() Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <152782824973.30340.11914706498791212568.stgit@noble> In-Reply-To: <152782754287.30340.4395718227884933670.stgit@noble> References: <152782754287.30340.4395718227884933670.stgit@noble> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org rhashtable_walk_prev() returns the object returned by the previous rhashtable_walk_next(), providing it is still in the table (or was during this grace period). This works even if rhashtable_walk_stop() and rhashtable_talk_start() have been called since the last rhashtable_walk_next(). If there have been no calls to rhashtable_walk_next(), or if the object is gone from the table, then NULL is returned. This can usefully be used in a seq_file ->start() function. If the pos is the same as was returned by the last ->next() call, then rhashtable_walk_prev() can be used to re-establish the current location in the table. If it returns NULL, then rhashtable_walk_next() should be used. Signed-off-by: NeilBrown --- include/linux/rhashtable.h | 1 + lib/rhashtable.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h index c612d2446d90..1b70d690ab65 100644 --- a/include/linux/rhashtable.h +++ b/include/linux/rhashtable.h @@ -247,6 +247,7 @@ static inline void rhashtable_walk_start(struct rhashtable_iter *iter) } void *rhashtable_walk_next(struct rhashtable_iter *iter); +void *rhashtable_walk_prev(struct rhashtable_iter *iter); void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); void rhashtable_free_and_destroy(struct rhashtable *ht, diff --git a/lib/rhashtable.c b/lib/rhashtable.c index 5ab0f4825271..a185f5a8b34d 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -915,6 +915,37 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter) } EXPORT_SYMBOL_GPL(rhashtable_walk_next); +/** + * rhashtable_walk_prev - Return the previously returned object, if available + * @iter: Hash table iterator + * + * If rhashtable_walk_next() has previously been called and the object + * it returned is still in the hash table, that object is returned again, + * otherwise %NULL is returned. + * + * If the recent rhashtable_walk_next() call was since the most recent + * rhashtable_walk_start() call then the returned object may not, strictly + * speaking, still be in the table. It will be safe to dereference. + * + * Note that the iterator is not changed and in particular it does not + * step backwards. + */ +void *rhashtable_walk_prev(struct rhashtable_iter *iter) +{ + struct rhashtable *ht = iter->ht; + struct rhash_head *p = iter->p; + + if (!p) + return NULL; + if (!iter->p_is_unsafe || ht->rhlist) + return p; + rht_for_each_rcu(p, iter->walker.tbl, iter->slot) + if (p == iter->p) + return p; + return NULL; +} +EXPORT_SYMBOL_GPL(rhashtable_walk_prev); + /** * rhashtable_walk_stop - Finish a hash table walk * @iter: Hash table iterator