My vision of usbmon
Subsystems (2)
Hi, Guys:
This is usbmon which I cooked up because I got tired from adding dbg()'s and polluting my dmesg. I use it to hunt bugs in USB storage devices so far, and it's useful, although limited at this stage.
I looked at the Harding's USBmon patch, and I think he got a few things right. The main of them is that I underestimated the benefits of placing the special files into the filesystem namespace. When we discussed it with Greg in the airport, we decided that having some sort of Netlink-style socket would be the best option. I decided to make a u-turn and attach those sockets into the namespace (currently under /dbg, but it can change). What this buys us is:
1. cat(1): never bet against it. It's too handy. And netcat is just not the same.
2. USBmon userland in Java. Just try to hack in JNI a little as I have and you'll see.
He also got some parts wrong. They are small things, but unfortunately, pervasive. For example, he relies on urb->dev, which is not a good idea in case of HCD which zero it far away from the completion call site, such as usb-ohci in 2.4. And it's error-prone and a maintenance problem to audit all HCDs and add usbmon calls. Races by design, too. Small things like that, but many. Eventually, I wrote everything from scratch. It's rather embarrassing that I could not save USBmon and gave in to NIH.
Since it's a big NIH, usbmon is not compatible with USBmon's userland. It can be made compatible, but it needs a small adaptation layer, because Harding aggregated at a device, and I do it on a bus (I can explain why, but it's rather long; it has to do with hotplug and races).
The architecture to support various output formats is present. Obvious candidates are Old USBmon format and a Binary format. But it's not done.
Please ask if something is not obvious in the code.
Bye,
-- Pete
drivers/usb/Kconfig +2 -0
@@ -56,6 +56,8 @@ source "drivers/usb/media/Kconfig" source "drivers/usb/net/Kconfig" +source "drivers/usb/mon/Kconfig" + comment "USB port drivers" depends on USB
drivers/usb/Makefile +2 -0
@@ -6,6 +6,8 @@ obj-$(CONFIG_USB) += core/ +obj-$(CONFIG_USB_MON) += mon/ + obj-$(CONFIG_USB_EHCI_HCD) += host/ obj-$(CONFIG_USB_OHCI_HCD) += host/ obj-$(CONFIG_USB_UHCI_HCD) += host/
drivers/usb/mon/Kconfig +21 -0
@@ -0,0 +1,21 @@ +# +# USB Monitor configuration +# + +# In normal life, it makes little sense to have usbmon as a module, and in fact +# it is harmful, because there is no way to autoload the module. +# The 'm' option is allowed for hackers who debug the usbmon itself, +# and for those who have usbcore as a module. +config USB_MON + tristate "USB Monitor" + depends on USB && SYSFS + default y + help + If you say Y here, a component which captures the USB traffic + between peripheral-specific drivers and HC drivers will be built. + The USB_MON is similar in spirit and may be compatible with Dave + Harding's USBMon. + + This is somewhat experimental at this time, but it should be safe. + + If unsure, say Y.
drivers/usb/mon/Makefile +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for USB Core files and filesystem +# + +usbmon-objs := mon_main.o mon_stat.o mon_text.o + +obj-$(CONFIG_USB_MON) += usbmon.o
drivers/usb/mon/mon_main.c +306 -0
306 lines not on this page · raw message
drivers/usb/mon/mon_stat.c +75 -0
75 lines not on this page · raw message
drivers/usb/mon/mon_text.c +295 -0
295 lines not on this page · raw message
drivers/usb/mon/usb_mon.h +59 -0
59 lines not on this page · raw message
include/linux/usb.h +8 -0
@@ -254,6 +254,7 @@ struct usb_bus {u8 otg_port;/* 0, or number of OTG/HNP port */ unsigned is_b_host:1; /* true during some HNP roleswitches */ unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */ + unsigned monitored:1; /* usbmon is active on this bus */int devnum_next;/* Next open device number in round-robin allocation */
@@ -277,6 +278,9 @@ struct usb_bus {struct class_device class_dev;/* class device for this bus */ void (*release)(struct usb_bus *bus);/* function to destroy this bus's memory */ +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +struct mon_bus *mon_bus; +#endif };#define to_usb_bus (d) container_of(d, struct usb_bus, class_dev)
@@ -804,6 +808,10 @@ struct urbint error_count;/* (return) number of ISO errors */ void *context;/* (in) context for completion */ usb_complete_t complete;/* (in) completion routine */ +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +usb_complete_t mon_saved_complete;/* (core private) */ +struct mon_bus *mon_bus;/* (core private) */ +#endif struct usb_iso_packet_descriptor iso_frame_desc[0 ];/* (in) ISO ONLY */ };
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 13 lines · Oliver Neukum
Am Montag, 20. Dezember 2004 08:04 schrieb Pete Zaitcev:
Quoted 11 lines
+ add_wait_queue(&rp->wait, &waita);
+ while ((ep =mon_text_fetch(rp, mbus)) == NULL) {
+ if (file->f_flags&O_NONBLOCK) {
+ remove_wait_queue(&rp->wait, &waita);
+ return -EWOULDBLOCK; /* Same as EAGAIN in Linux */ + }
+ /*
+ * We do not count nwaiters, because ->release is supposed + * to be called when all openers are gone only.
+ */
+ set_current_state(TASK_INTERRUPTIBLE);
Quoted 3 lines
Here you can lose a wakeup.
Quoted 1 line
+ schedule();
Thanks, Oliver. Indeed, it's a bug.
-- Pete
Quoted 13 lines · Pete Zaitcev
Hi, Guys:
This is usbmon which I cooked up because I got tired from adding dbg()'s and polluting my dmesg. I use it to hunt bugs in USB storage devices so far, and it's useful, although limited at this stage.
I looked at the Harding's USBmon patch, and I think he got a few things right. The main of them is that I underestimated the benefits of placing the special files into the filesystem namespace. When we discussed it with Greg in the airport, we decided that having some sort of Netlink-style socket would be the best option. I decided to make a u-turn and attach those sockets into the namespace (currently under /dbg, but it can change). What this buys us is:
Quoted 4 lines
1. cat(1): never bet against it. It's too handy. And netcat is just not the same.
2. USBmon userland in Java. Just try to hack in JNI a little as I have and you'll see.
I agree, file interfaces are just too easy and simple to use. Sorry for sending you down the wrong track with the socket stuff.
Quoted 2 lines
The architecture to support various output formats is present. Obvious candidates are Old USBmon format and a Binary format. But it's not done.
It looks great, thanks for doing this work. Let me know when you want it added to the kernel tree.
thanks,
greg k-h
Quoted 2 lines · Greg KH
It looks great, thanks for doing this work. Let me know when you want it added to the kernel tree.
Thanks, Greg. There's a little tidbit in usbmon about which I wish you to make a pronouncement explicitly:
+ /* XXX Is this how I pin struct bus? Ask linux-usb-devel */
+ kobject_get(&ubus->class_dev.kobj);
+ mbus->u_bus = ubus;
+ ubus->mon_bus = mbus;
Is this a good way to guarantee that mbus->u_bus won't be dangling? This is used not just when someone pulls a PCMCIA card, but also in case of plain rmmod ohci-hcd.
-- Pete
drivers/usb/mon/mon_main.c line 194
Quoted 12 lines · Pete Zaitcev
Quoted 2 lines · Greg KH
It looks great, thanks for doing this work. Let me know when you want it added to the kernel tree.
Thanks, Greg. There's a little tidbit in usbmon about which I wish you to make a pronouncement explicitly:
drivers/usb/mon/mon_main.c +4
-0+/* XXX Is this how I pin struct bus? Ask linux-usb-devel */ +kobject_get (&ubus->class_dev.kobj); + mbus->u_bus = ubus; + ubus->mon_bus = mbus;
Use usb_bus_get() instead. Ick, that function's implementation sucks, I'll go clean it up and export it for you to be able to use from your code.
thanks,
greg k-h
Quoted 9 lines · Greg KH
Quoted 8 lines · Pete Zaitcev
Quoted 2 lines · Greg KH
It looks great, thanks for doing this work. Let me know when you want it added to the kernel tree.
Thanks, Greg. There's a little tidbit in usbmon about which I wish you to make a pronouncement explicitly:
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ /* XXX Is this how I pin struct bus? Ask linux-usb-devel */ + kobject_get(&ubus->class_dev.kobj); + mbus->u_bus = ubus; + ubus->mon_bus = mbus;Use
usb_bus_get()instead. Ick, that function's implementation sucks, I'll go clean it up and export it for you to be able to use from your code.
Does this patch work for you?
thanks,
greg k-h
-----
USB: export usb_bus_get() and usb_bus_put()
hcd.c renamed from drivers/usb/core/hcd.c +7 -14
@@ -608,27 +608,20 @@ static int usb_rh_urb_dequeue (struct us/*-------------------------------------------------------------------------*/ -/* exported only within usbcore */ -struct usb_bus *usb_bus_get (struct usb_bus *bus) +struct usb_bus *usb_bus_get (struct usb_bus *bus) { -struct class_device *tmp; - -if (!bus) -return NULL ; - - tmp =class_device_get (&bus->class_dev); -if (tmp) -return to_usb_bus (tmp); -else -return NULL ; +if (bus) +class_device_get (&bus->class_dev); +return bus; } +EXPORT_SYMBOL_GPL (usb_bus_get); -/* exported only within usbcore */ -void usb_bus_put (struct usb_bus *bus) +void usb_bus_put (struct usb_bus *bus) {if (bus)class_device_put (&bus->class_dev); } +EXPORT_SYMBOL_GPL (usb_bus_put);/*-------------------------------------------------------------------------*/
Quoted 7 lines · Greg KH
Quoted 6 lines · Pete Zaitcev
Hi, Guys:
This is usbmon which I cooked up because I got tired from adding dbg()'s and polluting my dmesg. I use it to hunt bugs in USB storage devices so far, and it's useful, although limited at this stage.
Quoted 7 lines
Quoted 7 lines
I looked at the Harding's USBmon patch, and I think he got a few things right. The main of them is that I underestimated the benefits of placing the special files into the filesystem namespace. When we discussed it with Greg in the airport, we decided that having some sort of Netlink-style socket would be the best option. I decided to make a u-turn and attach those sockets into the namespace (currently under /dbg, but it can change). What this buys us is:
Is there any reason why these debug filesystems are going under the root directory? Why not /sys/debug or /sys/kernel/debug or something?
Nick
Quoted 16 lines · Nick Piggin
Quoted 7 lines · Greg KH
Quoted 6 lines · Pete Zaitcev
Hi, Guys:
This is usbmon which I cooked up because I got tired from adding dbg()'s and polluting my dmesg. I use it to hunt bugs in USB storage devices so far, and it's useful, although limited at this stage.
Quoted 7 lines
Quoted 7 lines
I looked at the Harding's USBmon patch, and I think he got a few things right. The main of them is that I underestimated the benefits of placing the special files into the filesystem namespace. When we discussed it with Greg in the airport, we decided that having some sort of Netlink-style socket would be the best option. I decided to make a u-turn and attach those sockets into the namespace (currently under /dbg, but it can change). What this buys us is:
Quoted 2 lines
Is there any reason why these debug filesystems are going under the root directory? Why not
/sys/debugor/sys/kernel/debugor something?
See the previous thread as to where to mount debugfs. In short, I don't really care :)
thanks,
greg k-h
Quoted 7 lines · Greg KH
Quoted 2 lines · Nick Piggin
Is there any reason why these debug filesystems are going under the root directory? Why not
/sys/debugor/sys/kernel/debugor something?See the previous thread as to where to mount debugfs. In short, I don't really care :)
Well, somebody should pick a single location, and try to use that consistently. Sure the sysadmin can change it, but a "preferred default" is always nice.
Jeff
Quoted 6 lines · Jeff Garzik
Quoted 5 lines · Greg KH
Quoted 2 lines · Nick Piggin
Is there any reason why these debug filesystems are going under the root directory? Why not
/sys/debugor/sys/kernel/debugor something?
Quoted 7 lines
Quoted 2 lines
See the previous thread as to where to mount debugfs. In short, I don't really care :)
Well, somebody should pick a single location, and try to use that consistently. Sure the sysadmin can change it, but a "preferred default" is always nice.
Bah, fine, make me make a policy decision, damm I tried hard to resist :)
Anyway, here's a patch I just applied that creates the /sys/kernel/debug directory (you need a small patch that exports the proper subsys for this to work, if anyone wants that too, I'll send it.) Now, if you want, you can mount debugfs at this location.
Now either this is going to make people happy, or make them mad I didn't pick their proposed location. Either way, I'm going on vacation in 2 days, so I will not be around to hear the screams...
thanks,
greg k-h
-------
debugfs: add /sys/kernel/debug mount point for people to mount debugfs on.
fs/debugfs/inode.c +14 -1
@@ -298,15 +298,28 @@ }EXPORT_SYMBOL_GPL (debugfs_remove); +static decl_subsys(debug, NULL, NULL); +static int __init debugfs_init (void ) { -return register_filesystem (&debug_fs_type); +int retval; + +kset_set_kset_s (&debug_subsys, kernel_subsys); + retval =subsystem_register (&debug_subsys); +if (retval) +return retval; + + retval =register_filesystem (&debug_fs_type); +if (retval) +subsystem_unregister (&debug_subsys); +return retval; } static void __exit debugfs_exit(void) {simple_release_fs (&debugfs_mount, &debugfs_mount_count);unregister_filesystem (&debug_fs_type); +subsystem_unregister (&debug_subsys); }core_initcall (debugfs_init);
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 9 lines · Oliver Neukum
Am Montag, 20. Dezember 2004 08:04 schrieb Pete Zaitcev:
Quoted 5 lines
+ memcpy(&mbus->shim_ops,
ubus->op,sizeof(struct usb_operations)); + mbus->shim_ops.submit_urb =mon_submit;
+ mbus->saved_op =ubus->op;
+ ubus->op =&mbus->shim_ops;
+ ubus->monitored = 1;I think you need
smp_wmb()here to make sure that an irq taken on another CPU sees the manipulations in the correct order.
Hmm, it seems you are right. I forgot about reordering issues. I relied on op being atomic, but if it points at an uninitialized shim, this will end badly. How about this?
memcpy(&mbus->shim_ops, ubus->op, sizeof(struct usb_operations));
mbus->shim_ops.submit_urb = mon_submit;
mbus->saved_op = ubus->op;
smp_mb(); /* ubus->op is not protected by spinlocks */
ubus->op = &mbus->shim_ops;
ubus->monitored = 1;Generally, the type of coding which requires a use of memory barriers in drivers is a bug or a latent bug, so I am sorry for the above. It was a sacrifice to make usbmon invisible if it's not actively monitoring. Sorry about that.
-- Pete
Quoted 4 lines · Pete Zaitcev
Generally, the type of coding which requires a use of memory barriers in drivers is a bug or a latent bug, so I am sorry for the above. It was a sacrifice to make usbmon invisible if it's not actively monitoring. Sorry about that.
Well, why do that? Why not do something like the security hooks do, and have a default "noop" hook if we don't have a monitor, and when you load the monitor, it switches the hook to point to your code?
thanks,
greg k-h
Am Mittwoch, 22. Dezember 2004 03:25 schrieb Pete Zaitcev:
Quoted 12 lines · Oliver Neukum
Quoted 10 lines
Am Montag, 20. Dezember 2004 08:04 schrieb Pete Zaitcev:
Quoted 5 lines
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â memcpy(&mbus->shim_ops,
ubus->op,sizeof(struct usb_operations)); +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mbus->shim_ops.submit_urb =mon_submit;
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mbus->saved_op =ubus->op;
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ubus->op =&mbus->shim_ops;
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ubus->monitored = 1;I think you need
smp_wmb()here to make sure that an irq taken on another CPU sees the manipulations in the correct order.
Quoted 8 lines
Hmm, it seems you are right. I forgot about reordering issues. I relied on op being atomic, but if it points at an uninitialized shim, this will end badly. How about this?
memcpy(&mbus->shim_ops, ubus->op, sizeof(struct usb_operations)); mbus->shim_ops.submit_urb = mon_submit; mbus->saved_op = ubus->op; smp_mb(); /* ubus->op is not protected by spinlocks */
smp_wmb() would do.
Quoted 6 lines
ubus->op = &mbus->shim_ops; ubus->monitored = 1;Generally, the type of coding which requires a use of memory barriers in drivers is a bug or a latent bug, so I am sorry for the above. It was a sacrifice to make usbmon invisible if it's not actively monitoring. Sorry about that.
That is the best way. It's just a little tricky.
Regards
Oliver