Skip to content

Commit f798488

Browse files
committed
Fix incorrect conditional usage of .owner
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
1 parent 5aabe58 commit f798488

File tree

4 files changed

+4
-11
lines changed

4 files changed

+4
-11
lines changed

examples/dht11.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ static ssize_t device_read(struct file *filp, char __user *buffer,
148148
}
149149

150150
static struct file_operations fops = {
151-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
152151
.owner = THIS_MODULE,
153-
#endif
154152
.open = device_open,
155153
.release = device_release,
156-
.read = device_read
154+
.read = device_read,
157155
};
158156

159157
/* Initialize the module - Register the character device */
@@ -182,9 +180,7 @@ static int __init dht11_init(void)
182180
MINOR(dht11_device.dev_num));
183181

184182
/* Prevents module unloading while operations are in use */
185-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
186183
dht11_device.cdev.owner = THIS_MODULE;
187-
#endif
188184

189185
cdev_init(&dht11_device.cdev, &fops);
190186
ret = cdev_add(&dht11_device.cdev, dht11_device.dev_num, 1);

examples/ioctl.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ static int test_ioctl_open(struct inode *inode, struct file *filp)
140140
}
141141

142142
static struct file_operations fops = {
143-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
144143
.owner = THIS_MODULE,
145-
#endif
146144
.open = test_ioctl_open,
147145
.release = test_ioctl_close,
148146
.read = test_ioctl_read,

examples/static_key.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ static struct class *cls;
4141
static DEFINE_STATIC_KEY_FALSE(fkey);
4242

4343
static struct file_operations chardev_fops = {
44-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
4544
.owner = THIS_MODULE,
46-
#endif
4745
.open = device_open,
4846
.release = device_release,
4947
.read = device_read,

examples/vinput.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ static ssize_t vinput_write(struct file *file, const char __user *buffer,
133133
}
134134

135135
static const struct file_operations vinput_fops = {
136-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
137136
.owner = THIS_MODULE,
138-
#endif
139137
.open = vinput_open,
140138
.release = vinput_release,
141139
.read = vinput_read,
@@ -337,6 +335,9 @@ ATTRIBUTE_GROUPS(vinput_class);
337335

338336
static struct class vinput_class = {
339337
.name = "vinput",
338+
/* .owner was removed in Linux v6.4 via upstream commit 6e30a66433af ("driver core: class: remove
339+
* struct module owner out of struct class")
340+
*/
340341
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
341342
.owner = THIS_MODULE,
342343
#endif

0 commit comments

Comments
 (0)