b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Marek Lindner <mareklindner@neomailbox.ch>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Antonio Quartulli <a@unstable.cc>
Subject: [B.A.T.M.A.N.] [PATCH v2 1/7] batman-adv: tp_meter - prevent concurrent tp_meter sessions by using workqueue
Date: Fri, 18 May 2018 09:47:48 +0800	[thread overview]
Message-ID: <20180518014754.23644-2-mareklindner@neomailbox.ch> (raw)
In-Reply-To: <20180518014754.23644-1-mareklindner@neomailbox.ch>

From: Antonio Quartulli <a@unstable.cc>

To ensure that no more than one tp_meter session runs at the
same time, use an ordered workqueue instead of spawning
one kthread per session.

Signed-off-by: Antonio Quartulli <a@unstable.cc>
---
 net/batman-adv/main.c     | 10 ++++-
 net/batman-adv/tp_meter.c | 77 +++++++++++++++++++--------------------
 net/batman-adv/tp_meter.h |  3 +-
 net/batman-adv/types.h    |  3 ++
 4 files changed, 50 insertions(+), 43 deletions(-)

diff --git a/net/batman-adv/main.c b/net/batman-adv/main.c
index 69c0d85b..cd3f1924 100644
--- a/net/batman-adv/main.c
+++ b/net/batman-adv/main.c
@@ -91,6 +91,10 @@ static int __init batadv_init(void)
 	if (ret < 0)
 		return ret;
 
+	ret = batadv_tp_meter_init();
+	if (ret < 0)
+		goto err_tp_meter;
+
 	INIT_LIST_HEAD(&batadv_hardif_list);
 	batadv_algo_init();
 
@@ -99,7 +103,6 @@ static int __init batadv_init(void)
 	batadv_v_init();
 	batadv_iv_init();
 	batadv_nc_init();
-	batadv_tp_meter_init();
 
 	batadv_event_workqueue = create_singlethread_workqueue("bat_events");
 	if (!batadv_event_workqueue)
@@ -118,9 +121,11 @@ static int __init batadv_init(void)
 	return 0;
 
 err_create_wq:
+	batadv_tp_meter_destroy();
+err_tp_meter:
 	batadv_tt_cache_destroy();
 
-	return -ENOMEM;
+	return ret;
 }
 
 static void __exit batadv_exit(void)
@@ -138,6 +143,7 @@ static void __exit batadv_exit(void)
 	rcu_barrier();
 
 	batadv_tt_cache_destroy();
+	batadv_tp_meter_destroy();
 }
 
 /**
diff --git a/net/batman-adv/tp_meter.c b/net/batman-adv/tp_meter.c
index 11520de9..c0d27f7d 100644
--- a/net/batman-adv/tp_meter.c
+++ b/net/batman-adv/tp_meter.c
@@ -32,7 +32,6 @@
 #include <linux/jiffies.h>
 #include <linux/kernel.h>
 #include <linux/kref.h>
-#include <linux/kthread.h>
 #include <linux/list.h>
 #include <linux/netdevice.h>
 #include <linux/param.h>
@@ -97,6 +96,9 @@
 
 static u8 batadv_tp_prerandom[4096] __read_mostly;
 
+/* ordered work queue */
+static struct workqueue_struct *batadv_tp_meter_queue;
+
 /**
  * batadv_tp_session_cookie() - generate session cookie based on session ids
  * @session: TP session identifier
@@ -808,19 +810,22 @@ static int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
 
 /**
  * batadv_tp_send() - main sending thread of a tp meter session
- * @arg: address of the related tp_vars
+ * @work: delayed work reference of the related tp_vars
  *
  * Return: nothing, this function never returns
  */
-static int batadv_tp_send(void *arg)
+static void batadv_tp_send(struct work_struct *work)
 {
-	struct batadv_tp_vars *tp_vars = arg;
-	struct batadv_priv *bat_priv = tp_vars->bat_priv;
 	struct batadv_hard_iface *primary_if = NULL;
 	struct batadv_orig_node *orig_node = NULL;
+	struct batadv_tp_vars *tp_vars;
 	size_t payload_len, packet_len;
+	struct batadv_priv *bat_priv;
 	int err = 0;
 
+	tp_vars = container_of(work, struct batadv_tp_vars, test_work);
+	bat_priv = tp_vars->bat_priv;
+
 	if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
 		err = BATADV_TP_REASON_DST_UNREACHABLE;
 		tp_vars->reason = err;
@@ -901,40 +906,17 @@ static int batadv_tp_send(void *arg)
 	batadv_tp_sender_cleanup(bat_priv, tp_vars);
 
 	batadv_tp_vars_put(tp_vars);
-
-	do_exit(0);
 }
 
 /**
- * batadv_tp_start_kthread() - start new thread which manages the tp meter
- *  sender
+ * batadv_tp_start_work - start new thread which manages the tp meter sender
  * @tp_vars: the private data of the current TP meter session
  */
-static void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
+static void batadv_tp_start_work(struct batadv_tp_vars *tp_vars)
 {
-	struct task_struct *kthread;
-	struct batadv_priv *bat_priv = tp_vars->bat_priv;
-	u32 session_cookie;
-
-	kref_get(&tp_vars->refcount);
-	kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
-	if (IS_ERR(kthread)) {
-		session_cookie = batadv_tp_session_cookie(tp_vars->session,
-							  tp_vars->icmp_uid);
-		pr_err("batadv: cannot create tp meter kthread\n");
-		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
-					      tp_vars->other_end,
-					      bat_priv, session_cookie);
-
-		/* drop reserved reference for kthread */
-		batadv_tp_vars_put(tp_vars);
-
-		/* cleanup of failed tp meter variables */
-		batadv_tp_sender_cleanup(bat_priv, tp_vars);
-		return;
-	}
-
-	wake_up_process(kthread);
+	/* init work item that will actually execute the test and schedule it */
+	INIT_WORK(&tp_vars->test_work, batadv_tp_send);
+	queue_work(batadv_tp_meter_queue, &tp_vars->test_work);
 }
 
 /**
@@ -1053,13 +1035,10 @@ void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
 	/* init work item for finished tp tests */
 	INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
 
-	/* start tp kthread. This way the write() call issued from userspace can
-	 * happily return and avoid to block
+	/* schedule the tp worker. This way the write() call issued from
+	 * userspace can happily return and avoid to block
 	 */
-	batadv_tp_start_kthread(tp_vars);
-
-	/* don't return reference to new tp_vars */
-	batadv_tp_vars_put(tp_vars);
+	batadv_tp_start_work(tp_vars);
 }
 
 /**
@@ -1498,8 +1477,26 @@ void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
 
 /**
  * batadv_tp_meter_init() - initialize global tp_meter structures
+ *
+ * Return: 0 on success, -ENOMEM if the tp_meter queue allocation fails
  */
-void __init batadv_tp_meter_init(void)
+int __init batadv_tp_meter_init(void)
 {
 	get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
+
+	batadv_tp_meter_queue = alloc_ordered_workqueue("bat_tp_meter", 0);
+	if (!batadv_tp_meter_queue)
+		return -ENOMEM;
+
+	return 0;
+}
+
+/**
+ * batadv_tp_meter_destroy() - destroy tp meter memory allocations
+ */
+void batadv_tp_meter_destroy(void)
+{
+	flush_workqueue(batadv_tp_meter_queue);
+	destroy_workqueue(batadv_tp_meter_queue);
+	batadv_tp_meter_queue = NULL;
 }
diff --git a/net/batman-adv/tp_meter.h b/net/batman-adv/tp_meter.h
index 68e60097..ab0bde26 100644
--- a/net/batman-adv/tp_meter.h
+++ b/net/batman-adv/tp_meter.h
@@ -25,7 +25,8 @@
 
 struct sk_buff;
 
-void batadv_tp_meter_init(void);
+int batadv_tp_meter_init(void);
+void batadv_tp_meter_destroy(void);
 void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
 		     u32 test_length, u32 *cookie);
 void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
index 360357f8..baae5206 100644
--- a/net/batman-adv/types.h
+++ b/net/batman-adv/types.h
@@ -1354,6 +1354,9 @@ struct batadv_tp_vars {
 	/** @finish_work: work item for the finishing procedure */
 	struct delayed_work finish_work;
 
+	/** @test_work: work item for the test process */
+	struct work_struct test_work;
+
 	/** @test_length: test length in milliseconds */
 	u32 test_length;
 
-- 
2.17.0


  reply	other threads:[~2018-05-18  1:47 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-18  1:47 [B.A.T.M.A.N.] [PATCH v2 0/7] B.A.T.M.A.N. V - fallback to tp meter estimation if throughput otherwise not available Marek Lindner
2018-05-18  1:47 ` Marek Lindner [this message]
2018-08-29  6:56   ` [B.A.T.M.A.N.] [PATCH v2 1/7] batman-adv: tp_meter - prevent concurrent tp_meter sessions by using workqueue Sven Eckelmann
2018-05-18  1:47 ` [B.A.T.M.A.N.] [PATCH v2 2/7] batman-adv: tp_meter - don't check for existing session Marek Lindner
2018-05-18  1:47 ` [B.A.T.M.A.N.] [PATCH v2 3/7] batman-adv: tp_meter - allow up to 10 queued sessions Marek Lindner
2018-05-18  1:47 ` [B.A.T.M.A.N.] [PATCH v2 4/7] batman-adv: tp_meter - add caller distinction Marek Lindner
2018-05-18  1:47 ` [B.A.T.M.A.N.] [PATCH v2 5/7] batman-adv: tp_meter - add option to perform one-hop test Marek Lindner
2018-05-18  1:47 ` [B.A.T.M.A.N.] [PATCH v2 6/7] batman-adv: ELP - use tp meter to estimate the throughput if otherwise not available Marek Lindner
2018-05-21 13:17   ` Linus Lüssing
2018-05-21 17:51     ` Sven Eckelmann
2018-05-21 19:06     ` Sven Eckelmann
2018-08-04  9:31       ` Antonio Quartulli
2018-05-21 14:43   ` Linus Lüssing
2018-08-04  9:35     ` Marek Lindner
2018-05-21 14:48   ` Linus Lüssing
2018-08-04  9:39     ` Antonio Quartulli
2018-05-21 15:01   ` Linus Lüssing
2018-08-04  8:59     ` Antonio Quartulli
2018-05-21 16:36   ` Sven Eckelmann
2018-05-18  1:47 ` [B.A.T.M.A.N.] [PATCH v2 7/7] batman-adv: ELP - add throughput meter test duration attribute Marek Lindner
2018-05-21 13:46   ` Linus Lüssing
2018-05-21 13:57     ` Linus Lüssing
2018-08-04  9:05     ` Marek Lindner
2018-05-21 14:34   ` Sven Eckelmann
2018-08-04  8:41     ` Antonio Quartulli
2018-08-04  9:02       ` Sven Eckelmann
2018-08-04  9:08         ` Antonio Quartulli

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=20180518014754.23644-2-mareklindner@neomailbox.ch \
    --to=mareklindner@neomailbox.ch \
    --cc=a@unstable.cc \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /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).