Skip to content

Commit 565a599

Browse files
authored
Merge pull request #149 from linD026/master
Fix potential concurrent problems in chardev2.c
2 parents aa4b5d7 + beb1ff1 commit 565a599

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

examples/chardev2.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ static int device_open(struct inode *inode, struct file *file)
3737
{
3838
pr_info("device_open(%p)\n", file);
3939

40-
/* We don't want to talk to two processes at the same time. */
41-
if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
42-
return -EBUSY;
43-
4440
try_module_get(THIS_MODULE);
4541
return SUCCESS;
4642
}
@@ -49,9 +45,6 @@ static int device_release(struct inode *inode, struct file *file)
4945
{
5046
pr_info("device_release(%p,%p)\n", inode, file);
5147

52-
/* We're now ready for our next caller */
53-
atomic_set(&already_open, CDEV_NOT_USED);
54-
5548
module_put(THIS_MODULE);
5649
return SUCCESS;
5750
}
@@ -129,6 +122,11 @@ device_ioctl(struct file *file, /* ditto */
129122
unsigned long ioctl_param)
130123
{
131124
int i;
125+
long ret = SUCCESS;
126+
127+
/* We don't want to talk to two processes at the same time. */
128+
if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
129+
return -EBUSY;
132130

133131
/* Switch according to the ioctl called */
134132
switch (ioctl_num) {
@@ -166,11 +164,14 @@ device_ioctl(struct file *file, /* ditto */
166164
/* This ioctl is both input (ioctl_param) and output (the return
167165
* value of this function).
168166
*/
169-
return (long)message[ioctl_param];
167+
ret = (long)message[ioctl_param];
170168
break;
171169
}
172170

173-
return SUCCESS;
171+
/* We're now ready for our next caller */
172+
atomic_set(&already_open, CDEV_NOT_USED);
173+
174+
return ret;
174175
}
175176

176177
/* Module Declarations */

0 commit comments

Comments
 (0)