From: Luis Chamberlain <email@example.com> To: David Laight <David.Laight@aculab.com> Cc: "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com>, Douglas Gilbert <firstname.lastname@example.org>, Hannes Reinecke <email@example.com>, "firstname.lastname@example.org" <email@example.com>, "firstname.lastname@example.org" <email@example.com> Subject: Re: [PATCH] kernel/module: add documentation for try_module_get() Date: Tue, 27 Jul 2021 10:30:36 -0700 [thread overview] Message-ID: <YQBCvKgH481C7o1c@bombadil.infradead.org> (raw) In-Reply-To: <dbf27fa2f8864e1d91f7015249b1a5f1@AcuMS.aculab.com> On Sat, Jul 24, 2021 at 12:15:10PM +0000, David Laight wrote: > From: Luis Chamberlain > > Sent: 22 July 2021 23:19 > > > > 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. > > > ... > > Some typos. > > > +/** > > + * try_module_get - yields to module removal and bumps reference count otherwise > > + * @module: the module we should check for > > + * > > + * This can be used to check if userspace has requested to remove a module, > a module be removed > > + * and if so let the caller give up. Otherwise it takes a reference count to > > + * ensure a request from userspace to remove the module cannot happen. > > + * > > + * Care must be taken to ensure the module cannot be removed during > > + * try_module_get(). This can be done by having another entity other than the > > + * module itself increment the module reference count, or through some other > > + * means which gaurantees the module could not be removed during an operation. > guarantees > > + * An example of this later case is using this call in a sysfs file which the > > + * module created. The sysfs store / read file operation is ensured to exist > ^^^^^^^^^^^^^^^^^^^ > Not sure what that is supposed to mean. I'll clarify further. How about: The sysfs store / read file operations are gauranteed to exist using kernfs's active reference (see kernfs_active()). > > + * and still be present by kernfs's active reference. If a sysfs file operation > > + * is being run, the module which created it must still exist as the module is > > + * in charge of removal of the sysfs file. > > + * > > + * The real value to try_module_get() is the module_is_live() check which > > + * ensures this the caller of try_module_get() can yields to userspace module > > + * removal requests and fail whatever it was about to process. > > + */ > > But is the comment even right? > I think you need to consider when try_module_get() can actually fail. Let's do that! > I believe the following is right. > The caller has to have valid module reference and module unload > must actually be in progress - ie the ref count is zero and > there are no active IO operations. If the refcount bump succeeded then module unload will simply not happen. So what exactly do you mean with the first part of "The caller has to have a valid module reference" ? > The module's unload function must (eventually) invalidate the > caller's module reference to stop try_module_get() being called > with a (very) stale pointer. Once a module's exit call is triggered the state is MODULE_STATE_GOING, which is what module_is_live() checks for. > So there is a potentially horrid race: > The module unload is going to do: > driver_data->module_ref = 0; > and elsewhere there'll be: > ref = driver_data->module_ref; > if (!ref || !try_module_get(ref)) > return -error; > > You have to have try_module_get() to allow the module unload > function to sleep. > But the above code still needs a driver lock to ensure the > unload code doesn't race with the try_module_get() and the > 'ref' be invalidated before try_module_get() looks at it. > (eg if an interrupt defers processing.) > > So there can be no 'yielding'. Oh but there is. Consider access to a random sysfs file 'add_new_device' which takes as input a name, for driver foo, and so foo's add_new_foobar_device(name="bar") is called. Unless sysfs file "yields" by using try_module_get() before trying to add a new foo device called "bar", it will essentially be racing with the exit routine of module foo, and depending on how locking is implemented (most drivers get it wrong), this easily leads to crashes. In fact, this documentation patch was motivated by my own solution to a possible deadlock when sysfs is used. Using the same example above, if the same sysfs file uses *any* lock, which is *also* used on the exit routine, you can easily trigger a deadlock. This can happen for example by the lock being obtained by the removal routine, then the sysfs file gets called, waits for the lock to complete, then the module's exit routine starts cleaning up and removing sysfs files, but we won't be able to remove the sysfs file (due to kernefs active reference) until the sysfs file complets, but it cannot complete because the lock is already held. Yes, this is a generic problem. Yes I have proof . Yes, a generic solution has been proposed , and because Greg is not convinced and I need to move on with life, I am suggesting a temporary driver specific solution (to which Greg is still NACK'ing, without even proposing any alternatives) .  https://firstname.lastname@example.org  https://lkml.kernel.org/r/20210401235925.GR4332@42.do-not-panic.com  https://lkml.kernel.org/r/20210723174919.ka3tzyre432uilf7@garbanzo > I'm pretty much certain try_module_get(THIS_MODULE) is pretty > much never going to fail. It would have to take something very asynchronous and detached from the module to run. But the only thing I can think now, is something takes a module pointer right before after try_stop_module() and then a piece of code in between try_stop_module() and free_module() asynchronously tries to run something with that pointer. In the end I can only think of buggy code. Perhaps the more type of common issue could be code which purposely leave module pointers around with the intent of cleaning up using a module removal notifier event and that for some stupid reason runs something asynchronously with that pointer. > (It is mostly needed to give a worker thread a reference.) Greg, do you have a real world example which demonstrates the race better? Or perhaps a selftest? Or a kunit test? Luis
next prev parent reply other threads:[~2021-07-27 17:31 UTC|newest] Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-07-22 22:19 Luis Chamberlain 2021-07-22 22:39 ` Stephen Hemminger 2021-07-23 2:33 ` Bart Van Assche 2021-07-24 12:15 ` David Laight 2021-07-27 17:30 ` Luis Chamberlain [this message] 2021-07-27 17:46 ` gregkh 2021-07-27 18:18 ` Luis Chamberlain 2021-07-27 18:38 ` gregkh 2021-07-27 20:54 ` Luis Chamberlain 2021-07-28 8:28 ` David Laight 2021-07-28 13:49 ` Luis Chamberlain
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=YQBCvKgH481C7o1c@bombadil.infradead.org \ --email@example.com \ --cc=David.Laight@aculab.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --firstname.lastname@example.org \ --email@example.com \ --subject='Re: [PATCH] kernel/module: add documentation for try_module_get()' \ /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
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).