Skip to contentSkip to conversation

watchdog: Set missing .owner = THIS_MODULE in watchdog_ops

PATCH In review Ian Rogers 4 participants 18 messages Nov 30 – Dec 2

Subsystems (5)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

cover watchdog: Set missing .owner = THIS_MODULE in watchdog_ops

watchdog_open() calls try_module_get(wdd->ops->owner) so a modular watchdog driver cannot be unloaded while /dev/watchdog is open. Five modular watchdog drivers omit .owner = THIS_MODULE in their const struct watchdog_ops definitions, leaving wdd->ops->owner NULL.

Split per driver to simplify review and stable backporting.

Tooling note: Found using Clang's require_explicit_initialization attribute; an AI coding assistant helped audit call sites, verify Fixes commits, and draft the changelogs. All five drivers were build-tested as modules (-DMODULE) with LLVM=1.

Ian Rogers (5):
watchdog: arm_smc_wdt: Set .owner = THIS_MODULE in watchdog_ops
watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops
watchdog: ebc-c384_wdt: Set .owner = THIS_MODULE in watchdog_ops
watchdog: max77620: Set .owner = THIS_MODULE in watchdog_ops
watchdog: meson_gxbb_wdt: Set .owner = THIS_MODULE in watchdog_ops

 drivers/watchdog/arm_smc_wdt.c    | 2 ++
 drivers/watchdog/bd96801_wdt.c    | 1 +
 drivers/watchdog/ebc-c384_wdt.c   | 1 +
 drivers/watchdog/max77620_wdt.c   | 1 +
 drivers/watchdog/meson_gxbb_wdt.c | 1 +
 5 files changed, 6 insertions(+)

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#1 lore raw

PATCH 1/5 watchdog: arm_smc_wdt: Set .owner = THIS_MODULE in watchdog_ops

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in smcwd_ops and smcwd_timeleft_ops, found with Clang's require_explicit_initialization attribute.

Fixes 72a9e7fea586 ("watchdog: Add new arm_smc_wdt watchdog driver")
Fixes cc0df5eba1b3 ("watchdog: arm_smc_wdt: get wdt status through SMCWD_GET_TIMELEFT")
---
 drivers/watchdog/arm_smc_wdt.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/watchdog/arm_smc_wdt.c +2 -0
index 479863450a9c..e593ee8f23c0 100644
@@ -97,6 +97,7 @@ static const struct watchdog_info smcwd_info = {
 };
 
 static const struct watchdog_ops smcwd_ops = {
+	.owner		= THIS_MODULE,
 	.start		= smcwd_start,
 	.stop		= smcwd_stop,
 	.ping		= smcwd_ping,
@@ -104,6 +105,7 @@ static const struct watchdog_ops smcwd_ops = {
 };
 
 static const struct watchdog_ops smcwd_timeleft_ops = {
+	.owner		= THIS_MODULE,
 	.start		= smcwd_start,
 	.stop		= smcwd_stop,
 	.ping		= smcwd_ping,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#2 lore raw

Re: [PATCH 1/5] watchdog: arm_smc_wdt: Set .owner = THIS_MODULE in watchdog_ops

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] watchdog: arm_smc_wdt: inaccurate Fixes trailer for cc0df5eba1b3 --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] watchdog: arm_smc_wdt: inaccurate Fixes trailer for cc0df5eba1b3 --

commit 734e0b2d4c499e1ba1d7b0b404129c165299863c
Author: Ian Rogers <[email protected]>

Quoted 9 lines

watchdog: arm_smc_wdt: Set .owner = THIS_MODULE in watchdog_ops

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 6 lines

Set .owner = THIS_MODULE in smcwd_ops and smcwd_timeleft_ops, found with Clang's require_explicit_initialization attribute.

Fixes 72a9e7fea586 ("watchdog: Add new arm_smc_wdt watchdog driver")
Fixes cc0df5eba1b3 ("watchdog: arm_smc_wdt: get wdt status through SMCWD_GET_TIMELEFT")

[Severity: Low]
Should the Fixes: tag for cc0df5eba1b3 ("watchdog: arm_smc_wdt: get wdt status through SMCWD_GET_TIMELEFT") be dropped?

Both smcwd_ops and smcwd_timeleft_ops were introduced without .owner in 72a9e7fea586 ("watchdog: Add new arm_smc_wdt watchdog driver"). Commit cc0df5eba1b3 ("watchdog: arm_smc_wdt: get wdt status through SMCWD_GET_TIMELEFT") only changed how smcwd_probe() selects between them, and citing it may prevent this fix from being backported to stable trees predating that commit.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH 2/5 watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

In reply to Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
---
 drivers/watchdog/bd96801_wdt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/watchdog/bd96801_wdt.c +1 -0
index ce93a900b0be..38d14b22b09d 100644
@@ -94,6 +94,7 @@ static const struct watchdog_info bd96801_wdt_info = {
 };
 
 static const struct watchdog_ops bd96801_wdt_ops = {
+	.owner		= THIS_MODULE,
 	.start		= bd96801_wdt_start,
 	.stop		= bd96801_wdt_stop,
 	.ping		= bd96801_wdt_ping,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
maintainer Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductor
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#5 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 11 lines · Matti Vaittinen
Quoted 10 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Quoted 9 lines
Quoted 2 lines
Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

Thanks,
Guenter

author
#6 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 23 lines · Guenter Roeck
Quoted 11 lines · Matti Vaittinen
Quoted 10 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Quoted 9 lines
Quoted 2 lines
Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Quoted 6 lines

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

Hi Matti, Guenter,

Thanks for the review and for applying the series (and fixing up the extra Fixes tag on patch 1)!

For preventing missing .owner in struct watchdog_ops going forward:
__must_init (wrapping Clang's require_explicit_initialization attribute) just landed in driver-core-next / linux-next for v7.5:
https://lore.kernel.org/lkml/[email protected]/

Once that reaches v7.5-rc1, adding __must_init to
struct watchdog_ops::owner will catch any driver that omits .owner at compile time on Clang 20+, including built-in or unprobed drivers where a runtime check on registration wouldn't catch a NULL owner. I can send that one-line follow-up patch once the macro is in mainline.

Thanks,
Ian

Quoted 3 lines

Thanks,
Guenter

maintainer
#7 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 19 lines · Ian Rogers
Quoted 18 lines · Guenter Roeck
Quoted 16 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

I was thinking something like renaming
devm_watchdog_register_device() to
__devm_watchdog_register_device()

and adding a macro
#define devm_watchdog_register_device(...)

which fills the .owner with THIS_MODULE and calls the __devm_watchdog_register_device().

Not sure how big of a hassle that would be though.

Quoted 3 lines
Quoted 3 lines

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

I was considering this but if I understand it right, omitting setting the .owner is legitimate when driver is built-in. I couldn't figure out how the core could check if driver doing the registration was built as a module.

Quoted 11 lines

Hi Matti, Guenter,

Thanks for the review and for applying the series (and fixing up the extra Fixes tag on patch 1)!

For preventing missing .owner in struct watchdog_ops going forward:
__must_init (wrapping Clang's require_explicit_initialization attribute) just landed in driver-core-next / linux-next for v7.5:
https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

Once that reaches v7.5-rc1, adding __must_init to
struct watchdog_ops::owner will catch any driver that omits .owner at compile time on Clang 20+, including built-in or unprobed drivers where a runtime check on registration wouldn't catch a NULL owner. I can send that one-line follow-up patch once the macro is in mainline.

Does this work if there are drivers which can't be build as modules, and which are omitting the initialization (assuming that's allowed)? If yes, I am all for it. Do you know if there is a gcc equivalent?

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductor
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
author
#8 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 68 lines · Matti Vaittinen
Quoted 19 lines · Ian Rogers
Quoted 18 lines · Guenter Roeck
Quoted 16 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

I was thinking something like renaming
devm_watchdog_register_device() to
__devm_watchdog_register_device()

and adding a macro
#define devm_watchdog_register_device(...)

which fills the .owner with THIS_MODULE and calls the __devm_watchdog_register_device().

Not sure how big of a hassle that would be though.

Quoted 3 lines
Quoted 3 lines

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

I was considering this but if I understand it right, omitting setting the .owner is legitimate when driver is built-in. I couldn't figure out how the core could check if driver doing the registration was built as a module.

Quoted 11 lines

Hi Matti, Guenter,

Thanks for the review and for applying the series (and fixing up the extra Fixes tag on patch 1)!

For preventing missing .owner in struct watchdog_ops going forward:
__must_init (wrapping Clang's require_explicit_initialization attribute) just landed in driver-core-next / linux-next for v7.5:
https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

Once that reaches v7.5-rc1, adding __must_init to
struct watchdog_ops::owner will catch any driver that omits .owner at compile time on Clang 20+, including built-in or unprobed drivers where a runtime check on registration wouldn't catch a NULL owner. I can send that one-line follow-up patch once the macro is in mainline.

Quoted 3 lines

Does this work if there are drivers which can't be build as modules, and which are omitting the initialization (assuming that's allowed)? If yes, I am all for it. Do you know if there is a gcc equivalent?

Sadly there isn't a gcc equivalent yet. With __must_init you can't omit the initialization but you can initialize to NULL, which at least requires a conscious decision that you want to omit the initialization. You can use __must_init on structs in kernel and module code.

Thanks,
Ian

Quoted 9 lines

Yours,
-- Matti

--
Matti Vaittinen
Linux kernel developer at ROHM Semiconductor
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

maintainer
#9 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 63 lines · Ian Rogers
Quoted 62 lines · Matti Vaittinen
Quoted 19 lines · Ian Rogers
Quoted 18 lines · Guenter Roeck
Quoted 16 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

I was thinking something like renaming
devm_watchdog_register_device() to
__devm_watchdog_register_device()

and adding a macro
#define devm_watchdog_register_device(...)

which fills the .owner with THIS_MODULE and calls the __devm_watchdog_register_device().

Not sure how big of a hassle that would be though.

Quoted 3 lines
Quoted 3 lines

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

I was considering this but if I understand it right, omitting setting the .owner is legitimate when driver is built-in. I couldn't figure out how the core could check if driver doing the registration was built as a module.

Quoted 11 lines

Hi Matti, Guenter,

Thanks for the review and for applying the series (and fixing up the extra Fixes tag on patch 1)!

For preventing missing .owner in struct watchdog_ops going forward:
__must_init (wrapping Clang's require_explicit_initialization attribute) just landed in driver-core-next / linux-next for v7.5:
https://lore.kernel.org/lkml/[email protected]/

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Once that reaches v7.5-rc1, adding __must_init to
struct watchdog_ops::owner will catch any driver that omits .owner at compile time on Clang 20+, including built-in or unprobed drivers where a runtime check on registration wouldn't catch a NULL owner. I can send that one-line follow-up patch once the macro is in mainline.

Please do, and while at it please also mark start as mandatory.

Quoted 10 lines
Quoted 3 lines

Does this work if there are drivers which can't be build as modules, and which are omitting the initialization (assuming that's allowed)? If yes, I am all for it. Do you know if there is a gcc equivalent?

Sadly there isn't a gcc equivalent yet. With __must_init you can't omit the initialization but you can initialize to NULL, which at least requires a conscious decision that you want to omit the initialization. You can use __must_init on structs in kernel and module code.

I am really not concerned about watchdog drivers which must be built into the kernel. AFAICS there are two of them. One (MPC5200_WDT) is not using the watchdog subsystem, and the other (RTD119X_WATCHDOG) already initializes .owner. THIS_MODULE is NULL for drivers built into the kernel, so the worst additional burden on driver developers would be the need to explicitly add ".owner = THIS_MODULE," to the driver.

Thanks,
Guenter

maintainer
#10 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

In reply to Ian Rogers
Quoted 25 lines · Ian Rogers
Quoted 24 lines · Matti Vaittinen
Quoted 22 lines · Ian Rogers
Quoted 21 lines · Guenter Roeck
Quoted 19 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

For preventing missing .owner in struct watchdog_ops going forward:
__must_init (wrapping Clang's require_explicit_initialization attribute) just landed in driver-core-next / linux-next for v7.5:
https://lore.kernel.org/lkml/[email protected]/

Once that reaches v7.5-rc1, adding __must_init to
struct watchdog_ops::owner will catch any driver that omits .owner at compile time on Clang 20+, including built-in or unprobed drivers where a runtime check on registration wouldn't catch a NULL owner. I can send that one-line follow-up patch once the macro is in mainline.

Quoted 5 lines
Quoted 3 lines

Does this work if there are drivers which can't be build as modules, and which are omitting the initialization (assuming that's allowed)? If yes, I am all for it. Do you know if there is a gcc equivalent?

Sadly there isn't a gcc equivalent yet. With __must_init you can't

Oh well, I suppose one can't have everything. This is still much better than nothing.

Quoted 4 lines

omit the initialization but you can initialize to NULL, which at least requires a conscious decision that you want to omit the initialization. You can use __must_init on structs in kernel and module code.

Sounds great to me. If I read Guenter's reply (to other mail in this thread) right, there aren't too many drivers which are built only in-kernel so requiring explicit initialization also for them doesn't sound bad to me.

The whole __must_init addition sounds good to me. Thanks for the lesson, nice to have a new tool in the box :)

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductor
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#11 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

In reply to Matti Vaittinen
Quoted 29 lines · Matti Vaittinen
Quoted 19 lines · Ian Rogers
Quoted 18 lines · Guenter Roeck
Quoted 16 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg-core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

Quoted 9 lines

I was thinking something like renaming
devm_watchdog_register_device() to
__devm_watchdog_register_device()

and adding a macro
#define devm_watchdog_register_device(...)

which fills the .owner with THIS_MODULE and calls the __devm_watchdog_register_device().

That would only work if watchdog_ops is not constant, and I really would not want to change that.

$ git grep "const struct watchdog_ops" | wc
    152    1059   12427
Quoted 8 lines

Not sure how big of a hassle that would be though.

Quoted 3 lines
Quoted 3 lines

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

I was considering this but if I understand it right, omitting setting the .owner is legitimate when driver is built-in. I couldn't figure out how the core could check if driver doing the registration was built as a module.

Oh, I don't know. I would think this should be an acceptable burden for the (I think) two watchdog drivers which must be built into the kernel.

Thanks,
Guenter

maintainer
#12 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 34 lines · Guenter Roeck
Quoted 33 lines · Matti Vaittinen
Quoted 19 lines · Ian Rogers
Quoted 18 lines · Guenter Roeck
Quoted 16 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg- core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

I was thinking something like renaming
devm_watchdog_register_device() to
__devm_watchdog_register_device()

Quoted 8 lines
Quoted 5 lines

and adding a macro
#define devm_watchdog_register_device(...)

which fills the .owner with THIS_MODULE and calls the __devm_watchdog_register_device().

That would only work if watchdog_ops is not constant, and I really would not want to change that.

Right. I missed this. It makes sense to keep ops const. Thanks for the explanation. Let's drop this idea then.

Quoted 10 lines
$ git grep "const struct watchdog_ops" | wc
     152    1059   12427
Quoted 6 lines

Not sure how big of a hassle that would be though.

Quoted 3 lines
Quoted 3 lines

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

Quoted 6 lines
Quoted 2 lines

I was considering this but if I understand it right, omitting setting the .owner is legitimate when driver is built-in. I couldn't figure out how the core could check if driver doing the registration was built as a module.

Oh, I don't know. I would think this should be an acceptable burden for the (I think) two watchdog drivers which must be built into the kernel.

You know it far better than me how requiring the .owner to be always populated impacts the drivers overall. I didn't know how common it is to only support building in the watchdog.

I think there's still another problem. Looking at the init.h, we have:

#ifdef MODULE
extern struct module __this_module;
#define THIS_MODULE (&__this_module)
#else
#define THIS_MODULE ((struct module *)0)
#endif

How to distinguish a case where the driver is configured to be built in "((struct module *)0)", from the case where the .owner is not set? Wouldn't the core check cause false alarms for all drivers when built in?

Anyways, adding the __must_init as suggested by Ian should help though. I've understood using Clang is quite popular (even though I still use gcc for a lot of my work).

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductor
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#13 lore raw

Re: [PATCH 2/5] watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops

Quoted 67 lines · Matti Vaittinen
Quoted 34 lines · Guenter Roeck
Quoted 33 lines · Matti Vaittinen
Quoted 19 lines · Ian Rogers
Quoted 18 lines · Guenter Roeck
Quoted 16 lines · Matti Vaittinen
Quoted 7 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Quoted 5 lines

Set .owner = THIS_MODULE in bd96801_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 09dad69757b6 ("watchdog: ROHM BD96801 PMIC WDG driver")
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks a lot!

This made me wonder, would it be possible to do some macro-magic so the .owner was automatically set upon driver registration to wdg- core, when driver is built as a module?

Possibly, either by defining a macro such as DEFINE_WATCHDOG_OPS, or by moving ownership out of watchdog_ops (no idea though if/how that might work, and it sounds complicated).

I was thinking something like renaming
devm_watchdog_register_device() to
__devm_watchdog_register_device()

Quoted 8 lines
Quoted 5 lines

and adding a macro
#define devm_watchdog_register_device(...)

which fills the .owner with THIS_MODULE and calls the __devm_watchdog_register_device().

That would only work if watchdog_ops is not constant, and I really would not want to change that.

Right. I missed this. It makes sense to keep ops const. Thanks for the explanation. Let's drop this idea then.

Quoted 10 lines
$ git grep "const struct watchdog_ops" | wc
     152    1059   12427
Quoted 6 lines

Not sure how big of a hassle that would be though.

Quoted 3 lines
Quoted 3 lines

It might be simpler to just add a check into the watchdog core and ensure that the owner is set when a watchdog is registered.

Quoted 6 lines
Quoted 2 lines

I was considering this but if I understand it right, omitting setting the .owner is legitimate when driver is built-in. I couldn't figure out how the core could check if driver doing the registration was built as a module.

Oh, I don't know. I would think this should be an acceptable burden for the (I think) two watchdog drivers which must be built into the kernel.

You know it far better than me how requiring the .owner to be always populated impacts the drivers overall. I didn't know how common it is to only support building in the watchdog.

I think there's still another problem. Looking at the init.h, we have:

Quoted 9 lines

#ifdef MODULE
extern struct module __this_module;
#define THIS_MODULE (&__this_module)
#else
#define THIS_MODULE ((struct module *)0)
#endif

How to distinguish a case where the driver is configured to be built in "((struct module *)0)", from the case where the .owner is not set? Wouldn't the core check cause false alarms for all drivers when built in?

It ends up being a bit complicated. One way to do it would be to actually introduce a shim macro as you had suggested, only use it to validate .owner instead of setting it.

extern int _watchdog_register_device(struct watchdog_device *, struct module *);

#define watchdog_register_device(struct watchdog_device *wdd) \
_watchdog_register_device(wdd, THIS_MODULE)

and replace the existing watchdog_register_device() with

int _watchdog_register_device(struct watchdog_device *wdd, struct module *owner)

{
	...

	if (wdd->owner != owner)
		return -EINVAL;
	...
}

I am not sure if that would be worth the trouble, though.

Quoted 2 lines

Anyways, adding the __must_init as suggested by Ian should help though. I've understood using Clang is quite popular (even though I still use gcc for a lot of my work).

Same here, but 0-day uses clang, so a failure to set .owner would still be reported. Also, I am sure that Sashiko will pick this up pretty quickly.

Thanks,
Guenter

author
#14 lore raw

PATCH 3/5 watchdog: ebc-c384_wdt: Set .owner = THIS_MODULE in watchdog_ops

In reply to Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in ebc_c384_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes c36a483d8007 ("watchdog: Add watchdog timer support for the WinSystems EBC-C384")
---
 drivers/watchdog/ebc-c384_wdt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/watchdog/ebc-c384_wdt.c +1 -0
index c80a28b09de0..e931f43bcc3f 100644
@@ -78,6 +78,7 @@ static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t)
 }
 
 static const struct watchdog_ops ebc_c384_wdt_ops = {
+	.owner = THIS_MODULE,
 	.start = ebc_c384_wdt_start,
 	.stop = ebc_c384_wdt_stop,
 	.set_timeout = ebc_c384_wdt_set_timeout
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#15 lore raw

PATCH 4/5 watchdog: max77620: Set .owner = THIS_MODULE in watchdog_ops

In reply to Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in max77620_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes ff3bb2f5c3cd ("watchdog: max77620: Add support for watchdog timer")
---
 drivers/watchdog/max77620_wdt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/watchdog/max77620_wdt.c +1 -0
index 4fc8a858d221..ad2b497f7c9a 100644
@@ -148,6 +148,7 @@ static const struct watchdog_info max77620_wdt_info = {
 };
 
 static const struct watchdog_ops max77620_wdt_ops = {
+	.owner		= THIS_MODULE,
 	.start		= max77620_wdt_start,
 	.stop		= max77620_wdt_stop,
 	.ping		= max77620_wdt_ping,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#16 lore raw

PATCH 5/5 watchdog: meson_gxbb_wdt: Set .owner = THIS_MODULE in watchdog_ops

In reply to Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) to prevent the watchdog driver module from being unloaded while /dev/watchdog is open, and watchdog_release() calls module_put(wdd->ops->owner). Because try_module_get(NULL) succeeds without incrementing any module reference count, omitting .owner = THIS_MODULE allows the module to be unloaded while the watchdog character device is open.

Set .owner = THIS_MODULE in meson_gxbb_wdt_ops, found with Clang's require_explicit_initialization attribute.

Fixes 683fa50f0e18 ("watchdog: Add Meson GXBB Watchdog Driver")
---
 drivers/watchdog/meson_gxbb_wdt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/watchdog/meson_gxbb_wdt.c +1 -0
index a88fe8cbeef5..7ee8632eba36 100644
@@ -108,6 +108,7 @@ static unsigned int meson_gxbb_wdt_get_timeleft(struct watchdog_device *wdt_dev)
 }
 
 static const struct watchdog_ops meson_gxbb_wdt_ops = {
+	.owner = THIS_MODULE,
 	.start = meson_gxbb_wdt_start,
 	.stop = meson_gxbb_wdt_stop,
 	.ping = meson_gxbb_wdt_ping,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
maintainer
#17 lore raw
In reply to Ian Rogers

Hi Ian,

Quoted 12 lines · Ian Rogers

watchdog_open() calls try_module_get(wdd->ops->owner) so a modular watchdog driver cannot be unloaded while /dev/watchdog is open. Five modular watchdog drivers omit .owner = THIS_MODULE in their const struct watchdog_ops definitions, leaving wdd->ops->owner NULL.

Split per driver to simplify review and stable backporting.

Tooling note: Found using Clang's require_explicit_initialization attribute; an AI coding assistant helped audit call sites, verify Fixes commits, and draft the changelogs. All five drivers were build-tested as modules (-DMODULE) with LLVM=1.

Quoted 6 lines

Ian Rogers (5):
watchdog: arm_smc_wdt: Set .owner = THIS_MODULE in watchdog_ops
watchdog: bd96801_wdt: Set .owner = THIS_MODULE in watchdog_ops
watchdog: ebc-c384_wdt: Set .owner = THIS_MODULE in watchdog_ops
watchdog: max77620: Set .owner = THIS_MODULE in watchdog_ops
watchdog: meson_gxbb_wdt: Set .owner = THIS_MODULE in watchdog_ops

Series applied. Note that I dropped the second Fixes: tag from the first patch as suggested by Sashiko.

Thanks,
Guenter