Skip to content

Conversation

visitorckw
Copy link
Contributor

@visitorckw visitorckw commented Sep 18, 2025

The examples code wrapped .owner = THIS_MODULE inside version checks, suggesting that it is no longer needed after v6.4. This is only true for driver types where the driver core sets .owner automatically (e.g. platform and i2c drivers).

For structures such as struct file_operations, .owner must still be set explicitly by the user. Without it, module reference counting will be broken, allowing a module to be unloaded while still in use, which can lead to kernel panics.

struct class is a special case: its .owner field was removed in upstream Linux commit 6e30a66433af ("driver core: class: remove struct module owner out of struct class"). Explicitly setting .owner for struct class is safe on older kernels but must be conditionally compiled out on newer kernels.

Remove the unnecessary version guards and unconditionally sets .owner = THIS_MODULE in the affected example code.

Closes: #348


Summary by cubic

Remove version guards and always set .owner = THIS_MODULE in example drivers to fix module refcounting and avoid unload panics. This also makes the examples clearer.

  • Bug Fixes
    • Always set .owner in struct file_operations and cdev; removed LINUX_VERSION_CODE checks to ensure correct refcounting.
    • Keep .owner in struct class unguarded; optional but correct and less confusing.

Copy link
Collaborator

@EricccTaiwan EricccTaiwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking a look.

@EricccTaiwan EricccTaiwan requested a review from jserv September 18, 2025 14:27
@visitorckw
Copy link
Contributor Author

Hmm. So it looks like the new kernel even removed the .owner field from struct class? I’ll double-check.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

@visitorckw visitorckw marked this pull request as draft September 18, 2025 14:37
@visitorckw visitorckw marked this pull request as ready for review September 18, 2025 14:53
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

The examples code wrapped .owner = THIS_MODULE inside version checks,
suggesting that it is no longer needed after v6.4. This is only true
for driver types where the driver core sets .owner automatically (e.g.
platform and i2c drivers).

For structures such as struct file_operations, .owner must still be set
explicitly by the user. Without it, module reference counting will be
broken, allowing a module to be unloaded while still in use, which can
lead to kernel panics.

struct class is a special case: its .owner field was removed in
upstream Linux commit 6e30a66433af ("driver core: class: remove struct
module owner out of struct class"). Explicitly setting .owner for
struct class is safe on older kernels but must be conditionally
compiled out on newer kernels.

Remove the unnecessary version guards and unconditionally sets
.owner = THIS_MODULE in the affected example code.

Closes: sysprog21#348
@jserv jserv merged commit ecf0ed9 into sysprog21:master Sep 19, 2025
1 check passed
@jserv
Copy link
Contributor

jserv commented Sep 19, 2025

Thank @visitorckw for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rationale for removing .owner = THIS_MODULE in struct file_operations (Linux v6.4+)
3 participants