Skip to contentSkip to conversation

kobject: avoid to cleanup kobject after module is unloaded

PATCH In review Ming Lei 4 participants 13 messages Nov 29, 2021 – Jul 7, 2022

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v2 cover kobject: avoid to cleanup kobject after module is unloaded

Hello,

The 1st patch improves CONFIG_DEBUG_KOBJECT_RELEASE, so that kobject cleanup after unloading module can be triggered easily. With this patch, 'modprobe kset-example' can trigger the issue very quickly.

The 2nd patch fixes the issue of cleaning up object after module is unloaded, since kobj->ktype and related callbacks are often allocated as module static variable. We need to make sure kobject is really cleaned up before freeing module. This issue is reported by Petr Mladek.

V2:
- add comment on 1st patch, suggested by Greg, 1/2
- fix build failure in case that CONFIG_MODULE is disabled, and add comment in kobj_module_callback(), 2/2

Ming Lei (2):
kobject: don't delay to cleanup module kobject
kobject: wait until kobject is cleaned up before freeing module

 include/linux/kobject.h |  1 +
 lib/kobject.c           | 82 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 1/2 kobject: don't delay to cleanup module kobject

CONFIG_DEBUG_KOBJECT_RELEASE is used for debugging kobject release and cleanup issue. The module kobject is released after module_exit() returns. If this kobject is delayed too much, and may cause other kobjects cleaned up a bit earlier before freeing module, then real issue is hidden.

So don't delay module kobject's cleanup, meantime module kobject is always cleaned up synchronously, and CONFIG_DEBUG_KOBJECT_RELEASE is actually needless for module kobject.

---
 lib/kobject.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/kobject.c +14 -0
index 4a56f519139d..b81319b0bd5a 100644
@@ -16,6 +16,7 @@
 #include <linux/stat.h>
 #include <linux/slab.h>
 #include <linux/random.h>
+#include <linux/module.h>
 
 /**
  * kobject_namespace() - Return @kobj's namespace tag.
@@ -727,6 +728,19 @@ static void kobject_release(struct kref *kref)
 	struct kobject *kobj = container_of(kref, struct kobject, kref);
 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
 	unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
+
+	/*
+	 * Don't delay to release module kobject so that we can detect late
+	 * kobject release more effectively because module unloading waits
+	 * for completion of module kobject release, see mod_kobject_put.
+	 *
+	 * Meantime mod_kobject_put() always waits for completion of module
+	 * kobject's release, CONFIG_DEBUG_KOBJECT_RELEASE is basically
+	 * useless for debugging module kobject's release.
+	 */
+	if (kobj->ktype == &module_ktype)
+		delay = 0;
+
 	pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
 		 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
 	INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH V2 1/2] kobject: don't delay to cleanup module kobject

Quoted 32 lines · Ming Lei

CONFIG_DEBUG_KOBJECT_RELEASE is used for debugging kobject release and cleanup issue. The module kobject is released after module_exit() returns. If this kobject is delayed too much, and may cause other kobjects cleaned up a bit earlier before freeing module, then real issue is hidden.

So don't delay module kobject's cleanup, meantime module kobject is always cleaned up synchronously, and CONFIG_DEBUG_KOBJECT_RELEASE is actually needless for module kobject.

---
 lib/kobject.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/kobject.c +3 -0
index 4a56f519139d..b81319b0bd5a 100644
@@ -16,6 +16,7 @@
 #include <linux/stat.h>
 #include <linux/slab.h>
 #include <linux/random.h>
+#include <linux/module.h>
 
 /**
  * kobject_namespace() - Return @kobj's namespace tag.
@@ -727,6 +728,19 @@ static void kobject_release(struct kref *kref)
 	struct kobject *kobj = container_of(kref, struct kobject, kref);
 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
 	unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
+
+	/*

lib/kobject.c static void kobject_release(struct kref *kref) · line 742

Quoted 10 lines
lib/kobject.c +10 -0
+	 * Don't delay to release module kobject so that we can detect late
+	 * kobject release more effectively because module unloading waits
+	 * for completion of module kobject release, see mod_kobject_put.
+	 *
+	 * Meantime mod_kobject_put() always waits for completion of module
+	 * kobject's release, CONFIG_DEBUG_KOBJECT_RELEASE is basically
+	 * useless for debugging module kobject's release.
+	 */
+	if (kobj->ktype == &module_ktype)
+		delay = 0;

again, no, module kobjects are not more special than anything else.

greg k-h

#3 lore raw

Re: [PATCH V2 1/2] kobject: don't delay to cleanup module kobject

In reply to Ming Lei
Thank you for the patch! Yet something to improve:

Hi Ming,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on driver-core/driver-core-testing] [also build test ERROR on v5.16-rc4 next-20211207]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Ming-Lei/kobject-avoid-to-cleanup-kobject-after-module-is-unloaded/20211129-114940
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git 2043727c2882928a10161ddee52b196b7db402fd
config: s390-randconfig-r033-20211207 (https://download.01.org/0day-ci/archive/20211207/202112072131.XiIJR8RS-lkp(a)intel.com/config)
compiler: s390-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/39e48129051282ab6eff0746002e84cf56218bd7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Ming-Lei/kobject-avoid-to-cleanup-kobject-after-module-is-unloaded/20211129-114940 git checkout 39e48129051282ab6eff0746002e84cf56218bd7
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=s390 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

lib/kobject.c: In function 'kobject_release':

>> lib/kobject.c:741:29: error: 'module_ktype' undeclared (first use in this function); did you mean 'module_state'?
     741 |         if (kobj->ktype == &module_ktype)
         |                             ^~~~~~~~~~~~
         |                             module_state

lib/kobject.c:741:29: note: each undeclared identifier is reported only once for each function it appears in

vim +741 lib/kobject.c

725
726 static void kobject_release(struct kref *kref)
727 {
728 struct kobject *kobj = container_of(kref, struct kobject, kref);
729 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
730 unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
731
732 /*
733 * Don't delay to release module kobject so that we can detect late
734 * kobject release more effectively because module unloading waits
735 * for completion of module kobject release, see mod_kobject_put.
736 *
737 * Meantime mod_kobject_put() always waits for completion of module
738 * kobject's release, CONFIG_DEBUG_KOBJECT_RELEASE is basically
739 * useless for debugging module kobject's release.
740 */

 > 741		if (kobj->ktype == &module_ktype)

742 delay = 0;
743
744 pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
745 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
746 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
747
748 schedule_delayed_work(&kobj->release, delay);
749 #else
750 kobject_cleanup(kobj);
751 #endif
752 }
753

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author
#4 lore raw

PATCH v2 2/2 kobject: wait until kobject is cleaned up before freeing module

In reply to Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject. Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

It is supposed that no activity is on kobject itself any more since module_exit() is started, so it is reasonable for the kobject user or driver to expect that kobject can be really released in the last run of kobject_put() in module_exit() code path. Otherwise, it can be thought as one driver's bug since the module is going away.

When the ->ktype and ->ktype->release are allocated as module static variable, it can cause trouble because the delayed cleanup handler may be run after the module is unloaded.

Fixes the issue by flushing scheduled kobject cleanup work before freeing module.

---
 include/linux/kobject.h |  1 +
 lib/kobject.c           | 68 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
include/linux/kobject.h +1 -0
index efd56f990a46..8d4e26171654 100644
@@ -70,6 +70,7 @@ struct kobject {
 	struct kernfs_node	*sd; /* sysfs directory entry */
 	struct kref		kref;
 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
+	struct list_head	node;
 	struct delayed_work	release;
 #endif
 	unsigned int state_initialized:1;
lib/kobject.c +68 -0

106 lines not on this page · raw message

Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Quoted 2 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 5 lines

It is supposed that no activity is on kobject itself any more since module_exit() is started, so it is reasonable for the kobject user or driver to expect that kobject can be really released in the last run of kobject_put() in module_exit() code path. Otherwise, it can be thought as one driver's bug since the module is going away.

Why is module_exit() somehow special here? What is so odd about that?

Quoted 3 lines

When the ->ktype and ->ktype->release are allocated as module static variable, it can cause trouble because the delayed cleanup handler may be run after the module is unloaded.

Why is ktype and release part of module code?

What module kobject is causing this problem?

Quoted 2 lines

Fixes the issue by flushing scheduled kobject cleanup work before freeing module.

Why are modules special here?

And if you enable this option, and then start unloading kernel modules, yes, things can go wrong, but that's not what this kernel option is for at all.

This feels like a hack for not a real problem.

thanks,

greg k-h

author
#6 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 11 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Yes, so CONFIG_DEBUG_KOBJECT_RELEASE needs to be fixed.

Quoted 5 lines
Quoted 3 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Quoted 8 lines
Quoted 6 lines

It is supposed that no activity is on kobject itself any more since module_exit() is started, so it is reasonable for the kobject user or driver to expect that kobject can be really released in the last run of kobject_put() in module_exit() code path. Otherwise, it can be thought as one driver's bug since the module is going away.

Why is module_exit() somehow special here? What is so odd about that?

After module_exit() is done, the module will be unloaded, then any code or data stored in the module can't be referred.

Quoted 6 lines
Quoted 4 lines

When the ->ktype and ->ktype->release are allocated as module static variable, it can cause trouble because the delayed cleanup handler may be run after the module is unloaded.

Why is ktype and release part of module code?

Lots of driver defines ktype and ktype->release in its module static variable.

Quoted 2 lines

What module kobject is causing this problem?

Any modules which defines its ktype and ktype->release in its module static variable, which is pretty common.

Quoted 11 lines
Quoted 3 lines

Fixes the issue by flushing scheduled kobject cleanup work before freeing module.

Why are modules special here?

And if you enable this option, and then start unloading kernel modules, yes, things can go wrong, but that's not what this kernel option is for at all.

This feels like a hack for not a real problem.

I think it is caused by CONFIG_DEBUG_KOBJECT_RELEASE, that is why this patch is posted. Otherwise I'd suggest to remove CONFIG_DEBUG_KOBJECT_RELEASE, which supposes to not panic kernel since there isn't anything wrong from driver side.

Thanks,
Ming

#7 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 8 lines · Ming Lei
Quoted 7 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

Quoted 6 lines
Quoted 4 lines

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Yes, so CONFIG_DEBUG_KOBJECT_RELEASE needs to be fixed.

What is broken with it today? It is there for you to find problems in your kernel code that uses kobjects. What oops/crash/whatever is it causing that you feel it should not be causing?

A module's kobject is "owned" by the module core, not the module code that is being unloaded, so I don't see the problem here. More details please.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 7 lines

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Is there any "real" kernel code that this causes problems on?

Again, this is for debugging, yes, this tiny example will crash that way, but that is fine, as we can obviously see that the kernel code here is correct.

And if you really want to ensure that it works properly, let's wait on release before allowing that module to be unloaded. But again, module unload is NOT a normal operation and is not what this debugging option was created to help out with.

Again, the confusion between kobjects (which protect data) and module references (which protect code) is getting mixed up here.

Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

It is supposed that no activity is on kobject itself any more since module_exit() is started, so it is reasonable for the kobject user or driver to expect that kobject can be really released in the last run of kobject_put() in module_exit() code path. Otherwise, it can be thought as one driver's bug since the module is going away.

Why is module_exit() somehow special here? What is so odd about that?

After module_exit() is done, the module will be unloaded, then any code or data stored in the module can't be referred.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

When the ->ktype and ->ktype->release are allocated as module static variable, it can cause trouble because the delayed cleanup handler may be run after the module is unloaded.

Why is ktype and release part of module code?

Lots of driver defines ktype and ktype->release in its module static variable.

They do? Where?

Quoted 4 lines
Quoted 1 line

What module kobject is causing this problem?

Any modules which defines its ktype and ktype->release in its module static variable, which is pretty common.

What non-example code does this? Let's fix that.

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

Fixes the issue by flushing scheduled kobject cleanup work before freeing module.

Why are modules special here?

And if you enable this option, and then start unloading kernel modules, yes, things can go wrong, but that's not what this kernel option is for at all.

Quoted 6 lines
Quoted 1 line

This feels like a hack for not a real problem.

I think it is caused by CONFIG_DEBUG_KOBJECT_RELEASE, that is why this patch is posted. Otherwise I'd suggest to remove CONFIG_DEBUG_KOBJECT_RELEASE, which supposes to not panic kernel since there isn't anything wrong from driver side.

Perhaps just put a nice warning in that debug option that says "beware of unloading modules with this option enabled."

Or better yet, forbid it if that option is enabled :)

thanks,

greg k-h

author
#8 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 14 lines · Greg Kroah-Hartman
Quoted 13 lines · Ming Lei
Quoted 11 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Quoted 7 lines
Quoted 1 line

Yes, so CONFIG_DEBUG_KOBJECT_RELEASE needs to be fixed.

What is broken with it today? It is there for you to find problems in your kernel code that uses kobjects. What oops/crash/whatever is it causing that you feel it should not be causing?

A module's kobject is "owned" by the module core, not the module code

No, this patch is nothing to do with module's kobject, we are talking about any kobjects allocated/released from one driver built as module.

Quoted 2 lines

that is being unloaded, so I don't see the problem here. More details please.

If CONFIG_DEBUG_KOBJECT_RELEASE is enabled, kobject_release() will schedule a (random time)delay work to run kobject_cleanup(), and the delay work may be run after the module which allocates/frees the kobject is unloaded.

kobject_cleanup():

	struct kobj_type *t = get_ktype(kobj);

	...
	if (t && t->release) {
                pr_debug("kobject: '%s' (%p): calling ktype release\n",
                         kobject_name(kobj), kobj);
                t->release(kobj);
	}

Both kobj_type and ->release are allocated in the module data/text section, so kernel panic is triggered when 't && t->release' is run from the delay work context.

Quoted 14 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 7 lines

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Quoted 5 lines

Is there any "real" kernel code that this causes problems on?

Again, this is for debugging, yes, this tiny example will crash that way, but that is fine, as we can obviously see that the kernel code here is correct.

Nothing is wrong with kset-example, the issue is just that foo_ktype and foo_release are allocated in code/data section of the module 'kset-example'.

There are ~150 such uses:

[linux]$ git grep -n "static struct kobj_type" ./ | grep "{" | wc

    153     923   11676

Most of the code can be built as module, so all should have such problem, that is why I think it as one generic issue, not kset-example specific. Here kset-example is referred just for showing the issue easily.

Quoted 3 lines

And if you really want to ensure that it works properly, let's wait on release before allowing that module to be unloaded. But again, module

Then all modules which uses kobject need such change.

Quoted 2 lines

unload is NOT a normal operation and is not what this debugging option was created to help out with.

But CONFIG_MODULE and CONFIG_DEBUG_KOBJECT_RELEASE can be enabled at the same time.

Quoted 50 lines

Again, the confusion between kobjects (which protect data) and module references (which protect code) is getting mixed up here.

Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

It is supposed that no activity is on kobject itself any more since module_exit() is started, so it is reasonable for the kobject user or driver to expect that kobject can be really released in the last run of kobject_put() in module_exit() code path. Otherwise, it can be thought as one driver's bug since the module is going away.

Why is module_exit() somehow special here? What is so odd about that?

After module_exit() is done, the module will be unloaded, then any code or data stored in the module can't be referred.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

When the ->ktype and ->ktype->release are allocated as module static variable, it can cause trouble because the delayed cleanup handler may be run after the module is unloaded.

Why is ktype and release part of module code?

Lots of driver defines ktype and ktype->release in its module static variable.

They do? Where?

Quoted 4 lines
Quoted 1 line

What module kobject is causing this problem?

Any modules which defines its ktype and ktype->release in its module static variable, which is pretty common.

What non-example code does this? Let's fix that.

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

Fixes the issue by flushing scheduled kobject cleanup work before freeing module.

Why are modules special here?

And if you enable this option, and then start unloading kernel modules, yes, things can go wrong, but that's not what this kernel option is for at all.

Quoted 6 lines
Quoted 1 line

This feels like a hack for not a real problem.

I think it is caused by CONFIG_DEBUG_KOBJECT_RELEASE, that is why this patch is posted. Otherwise I'd suggest to remove CONFIG_DEBUG_KOBJECT_RELEASE, which supposes to not panic kernel since there isn't anything wrong from driver side.

Quoted 4 lines

Perhaps just put a nice warning in that debug option that says "beware of unloading modules with this option enabled."

Or better yet, forbid it if that option is enabled :)

You mean disabling CONFIG_DEBUG_KOBJECT_RELEASE if CONFIG_MODULE is enabled?

Thanks,
Ming

#9 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

In reply to Greg Kroah-Hartman
Quoted 27 lines · Greg Kroah-Hartman
Quoted 18 lines · Ming Lei
Quoted 12 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Quoted 4 lines
Quoted 2 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 7 lines

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Quoted 8 lines

Is there any "real" kernel code that this causes problems on?

Again, this is for debugging, yes, this tiny example will crash that way, but that is fine, as we can obviously see that the kernel code here is correct.

And if you really want to ensure that it works properly, let's wait on release before allowing that module to be unloaded.

This is exactly what this patch is trying to achieve. IMHO, we should do it another way, see below.

Quoted 2 lines

But again, module unload is NOT a normal operation and is not what this debugging option was created to help out with.

But people do unload module and especially when testing kernel. IMHO, we want both CONFIG_DEBUG_KOBJECT_RELEASE and module unload enabled when testing kernel.

Quoted 2 lines

Again, the confusion between kobjects (which protect data) and module references (which protect code) is getting mixed up here.

This is perfect description of the problem. And the problem is real.

kobjects protect data but they need to call code (callbacks) when they are released. module unload is special because it removes the code. CONFIG_DEBUG_KOBJECT_RELEASE always delays the code calls. It results into a crash even when everything works as expected.

Now, back to the proposed patch. I agree that it looks weird. It makes CONFIG_DEBUG_KOBJECT_RELEASE useless in this scenario.

I have another idea. What about adding a pointer to struct module *mod into struct kobj_type. Some reference counter and wait_queue into struct module. They might be used to block the module_exit() until the reference counter reaches zero.

I mean something like:

Let's take samples/kobject/kset-sample.c as an example. We could define:

static struct kobj_type foo_ktype = {
.sysfs_ops = &foo_sysfs_ops,
.release = foo_release,
.default_groups = foo_default_groups,
.mod = THIS_MODULE,
};

then we might do:

static int kobject_add_internal(struct kobject *kobj) {
[...]

	if (kobject->ktype->mod)
		module_get_kobject_referece(kobject->ktype->mod);

[...]
}

and

static void kobject_cleanup(struct kobject *kobj)
{
[...]

	if (kobject->ktype->mod)
		module_put_kobject_referece(kobject->ktype->mod);

[...]
}

where

void module_get_kobject_referece(struct module *mod)

{
	mutex_lock(&module_mutex);
	mod->kobject_ref++;
	mutex_lock(&module_mutex);
}

void module_put_kobject_referece(struct module *mod)

{
	struct wait_queue_head *module_kobject_wq;

	mutex_lock(&module_mutex);
	mod->kobject_ref--;
	if (!mod->kobject_ref)
		wake_up(mod->kobj_release_wq);
	mutex_lock(&module_mutex);
}

and

SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
unsigned int, flags)
{
[...]

	wait_event_interruptible(mod->kobj_release_wq, !mod->kobj_ref);

[...]
}

There might be many details to be solved.

But it looks like a win-win solution. It should make module unload much more secure. Broken modules will just get blocked in module_cleanup forever. CONFIG_DEBUG_KOBJECT_RELEASE will still work as designed.

Best Regards,
Petr

author
#10 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 54 lines · Petr Mladek
Quoted 27 lines · Greg Kroah-Hartman
Quoted 18 lines · Ming Lei
Quoted 12 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Quoted 4 lines
Quoted 2 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 7 lines

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Quoted 8 lines

Is there any "real" kernel code that this causes problems on?

Again, this is for debugging, yes, this tiny example will crash that way, but that is fine, as we can obviously see that the kernel code here is correct.

And if you really want to ensure that it works properly, let's wait on release before allowing that module to be unloaded.

This is exactly what this patch is trying to achieve. IMHO, we should do it another way, see below.

Quoted 2 lines

But again, module unload is NOT a normal operation and is not what this debugging option was created to help out with.

But people do unload module and especially when testing kernel. IMHO, we want both CONFIG_DEBUG_KOBJECT_RELEASE and module unload enabled when testing kernel.

Quoted 2 lines

Again, the confusion between kobjects (which protect data) and module references (which protect code) is getting mixed up here.

This is perfect description of the problem. And the problem is real.

Quoted 8 lines

kobjects protect data but they need to call code (callbacks) when they are released. module unload is special because it removes the code. CONFIG_DEBUG_KOBJECT_RELEASE always delays the code calls. It results into a crash even when everything works as expected.

Now, back to the proposed patch. I agree that it looks weird. It makes CONFIG_DEBUG_KOBJECT_RELEASE useless in this scenario.

Can you explain how this patch makes CONFIG_DEBUG_KOBJECT_RELEASE useless? The kobject is still cleaned up with random delay.

Quoted 49 lines

I have another idea. What about adding a pointer to struct module *mod into struct kobj_type. Some reference counter and wait_queue into struct module. They might be used to block the module_exit() until the reference counter reaches zero.

I mean something like:

Let's take samples/kobject/kset-sample.c as an example. We could define:

static struct kobj_type foo_ktype = {
.sysfs_ops = &foo_sysfs_ops,
.release = foo_release,
.default_groups = foo_default_groups,
.mod = THIS_MODULE,
};

then we might do:

static int kobject_add_internal(struct kobject *kobj) {
[...]

	if (kobject->ktype->mod)
		module_get_kobject_referece(kobject->ktype->mod);

[...]
}

and

static void kobject_cleanup(struct kobject *kobj)
{
[...]

	if (kobject->ktype->mod)
		module_put_kobject_referece(kobject->ktype->mod);

[...]
}

where

void module_get_kobject_referece(struct module *mod)

{
	mutex_lock(&module_mutex);
	mod->kobject_ref++;
	mutex_lock(&module_mutex);
}

void module_put_kobject_referece(struct module *mod)

Quoted 8 lines
{
	struct wait_queue_head *module_kobject_wq;

	mutex_lock(&module_mutex);
	mod->kobject_ref--;
	if (!mod->kobject_ref)
		wake_up(mod->kobj_release_wq);
	mutex_lock(&module_mutex);

The question is why kobject is so special for taking one extra module ref here.

Quoted 13 lines

}

and

SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
unsigned int, flags)
{
[...]

	wait_event_interruptible(mod->kobj_release_wq, !mod->kobj_ref);

[...]
}

Quoted 6 lines

There might be many details to be solved.

But it looks like a win-win solution. It should make module unload much more secure. Broken modules will just get blocked in module_cleanup forever. CONFIG_DEBUG_KOBJECT_RELEASE will still work as designed.

The above approach might work, but it needs every driver with kobjects to be changed, so it is more complicated.

Thanks
Ming

#11 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 61 lines · Ming Lei
Quoted 60 lines · Petr Mladek
Quoted 27 lines · Greg Kroah-Hartman
Quoted 18 lines · Ming Lei
Quoted 12 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Quoted 4 lines
Quoted 2 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 7 lines

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Quoted 8 lines

Is there any "real" kernel code that this causes problems on?

Again, this is for debugging, yes, this tiny example will crash that way, but that is fine, as we can obviously see that the kernel code here is correct.

And if you really want to ensure that it works properly, let's wait on release before allowing that module to be unloaded.

This is exactly what this patch is trying to achieve. IMHO, we should do it another way, see below.

Quoted 2 lines

But again, module unload is NOT a normal operation and is not what this debugging option was created to help out with.

But people do unload module and especially when testing kernel. IMHO, we want both CONFIG_DEBUG_KOBJECT_RELEASE and module unload enabled when testing kernel.

Quoted 2 lines

Again, the confusion between kobjects (which protect data) and module references (which protect code) is getting mixed up here.

This is perfect description of the problem. And the problem is real.

kobjects protect data but they need to call code (callbacks) when they are released. module unload is special because it removes the code. CONFIG_DEBUG_KOBJECT_RELEASE always delays the code calls. It results into a crash even when everything works as expected.

Quoted 5 lines
Quoted 2 lines

Now, back to the proposed patch. I agree that it looks weird. It makes CONFIG_DEBUG_KOBJECT_RELEASE useless in this scenario.

Can you explain how this patch makes CONFIG_DEBUG_KOBJECT_RELEASE useless? The kobject is still cleaned up with random delay.

To make it clear. The patch still allows to detect many problems, like use-after-free.

I wrote "in this scenario". By "this scenario" I mean that a module might be removed while some kobjects might still use its code.

IMHO, the confusion is because CONFIG_DEBUG_KOBJECT_RELEASE does really bad job in this scenario. It is supposed to help to catch these problems. But it actively creates the problem even when the code is correct.

Your patch is trying to remove the false positives. My concern is that it reduces the chance to see real problems.

IMHO, there is a design problem:

+ CONFIG_DEBUG_KOBJECT_RELEASE delays the release because nobody knows which kobject_put() is the last one.

+ kobject release callback need to call a code from the module. But module_exit() does not wait because it is not aware of the still referenced callbacks.

By other words, all the crashes caused by CONFIG_DEBUG_KOBJECT_RELEASE and module removal are yelling at developers that there is this design problem.

IMHO, the right solution is to fix this design problem instead of calming down CONFIG_DEBUG_KOBJECT_RELEASE.

Quoted 75 lines
Quoted 48 lines

I have another idea. What about adding a pointer to struct module *mod into struct kobj_type. Some reference counter and wait_queue into struct module. They might be used to block the module_exit() until the reference counter reaches zero.

I mean something like:

Let's take samples/kobject/kset-sample.c as an example. We could define:

static struct kobj_type foo_ktype = {
.sysfs_ops = &foo_sysfs_ops,
.release = foo_release,
.default_groups = foo_default_groups,
.mod = THIS_MODULE,
};

then we might do:

static int kobject_add_internal(struct kobject *kobj) {
[...]

	if (kobject->ktype->mod)
		module_get_kobject_referece(kobject->ktype->mod);

[...]
}

and

static void kobject_cleanup(struct kobject *kobj)
{
[...]

	if (kobject->ktype->mod)
		module_put_kobject_referece(kobject->ktype->mod);

[...]
}

where

void module_get_kobject_referece(struct module *mod)

{
	mutex_lock(&module_mutex);
	mod->kobject_ref++;
	mutex_lock(&module_mutex);
}

void module_put_kobject_referece(struct module *mod)

Quoted 8 lines
{
	struct wait_queue_head *module_kobject_wq;

	mutex_lock(&module_mutex);
	mod->kobject_ref--;
	if (!mod->kobject_ref)
		wake_up(mod->kobj_release_wq);
	mutex_lock(&module_mutex);

The question is why kobject is so special for taking one extra module ref here.

Quoted 15 lines

}

and

SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
unsigned int, flags)
{
[...]

	wait_event_interruptible(mod->kobj_release_wq, !mod->kobj_ref);

[...]
}

There might be many details to be solved.

Quoted 7 lines
Quoted 4 lines

But it looks like a win-win solution. It should make module unload much more secure. Broken modules will just get blocked in module_cleanup forever. CONFIG_DEBUG_KOBJECT_RELEASE will still work as designed.

The above approach might work, but it needs every driver with kobjects to be changed, so it is more complicated.

As explained above. There is a design problem. modules do not wait for kobject release. kobject release is not synchronous by design.

Motivation for the more complex solution:

Bad reference counting or dependencies of kobjects might cause:

+ memory leak (not good but not fatal)

+ data use after free (serious but there is still chance to survive)

+ code use after module removal (always fatal)

From this POV, early module removal is more fatal than other possible problems. It would deserve some effort.

Regarding the complexity. If the approach with kobject_type worked then we would need to add ".mod = THIS_MODULE" into all/most statically defined kobject_type structures:

git grep "static struct kobj_type" | wc -l
156

Even mass change (using coccinelle?) might be safe. THIS_MODULE is NULL when the code is compiled in. It should use the right module when the code is built as module...

Of course, it is possible that I miss something important. My view is based a lot on some feeling/intuition. I am still trying to better understand and describe it.

Best Regards,
Petr

author
#12 lore raw

Re: [PATCH V2 2/2] kobject: wait until kobject is cleaned up before freeing module

Quoted 83 lines · Petr Mladek
Quoted 61 lines · Ming Lei
Quoted 60 lines · Petr Mladek
Quoted 27 lines · Greg Kroah-Hartman
Quoted 18 lines · Ming Lei
Quoted 12 lines · Greg Kroah-Hartman
Quoted 5 lines · Ming Lei

kobject_put() may become asynchronously because of CONFIG_DEBUG_KOBJECT_RELEASE, so once kobject_put() returns, the caller may expect the kobject is released after the last refcnt is dropped, however CONFIG_DEBUG_KOBJECT_RELEASE just schedules one delayed work function for cleaning up the kobject.

The caller should NOT expect the kobject to be released. That's the whole point of dynamic reference counted objects, you never "know" when the last object is released. This option just makes it obvious so that you know when to fix up code that has this assumption.

Quoted 4 lines
Quoted 2 lines

Inside the cleanup handler, kobj->ktype and kobj->ktype->release are required.

Yes. Is that a problem?

Quoted 7 lines

Of course for CONFIG_DEBUG_KOBJECT_RELEASE, which delays to call ->release after random time, when the module for storing ->ktype and ->ktype->release has been unloaded.

As I mentioned, the issue can be triggered 100% by 'modprobe -r kset-example' when CONFIG_DEBUG_KOBJECT_RELEASE is enabled if the 1st patch is applied.

Quoted 8 lines

Is there any "real" kernel code that this causes problems on?

Again, this is for debugging, yes, this tiny example will crash that way, but that is fine, as we can obviously see that the kernel code here is correct.

And if you really want to ensure that it works properly, let's wait on release before allowing that module to be unloaded.

This is exactly what this patch is trying to achieve. IMHO, we should do it another way, see below.

Quoted 2 lines

But again, module unload is NOT a normal operation and is not what this debugging option was created to help out with.

But people do unload module and especially when testing kernel. IMHO, we want both CONFIG_DEBUG_KOBJECT_RELEASE and module unload enabled when testing kernel.

Quoted 2 lines

Again, the confusion between kobjects (which protect data) and module references (which protect code) is getting mixed up here.

This is perfect description of the problem. And the problem is real.

kobjects protect data but they need to call code (callbacks) when they are released. module unload is special because it removes the code. CONFIG_DEBUG_KOBJECT_RELEASE always delays the code calls. It results into a crash even when everything works as expected.

Quoted 5 lines
Quoted 2 lines

Now, back to the proposed patch. I agree that it looks weird. It makes CONFIG_DEBUG_KOBJECT_RELEASE useless in this scenario.

Can you explain how this patch makes CONFIG_DEBUG_KOBJECT_RELEASE useless? The kobject is still cleaned up with random delay.

To make it clear. The patch still allows to detect many problems, like use-after-free.

I wrote "in this scenario". By "this scenario" I mean that a module might be removed while some kobjects might still use its code.

IMHO, the confusion is because CONFIG_DEBUG_KOBJECT_RELEASE does really bad job in this scenario. It is supposed to help to catch these problems. But it actively creates the problem even when the code is correct.

Your patch is trying to remove the false positives. My concern is that it reduces the chance to see real problems.

Quoted 8 lines

IMHO, there is a design problem:

+ CONFIG_DEBUG_KOBJECT_RELEASE delays the release because nobody knows which kobject_put() is the last one.

+ kobject release callback need to call a code from the module. But module_exit() does not wait because it is not aware of the still referenced callbacks.

Yeah, I think it is one fundamental issue. There is neither kobject_put_final() interface, nor reference counter returned from kobject_put(). Any kobject_put() called before module_exit() might trigger use after free if this kobject or any its ancestor's release handler is called after module_exit() returns.

Recently Changhui reports such problem on scsi_debug, turns out the scsi host's parent device release handler can be called after the scsi driver's module is unloaded.

sdebug_add_host_helper():

	sdbg_host->dev.release = &sdebug_release_adapter;
Quoted 7 lines

By other words, all the crashes caused by CONFIG_DEBUG_KOBJECT_RELEASE and module removal are yelling at developers that there is this design problem.

IMHO, the right solution is to fix this design problem instead of calming down CONFIG_DEBUG_KOBJECT_RELEASE.

Now I agree.

Quoted 82 lines
Quoted 82 lines
Quoted 50 lines

I have another idea. What about adding a pointer to struct module *mod into struct kobj_type. Some reference counter and wait_queue into struct module. They might be used to block the module_exit() until the reference counter reaches zero.

I mean something like:

Let's take samples/kobject/kset-sample.c as an example. We could define:

static struct kobj_type foo_ktype = {
.sysfs_ops = &foo_sysfs_ops,
.release = foo_release,
.default_groups = foo_default_groups,
.mod = THIS_MODULE,
};

then we might do:

static int kobject_add_internal(struct kobject *kobj) {
[...]

	if (kobject->ktype->mod)
		module_get_kobject_referece(kobject->ktype->mod);

[...]
}

and

static void kobject_cleanup(struct kobject *kobj)
{
[...]

	if (kobject->ktype->mod)
		module_put_kobject_referece(kobject->ktype->mod);

[...]
}

where

void module_get_kobject_referece(struct module *mod)

{
	mutex_lock(&module_mutex);
	mod->kobject_ref++;
	mutex_lock(&module_mutex);
}

void module_put_kobject_referece(struct module *mod)

Quoted 8 lines
{
	struct wait_queue_head *module_kobject_wq;

	mutex_lock(&module_mutex);
	mod->kobject_ref--;
	if (!mod->kobject_ref)
		wake_up(mod->kobj_release_wq);
	mutex_lock(&module_mutex);

The question is why kobject is so special for taking one extra module ref here.

Quoted 19 lines

}

and

SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
unsigned int, flags)
{
[...]

	wait_event_interruptible(mod->kobj_release_wq, !mod->kobj_ref);

[...]
}

There might be many details to be solved.

But it looks like a win-win solution. It should make module unload much more secure. Broken modules will just get blocked in module_cleanup forever. CONFIG_DEBUG_KOBJECT_RELEASE will still work as designed.

Quoted 5 lines
Quoted 2 lines

The above approach might work, but it needs every driver with kobjects to be changed, so it is more complicated.

As explained above. There is a design problem. modules do not wait for kobject release. kobject release is not synchronous by design.

It could be one generic referencing counting vs. module unload.

Either the reference counter data or the associated released handler can't be referred after the module is unloaded.

Quoted 16 lines

Motivation for the more complex solution:

Bad reference counting or dependencies of kobjects might cause:

+ memory leak (not good but not fatal)

+ data use after free (serious but there is still chance to survive)

+ code use after module removal (always fatal)

From this POV, early module removal is more fatal than other possible problems. It would deserve some effort.

Quoted 6 lines

Regarding the complexity. If the approach with kobject_type worked then we would need to add ".mod = THIS_MODULE" into all/most statically defined kobject_type structures:

git grep "static struct kobj_type" | wc -l
156

You didn't count device release handler, which should have more use cases. And almost every 'struct device' and its container is allocated dynamically actually, but code is linked statically.

Thanks,
Ming