Skip to content

Commit ab1e29c

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. For struct class, the core can safely omit .owner, but explicitly setting it remains correct and safe. Adding conditional #if checks around it only makes the examples harder to read and more confusing for readers. Remove the unnecessary version guards and unconditionally sets .owner = THIS_MODULE in the affected example code. Closes: #348
1 parent 5aabe58 commit ab1e29c

File tree

4 files changed

+0
-12
lines changed

4 files changed

+0
-12
lines changed

examples/dht11.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ 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,
156154
.read = device_read
@@ -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: 0 additions & 4 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,9 +335,7 @@ ATTRIBUTE_GROUPS(vinput_class);
337335

338336
static struct class vinput_class = {
339337
.name = "vinput",
340-
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
341338
.owner = THIS_MODULE,
342-
#endif
343339
.class_groups = vinput_class_groups,
344340
};
345341

0 commit comments

Comments
 (0)