All of lore.kernel.org
 help / color / mirror / Atom feed
From: Herbert Xu <herbert@gondor.apana.org.au>
To: Thomas Graf <tgraf@suug.ch>, Ying Xue <ying.xue@windriver.com>,
	davem@davemloft.net, kaber@trash.net, paulmck@linux.vnet.ibm.com,
	netdev@vger.kernel.org, netfilter-devel@vger.kernel.org
Subject: [PATCH 1/2] rhashtable: Introduce rhashtable_walk_*
Date: Wed, 28 Jan 2015 10:20:48 +1100	[thread overview]
Message-ID: <E1YGFRA-0002yN-I0@gondolin.me.apana.org.au> (raw)
In-Reply-To: 20150127231950.GA11374@gondor.apana.org.au

Some existing rhashtable users get too intimate with it by walking
the buckets directly.  This prevents us from easily changing the
internals of rhashtable.

This patch adds the helpers rhashtable_walk_init/next/end which
will replace these custom walkers.

They are meant to be usable for both procfs seq_file walks as well
as walking by a netlink dump.  The iterator structure should fit
inside a netlink dump cb structure, with at least one element to
spare.
  
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---

 include/linux/rhashtable.h |   44 +++++++++++++++++++++
 lib/rhashtable.c           |   91 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+)

diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
index 6d7e840..b03b375 100644
--- a/include/linux/rhashtable.h
+++ b/include/linux/rhashtable.h
@@ -18,6 +18,7 @@
 #ifndef _LINUX_RHASHTABLE_H
 #define _LINUX_RHASHTABLE_H
 
+#include <linux/compiler.h>
 #include <linux/list_nulls.h>
 #include <linux/workqueue.h>
 #include <linux/mutex.h>
@@ -123,6 +124,22 @@ struct rhashtable {
 	bool                            being_destroyed;
 };
 
+/**
+ * struct rhashtable_iter - Hash table iterator
+ * @ht: Table to iterate through
+ * @p: Current pointer
+ * @lock: Slot lock
+ * @slot: Current slot
+ * @skip: Number of entries to skip in slot
+ */
+struct rhashtable_iter {
+	struct rhashtable *ht;
+	struct rhash_head *p;
+	spinlock_t *lock;
+	unsigned int slot;
+	unsigned int skip;
+};
+
 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
 {
 	return NULLS_MARKER(ht->p.nulls_base + hash);
@@ -178,6 +195,33 @@ bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
 				      bool (*compare)(void *, void *),
 				      void *arg);
 
+/**
+ * rhashtable_walk_init - Initialise an iterator
+ * @ht:		Table to walk over
+ * @iter:	Hash table Iterator
+ *
+ * This function prepares a hash table walk.
+ * Note that if you restart a walk after rhashtable_walk_stop you
+ * may see the same object twice.  Also, you may miss objects if
+ * there are removals in between rhashtable_walk_stop and the next
+ * call to rhashtable_walk_start.
+ *
+ * For a completely stable walk you should construct your own data
+ * structure outside the hash table.
+ */
+static inline void rhashtable_walk_init(struct rhashtable *ht,
+					struct rhashtable_iter *iter)
+{
+	iter->ht = ht;
+	iter->p = NULL;
+	iter->slot = 0;
+	iter->skip = 0;
+}
+
+int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
+void *rhashtable_walk_next(struct rhashtable_iter *iter);
+void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
+
 void rhashtable_destroy(struct rhashtable *ht);
 
 #define rht_dereference(p, ht) \
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 71c6aa1..d51fb06 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -813,6 +813,97 @@ exit:
 }
 EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
 
+/**
+ * rhashtable_walk_start - Start a hash table walk
+ * @iter:	Hash table iterator
+ *
+ * Start a hash table walk.
+ *
+ * Returns zero if successful.  Returns -EINTR if we couldn't
+ * obtain the resize lock.
+ */
+int rhashtable_walk_start(struct rhashtable_iter *iter)
+{
+	struct rhashtable *ht = iter->ht;
+	int err;
+
+	err = mutex_lock_interruptible(&ht->mutex);
+	rcu_read_lock();
+
+	if (!err)
+		mutex_unlock(&ht->mutex);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(rhashtable_walk_start);
+
+/**
+ * rhashtable_walk_next - Return the next object and advance the iterator
+ * @iter:	Hash table iterator
+ *
+ * Note that you must call rhashtable_walk_stop when you are finished
+ * with the walk.
+ *
+ * Returns the next object or NULL when the end of the table is reached.
+ */
+void *rhashtable_walk_next(struct rhashtable_iter *iter)
+{
+	const struct bucket_table *tbl;
+	struct rhashtable *ht = iter->ht;
+	struct rhash_head *p = iter->p;
+
+	tbl = rht_dereference_rcu(ht->tbl, ht);
+
+	if (p) {
+		p = rht_dereference_bucket(p->next, tbl, iter->slot);
+		goto next;
+	}
+
+	for (; iter->slot < tbl->size; iter->slot++) {
+		int skip = iter->skip;
+
+		iter->lock = bucket_lock(tbl, iter->slot);
+		spin_lock_bh(iter->lock);
+
+		rht_for_each(p, tbl, iter->slot) {
+			if (!skip)
+				break;
+			skip--;
+		}
+
+next:
+		if (!rht_is_a_nulls(p)) {
+			iter->skip++;
+			iter->p = p;
+			return rht_obj(ht, p);
+		}
+		spin_unlock_bh(iter->lock);
+
+		iter->skip = 0;
+	}
+
+	iter->p = NULL;
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(rhashtable_walk_next);
+
+/**
+ * rhashtable_walk_stop - Finish a hash table walk
+ * @iter:	Hash table iterator
+ *
+ * Finish a hash table walk.
+ */
+void rhashtable_walk_stop(struct rhashtable_iter *iter)
+{
+	if (iter->p)
+		spin_unlock_bh(iter->lock);
+
+	rcu_read_unlock();
+
+	iter->p = NULL;
+}
+EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
+
 static size_t rounded_hashtable_size(struct rhashtable_params *params)
 {
 	return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),

  reply	other threads:[~2015-01-27 23:20 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-20 13:20 [PATCH 0/3 net-next] rhashtable: Notify on resize to allow signaling interrupted dumps Thomas Graf
2015-01-20 13:20 ` [PATCH 1/3] rhashtable: Provide notifier for deferred resizes Thomas Graf
2015-01-20 13:20 ` [PATCH 2/3] netlink: Mark dumps as inconsistent which have been interrupted by a resize Thomas Graf
2015-01-21  8:13   ` Ying Xue
2015-01-21 12:17     ` Thomas Graf
2015-01-22  8:49       ` Herbert Xu
2015-01-22  8:56         ` Patrick McHardy
2015-01-22  9:22           ` Herbert Xu
2015-01-22 10:07             ` Patrick McHardy
2015-01-25 23:20         ` [PATCH 0/2] rhashtable: Add walk iterator primitives and use them in netlink Herbert Xu
2015-01-25 23:21           ` [PATCH 1/2] rhashtable: Introduce rhashtable_walk_* Herbert Xu
2015-01-26  8:20             ` Thomas Graf
2015-01-26 22:21               ` Herbert Xu
2015-01-26 10:09             ` David Laight
2015-01-26 22:23               ` Herbert Xu
2015-01-26 22:36                 ` David Miller
2015-01-26 22:42                   ` Herbert Xu
2015-01-26 23:31                     ` Herbert Xu
2015-01-27  9:45                       ` Thomas Graf
2015-01-27  9:54                         ` Herbert Xu
2015-01-27 10:15                           ` Thomas Graf
2015-01-27 10:24                             ` Herbert Xu
2015-01-27 11:16                               ` Thomas Graf
2015-01-27 11:23                                 ` Herbert Xu
2015-01-27 11:40                                   ` Thomas Graf
2015-01-27 20:39                                     ` Herbert Xu
2015-01-27 22:10                                       ` David Miller
2015-01-27 23:16                                         ` Herbert Xu
2015-01-27 13:09                                   ` Patrick McHardy
2015-01-27 20:36                                     ` Herbert Xu
2015-01-28 19:07                                       ` Patrick McHardy
2015-01-30  5:58                                         ` Herbert Xu
2015-01-30  8:10                                           ` Patrick McHardy
2015-01-27 10:09                 ` David Laight
2015-01-27 10:12                   ` Herbert Xu
2015-01-25 23:21           ` [PATCH 2/2] netlink: Use rhashtable walk iterator Herbert Xu
2015-01-27 23:19           ` [PATCH 0/2] rhashtable: Add walk iterator primitives and use them in netlink Herbert Xu
2015-01-27 23:20             ` Herbert Xu [this message]
2015-01-29 22:26               ` [PATCH 1/2] rhashtable: Introduce rhashtable_walk_* Thomas Graf
2015-01-27 23:20             ` [PATCH 2/2] netlink: Use rhashtable walk iterator Herbert Xu
2015-01-29 22:27               ` Thomas Graf
2015-01-29 22:42             ` [PATCH 0/2] rhashtable: Add walk iterator primitives and use them in netlink David Miller
2015-01-31  3:13               ` Herbert Xu
2015-01-31  3:14                 ` [PATCH 1/2] rhashtable: Introduce rhashtable_walk_* Herbert Xu
2015-01-31  3:14                 ` [PATCH 2/2] netlink: Use rhashtable walk iterator Herbert Xu
2015-01-31  4:31                 ` netfilter: " Herbert Xu
2015-02-01  7:45                   ` Patrick McHardy
2015-02-03  3:19                   ` David Miller
2015-02-03  3:19                 ` [PATCH 0/2] rhashtable: Add walk iterator primitives and use them in netlink David Miller
2015-01-20 13:20 ` [PATCH 3/3] netlink: Lock out table resizes while dumping Netlink sockets Thomas Graf
2015-01-20 14:31   ` Patrick McHardy
2015-01-20 14:55     ` Thomas Graf
2015-01-20 15:21       ` Patrick McHardy
2015-01-20 15:35         ` Thomas Graf
2015-01-21  5:08           ` Herbert Xu
2015-01-21  5:15             ` Herbert Xu
2015-01-21  9:14               ` Herbert Xu
2015-01-21  9:56                 ` Thomas Graf
2015-01-21  9:59                   ` Herbert Xu
2015-01-21 10:00                   ` Patrick McHardy
2015-01-21  9:37             ` Thomas Graf
2015-01-21  9:38               ` Herbert Xu
2015-01-21  9:49                 ` Thomas Graf
2015-01-21  9:58                   ` Herbert Xu
2015-01-21 10:23                     ` Thomas Graf
2015-01-22  6:35                       ` Herbert Xu
2015-01-22  7:20                         ` Herbert Xu
2015-01-22  9:05                           ` Thomas Graf
2015-01-22  9:50                             ` Herbert Xu
2015-01-21 10:34                     ` Thomas Graf
2015-01-21 10:40                       ` Patrick McHardy
2015-01-21 11:37                         ` Thomas Graf
2015-01-21 11:59                           ` Patrick McHardy
2015-01-21 12:07                             ` Thomas Graf
2015-01-21 12:09                               ` Patrick McHardy
2015-01-21 10:36               ` David Laight
2015-01-20 15:00     ` David Laight
2015-01-20 15:05       ` Thomas Graf
2015-01-21  5:11     ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1YGFRA-0002yN-I0@gondolin.me.apana.org.au \
    --to=herbert@gondor.apana.org.au \
    --cc=davem@davemloft.net \
    --cc=kaber@trash.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=tgraf@suug.ch \
    --cc=ying.xue@windriver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.