From: Dave P Martin <Dave.Martin@arm.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org
Subject: debugfs and module unloading
Date: Wed, 21 May 2014 17:48:28 +0100 [thread overview]
Message-ID: <20140521164827.GB3501@e103592.cambridge.arm.com> (raw)
Hi there,
I have a quick debugfs question:
How can I protect against a module being unloaded while debugfs files it
provides are open?
I can do something like the following:
static int my_debugfs_file_open(struct inode *, struct file *)
{
if (!try_module_get())
return -EIO;
/* ... */
}
static int my_debugfs_file_release(struct inode *, struct file *)
{
/* ... */
module_put();
return 0;
}
static const struct file_operations my_debugfs_fops = {
/* ... */
.open = my_debugfs_file_open,
.release = my_debugfs_file_release,
/* ... */
};
static struct device_driver my_driver {
/* ... */
.owner = THIS_MODULE;
/* ... */
};
static int my_module_init(void)
{
driver_register(&my_driver);
debugfs_create_file(..., &my_debugfs_fops);
}
static void my_module_exit(void)
{
debugfs_remove_recusrive(...);
driver_unregister(&my_driver);
}
... but it doesn't quite work. debugfs_remove_recursive() prevents any
new file handles being opened, but files already open remain open -- so
it's still possible for the module text to get unloaded between the
module refcount being decreased inside module_put() and the time
my_debugfs_file_release() returns.
The scenario to consider is when a request to unload the module races
with closure of the last debugfs file.
The only obvious way I can see to solve this without changing the debugfs
code is to make the module impossible to unload by calling __module_get()
during initialisation, before any debugfs file is created.
A similar dependency problem exists when a pointer to some device
instance data is passed to debugfs_create_file(). For pluggable
devices, the device might go away at any time. I hoped this could
be solved by calling get_device() ... put_device() in the debugfs file
open and release methods, but I found that a device instance can
get removed, and the module unloaded, even though the struct device
refcount is not zero.
Am I missing something?
Cheers
---Dave
next reply other threads:[~2014-05-21 16:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-21 16:48 Dave P Martin [this message]
2014-05-21 22:01 ` Greg Kroah-Hartman
2014-05-22 9:26 ` Dave Martin
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=20140521164827.GB3501@e103592.cambridge.arm.com \
--to=dave.martin@arm.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.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
all inboxes | Powered by JetHome®