[Devel] [PATCH 06/12] drop_monitor: Use pre_doit / post_doit hooks

Alexander Mikhalitsyn alexander.mikhalitsyn at virtuozzo.com
Mon Jul 4 21:51:56 MSK 2022


From: Ido Schimmel <idosch at mellanox.com>

Each operation from user space should be protected by the global drop
monitor mutex. Use the pre_doit / post_doit hooks to take / release the
lock instead of doing it explicitly in each function.

Signed-off-by: Ido Schimmel <idosch at mellanox.com>
Signed-off-by: David S. Miller <davem at davemloft.net>
(cherry picked from commit b19d955055480ac4e03f5afec0ca80f0de7b7013)
---
 net/core/drop_monitor.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/net/core/drop_monitor.c b/net/core/drop_monitor.c
index 7da7c634f267..0e2be8e3e884 100644
--- a/net/core/drop_monitor.c
+++ b/net/core/drop_monitor.c
@@ -245,12 +245,8 @@ static int set_all_monitor_traces(int state)
 	struct dm_hw_stat_delta *new_stat = NULL;
 	struct dm_hw_stat_delta *temp;
 
-	mutex_lock(&net_dm_mutex);
-
-	if (state == trace_state) {
-		rc = -EAGAIN;
-		goto out_unlock;
-	}
+	if (state == trace_state)
+		return -EAGAIN;
 
 	switch (state) {
 	case TRACE_ON:
@@ -292,9 +288,6 @@ static int set_all_monitor_traces(int state)
 	else
 		rc = -EINPROGRESS;
 
-out_unlock:
-	mutex_unlock(&net_dm_mutex);
-
 	return rc;
 }
 
@@ -374,10 +367,26 @@ static const struct genl_ops dropmon_ops[] = {
 	},
 };
 
+static int net_dm_nl_pre_doit(const struct genl_ops *ops,
+			      struct sk_buff *skb, struct genl_info *info)
+{
+	mutex_lock(&net_dm_mutex);
+
+	return 0;
+}
+
+static void net_dm_nl_post_doit(const struct genl_ops *ops,
+				struct sk_buff *skb, struct genl_info *info)
+{
+	mutex_unlock(&net_dm_mutex);
+}
+
 static struct genl_family net_drop_monitor_family = {
 	.hdrsize        = 0,
 	.name           = "NET_DM",
 	.version        = 2,
+	.pre_doit	= net_dm_nl_pre_doit,
+	.post_doit	= net_dm_nl_post_doit,
 	.module		= THIS_MODULE,
 	.ops		= dropmon_ops,
 	.n_ops		= ARRAY_SIZE(dropmon_ops),
-- 
2.36.1



More information about the Devel mailing list