linux-spdx.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luis Chamberlain <mcgrof@kernel.org>
To: tj@kernel.org, gregkh@linuxfoundation.org,
	akpm@linux-foundation.org, jeyu@kernel.org, shuah@kernel.org
Cc: bvanassche@acm.org, dan.j.williams@intel.com, joe@perches.com,
	tglx@linutronix.de, mcgrof@kernel.org, keescook@chromium.org,
	rostedt@goodmis.org, minchan@kernel.org,
	linux-spdx@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v9 6/6] kernel/module: add documentation for try_module_get()
Date: Fri, 29 Oct 2021 11:45:00 -0700	[thread overview]
Message-ID: <20211029184500.2821444-7-mcgrof@kernel.org> (raw)
In-Reply-To: <20211029184500.2821444-1-mcgrof@kernel.org>

There is quite a bit of tribal knowledge around proper use of
try_module_get() and that it must be used only in a context which
can ensure the module won't be gone during the operation. Document
this little bit of tribal knowledge.

I'm extending this tribal knowledge with new developments which it
seems some folks do not yet believe to be true: we can be sure a
module will exist during the lifetime of a sysfs file operation.
For proof, refer to test_sysfs test #32:

./tools/testing/selftests/sysfs/sysfs.sh -t 0032

Without this being true, the write would fail or worse,
a crash would happen, in this test. It does not.

Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 include/linux/module.h | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/include/linux/module.h b/include/linux/module.h
index c9f1200b2312..35c98e4196cb 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -609,10 +609,43 @@ void symbol_put_addr(void *addr);
    to handle the error case (which only happens with rmmod --wait). */
 extern void __module_get(struct module *module);
 
-/* This is the Right Way to get a module: if it fails, it's being removed,
- * so pretend it's not there. */
+/**
+ * try_module_get() - Take module refcount unless module is being removed
+ * @module: the module we should check for
+ *
+ * This can be used to try to bump the reference count of a module, so to
+ * prevent module removal. The reference count of a module is not allowed
+ * to be incremented if the module is already being removed.
+ *
+ * Care must be taken to ensure the module cannot be removed during the call to
+ * try_module_get() because otherwise use of this routine could crash the kernel
+ * when racing to remove a module. Proper care can be taken by having another
+ * entity other than the module itself increment the module reference count,
+ * or through some other means which guarantees the module could not be removed
+ * during an operation. An example of this later case is using try_module_get()
+ * in a sysfs file which the module created. The sysfs store / read file
+ * operations are gauranteed to exist through the use of kernfs's active
+ * reference (see kernfs_active()). If a sysfs file operation is being run,
+ * the module which created it must still exist as the module is in charge of
+ * removing the same sysfs file being used. A sysfs / kernfs file removal
+ * cannot happen unless the same file does not have an active reference.
+ *
+ * One of the real values to try_module_get() is the module_is_live() check
+ * which ensures this the caller of try_module_get() can yield to userspace
+ * module removal requests and fail whatever it was about to process.
+ *
+ * Returns true if the reference count was successfully incremented.
+ */
 extern bool try_module_get(struct module *module);
 
+/**
+ * module_put() - release a reference count to a module
+ * @module: the module we should release a reference count for
+ *
+ * If you successfully bump a reference count to a module with try_module_get(),
+ * when you are finished you must call module_put() to release that reference
+ * count.
+ */
 extern void module_put(struct module *module);
 
 #else /*!CONFIG_MODULE_UNLOAD*/
-- 
2.30.2


      parent reply	other threads:[~2021-10-29 18:45 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-29 18:44 [PATCH v9 0/6] test_sysfs: add new selftest for sysfs Luis Chamberlain
2021-10-29 18:44 ` [PATCH v9 1/6] LICENSES: Add the copyleft-next-0.3.1 license Luis Chamberlain
2022-05-23 21:10   ` Thomas Gleixner
2022-05-24 13:59     ` Richard Fontana
2022-05-25 17:10     ` Luis Chamberlain
2022-05-25 19:13     ` Bradley M. Kuhn
2022-05-25 21:50       ` J Lovejoy
2022-05-25 22:29         ` Bradley M. Kuhn
2022-05-23 21:22   ` Thomas Gleixner
2022-05-25 16:57     ` Luis Chamberlain
2022-05-25 20:51       ` Thomas Gleixner
2022-05-25 23:53         ` Luis Chamberlain
2022-05-23 21:27   ` Thomas Gleixner
2022-05-25 16:43     ` Luis Chamberlain
2022-05-25 17:05       ` Bird, Tim
2022-05-25 18:11         ` Luis Chamberlain
2022-05-25 19:05           ` Bird, Tim
2022-05-25 19:44             ` Luis Chamberlain
2022-05-25 22:16             ` Thomas Gleixner
2022-06-02  4:11               ` Bird, Tim
2022-06-02  6:20                 ` gregkh
2022-06-02 19:41                   ` Luis Chamberlain
2022-06-02 19:30                 ` Luis Chamberlain
2021-10-29 18:44 ` [PATCH v9 2/6] testing: use the copyleft-next-0.3.1 SPDX tag Luis Chamberlain
2021-10-29 18:44 ` [PATCH v9 3/6] selftests: add tests_sysfs module Luis Chamberlain
2021-12-03 15:29   ` Greg KH
2021-12-09  1:48     ` Luis Chamberlain
2021-12-10 21:39       ` Luis Chamberlain
2021-12-14 19:31       ` [copyleft-next] " Richard Fontana
2022-05-22 14:37     ` Thomas Gleixner
2022-05-22 14:47       ` Greg KH
2022-05-22 15:06         ` Thomas Gleixner
2022-05-23 19:37           ` Luis Chamberlain
2021-10-29 18:44 ` [PATCH v9 4/6] kernfs: add initial failure injection support Luis Chamberlain
2021-10-29 18:44 ` [PATCH v9 5/6] test_sysfs: add support to use kernfs failure injection Luis Chamberlain
2021-10-29 18:45 ` Luis Chamberlain [this message]

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=20211029184500.2821444-7-mcgrof@kernel.org \
    --to=mcgrof@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bvanassche@acm.org \
    --cc=dan.j.williams@intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jeyu@kernel.org \
    --cc=joe@perches.com \
    --cc=keescook@chromium.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-spdx@vger.kernel.org \
    --cc=minchan@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --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 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).