All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sven Van Asbroeck <thesven73@gmail.com>
To: Tejun Heo <tj@kernel.org>
Cc: Lai Jiangshan <jiangshanlai@gmail.com>,
	linux-kernel@vger.kernel.org, Sebastian Reichel <sre@kernel.org>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Kees Cook <keescook@chromium.org>
Subject: [RFC v1 1/3] workqueue: Add resource-managed version of INIT_[DELAYED_]WORK()
Date: Mon,  4 Feb 2019 17:09:50 -0500	[thread overview]
Message-ID: <20190204220952.30761-2-TheSven73@googlemail.com> (raw)
In-Reply-To: <20190204220952.30761-1-TheSven73@googlemail.com>

In modules which extensively use devm_ resource management, it is often
easy to overlook (delayed) work that is left pending or running after the
module is unloaded. This could introduce user-after-free issues.

Nudge kernel developers into 'doing the right thing' by introducing a
resource-managed version of INIT_[DELAYED_]WORK(). This can be used as
an elegant way to ensure that work is not left pending or running after
its dependencies are released.

Functions introduced in workqueue.h :
- devm_init_work()
- devm_init_delayed_work()

Signed-off-by: Sven Van Asbroeck <TheSven73@googlemail.com>
---
 include/linux/workqueue.h |  7 +++++
 kernel/workqueue.c        | 54 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 60d673e15632..eee148eb9908 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -15,6 +15,7 @@
 #include <linux/cpumask.h>
 #include <linux/rcupdate.h>
 
+struct device;
 struct workqueue_struct;
 
 struct work_struct;
@@ -670,4 +671,10 @@ int workqueue_offline_cpu(unsigned int cpu);
 int __init workqueue_init_early(void);
 int __init workqueue_init(void);
 
+int __must_check devm_init_work(struct device *dev, struct work_struct *work,
+					work_func_t func);
+int __must_check devm_init_delayed_work(struct device *dev,
+					struct delayed_work *dw,
+					work_func_t func);
+
 #endif
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index fc5d23d752a5..ab814b0b6c81 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -5837,3 +5837,57 @@ int __init workqueue_init(void)
 
 	return 0;
 }
+
+static void devm_work_release(void *data)
+{
+	struct work_struct *work = data;
+
+	cancel_work_sync(work);
+}
+
+/**
+ * devm_init_work - resource-controlled version of INIT_WORK()
+ * @dev: valid struct device pointer
+ * @work: work pointer to initialize
+ * @func: work function to initialize 'work' with
+ *
+ * Initialize the work pointer just like INIT_WORK(), but use resource control
+ * to help ensure work is not left running or pending when dev is destroyed.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int __must_check devm_init_work(struct device *dev, struct work_struct *work,
+					work_func_t func)
+{
+	INIT_WORK(work, func);
+	return devm_add_action(dev, devm_work_release, work);
+}
+EXPORT_SYMBOL_GPL(devm_init_work);
+
+static void devm_delayed_work_release(void *data)
+{
+	struct delayed_work *dw = data;
+
+	cancel_delayed_work_sync(dw);
+}
+
+/**
+ * devm_init_delayed_work - resource-controlled version of INIT_DELAYED_WORK()
+ * @dev: valid struct device pointer
+ * @dw: delayed_work pointer to initialize
+ * @func: work function to initialize 'dw' with
+ *
+ * Initialize the delayed_work pointer just like INIT_DELAYED_WORK(), but use
+ * resource control to help ensure delayed work is not left running or pending
+ * when dev is destroyed.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int __must_check devm_init_delayed_work(struct device *dev,
+					struct delayed_work *dw,
+					work_func_t func)
+{
+	INIT_DELAYED_WORK(dw, func);
+	return devm_add_action(dev, devm_delayed_work_release, dw);
+}
+EXPORT_SYMBOL_GPL(devm_init_delayed_work);
-- 
2.17.1


  reply	other threads:[~2019-02-04 22:10 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-04 22:09 [RFC v1 0/3] Address potential user-after-free on module unload Sven Van Asbroeck
2019-02-04 22:09 ` Sven Van Asbroeck [this message]
2019-02-08 17:06   ` [RFC v1 1/3] workqueue: Add resource-managed version of INIT_[DELAYED_]WORK() Tejun Heo
2019-02-08 18:15     ` Sven Van Asbroeck
2019-02-04 22:09 ` [RFC v1 2/3] max17042_battery: fix potential user-after-free on module unload Sven Van Asbroeck
2019-02-05  8:27   ` Dmitry Torokhov
2019-02-05 14:27     ` Sven Van Asbroeck
2019-02-05 17:21       ` Sebastian Reichel
2019-02-04 22:09 ` [RFC v1 3/3] cap11xx: " Sven Van Asbroeck
2019-02-05  8:18   ` Dmitry Torokhov
2019-02-05  8:34     ` Dmitry Torokhov
2019-02-05 21:24     ` Jacek Anaszewski
2019-02-05 21:43       ` Dmitry Torokhov
2019-02-05 22:03         ` Sven Van Asbroeck
2019-02-05 14:57 ` [RFC v1 0/3] Address " Kees Cook
2019-02-05 15:22   ` Sven Van Asbroeck
2019-02-05 18:43     ` Greg KH
2019-02-05 19:12       ` Sven Van Asbroeck
2019-02-06 16:46         ` Greg KH
2019-02-06 17:30           ` Dmitry Torokhov
2019-02-06 17:49             ` Sven Van Asbroeck
2019-02-08  6:51             ` Greg KH
2019-02-05 18:42   ` Greg KH
2019-02-07 21:49   ` Sven Van Asbroeck
2019-02-07 22:20     ` Dmitry Torokhov
2019-02-07 22:27       ` Sven Van Asbroeck
2019-02-07 22:32       ` Sven Van Asbroeck
2019-02-07 22:48         ` Dmitry Torokhov
2019-02-08  4:30         ` Miguel Ojeda
2019-02-10 18:05           ` Sven Van Asbroeck
2019-02-14  1:11             ` Miguel Ojeda
2019-02-14 15:23               ` Sven Van Asbroeck
     [not found]     ` <CAGngYiXcogd69n-MvBD1n5ZJpBzqCau8UOfLMgXEXLnAev=srw@mail.gmail.com>
     [not found]       ` <alpine.DEB.2.21.1902080745480.4201@hadrien>
2019-02-14 17:52         ` Fwd: " Sven Van Asbroeck

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=20190204220952.30761-2-TheSven73@googlemail.com \
    --to=thesven73@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jiangshanlai@gmail.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sre@kernel.org \
    --cc=tj@kernel.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 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.