kmod: add patient module removal support
Subsystems (2)
v2 cover kmod: add patient module removal support
The kernel used to have wait support for delete_module() system call. This was removed via commmit 3f2b9c9cdf389e ("module: remove rmmod --wait option.") on v3.13 in favor for 10 second sleep on kmod. Lucas later remove that sleep(10) on kmod commit 017893f244 ("rmmod: remove --wait option") so on kmod 16.
There are races in module removal I have been chasing down and clearly documenting them. Module removal is not crazy stuff, its used in many test frameworks such as fstests and blktests and can even be used to remove live patches if a distribution supports that. If you are doing tests in a loop you can easily run into these races as false positives in your testing results.
Contrary to the last kernel wait delete_module() effort this series instead adds a patient module removal support into kmod and extends modprobe and rmmod to use it.
The most important change other than the requested during patch review this also now uses the same timeout to also re-try module removal if it fails when patient module removal is used.
Changes on v2:
- replace looking at the refcnt with poll() as requested by Lucas. This uses clock_gettime(CLOCK_MONOTONIC), and systems without that won't get patient module removal support, we'd revert back to regular module removal attempt support. Since poll() in practice just busy loops on the refcnt sysfs file today, we guard verbose prints to only every 1/2 second. We can enhance poll() on the refcnt later, for now this busy read helps prove the issue and test for it.
- replaces the sleep(1) calls and make thie programmable timeouts as requested by Lucas
- extends macros to allow us to print something *more* on the library when something like modprobe -v is used.
- upon further testing and investigation of the refcnt issue where the refcnt is 0 but the module cannot be removed [0] I've determined that the only reasonable thing userspace can do is to retry the delete_modue() call when doing a patient removal. You *might* be tempted to look at this and suggest a new quiesce state which userspace can request, however such quiesce states could actually prove more problemantic than resolve anything. Consider how some subystems may need to re-open a device only to close it when tearing something down. Such quiesce efforts would break those subsystems.
[0] https://bugzilla.kernel.org/show_bug.cgi?id=214015
Luis Chamberlain (3):
libkmod: add a library notice log level print libkmod/libkmod-module: add refcnt fd helper
libkmod-module: add support for a patient module removal option
libkmod/docs/libkmod-sections.txt | 4 + libkmod/libkmod-internal.h | 2 + libkmod/libkmod-module.c | 365 ++++++++++++++++++++++++++++- libkmod/libkmod.c | 71 ++++++ libkmod/libkmod.h | 7 + libkmod/libkmod.sym | 4 + libkmod/python/kmod/_libkmod_h.pxd | 3 + libkmod/python/kmod/module.pyx | 4 + man/modprobe.xml | 59 +++++ man/rmmod.xml | 60 +++++ tools/modprobe.c | 21 +- tools/remove.c | 12 +- tools/rmmod.c | 27 ++- 13 files changed, 610 insertions(+), 29 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 1/3 libkmod: add a library notice log level print
When you use pass the -v argument to modprobe we bump the log level from the default modprobe log level of LOG_WARNING (4) to LOG_NOTICE (5), however the library only has avaiable to print:
#define DBG(ctx, arg...) kmod_log_cond(ctx, LOG_DEBUG, ## arg) #define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ## arg) #define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ## arg)
LOG_INFO (6) however is too high of a level for it to be effective at printing anything when modprobe -v is passed. And so the only way in which modprobe -v can trigger the library to print a verbose message is to use ERR() but that always prints something and we don't want that in some situations.
We need to add a new log level macro which uses LOG_NOTICE (5) for a "normal but significant condition" which users and developers can use to look underneath the hood to confirm if a situation is happening.
--- libkmod/libkmod-internal.h | 2 ++ 1 file changed, 2 insertions(+)
libkmod/libkmod-internal.h +2 -0
@@ -25,10 +25,12 @@ static _always_inline_ _printf_format_(2, 3) void# else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg)# endif +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg) # define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ## arg) # define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ## arg)#else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg) +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg) # define INFO(ctx, arg...) kmod_log_null(ctx, ## arg) # define ERR(ctx, arg...) kmod_log_null(ctx, ## arg)#endif
Signature · 1 line
2.30.2
Re: [PATCH v2 1/3] libkmod: add a library notice log level print
Quoted 32 lines · Luis Chamberlain
When you use pass the -v argument to modprobe we bump the log level from the default modprobe log level of
LOG_WARNING(4) toLOG_NOTICE(5), however the library only has avaiable to print:#define
DBG(ctx, arg...)kmod_log_cond(ctx, LOG_DEBUG, ## arg)#defineINFO(ctx, arg...)kmod_log_cond(ctx, LOG_INFO, ## arg)#defineERR(ctx, arg...)kmod_log_cond(ctx, LOG_ERR, ## arg)
LOG_INFO(6) however is too high of a level for it to be effective at printing anything when modprobe -v is passed. And so the only way in which modprobe -v can trigger the library to print a verbose message is to useERR()but that always prints something and we don't want that in some situations.We need to add a new log level macro which uses
LOG_NOTICE(5) for a "normal but significant condition" which users and developers can use to look underneath the hood to confirm if a situation is happening.Signed-off-by Luis Chamberlain <[email protected]>--- libkmod/libkmod-internal.h | 2 ++ 1 file changed, 2 insertions(+)libkmod/libkmod-internal.h +0
-0
libkmod/libkmod-internal.h static _always_inline_ _printf_format_(2, 3) void · line 33
Quoted 9 lines
libkmod/libkmod-internal.h +2
-0@@ -25,10 +25,12 @@ static _always_inline_ _printf_format_(2, 3) void# else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg)# endif +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg) # define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ## arg) # define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ## arg)#else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg) +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg)
did you mean kmod_log_null()?
Lucas De Marchi
Quoted 6 lines
libkmod/libkmod-internal.h +0
-1# define INFO(ctx, arg...) kmod_log_null(ctx, ## arg) # define ERR(ctx, arg...) kmod_log_null(ctx, ## arg)#endif --2.30.2
Re: [PATCH v2 1/3] libkmod: add a library notice log level print
Quoted 28 lines · Lucas De Marchi
Quoted 27 lines · Luis Chamberlain
When you use pass the -v argument to modprobe we bump the log level from the default modprobe log level of
LOG_WARNING(4) toLOG_NOTICE(5), however the library only has avaiable to print:#define
DBG(ctx, arg...)kmod_log_cond(ctx, LOG_DEBUG, ## arg)#defineINFO(ctx, arg...)kmod_log_cond(ctx, LOG_INFO, ## arg)#defineERR(ctx, arg...)kmod_log_cond(ctx, LOG_ERR, ## arg)
LOG_INFO(6) however is too high of a level for it to be effective at printing anything when modprobe -v is passed. And so the only way in which modprobe -v can trigger the library to print a verbose message is to useERR()but that always prints something and we don't want that in some situations.We need to add a new log level macro which uses
LOG_NOTICE(5) for a "normal but significant condition" which users and developers can use to look underneath the hood to confirm if a situation is happening.Signed-off-by Luis Chamberlain <[email protected]>--- libkmod/libkmod-internal.h | 2 ++ 1 file changed, 2 insertions(+)
Quoted 16 lines
Quoted 5 lines
libkmod/libkmod-internal.h +0
-0libkmod/libkmod-internal.h static _always_inline_ _printf_format_(2, 3) void · line 33
Quoted 9 lines
libkmod/libkmod-internal.h +2
-0@@ -25,10 +25,12 @@ static _always_inline_ _printf_format_(2, 3) void# else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg)# endif +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg) # define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ## arg) # define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ## arg)#else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg) +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg)did you mean
kmod_log_null()?
Sure, feel free to change on your end or let me know if you would prefer if I respin.
Luis
Re: [PATCH v2 1/3] libkmod: add a library notice log level print
Quoted 46 lines · Luis Chamberlain
Quoted 28 lines · Lucas De Marchi
Quoted 27 lines · Luis Chamberlain
When you use pass the -v argument to modprobe we bump the log level from the default modprobe log level of
LOG_WARNING(4) toLOG_NOTICE(5), however the library only has avaiable to print:#define
DBG(ctx, arg...)kmod_log_cond(ctx, LOG_DEBUG, ## arg)#defineINFO(ctx, arg...)kmod_log_cond(ctx, LOG_INFO, ## arg)#defineERR(ctx, arg...)kmod_log_cond(ctx, LOG_ERR, ## arg)
LOG_INFO(6) however is too high of a level for it to be effective at printing anything when modprobe -v is passed. And so the only way in which modprobe -v can trigger the library to print a verbose message is to useERR()but that always prints something and we don't want that in some situations.We need to add a new log level macro which uses
LOG_NOTICE(5) for a "normal but significant condition" which users and developers can use to look underneath the hood to confirm if a situation is happening.Signed-off-by Luis Chamberlain <[email protected]>--- libkmod/libkmod-internal.h | 2 ++ 1 file changed, 2 insertions(+)Quoted 16 lines
Quoted 5 lines
libkmod/libkmod-internal.h +0
-0libkmod/libkmod-internal.h static _always_inline_ _printf_format_(2, 3) void · line 33
Quoted 9 lines
libkmod/libkmod-internal.h +2
-0@@ -25,10 +25,12 @@ static _always_inline_ _printf_format_(2, 3) void# else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg)# endif +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg) # define INFO(ctx, arg...) kmod_log_cond(ctx, LOG_INFO, ## arg) # define ERR(ctx, arg...) kmod_log_cond(ctx, LOG_ERR, ## arg)#else # define DBG(ctx, arg...) kmod_log_null(ctx, ## arg) +# define NOTICE(ctx, arg...) kmod_log_cond(ctx, LOG_NOTICE, ## arg)did you mean
kmod_log_null()?
Quoted 2 lines
Sure, feel free to change on your end or let me know if you would prefer if I respin.
fixed that and pushed this patch.
thanks
Lucas De Marchi
Quoted 2 lines
Luis
PATCH v2 2/3 libkmod/libkmod-module: add refcnt fd helper
In reply to Luis ChamberlainThe first part of kmod_module_get_refcnt() is to open the file descriptor for the refcnt file. Add a helper to do this as it can then be used in other cases in the library.
--- libkmod/libkmod-module.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-)
libkmod/libkmod-module.c +24 -13
@@ -1877,25 +1877,16 @@ done:return size; } -/** -* kmod_module_get_refcnt: -* @mod: kmod module -* -* Get the ref count of this @mod, as returned by Linux Kernel, by reading -* /sys filesystem. -* -* Returns: the reference count on success or < 0 on failure. -*/ -KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) +static int kmod_module_get_refcnt_fd (const struct kmod_module *mod) {char path[PATH_MAX ]; -long refcnt;int fd, err;if (mod ==NULL )return -ENOENT ;snprintf (path,sizeof (path),"/sys/module/%s/refcnt" , mod->name); + fd =open (path,O_RDONLY |O_CLOEXEC );if (fd <0 ) { err = -errno;
@@ -1903,12 +1894,32 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) path, strerror(errno));return err; } +return fd; +} + +/** +* kmod_module_get_refcnt: +* @mod: kmod module +* +* Get the ref count of this @mod, as returned by Linux Kernel, by reading +* /sys filesystem. +* +* Returns: the reference count on success or < 0 on failure. +*/ +KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) +{ +long refcnt; +int fd, err; + + fd =kmod_module_get_refcnt_fd (mod); +if (fd <0 ) +return fd; err =read_str_long (fd, &refcnt,10 );close (fd);if (err <0 ) { -ERR (mod->ctx,"could not read integer from '%s': '%s'\n" , - path,strerror (-err)); +ERR (mod->ctx,"could not read integer from '/sys/module/%s/refcnt': '%s'\n" , + mod->name,strerror (-err));return err; }
Signature · 1 line
2.30.2
PATCH v2 3/3 libkmod-module: add support for a patient module removal option
In reply to Luis ChamberlainWhen doing tests with modules such as scsi_debug, on test frameworks such as fstests [0] you may run into situations where false positives are triggered and a test fails but the reality is that the test did not fail, but what did fail was the removal of the module since the refcnt is not yet 0. There are many races possible with modules, and each subsystem would have their own quirks for why these races exist. Additionally, even after the refcnt is 0, userspace cannot be sure in any way that the refcnt will remain 0, and so its easy to run into a situation where the refcnt is bumped from 0 after modprobe just saw it as 0.
The scsi_debug races are all documented on korg#21233 [1], some issues are part scsi_debug alone, however other parts of the race issue are completely due to incomplete described semantics for modules and the inability for userspace to be 100% sure that a race cannot happen to bump the refcnt once it observes the refcnt is 0.
I've written a script mod-refcnt-race.sh attached to korg#214015 [2] which helps us easily reproduce the races described and abstracts them away from fstests. This can be used to both verify the issue and also test the new kmod patient module remover. The same bug report also has a simple busy-open-block-device-sleep.c which busy opens a block device, sleep 4 seconds in a loop, closes the file descriptor, which can easily be used to force failure of the module removal (delete_module()) and test this new patient removal.
Although there are patches for fstests to account for this [3] and work around it, a much suitable solution long term is for these hacks to use a patient module remover from kmod and modprobe directly. This patch does just that, it adds these new arguments to modprobe and rmmod:
-p | --remove-patiently patiently removes the module -t | --timeout timeout in ms to remove the module
The patient removal works by polling until the module refcnt /sys/module/name/refcnt goes to 0. One that does happens it tries to also patiently remove the module. This second step is required as a new temporary refcnt bump can occur after a module refcnt becomes 0. By default modprobe and rmmod will wait forever, however a timeout can be specified to in milliseconds to ensure only that amount of time is spent trying to remove the module.
If a timeout is used it applies cumulatively to both the refcnt wait, and for trying to remove the module once modprobe sees the refcnt is already 0.
This new fatures is useful for cases where you know the refcnt is going to become 0, and it is just a matter of time, such as with many fstests / blktests tests.
It is worth noting that prior to kernel v3.13 the kernel module delete system call used to support a wait flag, this was removed via commit 3f2b9c9cdf389 ("module: remove rmmod --wait option.") in favor for a 10 second sleep in kmod. A 10 second sleep does not address the issues, and so we must implement a patient removal properly in kmod.
[0] git://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git
[1] https://bugzilla.kernel.org/show_bug.cgi?id=21233
[2] https://bugzilla.kernel.org/show_bug.cgi?id=214015
[3] https://lkml.kernel.org/r/[email protected]
--- libkmod/docs/libkmod-sections.txt | 4 + libkmod/libkmod-module.c | 328 +++++++++++++++++++++++++++++ libkmod/libkmod.c | 71 +++++++ libkmod/libkmod.h | 7 + libkmod/libkmod.sym | 4 + libkmod/python/kmod/_libkmod_h.pxd | 3 + libkmod/python/kmod/module.pyx | 4 + man/modprobe.xml | 59 ++++++ man/rmmod.xml | 60 ++++++ tools/modprobe.c | 21 +- tools/remove.c | 12 +- tools/rmmod.c | 27 ++- 12 files changed, 584 insertions(+), 16 deletions(-)
libkmod/docs/libkmod-sections.txt +4 -0
@@ -12,6 +12,8 @@ kmod_dump_index kmod_set_log_priority kmod_get_log_priority +kmod_get_refcnt_timeout +kmod_set_refcnt_timeout kmod_set_log_fn kmod_get_userdata kmod_set_userdata
@@ -56,6 +58,7 @@ kmod_module_unref_list kmod_module_insert_module kmod_module_probe_insert_module kmod_module_remove_module +kmod_module_remove_module_wait kmod_module_get_module kmod_module_get_dependencies
@@ -102,5 +105,6 @@ kmod_module_get_initstate kmod_module_initstate_str kmod_module_get_size kmod_module_get_refcnt +kmod_module_get_refcnt_wait kmod_module_get_holders </SECTION>
libkmod/libkmod-module.c +328 -0
346 lines not on this page · raw message
libkmod/libkmod.c +71 -0
89 lines not on this page · raw message
libkmod/libkmod.h +7 -0
@@ -48,6 +48,10 @@ void kmod_set_log_fn(struct kmod_ctx *ctx, const void *data);int kmod_get_log_priority (const struct kmod_ctx *ctx);void kmod_set_log_priority (struct kmod_ctx *ctx,int priority); +int kmod_get_refcnt_timeout (const struct kmod_ctx *ctx); +void kmod_set_refcnt_timeout (struct kmod_ctx *ctx,int timeout_ms); +int kmod_get_removal_timeout (const struct kmod_ctx *ctx); +void kmod_set_removal_timeout (struct kmod_ctx *ctx,int timeout_ms);void *kmod_get_userdata (const struct kmod_ctx *ctx);void kmod_set_userdata (struct kmod_ctx *ctx,const void *userdata);
@@ -172,6 +176,8 @@ enum kmod_filter { };int kmod_module_remove_module (struct kmod_module *mod,unsigned int flags); +int kmod_module_remove_module_wait (struct kmod_module *mod,unsigned int flags, +bool wait);int kmod_module_insert_module (struct kmod_module *mod,unsigned int flags,const char *options); int kmod_module_probe_insert_module(struct kmod_module *mod,
@@ -217,6 +223,7 @@ enum kmod_module_initstate {const char *kmod_module_initstate_str (enum kmod_module_initstate state);int kmod_module_get_initstate (const struct kmod_module *mod);int kmod_module_get_refcnt (const struct kmod_module *mod); +int kmod_module_get_refcnt_wait (const struct kmod_module *mod,bool wait);struct kmod_list *kmod_module_get_holders (const struct kmod_module *mod);struct kmod_list *kmod_module_get_sections (const struct kmod_module *mod);const char *kmod_module_section_get_name (const struct kmod_list *entry);
libkmod/libkmod.sym +4 -0
@@ -6,6 +6,8 @@ global: kmod_ref; kmod_set_log_fn; kmod_set_log_priority; + kmod_get_refcnt_timeout_ms; + kmod_set_refcnt_timeout; kmod_set_userdata; kmod_unref; kmod_list_next;
@@ -36,6 +38,7 @@ global: kmod_module_unref_list; kmod_module_get_module; kmod_module_remove_module; + kmod_module_remove_module_wait; kmod_module_insert_module; kmod_module_probe_insert_module;
@@ -49,6 +52,7 @@ global: kmod_module_initstate_str; kmod_module_get_initstate; kmod_module_get_refcnt; + kmod_module_get_refcnt_wait; kmod_module_get_sections; kmod_module_section_free_list; kmod_module_section_get_name;
libkmod/python/kmod/_libkmod_h.pxd +3 -0
@@ -79,6 +79,8 @@ cdef extern from 'libkmod/libkmod.h': int kmod_module_remove_module( kmod_module *mod, unsigned int flags) + int kmod_module_remove_module_wait( + kmod_module *mod, unsigned int flags, bool wait) int kmod_module_insert_module( kmod_module *mod, unsigned int flags, const_char_ptr options) int kmod_module_probe_insert_module(
@@ -99,6 +101,7 @@ cdef extern from 'libkmod/libkmod.h': # Information regarding "live information" from module's state, as # returned by kernel int kmod_module_get_refcnt(const_kmod_module_ptr mod) + int kmod_module_get_refcnt_wait(const_kmod_module_ptr mod, bool install) long kmod_module_get_size(const_kmod_module_ptr mod) # Information retrieved from ELF headers and section
libkmod/python/kmod/module.pyx +4 -0
@@ -72,6 +72,10 @@ cdef class Module (object): return _libkmod_h.kmod_module_get_refcnt(self.module) refcnt = property(fget=_refcnt_get) + def _refcnt_get_wait(self, wait=False): + return _libkmod_h.kmod_module_get_refcnt_wait(self.module, wait) + refcnt = property(fget=_refcnt_get_wait) + def _size_get(self): return _libkmod_h.kmod_module_get_size(self.module) size = property(fget=_size_get)
man/modprobe.xml +59 -0
65 lines not on this page · raw message
man/rmmod.xml +60 -0
72 lines not on this page · raw message
tools/modprobe.c +18 -3
@@ -56,11 +56,14 @@ static int strip_modversion = 0;static int strip_vermagic =0 ;static int remove_dependencies =0 ;static int quiet_inuse =0 ; +static int do_remove_patient =0 ; +static int use_timeout_ms =0 ; -static const char cmdopts_s[] ="arRibfDcnC:d:S:sqvVh" ; +static const char cmdopts_s[] ="arpRibfDcnC:d:S:st:qvVh" ;static const struct option cmdopts[] = { {"all" , no_argument,0 ,'a' }, {"remove" , no_argument,0 ,'r' }, + {"remove-patiently" , no_argument,0 ,'p' }, {"remove-dependencies" , no_argument,0 ,5 }, {"resolve-alias" , no_argument,0 ,'R' }, {"first-time" , no_argument,0 ,3 },
@@ -81,6 +84,7 @@ static const struct option cmdopts[] = { {"dry-run", no_argument, 0, 'n'}, {"show", no_argument, 0, 'n'}, + {"timeout", required_argument, 0, 't'}, {"config", required_argument, 0, 'C'}, {"dirname", required_argument, 0, 'd'}, {"set-version", required_argument, 0, 'S'},
@@ -107,6 +111,8 @@ static void help(void)"\t be a module name to be inserted\n" "\t or removed (-r)\n" "\t-r, --remove Remove modules instead of inserting\n" +"\t-p, --remove-patiently Patiently wait until the refcnt is 0 to remove\n" +"\t-t, --timeout Timeout in ms to use for patient removal\n" "\t --remove-dependencies Also remove modules depending on it\n" "\t-R, --resolve-alias Only lookup and print alias and exit\n" "\t --first-time Fail if module already inserted or removed\n"
@@ -331,7 +337,7 @@ static int rmmod_do_remove_module(struct kmod_module *mod)if (force) flags |=KMOD_REMOVE_FORCE ; - err =kmod_module_remove_module (mod, flags); + err =kmod_module_remove_module_wait (mod, flags, do_remove_patient);if (err == -EEXIST ) {if (!first_time) err =0 ;
@@ -424,7 +430,7 @@ static int rmmod_do_module(struct kmod_module *mod, int flags) }if (!ignore_loaded && !cmd) { -int usage =kmod_module_get_refcnt (mod); +int usage =kmod_module_get_refcnt_wait (mod, do_remove_patient);if (usage >0 ) {if (!quiet_inuse)
@@ -785,6 +791,10 @@ static int do_modprobe(int argc, char **orig_argv)case 'r' : do_remove =1 ;break ; +case 'p' : + do_remove =1 ; + do_remove_patient =1 ; +break ;case 5 : remove_dependencies =1 ;break ;
@@ -843,6 +853,9 @@ static int do_modprobe(int argc, char **orig_argv)env_modprobe_options_append (optarg);break ; } +case 't' : + use_timeout_ms =atoi (optarg); +break ;case 'd' : root = optarg;break ;
@@ -921,6 +934,8 @@ static int do_modprobe(int argc, char **orig_argv)log_setup_kmod_log (ctx, verbose);kmod_load_resources (ctx); +if (use_timeout_ms) +kmod_set_refcnt_timeout (ctx, use_timeout_ms);if (do_show_config) err =show_config (ctx);
tools/remove.c +9 -3
@@ -27,9 +27,12 @@#include "kmod.h" -static const char cmdopts_s[] ="h" ; +static int do_remove_patient =0 ; + +static const char cmdopts_s[] ="hp" ;static const struct option cmdopts[] = { {"help" , no_argument,0 ,'h' }, + {"remove-patiently" , no_argument,0 ,'p' }, { } };
@@ -74,7 +77,7 @@ static int check_module_inuse(struct kmod_module *mod) {return -EBUSY ; } - ret =kmod_module_get_refcnt (mod); + ret =kmod_module_get_refcnt_wait (mod, do_remove_patient);if (ret >0 ) {ERR ("Module %s is in use\n" ,kmod_module_get_name (mod));return -EBUSY ;
@@ -101,6 +104,9 @@ static int do_remove(int argc, char *argv[])case 'h' :help ();return EXIT_SUCCESS ; +case 'p' : + do_remove_patient =1 ; +break ;default :ERR ("Unexpected getopt_long() value '%c'.\n" , c);
@@ -130,7 +136,7 @@ static int do_remove(int argc, char *argv[])if (err <0 ) goto unref; - err =kmod_module_remove_module (mod,0 ); + err =kmod_module_remove_module_wait (mod,0 , do_remove_patient);if (err <0 ) goto unref;
tools/rmmod.c +17 -10
@@ -35,10 +35,12 @@#define DEFAULT_VERBOSE LOG_ERRstatic int verbose =DEFAULT_VERBOSE ;static int use_syslog; +static int do_remove_patient; -static const char cmdopts_s[] ="fsvVwh" ; +static const char cmdopts_s[] ="fpsvVwh" ;static const struct option cmdopts[] = { {"force" , no_argument,0 ,'f' }, + {"remove-patiently" , no_argument,0 ,'p' }, {"syslog" , no_argument,0 ,'s' }, {"verbose" , no_argument,0 ,'v' }, {"version" , no_argument,0 ,'V' },
@@ -51,13 +53,14 @@ static void help(void)printf ("Usage:\n" "\t%s [options] modulename ...\n" "Options:\n" -"\t-f, --force forces a module unload and may crash your\n" -"\t machine. This requires Forced Module Removal\n" -"\t option in your kernel. DANGEROUS\n" -"\t-s, --syslog print to syslog, not stderr\n" -"\t-v, --verbose enables more messages\n" -"\t-V, --version show version\n" -"\t-h, --help show this help\n" , +"\t-f, --force forces a module unload and may crash your\n" +"\t machine. This requires Forced Module Removal\n" +"\t option in your kernel. DANGEROUS\n" +"\t-p, --remove-patiently patiently tries to remove the module\n" +"\t-s, --syslog print to syslog, not stderr\n" +"\t-v, --verbose enables more messages\n" +"\t-V, --version show version\n" +"\t-h, --help show this help\n" , program_invocation_short_name); }
@@ -93,7 +96,7 @@ static int check_module_inuse(struct kmod_module *mod) {return -EBUSY ; } - ret =kmod_module_get_refcnt (mod); + ret =kmod_module_get_refcnt_wait (mod, do_remove_patient);if (ret >0 ) {ERR ("Module %s is in use\n" ,kmod_module_get_name (mod));return -EBUSY ;
@@ -120,6 +123,9 @@ static int do_rmmod(int argc, char *argv[])case 'f' : flags |=KMOD_REMOVE_FORCE ;break ; +case 'p' : + do_remove_patient =1 ; +break ;case 's' : use_syslog =1 ;break ;
@@ -178,7 +184,8 @@ static int do_rmmod(int argc, char *argv[]) goto next; } - err =kmod_module_remove_module (mod, flags); + err =kmod_module_remove_module_wait (mod, flags, + do_remove_patient);if (err <0 ) {ERR ("could not remove module %s: %s\n" , arg,strerror (-err));
Signature · 1 line
2.30.2
Re: [PATCH v2 3/3] libkmod-module: add support for a patient module removal option
Quoted 58 lines · Luis Chamberlain
When doing tests with modules such as
scsi_debug, on test frameworks such as fstests [0] you may run into situations where false positives are triggered and a test fails but the reality is that the test did not fail, but what did fail was the removal of the module since the refcnt is not yet 0. There are many races possible with modules, and each subsystem would have their own quirks for why these races exist. Additionally, even after the refcnt is 0, userspace cannot be sure in any way that the refcnt will remain 0, and so its easy to run into a situation where the refcnt is bumped from 0 after modprobe just saw it as 0.The
scsi_debugraces are all documented on korg#21233 [1], some issues are partscsi_debugalone, however other parts of the race issue are completely due to incomplete described semantics for modules and the inability for userspace to be 100% sure that a race cannot happen to bump the refcnt once it observes the refcnt is 0.I've written a script
mod-refcnt-race.shattached to korg#214015 [2] which helps us easily reproduce the races described and abstracts them away from fstests. This can be used to both verify the issue and also test the new kmod patient module remover. The same bug report also has a simplebusy-open-block-device-sleep.cwhich busy opens a block device, sleep 4 seconds in a loop, closes the file descriptor, which can easily be used to force failure of the module removal (delete_module()) and test this new patient removal.Although there are patches for fstests to account for this [3] and work around it, a much suitable solution long term is for these hacks to use a patient module remover from kmod and modprobe directly. This patch does just that, it adds these new arguments to modprobe and rmmod:
(quoted diff) +0
-2-p | --remove-patiently patiently removes the module -t | --timeout timeout in ms to remove the moduleThe patient removal works by polling until the module refcnt
/sys/module/name/refcntgoes to 0. One that does happens it tries to also patiently remove the module. This second step is required as a new temporary refcnt bump can occur after a module refcnt becomes 0. By default modprobe and rmmod will wait forever, however a timeout can be specified to in milliseconds to ensure only that amount of time is spent trying to remove the module.If a timeout is used it applies cumulatively to both the refcnt wait, and for trying to remove the module once modprobe sees the refcnt is already 0.
This new fatures is useful for cases where you know the refcnt is going to become 0, and it is just a matter of time, such as with many fstests / blktests tests.
Quoted 6 lines
It is worth noting that prior to kernel v3.13 the kernel module delete system call used to support a wait flag, this was removed via commit
3f2b9c9cdf389("module: remove rmmod --wait option.") in favor for a 10 second sleep in kmod. A 10 second sleep does not address the issues, and so we must implement a patient removal properly in kmod.
The story was not kind like that. It wasn't removed "in favor for a 10 second sleep" in the sense that the sleep would replace the wait.
It was actually for "this wait logic in the kernel is complex and buggy, let's try to remove it". So we decided to deprecate it and add a sleep rmmod to see if anyone complained. 1 year later of no complains we removed it from kernel. This was all after noticing we had never implemented the wait logic in modprobe - it was only done in rmmod.
Quoted 68 lines
[0]
git://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git
[1] https://bugzilla.kernel.org/show_bug.cgi?id=21233
[2] https://bugzilla.kernel.org/show_bug.cgi?id=214015
[3] https://lkml.kernel.org/r/[email protected]Signed-off-by Luis Chamberlain <[email protected]>---
libkmod/docs/libkmod-sections.txt | 4 + libkmod/libkmod-module.c | 328 +++++++++++++++++++++++++++++ libkmod/libkmod.c | 71 +++++++ libkmod/libkmod.h | 7 + libkmod/libkmod.sym | 4 + libkmod/python/kmod/_libkmod_h.pxd | 3 + libkmod/python/kmod/module.pyx | 4 + man/modprobe.xml | 59 ++++++ man/rmmod.xml | 60 ++++++ tools/modprobe.c | 21 +- tools/remove.c | 12 +- tools/rmmod.c | 27 ++- 12 files changed, 584 insertions(+), 16 deletions(-)libkmod/docs/libkmod-sections.txt +0
-0@@ -12,6 +12,8 @@ kmod_dump_index
kmod_set_log_prioritykmod_get_log_prioritylibkmod/docs/libkmod-sections.txt +2
-0+kmod_get_refcnt_timeout +kmod_set_refcnt_timeout
kmod_set_log_fnkmod_get_userdatakmod_set_userdatalibkmod/docs/libkmod-sections.txt +0
-0@@ -56,6 +58,7 @@ kmod_module_unref_list
kmod_module_insert_modulekmod_module_probe_insert_modulekmod_module_remove_module
+kmod_module_remove_module_wait
kmod_module_get_modulekmod_module_get_dependencieslibkmod/docs/libkmod-sections.txt +0
-0@@ -102,5 +105,6 @@ kmod_module_get_initstate
kmod_module_initstate_strkmod_module_get_sizekmod_module_get_refcnt
+kmod_module_get_refcnt_waitkmod_module_get_holders
</SECTION>libkmod/libkmod-module.c +4
-0@@ -30,6 +30,9 @@#include <stdlib.h> #include <string.h> #include <unistd.h> +#include <poll.h> +#include <time.h> +#include <math.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/syscall.h> @@ -802,6 +805,143 @@ KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,return err; } +static int timespec_to_ms (struct timespec *t)
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 818
Quoted 10 lines
libkmod/libkmod-module.c +10
-0+{ +return (t->tv_sec *1000 ) +lround (t->tv_nsec /1000000 ); +} + +static int time_delta_ms (struct timespec *before,struct timespec *after) +{ +if (!before || !after) +return 0 ; +return timespec_to_ms (after) -timespec_to_ms (before); +}
we have a similar thing in util.[ch]
Quoted 7 lines
libkmod/libkmod-module.c +7
-0+ +/** +* kmod_module_remove_module_wait: +* @mod: kmod module +* @flags: flags to pass to Linux kernel when removing the module. The only valid flag is +* KMOD_REMOVE_FORCE: force remove module regardless if it's still in +* use by a kernel subsystem or other process;
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 835
Quoted 10 lines
libkmod/libkmod-module.c +10
-0+* KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to +* delete_module(2). We do the waiting in userspace, if a wait was desired. +* +* Remove a module from Linux kernel patiently. +* +* Returns: 0 on success or < 0 on failure. +*/ +KMOD_EXPORT int kmod_module_remove_module_wait(struct kmod_module *mod, +unsigned int flags, +bool wait)
why do you have kmod_get_refcnt_timeout/kmod_set_refcnt_timeout instead of just doing s/bool wait/unsigned int wait_msec/)?
Quoted 200 lines
libkmod/libkmod-module.c +193
-0+{ +int err, err_time, t_delta, t_delta_print, orig_timeout; +int time_between_prints =500 ;/* 1/2 second */ +int timeout_ms; +struct timespec t1, t2, t_print = { .tv_sec =0 , .tv_nsec =0 }; +bool printed_msg = false; + +if (mod ==NULL ) +return -ENOENT ; + +if (wait) + timeout_ms =kmod_get_refcnt_timeout (mod->ctx); + +if (!wait || !timeout_ms) +return kmod_module_remove_module (mod, flags); + + err_time =clock_gettime (CLOCK_MONOTONIC , &t1); +if (err_time !=0 ) +return kmod_module_remove_module (mod, flags); + +/* +* We now know an original patient removal was requested and +* an associated timeout was also provided. timeout_removal_ms +* should be set with what we are left with. +*/ + timeout_ms = orig_timeout =kmod_get_removal_timeout (mod->ctx); +DBG (mod->ctx,"removal timeout is %d for '%s'\n" , timeout_ms, mod->name); + +/* +* Filter out other flags and force ONONBLOCK +* +* This is true even for the patient removal. The wait flag was +* a now deprecated flag, see kernel commits: +* +* 3f2b9c9cdf389e ("module: remove rmmod --wait option." on v3.13 +* 79465d2fd48e68 ("module: remove warning about waiting module removal.") on v3.15 +* +* Patient removal is a userspace implementation. +*/ + flags &=KMOD_REMOVE_FORCE ; + flags |=KMOD_REMOVE_NOWAIT ; + +while (true) { + err =delete_module (mod->name, flags); +if (err ==0 ) +return 0 ; + err = -errno; + +if (timeout_ms ==0 ) { +ERR (mod->ctx,"timeout (%d) trying to remove module immediately '%s': %m %s\n" , + timeout_ms, mod->name,strerror (errno)); +return err; + } + +/* Wait forever request */ +if (timeout_ms <0 ) { +if (!printed_msg) { +NOTICE (mod->ctx,"%s trying forever to remove...\n" , + mod->name); + printed_msg = true; + } +continue ; + } + +/* All below is when we know we have a timeout */ + + err_time =clock_gettime (CLOCK_MONOTONIC , &t2); +if (err_time !=0 ) { +ERR (mod->ctx,"Using CLOCK_MONOTONIC failed, cannot wait, prior module removal module failed for '%s': %m %s\n" , + mod->name,strerror (errno)); +return err; + } + + t_delta =time_delta_ms (&t1, &t2); +DBG (mod->ctx,"%s removal time delta: %d ms\n" , mod->name, t_delta); + +if (t_delta >= orig_timeout) { +ERR (mod->ctx,"timeout (%d) trying to remove module '%s': %m %s\n" , + timeout_ms, mod->name,strerror (errno)); +return err; + } + +/* +* We have more time to remove the module. The above check +* also ensures that timeout_ms won't ever be 0. +*/ + timeout_ms = orig_timeout - t_delta; + +if (!printed_msg) { +NOTICE (mod->ctx,"%s removal failed. Re-trying for %d more ms (total timeout of %d ms)\n" , + mod->name, timeout_ms, orig_timeout); + t_print = t2; + printed_msg = true; + }else { +/* +* Computes the delta between our last print +* and the time of our current current +* evaluation. +*/ + t_delta_print =time_delta_ms (&t_print, &t2); +if (t_delta_print < time_between_prints) +continue ; +NOTICE (mod->ctx,"%s removal failed. Re-trying for %d more ms (total timeout of %d ms)\n" , + mod->name, timeout_ms, orig_timeout); + t_print = t2; + } + } +} +extern long init_module (const void *mem,unsigned long len,const char *args);/** @@ -1926,6 +2066,194 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)return (int )refcnt; } +/** +* kmod_module_get_refcnt_wait: +* @mod: kmod module +* @wait: if true will wait until the refcnt is 0 patiently forever +* +* Get the ref count of this @mod, as returned by Linux Kernel, by reading +* /sys filesystem and wait patiently until the refcnt is 0, if the wait bool +* argument is set to true, otherwise this behaves just as the call +* kmod_module_get_refcnt(). Enabling wait is useful if you know for sure that +* the module is quiescing soon, and so you should be able to remove it soon. +* If wait is enabled, we wait a predetermined timeout setting which is +* is configurable with kmod_set_refcnt_timeout(), by default we wait +* forever. +* +* Returns: the reference count on success or < 0 on failure. +*/ +KMOD_EXPORT int kmod_module_get_refcnt_wait(const struct kmod_module *mod, bool wait) +{ +struct pollfd fdset; +int err, err_time, num_fds=1 , t_delta, t_delta_print, orig_timeout; +int time_between_prints =500 ;/* 1/2 second */ +int timeout_ms; +long refcnt; +struct timespec t1, t2, t_print = { .tv_sec =0 , .tv_nsec =0 }; +bool printed_msg = false; + +if (wait) + timeout_ms =kmod_get_refcnt_timeout (mod->ctx); + +if (!wait || !timeout_ms) +return kmod_module_get_refcnt (mod); + + orig_timeout = timeout_ms; + +/* +* The refcnt may be something like 2, and poll() may return when +* the refcnt becomes 1, and so we must also check for the overall +* time ran and on each instance make the timeout available shorter +* +* If using the CLOCK_MONOTONIC failed and we have a timeout set, we +* simply cannot wait as we cannot trust time differences. +*/ + err_time =clock_gettime (CLOCK_MONOTONIC , &t1); +if (err_time !=0 ) { +ERR (mod->ctx,"Using the CLOCK_MONOTONIC failed (%s) so processing removal for %s without a wait for the refcnt\n" , +strerror (errno), mod->name); +return kmod_module_get_refcnt (mod); + } + +kmod_set_removal_timeout (mod->ctx, orig_timeout); + +while (true) { +DBG (mod->ctx,"%s about to poll for refcnt using timeout %d ms\n" , mod->name, timeout_ms); +memset (&fdset,0 ,sizeof (struct pollfd )); + fdset.events =POLLPRI |POLLIN ; + fdset.fd =kmod_module_get_refcnt_fd (mod); +if (fdset.fd <0 ) +return fdset.fd; + err =poll (&fdset, num_fds, timeout_ms); +if (err ==-1 ) { +DBG (mod->ctx,"%s refcnt poll failed with err: %s\n" , mod->name,strerror (errno)); + goto err; + } +if (err ==0 ) {/* timeout before fds were read */ +NOTICE (mod->ctx,"%s refcnt did not become 0 after %d ms\n" , + mod->name, orig_timeout); +kmod_set_removal_timeout (mod->ctx,0 ); + err = -ETIMEDOUT ; + goto err; + } +if (err != num_fds) {/* on success, we only asked for one fd */ + err = -EINVAL ; + goto err; + } +if (!fdset.revents) { +close (fdset.fd); +continue ; + } +if ((!fdset.revents &POLLIN )) { + err = -EINVAL ; + goto err; + } + + err =read_str_long (fdset.fd, &refcnt,10 );
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) · line 2162
Quoted 10 lines
libkmod/libkmod-module.c +10
-0+if (err <0 ) + goto err; + +close (fdset.fd); + +if ((refcnt <=0 ) || (refcnt >0 && !wait)) { +NOTICE (mod->ctx,"%s refcnt is %d\n" , mod->name, (int ) refcnt); + err_time =clock_gettime (CLOCK_MONOTONIC , &t2); +if (err_time !=0 ) +kmod_set_removal_timeout (mod->ctx,0 );
I don't follow why kmod_module_get_refcnt_wait() is setting the removal timeout at all. This seems to be doing it behind users back.
The idea of using the refcnt fd was actually that then
users could integrate it on their mainloops (probably using epoll). And then the same impl could be shared by kmod_module_remove_module_wait(), which would do a select().
This seems more like a kmod_module_refcnt_wait_zero() using poll()
+ adjusting the timeout
...
Quoted 41 lines
tools/rmmod.c +11
-8@@ -35,10 +35,12 @@#define DEFAULT_VERBOSE LOG_ERRstatic int verbose =DEFAULT_VERBOSE ;static int use_syslog; +static int do_remove_patient; -static const char cmdopts_s[] ="fsvVwh" ; +static const char cmdopts_s[] ="fpsvVwh" ;static const struct option cmdopts[] = { {"force" , no_argument,0 ,'f' }, + {"remove-patiently" , no_argument,0 ,'p' }, {"syslog" , no_argument,0 ,'s' }, {"verbose" , no_argument,0 ,'v' }, {"version" , no_argument,0 ,'V' },@@ -51,13 +53,14 @@ static void help(void)printf ("Usage:\n" "\t%s [options] modulename ...\n" "Options:\n" -"\t-f, --force forces a module unload and may crash your\n" -"\t machine. This requires Forced Module Removal\n" -"\t option in your kernel. DANGEROUS\n" -"\t-s, --syslog print to syslog, not stderr\n" -"\t-v, --verbose enables more messages\n" -"\t-V, --version show version\n" -"\t-h, --help show this help\n" , +"\t-f, --force forces a module unload and may crash your\n" +"\t machine. This requires Forced Module Removal\n" +"\t option in your kernel. DANGEROUS\n" +"\t-p, --remove-patiently patiently tries to remove the module\n" +"\t-s, --syslog print to syslog, not stderr\n" +"\t-v, --verbose enables more messages\n" +"\t-V, --version show version\n" +"\t-h, --help show this help\n" , program_invocation_short_name); }
tools/rmmod.c static int check_module_inuse(struct kmod_module *mod) { · line 99
Quoted 5 lines
tools/rmmod.c +1
-1@@ -93,7 +96,7 @@ static int check_module_inuse(struct kmod_module *mod) {return -EBUSY ; } - ret =kmod_module_get_refcnt (mod); + ret =kmod_module_get_refcnt_wait (mod, do_remove_patient);
for tool implementation, shouldn't we just ignorekmod_module_get_refcnt() and proceed tokmod_module_remove_module_wait()?
Quoted 26 lines
tools/rmmod.c +5
-1if (ret >0 ) {ERR ("Module %s is in use\n" ,kmod_module_get_name (mod));return -EBUSY ;@@ -120,6 +123,9 @@ static int do_rmmod(int argc, char *argv[])case 'f' : flags |=KMOD_REMOVE_FORCE ;break ; +case 'p' : + do_remove_patient =1 ; +break ;case 's' : use_syslog =1 ;break ;@@ -178,7 +184,8 @@ static int do_rmmod(int argc, char *argv[]) goto next; } - err =kmod_module_remove_module (mod, flags); + err =kmod_module_remove_module_wait (mod, flags, + do_remove_patient);if (err <0 ) {ERR ("could not remove module %s: %s\n" , arg,strerror (-err));Signature · 1 line
2.30.2
Re: [PATCH v2 3/3] libkmod-module: add support for a patient module removal option
Quoted 9 lines · Lucas De Marchi
On Mon, Aug 09, 2021 at 10:16:02PM -0700, Luis Chamberlain wrote:
The story was not kind like that. It wasn't removed "in favor for a 10 second sleep" in the sense that the sleep would replace the wait.It was actually for "this wait logic in the kernel is complex and buggy, let's try to remove it". So we decided to deprecate it and add a sleep rmmod to see if anyone complained. 1 year later of no complains we removed it from kernel. This was all after noticing we had never implemented the wait logic in modprobe - it was only done in rmmod.
OK fixed the commit log thanks!
Quoted 16 lines
Quoted 16 lines
libkmod/libkmod-module.c +3
-0@@ -30,6 +30,9 @@#include <stdlib.h> #include <string.h> #include <unistd.h> +#include <poll.h> +#include <time.h> +#include <math.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/syscall.h> @@ -802,6 +805,143 @@ KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,return err; }
Quoted 13 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 818
Quoted 11 lines
libkmod/libkmod-module.c +11
-0+static int timespec_to_ms (struct timespec *t) +{ +return (t->tv_sec *1000 ) +lround (t->tv_nsec /1000000 ); +} + +static int time_delta_ms (struct timespec *before,struct timespec *after) +{ +if (!before || !after) +return 0 ; +return timespec_to_ms (after) -timespec_to_ms (before); +}we have a similar thing in util.[ch]
Alright, this OK?
shared/util.c +13 -0
@@ -466,6 +466,19 @@ unsigned long long ts_usec(const struct timespec *ts) (unsigned long long ) ts->tv_nsec /NSEC_PER_USEC ; } +unsigned long long ts_msec (const struct timespec *ts) +{ +return ts_usec (ts) *1000 ; +} + +unsigned long long ts_delta_ms (const struct timespec *before, +const struct timespec *after) +{ +if (!before || !after) +return 0 ; +return ts_msec (after) -ts_msec (before); +} +unsigned long long stat_mstamp (const struct stat *st) {#ifdef HAVE_STRUCT_STAT_ST_MTIM
shared/util.h +3 -0
@@ -43,6 +43,9 @@ int mkdir_p(const char *path, int len, mode_t mode);int mkdir_parents (const char *path,mode_t mode);unsigned long long stat_mstamp (const struct stat *st);unsigned long long ts_usec (const struct timespec *ts); +unsigned long long ts_msec (const struct timespec *ts); +unsigned long long ts_delta_ms (const struct timespec *before, +const struct timespec *after);/* endianess and alignments */ /* ************************************************************************ */
Quoted 11 lines
Quoted 11 lines
libkmod/libkmod-module.c +11
-0+/** +* kmod_module_remove_module_wait: +* @mod: kmod module +* @flags: flags to pass to Linux kernel when removing the module. The only valid flag is +* KMOD_REMOVE_FORCE: force remove module regardless if it's still in +* use by a kernel subsystem or other process; +* KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to +* delete_module(2). We do the waiting in userspace, if a wait was desired. +* +* Remove a module from Linux kernel patiently. +*
Quoted 8 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 834
Quoted 5 lines
libkmod/libkmod-module.c +5
-0+* Returns: 0 on success or < 0 on failure. +*/ +KMOD_EXPORT int kmod_module_remove_module_wait(struct kmod_module *mod, +unsigned int flags, +bool wait)why do you have kmod_get_refcnt_timeout/kmod_set_refcnt_timeout instead of just doing s/bool wait/unsigned int wait_msec/)?
Because it lets us do a smaller change on the respetive tools:
tools/modprobe.c- flags |= KMOD_REMOVE_FORCE;tools/modprobe.c-tools/modprobe.c: err = kmod_module_remove_module_wait(mod, flags, do_remove_patient);tools/modprobe.c- if (err == -EEXIST) {tools/modprobe.c- if (!first_time)
--tools/remove.c- goto unref;tools/remove.c-tools/remove.c: err = kmod_module_remove_module_wait(mod, 0, do_remove_patient); tools/remove.c- if (err < 0)tools/remove.c- goto unref;
--tools/rmmod.c- }tools/rmmod.c-tools/rmmod.c: err = kmod_module_remove_module_wait(mod, flags,tools/rmmod.c- do_remove_patient);tools/rmmod.c- if (err < 0) {
That is, the timeout is contextual of the context.
Quoted 8 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 839
Quoted 5 lines
libkmod/libkmod-module.c +5
-0+if ((refcnt <=0 ) || (refcnt >0 && !wait)) { +NOTICE (mod->ctx,"%s refcnt is %d\n" , mod->name, (int ) refcnt); + err_time =clock_gettime (CLOCK_MONOTONIC , &t2); +if (err_time !=0 ) +kmod_set_removal_timeout (mod->ctx,0 );I don't follow why
kmod_module_get_refcnt_wait()is setting the removal timeout at all. This seems to be doing it behind users back.
Because if clock_gettime() returns something other than 0 then your clock is messed up and you should not be using a timeout, so yes, we correct that then. We can scream loud, or use a default.
I figured not using one would be better in that case.
Quoted 7 lines
The idea of using the refcnt fd was actually that then
users could integrate it on their mainloops (probably using epoll). And then the same impl could be shared bykmod_module_remove_module_wait(), which would do aselect().This seems more like a
kmod_module_refcnt_wait_zero()usingpoll()
+ adjusting the timeout
Sorry don't follow. And since I have one day before vacation, I suppose I won't get to this until I get back. But I'd be happy if you massage it as you see fit as you're used to the code base and I'm sure have a better idea of what likely is best for the library.
Quoted 5 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 840
Quoted 1 line
libkmod/libkmod-module.c +1
-0+ ret =kmod_module_get_refcnt_wait (mod, do_remove_patient);for tool implementation, shouldn't we just ignore
kmod_module_get_refcnt()and proceed tokmod_module_remove_module_wait()?
I'll let you decide. Otherwise this will have to wait until I get back from vacation.
Luis
Re: [PATCH v2 3/3] libkmod-module: add support for a patient module removal option
Quoted 146 lines · Luis Chamberlain
Quoted 9 lines · Lucas De Marchi
On Mon, Aug 09, 2021 at 10:16:02PM -0700, Luis Chamberlain wrote:
The story was not kind like that. It wasn't removed "in favor for a 10 second sleep" in the sense that the sleep would replace the wait.It was actually for "this wait logic in the kernel is complex and buggy, let's try to remove it". So we decided to deprecate it and add a sleep rmmod to see if anyone complained. 1 year later of no complains we removed it from kernel. This was all after noticing we had never implemented the wait logic in modprobe - it was only done in rmmod.
OK fixed the commit log thanks!
Quoted 16 lines
Quoted 16 lines
libkmod/libkmod-module.c +3
-0@@ -30,6 +30,9 @@#include <stdlib.h> #include <string.h> #include <unistd.h> +#include <poll.h> +#include <time.h> +#include <math.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/syscall.h> @@ -802,6 +805,143 @@ KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,return err; }Quoted 13 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 818
Quoted 11 lines
libkmod/libkmod-module.c +11
-0+static int timespec_to_ms (struct timespec *t) +{ +return (t->tv_sec *1000 ) +lround (t->tv_nsec /1000000 ); +} + +static int time_delta_ms (struct timespec *before,struct timespec *after) +{ +if (!before || !after) +return 0 ; +return timespec_to_ms (after) -timespec_to_ms (before); +}we have a similar thing in util.[ch]
Alright, this OK?
shared/util.c +13
-0@@ -466,6 +466,19 @@ unsigned long long ts_usec(const struct timespec *ts) (unsigned long long ) ts->tv_nsec /NSEC_PER_USEC ; } +unsigned long long ts_msec (const struct timespec *ts) +{ +return ts_usec (ts) *1000 ; +} + +unsigned long long ts_delta_ms (const struct timespec *before, +const struct timespec *after) +{ +if (!before || !after) +return 0 ; +return ts_msec (after) -ts_msec (before); +} +unsigned long long stat_mstamp (const struct stat *st) {#ifdef HAVE_STRUCT_STAT_ST_MTIM shared/util.h +3
-0@@ -43,6 +43,9 @@ int mkdir_p(const char *path, int len, mode_t mode);int mkdir_parents (const char *path,mode_t mode);unsigned long long stat_mstamp (const struct stat *st);unsigned long long ts_usec (const struct timespec *ts); +unsigned long long ts_msec (const struct timespec *ts); +unsigned long long ts_delta_ms (const struct timespec *before, +const struct timespec *after);/* endianess and alignments */ /* ************************************************************************ */ Quoted 12 lines
Quoted 12 lines
libkmod/libkmod-module.c +11
-0+/** +* kmod_module_remove_module_wait: +* @mod: kmod module +* @flags: flags to pass to Linux kernel when removing the module. The only valid flag is +* KMOD_REMOVE_FORCE: force remove module regardless if it's still in +* use by a kernel subsystem or other process; +* KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to +* delete_module(2). We do the waiting in userspace, if a wait was desired. +* +* Remove a module from Linux kernel patiently. +* Quoted 8 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 834
Quoted 5 lines
libkmod/libkmod-module.c +5
-0+* Returns: 0 on success or < 0 on failure. +*/ +KMOD_EXPORT int kmod_module_remove_module_wait(struct kmod_module *mod, +unsigned int flags, +bool wait)why do you have kmod_get_refcnt_timeout/kmod_set_refcnt_timeout instead of just doing s/bool wait/unsigned int wait_msec/)?
Because it lets us do a smaller change on the respetive tools:
tools/modprobe.c-flags |=KMOD_REMOVE_FORCE;tools/modprobe.c-tools/modprobe.c: err =kmod_module_remove_module_wait(mod, flags, do_remove_patient);tools/modprobe.c-if (err ==-EEXIST) {tools/modprobe.c-if (!first_time)
--tools/remove.c-goto unref;tools/remove.c-tools/remove.c: err =kmod_module_remove_module_wait(mod, 0, do_remove_patient);tools/remove.c-if (err < 0)tools/remove.c-goto unref;
--tools/rmmod.c-}tools/rmmod.c-tools/rmmod.c: err = kmod_module_remove_module_wait(mod, flags,tools/rmmod.c-do_remove_patient);tools/rmmod.c-if (err < 0) {That is, the timeout is contextual of the context.
Quoted 8 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 839
Quoted 5 lines
libkmod/libkmod-module.c +5
-0+if ((refcnt <=0 ) || (refcnt >0 && !wait)) { +NOTICE (mod->ctx,"%s refcnt is %d\n" , mod->name, (int ) refcnt); + err_time =clock_gettime (CLOCK_MONOTONIC , &t2); +if (err_time !=0 ) +kmod_set_removal_timeout (mod->ctx,0 );I don't follow why
kmod_module_get_refcnt_wait()is setting the removal timeout at all. This seems to be doing it behind users back.Because if
clock_gettime()returns something other than 0 then your clock is messed up and you should not be using a timeout, so yes, we correct that then. We can scream loud, or use a default.I figured not using one would be better in that case.
Quoted 5 lines
The idea of using the refcnt fd was actually that then
users could integrate it on their mainloops (probably using epoll). And then the same impl could be shared bykmod_module_remove_module_wait(), which would do aselect().
Quoted 7 lines
Quoted 2 lines
This seems more like a
kmod_module_refcnt_wait_zero()usingpoll()
+ adjusting the timeoutSorry don't follow. And since I have one day before vacation, I suppose I won't get to this until I get back. But I'd be happy if you massage it as you see fit as you're used to the code base and I'm sure have a better idea of what likely is best for the library.
sure, np. I will take a look as time permits.
thanks
Lucas De Marchi
Quoted 11 lines
Quoted 6 lines
libkmod/libkmod-module.c KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, · line 840
Quoted 2 lines
libkmod/libkmod-module.c +1
-0+ ret =kmod_module_get_refcnt_wait (mod, do_remove_patient);for tool implementation, shouldn't we just ignore
kmod_module_get_refcnt()and proceed tokmod_module_remove_module_wait()?I'll let you decide. Otherwise this will have to wait until I get back from vacation.
Luis
Re: [PATCH v2 3/3] libkmod-module: add support for a patient module removal option
Quoted 7 lines · Lucas De Marchi
Quoted 4 lines
Sorry don't follow. And since I have one day before vacation, I suppose I won't get to this until I get back. But I'd be happy if you massage it as you see fit as you're used to the code base and I'm sure have a better idea of what likely is best for the library.
sure, np. I will take a look as time permits.
Just a friendly poke.
Luis
Re: [PATCH v2 3/3] libkmod-module: add support for a patient module removal option
Quoted 10 lines · Luis Chamberlain
Quoted 7 lines · Lucas De Marchi
Quoted 4 lines
Sorry don't follow. And since I have one day before vacation, I suppose I won't get to this until I get back. But I'd be happy if you massage it as you see fit as you're used to the code base and I'm sure have a better idea of what likely is best for the library.
sure, np. I will take a look as time permits.
Just a friendly poke.
Just another friendy poke.
Luis
*Friendly poke*
Luis
Quoted 2 lines · Luis Chamberlain
*Friendly poke*
Sorry for the delay. Let me take a look in detail tomorrow.
Lucas De Marchi
Quoted 2 lines
Luis
Quoted 5 lines · Lucas De Marchi
Quoted 2 lines · Luis Chamberlain
*Friendly poke*
Sorry for the delay. Let me take a look in detail tomorrow.
*Friendly poke*
Luis
Quoted 8 lines · Luis Chamberlain
Quoted 5 lines · Lucas De Marchi
Quoted 2 lines · Luis Chamberlain
*Friendly poke*
Sorry for the delay. Let me take a look in detail tomorrow.
*Friendly poke*
oh, I dropped the ball here, sorry. Then I got busy with i915 and other projects. Let me take a look again at your implementation and respin it
Lucas De Marchi
Quoted 2 lines
Luis