Skip to content

Commit e413729

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 e413729

File tree

4 files changed

+7
-16
lines changed

4 files changed

+7
-16
lines changed

examples/dht11.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,10 @@ static ssize_t device_read(struct file *filp, char __user *buffer,
147147
return bytes_read;
148148
}
149149

150-
static struct file_operations fops = {
151-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
152-
.owner = THIS_MODULE,
153-
#endif
154-
.open = device_open,
155-
.release = device_release,
156-
.read = device_read
157-
};
150+
static struct file_operations fops = { .owner = THIS_MODULE,
151+
.open = device_open,
152+
.release = device_release,
153+
.read = device_read };
158154

159155
/* Initialize the module - Register the character device */
160156
static int __init dht11_init(void)
@@ -182,9 +178,7 @@ static int __init dht11_init(void)
182178
MINOR(dht11_device.dev_num));
183179

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

189183
cdev_init(&dht11_device.cdev, &fops);
190184
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)