netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cai Huoqing <cai.huoqing@linux.dev>
To: cai.huoqing@linux.dev
Cc: Jakub Kicinski <kuba@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] netdevsim: fib: Make use of rhashtable_iter
Date: Tue, 25 Apr 2023 22:45:55 +0800	[thread overview]
Message-ID: <20230425144556.98799-1-cai.huoqing@linux.dev> (raw)

Iterating 'fib_rt_ht' by rhashtable_walk_next and rhashtable_iter directly
instead of using list_for_each, because each entry of fib_rt_ht can be
found by rhashtable API. And remove fib_rt_list.

Signed-off-by: Cai Huoqing <cai.huoqing@linux.dev>
---
 drivers/net/netdevsim/fib.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c
index a1f91ff8ec56..1a50c8e14665 100644
--- a/drivers/net/netdevsim/fib.c
+++ b/drivers/net/netdevsim/fib.c
@@ -48,7 +48,6 @@ struct nsim_fib_data {
 	struct nsim_per_fib_data ipv6;
 	struct nsim_fib_entry nexthops;
 	struct rhashtable fib_rt_ht;
-	struct list_head fib_rt_list;
 	struct mutex fib_lock; /* Protects FIB HT and list */
 	struct notifier_block nexthop_nb;
 	struct rhashtable nexthop_ht;
@@ -75,7 +74,6 @@ struct nsim_fib_rt_key {
 struct nsim_fib_rt {
 	struct nsim_fib_rt_key key;
 	struct rhash_head ht_node;
-	struct list_head list;	/* Member of fib_rt_list */
 };
 
 struct nsim_fib4_rt {
@@ -247,12 +245,6 @@ static void nsim_fib_rt_init(struct nsim_fib_data *data,
 	fib_rt->key.prefix_len = prefix_len;
 	fib_rt->key.family = family;
 	fib_rt->key.tb_id = tb_id;
-	list_add(&fib_rt->list, &data->fib_rt_list);
-}
-
-static void nsim_fib_rt_fini(struct nsim_fib_rt *fib_rt)
-{
-	list_del(&fib_rt->list);
 }
 
 static struct nsim_fib_rt *nsim_fib_rt_lookup(struct rhashtable *fib_rt_ht,
@@ -295,7 +287,6 @@ nsim_fib4_rt_create(struct nsim_fib_data *data,
 static void nsim_fib4_rt_destroy(struct nsim_fib4_rt *fib4_rt)
 {
 	fib_info_put(fib4_rt->fi);
-	nsim_fib_rt_fini(&fib4_rt->common);
 	kfree(fib4_rt);
 }
 
@@ -570,7 +561,6 @@ nsim_fib6_rt_create(struct nsim_fib_data *data,
 	for (i--; i >= 0; i--) {
 		nsim_fib6_rt_nh_del(fib6_rt, rt_arr[i]);
 	}
-	nsim_fib_rt_fini(&fib6_rt->common);
 	kfree(fib6_rt);
 	return ERR_PTR(err);
 }
@@ -582,7 +572,6 @@ static void nsim_fib6_rt_destroy(struct nsim_fib6_rt *fib6_rt)
 	list_for_each_entry_safe(iter, tmp, &fib6_rt->nh_list, list)
 		nsim_fib6_rt_nh_del(fib6_rt, iter->rt);
 	WARN_ON_ONCE(!list_empty(&fib6_rt->nh_list));
-	nsim_fib_rt_fini(&fib6_rt->common);
 	kfree(fib6_rt);
 }
 
@@ -1091,7 +1080,9 @@ static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
 {
 	struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
 						  fib_nb);
-	struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
+	struct nsim_fib_rt *fib_rt;
+	struct rhashtable_iter hti;
+	struct rhash_head *pos;
 
 	/* Flush the work to make sure there is no race with notifications. */
 	flush_work(&data->fib_event_work);
@@ -1099,9 +1090,12 @@ static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
 	/* The notifier block is still not registered, so we do not need to
 	 * take any locks here.
 	 */
-	list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
-		rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
+	rhashtable_walk_enter(&data->fib_rt_ht, &hti);
+	rhashtable_walk_start(&hti);
+	while ((pos = rhashtable_walk_next(&hti))) {
+		rhashtable_remove_fast(&data->fib_rt_ht, hti.p,
 				       nsim_fib_rt_ht_params);
+		fib_rt = rhashtable_walk_peek(&hti);
 		nsim_fib_rt_free(fib_rt, data);
 	}
 
@@ -1501,17 +1495,24 @@ static void nsim_fib_flush_work(struct work_struct *work)
 {
 	struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
 						  fib_flush_work);
-	struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
+	struct nsim_fib_rt *fib_rt;
+	struct rhashtable_iter hti;
+	struct rhash_head *pos;
+
 
 	/* Process pending work. */
 	flush_work(&data->fib_event_work);
 
 	mutex_lock(&data->fib_lock);
-	list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
-		rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
+	rhashtable_walk_enter(&data->fib_rt_ht, &hti);
+	rhashtable_walk_start(&hti);
+	while ((pos = rhashtable_walk_next(&hti))) {
+		rhashtable_remove_fast(&data->fib_rt_ht, hti.p,
 				       nsim_fib_rt_ht_params);
+		fib_rt = rhashtable_walk_peek(&hti);
 		nsim_fib_rt_free(fib_rt, data);
 	}
+
 	mutex_unlock(&data->fib_lock);
 }
 
@@ -1571,7 +1572,6 @@ struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
 		goto err_debugfs_exit;
 
 	mutex_init(&data->fib_lock);
-	INIT_LIST_HEAD(&data->fib_rt_list);
 	err = rhashtable_init(&data->fib_rt_ht, &nsim_fib_rt_ht_params);
 	if (err)
 		goto err_rhashtable_nexthop_destroy;
@@ -1661,7 +1661,6 @@ void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data)
 	rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
 				    data);
 	WARN_ON_ONCE(!list_empty(&data->fib_event_queue));
-	WARN_ON_ONCE(!list_empty(&data->fib_rt_list));
 	mutex_destroy(&data->fib_lock);
 	mutex_destroy(&data->nh_lock);
 	nsim_fib_debugfs_exit(data);
-- 
2.34.1


             reply	other threads:[~2023-04-25 14:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-25 14:45 Cai Huoqing [this message]
2023-04-25 15:41 ` [PATCH] netdevsim: fib: Make use of rhashtable_iter Jakub Kicinski
2023-04-26  9:37 ` Herbert Xu
2023-04-28 15:11   ` Cai Huoqing
2023-04-30  8:20     ` Ido Schimmel

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=20230425144556.98799-1-cai.huoqing@linux.dev \
    --to=cai.huoqing@linux.dev \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).