cleanup of hiddev
Hi Jiřis,
this is the cleanup of hiddev against current vanilla. What do you think?
Regards
Oliver
---
drivers/hid/usbhid/hiddev.c +18 -17
@@ -266,11 +266,11 @@ static int hiddev_release(struct inode * inode, struct file * file)static int hiddev_open (struct inode *inode,struct file *file) {struct hiddev_list *list; -unsigned long flags; +int res;int i =iminor (inode) -HIDDEV_MINOR_BASE ; -if (i >=HIDDEV_MINORS || !hiddev_table[i]) +if (i >=HIDDEV_MINORS || i <0 || !hiddev_table[i])return -ENODEV ;if (!(list =kzalloc (sizeof (struct hiddev_list ),GFP_KERNEL )))
@@ -278,17 +278,25 @@ static int hiddev_open(struct inode *inode, struct file *file) list->hiddev = hiddev_table[i]; -spin_lock_irqsave (&list->hiddev->list_lock, flags); -list_add_tail (&list->node, &hiddev_table[i]->list); -spin_unlock_irqrestore (&list->hiddev->list_lock, flags); - file->private_data = list; -if (!list->hiddev->open++) -if (list->hiddev->exist) -usbhid_open (hiddev_table[i]->hid); +if (!list->hiddev->open++) { +if (list->hiddev->exist) { + res =usbhid_open (hiddev_table[i]->hid); +if (res <0 ) + goto bail; + } + } + +spin_lock_irq (&list->hiddev->list_lock); +list_add_tail (&list->node, &hiddev_table[i]->list); +spin_unlock_irq (&list->hiddev->list_lock);return 0 ; +bail: + file->private_data =NULL ; +kfree (list->hiddev); +return -EIO ; }/*
@@ -317,8 +325,8 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t counwhile (retval ==0 ) {if (list->head == list->tail) { -add_wait_queue (&list->hiddev->wait, &wait);set_current_state (TASK_INTERRUPTIBLE ); +add_wait_queue (&list->hiddev->wait, &wait);while (list->head == list->tail) {if (file->f_flags &O_NONBLOCK ) {
@@ -335,7 +343,6 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun }schedule (); -set_current_state (TASK_INTERRUPTIBLE ); }set_current_state (TASK_RUNNING );
@@ -810,13 +817,6 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)if (!(hiddev =kzalloc (sizeof (struct hiddev ),GFP_KERNEL )))return -1 ; - retval =usb_register_dev (usbhid->intf, &hiddev_class); -if (retval) { -err_hid ("Not able to get a minor for this device." ); -kfree (hiddev); -return -1 ; - }
Oliver Neukum napsal(a):
Quoted 1 line
Hi Jiřis,
:)
Hi.
Quoted 1 line
this is the cleanup of hiddev against current vanilla. What do you think?
See my comments below.
drivers/hid/usbhid/hiddev.c static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun · line 329
Quoted 11 lines
drivers/hid/usbhid/hiddev.c +1
-1@@ -317,8 +325,8 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t counwhile (retval ==0 ) {if (list->head == list->tail) { -add_wait_queue (&list->hiddev->wait, &wait);set_current_state (TASK_INTERRUPTIBLE ); +add_wait_queue (&list->hiddev->wait, &wait);
maybe better to convert to prepare_to_wait()?
drivers/hid/usbhid/hiddev.c static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun · line 331
Quoted 2 lines
drivers/hid/usbhid/hiddev.c +0
-0while (list->head == list->tail) {
But moved here (or the set_current_state).
drivers/hid/usbhid/hiddev.c static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun · line 338
Quoted 6 lines
drivers/hid/usbhid/hiddev.c +0
-1if (file->f_flags &O_NONBLOCK ) {@@ -335,7 +343,6 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun }schedule (); -set_current_state (TASK_INTERRUPTIBLE );
This is wrong. Schedule ensures state to be TASK_RUNNING. Not setting it again for the next loop would be a bug (but better to be done right after the while statement).
drivers/hid/usbhid/hiddev.c static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun · line 348
Quoted 3 lines
drivers/hid/usbhid/hiddev.c +0
-0}set_current_state (TASK_RUNNING );
and finish_wait()
Quoted 15 lines
drivers/hid/usbhid/hiddev.c +0
-7@@ -810,13 +817,6 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)if (!(hiddev =kzalloc (sizeof (struct hiddev ),GFP_KERNEL )))return -1 ; - retval =usb_register_dev (usbhid->intf, &hiddev_class); -if (retval) { -err_hid ("Not able to get a minor for this device." ); -kfree (hiddev); -return -1 ; - } -init_waitqueue_head (&hiddev->wait);INIT_LIST_HEAD (&hiddev->list);spin_lock_init (&hiddev->list_lock);
drivers/hid/usbhid/hiddev.c int hiddev_connect(struct hid_device *hid, unsigned int force) · line 829
Quoted 2 lines
drivers/hid/usbhid/hiddev.c +0
-0@@ -828,6 +828,14 @@ int hiddev_connect(struct hid_device *hid, unsigned int force) hiddev_table[usbhid->intf->minor -HIDDEV_MINOR_BASE ] = hiddev;
usbhid->intf->minor is set even after this call:
drivers/hid/usbhid/hiddev.c int hiddev_connect(struct hid_device *hid, unsigned int force) · line 840
Quoted 11 lines
drivers/hid/usbhid/hiddev.c +8
-0+ retval =usb_register_dev (usbhid->intf, &hiddev_class); +if (retval) { +err_hid ("Not able to get a minor for this device." ); + hiddev_table[usbhid->intf->minor -HIDDEV_MINOR_BASE ] =NULL ; +kfree (hiddev); +return -1 ; + } +return 0 ; }
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): [email protected]
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 6 lines
Oliver Neukum napsal(a):
Quoted 1 line
Hi Jiřis,
:)
Hi.
Hi,
Quoted 4 lines
Quoted 2 lines
this is the cleanup of hiddev against current vanilla. What do you think?
See my comments below.
OK, all clear, except for:
Quoted 5 lines
Review int hiddev_connect(struct hid_device *hid, unsigned int force) · line 829
Quoted 3 lines
(quoted diff) +0
-0@@ -828,6 +828,14 @@ int hiddev_connect(struct hid_device *hid, unsigned int force) hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
usbhid->intf->minoris set even after this call:
What does this mean?
Regards
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 200811041317.40482.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org
Oliver Neukum napsal(a):
Quoted 7 lines
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 4 lines
Review int hiddev_connect(struct hid_device *hid, unsigned int force) · line 829
Quoted 3 lines
(quoted diff) +0
-0@@ -828,6 +828,14 @@ int hiddev_connect(struct hid_device *hid, unsigned int force) hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
usbhid->intf->minoris set even after this call:What does this mean?
You removed the usb_register_dev() call when replying. It sets the intf->minor, but you use it above it now.
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): [email protected]
Am Dienstag, 4. November 2008 16:19:57 schrieb Jiri Slaby:
Quoted 4 lines
Oliver Neukum napsal(a):
Quoted 3 lines
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 2 lines
Quoted 2 lines
(quoted diff) +0
-0@@ -828,6 +828,14 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Review int hiddev_connect(struct hid_device *hid, unsigned int force) · line 829
Quoted 1 line
(quoted diff) +0
-0hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
usbhid->intf->minoris set even after this call:What does this mean?
You removed the
usb_register_dev()call when replying. It sets theintf->minor, but you use it above it now.
Yes, I see. However, the device must not be registered before the table is correctly filled and the minor must be filled out before. A mutex then?
Regards
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Oliver Neukum napsal(a):
Quoted 2 lines
Yes, I see. However, the device must not be registered before the table is correctly filled and the minor must be filled out before. A mutex then?
It's enough to assign the hiddev_table after the register, isn't it? Actually, it's how it's done now... BTW what was the reason to move with that?
Am Dienstag, 4. November 2008 16:33:49 schrieb Jiri Slaby:
Quoted 6 lines
Oliver Neukum napsal(a):
Quoted 2 lines
Yes, I see. However, the device must not be registered before the table is correctly filled and the minor must be filled out before. A mutex then?
It's enough to assign the
hiddev_tableafter the register, isn't it? Actually, it's how it's done now... BTW what was the reason to move with that?
No, you have a race condition where open() for an announced device will fail.
Regards
Oliver
A message that is not in the archive (sent off-list or not yet received): 200811041856.47950.oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org
Oliver Neukum napsal(a):
Quoted 9 lines
Am Dienstag, 4. November 2008 16:33:49 schrieb Jiri Slaby:
Quoted 5 lines
Oliver Neukum napsal(a):
Quoted 2 lines
Yes, I see. However, the device must not be registered before the table is correctly filled and the minor must be filled out before. A mutex then?
It's enough to assign the
hiddev_tableafter the register, isn't it? Actually, it's how it's done now... BTW what was the reason to move with that?No, you have a race condition where
open()for an announced device will fail.
Could you expand this? I don't understand.
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Am Dienstag, 4. November 2008 21:34:43 schrieb Jiri Slaby:
Quoted 5 lines
Oliver Neukum napsal(a):
Quoted 4 lines
Am Dienstag, 4. November 2008 16:33:49 schrieb Jiri Slaby:
Quoted 3 lines
Oliver Neukum napsal(a):
Quoted 2 lines
Yes, I see. However, the device must not be registered before the table is correctly filled and the minor must be filled out before. A mutex then?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
It's enough to assign the
hiddev_tableafter the register, isn't it? Actually, it's how it's done now... BTW what was the reason to move with that?No, you have a race condition where
open()for an announced device will fail.Could you expand this? I don't understand.
If you call usb_register_dev() before you set hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] this check in hiddev_open() may fail:
if (i >= HIDDEV_MINORS || i < 0 || !hiddev_table[i]) return -ENODEV; Regards Oliver
Quoted 5 lines · Oliver Neukum
If you call
usb_register_dev()before you set hiddev_table[usbhid->intf->minor -HIDDEV_MINOR_BASE] this check inhiddev_open()may fail:if (i >= HIDDEV_MINORS || i < 0 || !hiddev_table[i]) return -ENODEV;
I think, that's the point. You should also get ENODEV if you don't call usb_register_dev(), create a node for non-existent minor manually and try to open it. It behaves the same as if there was a lock you suggested...
Ex.:
# grep 137 /proc/devices # mknod node c 137 0 # cat node cat: node: No such device or address
Am Dienstag, 4. November 2008 23:57:02 schrieb Jiri Slaby:
Quoted 10 lines · Oliver Neukum
Quoted 5 lines
If you call
usb_register_dev()before you set hiddev_table[usbhid->intf->minor -HIDDEV_MINOR_BASE] this check inhiddev_open()may fail:
if (i >=HIDDEV_MINORS|| i < 0 || !hiddev_table[i]) return-ENODEV;I think, that's the point. You should also get ENODEV if you don't call
usb_register_dev(), create a node for non-existent minor manually and try to open it. It behaves the same as if there was a lock you suggested...
But by calling usb_register_dev() we also cause the kernel to emit a hotplug event that tells udev that the device exists.
Regards
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 5 lines
Hi.
Quoted 1 line
this is the cleanup of hiddev against current vanilla. What do you think?
See my comments below.
Hi,
there still is a race of disconnect() with ioctl(). It seems like that code still operates under the assumption that both methods take BKL. Do you prefer a conservative fix with selective application of BKL or introduction of a new mutex?
Regards
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Oliver Neukum
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 4 lines
Hi.
Quoted 1 line
this is the cleanup of hiddev against current vanilla. What do you think?
Quoted 8 lines
Quoted 1 line
See my comments below.
Hi,
there still is a race of
disconnect()withioctl(). It seems like that code still operates under the assumption that both methods take BKL. Do you prefer a conservative fix with selective application of BKL or introduction of a new mutex?
It's none of my business... but IMO we should try to move away from the BKL as much as possible.
Alan Stern
Am Dienstag, 4. November 2008 16:00:06 schrieb Alan Stern:
Quoted 11 lines · Oliver Neukum
Quoted 10 lines
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 5 lines
Hi.
Quoted 1 line
this is the cleanup of hiddev against current vanilla. What do you think?
See my comments below.
Hi,
Quoted 7 lines
Quoted 4 lines
there still is a race of
disconnect()withioctl(). It seems like that code still operates under the assumption that both methods take BKL. Do you prefer a conservative fix with selective application of BKL or introduction of a new mutex?It's none of my business... but IMO we should try to move away from the BKL as much as possible.
This is true but hiddev is in a sense legacy code which should be replaced with hidraw, so I tend to fix it conservatively.
Regards
Oliver
Oliver Neukum napsal(a):
Quoted 14 lines
Am Dienstag, 4. November 2008 16:00:06 schrieb Alan Stern:
Quoted 13 lines · Oliver Neukum
Quoted 12 lines
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 4 lines
Hi.
Quoted 1 line
this is the cleanup of hiddev against current vanilla. What do you think?
See my comments below.
Hi,
there still is a race of
disconnect()withioctl(). It seems like that code still operates under the assumption that both methods take BKL. Do you prefer a conservative fix with selective application of BKL or introduction of a new mutex?
Quoted 5 lines
Quoted 2 lines
It's none of my business... but IMO we should try to move away from the BKL as much as possible.
This is true but hiddev is in a sense legacy code which should be replaced with hidraw, so I tend to fix it conservatively.
Jiri K. is off for few days, I'm not responsible for the HID layer, he is, I would let him to decide ;).
Am Montag, 3. November 2008 20:58:11 schrieb Jiri Slaby:
Quoted 5 lines
Hi.
Quoted 1 line
this is the cleanup of hiddev against current vanilla. What do you think?
See my comments below.
Hi,
I've addressed all comments I understood. In addition it seems to me that the read method is not thread-safe.
Regards
Oliver
---
drivers/hid/usbhid/hiddev.c +48 -26
@@ -63,6 +63,7 @@ struct hiddev_list {struct fasync_struct *fasync;struct hiddev *hiddev;struct list_head node; +struct mutex thread_lock; };static struct hiddev *hiddev_table[HIDDEV_MINORS ];
@@ -266,29 +267,38 @@ static int hiddev_release(struct inode * inode, struct file * file)static int hiddev_open (struct inode *inode,struct file *file) {struct hiddev_list *list; -unsigned long flags; +int res;int i =iminor (inode) -HIDDEV_MINOR_BASE ; -if (i >=HIDDEV_MINORS || !hiddev_table[i]) +if (i >=HIDDEV_MINORS || i <0 || !hiddev_table[i])return -ENODEV ;if (!(list =kzalloc (sizeof (struct hiddev_list ),GFP_KERNEL )))return -ENOMEM ; +mutex_init (&list->thread_lock); list->hiddev = hiddev_table[i]; -spin_lock_irqsave (&list->hiddev->list_lock, flags); -list_add_tail (&list->node, &hiddev_table[i]->list); -spin_unlock_irqrestore (&list->hiddev->list_lock, flags); - file->private_data = list; -if (!list->hiddev->open++) -if (list->hiddev->exist) -usbhid_open (hiddev_table[i]->hid); +if (!list->hiddev->open++) { +if (list->hiddev->exist) { + res =usbhid_open (hiddev_table[i]->hid); +if (res <0 ) + goto bail; + } + } + +spin_lock_irq (&list->hiddev->list_lock); +list_add_tail (&list->node, &hiddev_table[i]->list); +spin_unlock_irq (&list->hiddev->list_lock);return 0 ; +bail: + file->private_data =NULL ; +kfree (list->hiddev); +return -EIO ; }/*
@@ -307,7 +317,7 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t counDECLARE_WAITQUEUE (wait, current);struct hiddev_list *list = file->private_data;int event_size; -int retval =0 ; +int retval; event_size = ((list->flags &HIDDEV_FLAG_UREF ) !=0 ) ?sizeof (struct hiddev_usage_ref ) :sizeof (struct hiddev_event );
@@ -315,10 +325,14 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t counif (count < event_size)return 0 ; +/* lock against other threads */ + retval =mutex_lock_interruptible (&list->thread_lock); +if (retval) +return -ERESTARTSYS ; +while (retval ==0 ) {if (list->head == list->tail) { -add_wait_queue (&list->hiddev->wait, &wait); -set_current_state (TASK_INTERRUPTIBLE ); +prepare_to_wait (&list->hiddev->wait, &wait,TASK_INTERRUPTIBLE );while (list->head == list->tail) {if (file->f_flags &O_NONBLOCK ) {
@@ -337,32 +351,38 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t counschedule ();set_current_state (TASK_INTERRUPTIBLE ); } +finish_wait (&list->hiddev->wait, &wait); -set_current_state (TASK_RUNNING ); -remove_wait_queue (&list->hiddev->wait, &wait); } -if (retval) +if (retval) { +mutex_unlock (&list->thread_lock);return retval; + }while (list->head != list->tail && retval + event_size <= count) {if ((list->flags &HIDDEV_FLAG_UREF ) ==0 ) { -if (list->buffer[list->tail].field_index != -HID_FIELD_INDEX_NONE ) { +if (list->buffer[list->tail].field_index !=HID_FIELD_INDEX_NONE ) {struct hiddev_event event; + event.hid = list->buffer[list->tail].usage_code; event.value = list->buffer[list->tail].value; -if (copy_to_user (buffer + retval, &event,sizeof (struct hiddev_event ))) +if (copy_to_user (buffer + retval, &event,sizeof (struct hiddev_event ))) { +mutex_unlock (&list->thread_lock);return -EFAULT ; + } retval +=sizeof (struct hiddev_event ); } }else {if (list->buffer[list->tail].field_index !=HID_FIELD_INDEX_NONE || (list->flags &HIDDEV_FLAG_REPORT ) !=0 ) { -if (copy_to_user (buffer + retval, list->buffer + list->tail,sizeof (struct hiddev_usage_ref ))) + +if (copy_to_user (buffer + retval, list->buffer + list->tail,sizeof (struct hiddev_usage_ref ))) { +mutex_unlock (&list->thread_lock);return -EFAULT ; + } retval +=sizeof (struct hiddev_usage_ref ); } }
@@ -370,6 +390,7 @@ static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t coun } } +mutex_unlock (&list->thread_lock);return retval; }
@@ -810,13 +831,6 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)if (!(hiddev =kzalloc (sizeof (struct hiddev ),GFP_KERNEL )))return -1 ; - retval =usb_register_dev (usbhid->intf, &hiddev_class); -if (retval) { -err_hid ("Not able to get a minor for this device." ); -kfree (hiddev); -return -1 ; - } -init_waitqueue_head (&hiddev->wait);INIT_LIST_HEAD (&hiddev->list);spin_lock_init (&hiddev->list_lock);
@@ -828,6 +842,14 @@ int hiddev_connect(struct hid_device *hid, unsigned int force) hiddev_table[usbhid->intf->minor -HIDDEV_MINOR_BASE ] = hiddev; + retval =usb_register_dev (usbhid->intf, &hiddev_class); +if (retval) { +err_hid ("Not able to get a minor for this device." ); + hiddev_table[usbhid->intf->minor -HIDDEV_MINOR_BASE ] =NULL ; +kfree (hiddev); +return -1 ; + } +return 0 ; }