Skip to contentSkip to conversation

usb:Push BKL on open down into the drivers

PATCH Jiri Kosina 2 participants 5 messages Mar 29, 2010 – Mar 30, 2010 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 62 lines · Oliver Neukum

From 271f10a4643c788be08cc481dc594ee87af25140 Mon Sep 17 00:00:00 2001
From: Oliver Neukum <[email protected]>
Date: Wed, 13 Jan 2010 14:46:08 +0100
Subject: [PATCH 4/5] usb:Push BKL on open down into the drivers

Straightforward push into the drivers to allow
auditing individual drivers separately

---
 drivers/hid/usbhid/hiddev.c           |    7 +++++--
 drivers/media/video/dabusb.c          |    8 +++++++-
 drivers/staging/frontier/alphatrack.c |    2 ++
 drivers/staging/frontier/tranzport.c  |    2 ++
 drivers/usb/class/cdc-wdm.c           |    3 +++
 drivers/usb/class/usblp.c             |    3 +++
 drivers/usb/class/usbtmc.c            |    3 +++
 drivers/usb/core/file.c               |    2 --
 drivers/usb/image/mdc800.c            |    3 +++
 drivers/usb/misc/adutux.c             |    3 +++
 drivers/usb/misc/ftdi-elan.c          |   15 ++++++++++++---
 drivers/usb/misc/idmouse.c            |    8 +++++++-
 drivers/usb/misc/iowarrior.c          |    4 ++++
 drivers/usb/misc/ldusb.c              |   12 ++++++++++--
 drivers/usb/misc/legousbtower.c       |    3 +++
 drivers/usb/misc/rio500.c             |    3 ++-
 drivers/usb/misc/sisusbvga/sisusb.c   |   14 ++++++++++++--
 drivers/usb/misc/usblcd.c             |    5 +++++
 drivers/usb/misc/vstusb.c             |    9 ++++++++-
 drivers/usb/usb-skeleton.c            |    3 +++
 20 files changed, 97 insertions(+), 15 deletions(-)
drivers/hid/usbhid/hiddev.c +5 -2
index 867e084..433602a 100644
@@ -265,9 +265,10 @@ static int hiddev_release(struct inode * inode, struct file * file)
 static int hiddev_open(struct inode *inode, struct file *file)
 {
 	struct hiddev_list *list;
-	int res;
+	int res, i;
 
-	int i = iminor(inode) - HIDDEV_MINOR_BASE;
+	lock_kernel();
+	i = iminor(inode) - HIDDEV_MINOR_BASE;
 
 	if (i >= HIDDEV_MINORS || i < 0 || !hiddev_table[i])
 		return -ENODEV;
@@ -313,10 +314,12 @@ static int hiddev_open(struct inode *inode, struct file *file)
 			usbhid_open(hid);
 		}
 
+	unlock_kernel();
 	return 0;
 bail:
 	file->private_data = NULL;
 	kfree(list);
+	unlock_kernel();
 	return res;
 }
 

[ ... snip ... ]

Quoted 13 lines
drivers/usb/core/file.c +0 -1
index bfc6c2e..c3536f1 100644
@@ -34,7 +34,6 @@ static int usb_open(struct inode * inode, struct file * file)
 	int err = -ENODEV;
 	const struct file_operations *old_fops, *new_fops = NULL;
 
-	lock_kernel();
 	down_read(&minor_rwsem);
 	c = usb_minors[minor];
 

drivers/usb/core/file.c static int usb_open(struct inode * inode, struct file * file) · line 57

Quoted 7 lines
drivers/usb/core/file.c +0 -1
@@ -53,7 +52,6 @@ static int usb_open(struct inode * inode, struct file * file)
 	fops_put(old_fops);
  done:
 	up_read(&minor_rwsem);
-	unlock_kernel();
 	return err;
 }
 

Hi Oliver,

looking at this a little bit more, this seems wrong, at least for the hiddev case. The reason is -- it changes the dependency order of BKL and minor_rwsem.

For hiddev, this causes a problem, as you introduced BKL into hiddev_connect() exactly because of the proper dependency -- we have the BKL there to avoid race after usb_register_dev() -- once the device node has been created, open() could happen on it.

The obvious fix -- introducing mutex to guard hiddev_table[] -- is wrong work, as usb_open() and usb_register_dev() both take minor_rwsem, thus there will be AB-BA deadlock between this mutex and minor_rwsem.

So you avoided this situation by BKL (as usb_open() has been taking it in the right order), but now we are exactly in AB-BA deadlock situation because of the reversed dependency between BKL and minor_rwsem.

Signature · 2 lines
Jiri Kosina
SUSE Labs, Novell Inc.
#2 lore raw

Am Montag 29 März 2010 12:04:51 schrieb Jiri Kosina:

Quoted 4 lines

For hiddev, this causes a problem, as you introduced BKL into hiddev_connect() exactly because of the proper dependency -- we have the BKL there to avoid race after usb_register_dev() -- once the device node has been created, open() could happen on it.

Hi,

it seems to me you cannot deadlock with the BKL and an rwsem, as BKL will be dropped if a task blocks on the rwsem. This is not a nice feature of the BKL to use. Do you have an alternative idea?

Regards
Oliver

#3 lore raw
Quoted 9 lines · Oliver Neukum
Quoted 4 lines

For hiddev, this causes a problem, as you introduced BKL into hiddev_connect() exactly because of the proper dependency -- we have the BKL there to avoid race after usb_register_dev() -- once the device node has been created, open() could happen on it.

Hi,

it seems to me you cannot deadlock with the BKL and an rwsem, as BKL will be dropped if a task blocks on the rwsem.

Ah, right you are.

Quoted 2 lines

This is not a nice feature of the BKL to use. Do you have an alternative idea?

Not right now, without introducing additional lock into USB core or redesigning the hiddev node registration completely. Hmmm ...

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs, Novell Inc.
#4 lore raw

Am Montag, 29. März 2010 15:46:59 schrieb Jiri Kosina:

Quoted 10 lines
Quoted 2 lines

it seems to me you cannot deadlock with the BKL and an rwsem, as BKL will be dropped if a task blocks on the rwsem.

Ah, right you are.

Quoted 2 lines

This is not a nice feature of the BKL to use. Do you have an alternative idea?

Not right now, without introducing additional lock into USB core or redesigning the hiddev node registration completely. Hmmm ...

Then you might want to add a comment so the next developer who wants to eradicate BKL is warned.

Regards
Oliver

#5 lore raw
Quoted 8 lines · Oliver Neukum
Quoted 5 lines
Quoted 2 lines

This is not a nice feature of the BKL to use. Do you have an alternative idea?

Not right now, without introducing additional lock into USB core or redesigning the hiddev node registration completely. Hmmm ...

Then you might want to add a comment so the next developer who wants to eradicate BKL is warned.

That I already have in my tree.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs, Novell Inc.