Skip to contentSkip to conversation

compat ioctl for submiting URB

PATCH New Christopher Li 6 participants 10 messages Jan 29, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

The compatible ioctl is missing for submitting URB from 32 bit application on a x86_64 system. For people who need to refresh their mind, please read the big comment after do_usbdevfs_bulk in fs/compat_ioctl.c

VMware is a big user of the usbdevfs, we translate guest USB IO to usbdevfs, by submitting URB. On the x86_64 system, we need those compatible ioctl for submitting URBs. For now we make a hack to submit it through the vmmon driver. But that is very ugly.

I do want this problem get fixed in the linux kernel eventually. I have been toying with two different ways to solve it. It seems that it is unavoidable to get hands dirty in the usbdevfs internals. The first one is just educate the usbdevfs to know about the 32 bit URB ioctls. So it don't need to keep around a bounce buffer.

The second idea is have a bounce buffer, and let the usbdevfs internals to know about his bounce buffer and free it when the async structure destroyed (except for reap).

I attach a patch just implement the first approach. Any comment are welcome.

Chris

include/linux/compat_ioctl.h +1 -0
Index: linux-2.5/include/linux/compat_ioctl.h
===================================================================
@@ -692,6 +692,7 @@
 COMPATIBLE_IOCTL(USBDEVFS_CONNECTINFO)
 COMPATIBLE_IOCTL(USBDEVFS_HUB_PORTINFO)
 COMPATIBLE_IOCTL(USBDEVFS_RESET)
+COMPATIBLE_IOCTL(USBDEVFS_SUBMITURB32)
 COMPATIBLE_IOCTL(USBDEVFS_CLEAR_HALT)
 /* MTD */
 COMPATIBLE_IOCTL(MEMGETINFO)
include/linux/usbdevice_fs.h +18 -1
Index: linux-2.5/include/linux/usbdevice_fs.h
===================================================================
@@ -32,6 +32,7 @@
 #define _LINUX_USBDEVICE_FS_H
 
 #include <linux/types.h>
+#include <linux/compat.h>
 
 /* --------------------------------------------------------------------- */
 
@@ -123,6 +124,22 @@
 	char port [127];	/* e.g. port 3 connects to device 27 */
 };
 
+struct usbdevfs_urb32 {
+	unsigned char type;
+	unsigned char endpoint;
+	compat_int_t status;
+	compat_uint_t flags;
+	compat_caddr_t buffer;
+	compat_int_t buffer_length;
+	compat_int_t actual_length;
+	compat_int_t start_frame;
+	compat_int_t number_of_packets;
+	compat_int_t error_count;
+	compat_uint_t signr;
+	compat_caddr_t usercontext; /* unused */
+	struct usbdevfs_iso_packet_desc iso_frame_desc[0];
+};
+
 #define USBDEVFS_CONTROL           _IOWR('U', 0, struct usbdevfs_ctrltransfer)
 #define USBDEVFS_BULK              _IOWR('U', 2, struct usbdevfs_bulktransfer)
 #define USBDEVFS_RESETEP           _IOR('U', 3, unsigned int)
@@ -130,6 +147,7 @@
 #define USBDEVFS_SETCONFIGURATION  _IOR('U', 5, unsigned int)
 #define USBDEVFS_GETDRIVER         _IOW('U', 8, struct usbdevfs_getdriver)
 #define USBDEVFS_SUBMITURB         _IOR('U', 10, struct usbdevfs_urb)
+#define USBDEVFS_SUBMITURB32       _IOR('U', 10, struct usbdevfs_urb32)
 #define USBDEVFS_DISCARDURB        _IO('U', 11)
 #define USBDEVFS_REAPURB           _IOW('U', 12, void *)
 #define USBDEVFS_REAPURBNDELAY     _IOW('U', 13, void *)
@@ -143,5 +161,4 @@
 #define USBDEVFS_CLEAR_HALT        _IOR('U', 21, unsigned int)
 #define USBDEVFS_DISCONNECT        _IO('U', 22)
 #define USBDEVFS_CONNECT           _IO('U', 23)
-
 #endif /* _LINUX_USBDEVICE_FS_H */
include/linux/usb.h +1 -0
Index: linux-2.5/include/linux/usb.h
===================================================================
@@ -608,6 +608,7 @@
 #define URB_NO_FSBR		0x0020	/* UHCI-specific */
 #define URB_ZERO_PACKET		0x0040	/* Finish bulk OUTs with short packet */
 #define URB_NO_INTERRUPT	0x0080	/* HINT: no non-error interrupt needed */
+#define URB_COMPAT		0x0100	/* compat mode */
 
 struct usb_iso_packet_descriptor {
 	unsigned int offset;
fs/compat_ioctl.c +6 -216

241 lines not on this page · raw message

drivers/usb/core/urb.c +1 -0
Index: linux-2.5/drivers/usb/core/urb.c
===================================================================
@@ -312,6 +312,7 @@
 	allowed = URB_ASYNC_UNLINK;	// affects later unlinks
 	allowed |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
 	allowed |= URB_NO_INTERRUPT;
+	allowed |= URB_COMPAT;
 	switch (temp) {
 	case PIPE_BULK:
 		if (is_out)
drivers/usb/core/devio.c +154 -69

384 lines not on this page · raw message

#1 lore raw

drivers/usb/core/devio.c line 1375

Quoted 9 lines · Christopher Li
drivers/usb/core/devio.c +9 -0
+#ifdef CONFIG_IA32_EMULATION
+
+       case USBDEVFS_SUBMITURB32:
+               snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
+               ret = proc_submiturb_compat(ps, p);
+               if (ret >= 0)
+                       inode->i_mtime = CURRENT_TIME;
+               break;
+#endif

Why don't other 64bit architectures need this chunk?

Signature · 2 lines
// Gianni Tedesco (gianni at scaramanga dot co dot uk)
lynx --source www.scaramanga.co.uk/scaramanga.asc | gpg --import
#2 lore raw

This patch is for the case that running 32 bit application on a 64 bit kernel. So far only x86_64 allow you to do that.

I am not aware of other 64bit architecture need the 32bit emulation.

Chris

Quoted 17 lines · Gianni Tedesco

drivers/usb/core/devio.c line 1375

Quoted 9 lines · Christopher Li
drivers/usb/core/devio.c +9 -0
+#ifdef CONFIG_IA32_EMULATION
+
+       case USBDEVFS_SUBMITURB32:
+               snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
+               ret = proc_submiturb_compat(ps, p);
+               if (ret >= 0)
+                       inode->i_mtime = CURRENT_TIME;
+               break;
+#endif

Why don't other 64bit architectures need this chunk?

Signature · 2 lines
// Gianni Tedesco (gianni at scaramanga dot co dot uk)
lynx --source www.scaramanga.co.uk/scaramanga.asc | gpg --import
#3 lore raw

Christopher> This patch is for the case that running 32 bit Christopher> application on a 64 bit kernel. So far only x86_64 Christopher> allow you to do that.

Actually, at least ia64, mips, parisc, ppc64, s390 and sparc64 also support 32-bit applications on a 64-bit kernel. All of those architectures except s390 can use USB. I guess vmware doesn't run on most of those architectures but any solution in the mainline kernel should be generic enough to handle them all.

- R.

#4 lore raw

It is nice to know all that. I guess I did not know much about the other 64 bit systems. I will update and resend my patch.

Thanks!

Chris

Quoted 16 lines · Roland Dreier

Christopher> This patch is for the case that running 32 bit Christopher> application on a 64 bit kernel. So far only x86_64 Christopher> allow you to do that.

Actually, at least ia64, mips, parisc, ppc64, s390 and sparc64 also support 32-bit applications on a 64-bit kernel. All of those architectures except s390 can use USB. I guess vmware doesn't run on most of those architectures but any solution in the mainline kernel should be generic enough to handle them all.

- R.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#5 lore raw
In reply to Christopher Li
Quoted 5 lines · Christopher Li

This patch is for the case that running 32 bit application on a 64 bit kernel. So far only x86_64 allow you to do that.

I am not aware of other 64bit architecture need the 32bit emulation.

Huh???
a) ppc64 runs ppc32 userland
b) sparc64 runs sparc32 userland (as the matter of fact, very
few userland programs are normally built 64bit there - no benefits in doing that for most applications, it only bloats the memory footprint)
c) mips64 runs mips32 userland
d) itanic, IIRC, runs i386 userland
e) s390x runs s390 userland
f) parisc64 runs parisc32 userland

It's normal situation, not an exception. The only pair I'm not sure about is sh64/sh. AFAICS, the only other supported 64bit platform without 32bit emulation is alpha - and in that case there's no corresponding 32bit processor to emulate.

#6 lore raw
In reply to Christopher Li
Quoted 5 lines · Christopher Li

This patch is for the case that running 32 bit application on a 64 bit kernel. So far only x86_64 allow you to do that.

I am not aware of other 64bit architecture need the 32bit emulation.

A lot of them do. Just use CONFIG_COMPAT instead.

-Andi

#7 lore raw
In reply to Christopher Li
Quoted 11 lines · Christopher Li

VMware is a big user of the usbdevfs, we translate guest USB IO to usbdevfs, by submitting URB. On the x86_64 system, we need those compatible ioctl for submitting URBs. For now we make a hack to submit it through the vmmon driver. But that is very ugly.

I do want this problem get fixed in the linux kernel eventually. I have been toying with two different ways to solve it. It seems that it is unavoidable to get hands dirty in the usbdevfs internals. The first one is just educate the usbdevfs to know about the 32 bit URB ioctls. So it don't need to keep around a bounce buffer.

Looks reasonable from a first look.

Issues:
- Should use CONFIG_COMPAT, not x86-64 specific symbols
- Why can't you set URB_COMPAT transparently in the emulation layer? Then existing applications would hopefully work without changes, right?

You may also want to preserve the __user casts, otherwise Al Viro and other sparse users will be unhappy.

Thanks for attacking this long standing problem.

-Andi

#8 lore raw
Quoted 5 lines · Andi Kleen

Looks reasonable from a first look.

Issues:
- Should use CONFIG_COMPAT, not x86-64 specific symbols

Agree.

Quoted 3 lines

- Why can't you set URB_COMPAT transparently in the emulation layer? Then existing applications would hopefully work without changes, right?

The existing application is don't need to set the USB_COMPAT flag. It is use internally to track the URB is submit from 32 bit user space. I guess I don't have to use that flag.

Quoted 3 lines

You may also want to preserve the __user casts, otherwise Al Viro and other sparse users will be unhappy.

I did try. Which place are you referring to? I guess miss some of it.

Chris

Quoted 10 lines

Thanks for attacking this long standing problem.

fs/compat_ioctl.c +0 -2
-Andi
 
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#9 lore raw

PATCH 1/1 compat USB ioctl take II was Re: compat ioctl for submiting URB

In reply to Andi Kleen
Quoted 2 lines · Andi Kleen

Issues:
- Should use CONFIG_COMPAT, not x86-64 specific symbols

Fixed.

Quoted 3 lines

- Why can't you set URB_COMPAT transparently in the emulation layer? Then existing applications would hopefully work without changes, right?

Now I see it. That is not what I intent. I must misplace it. I remove the USB_COMPAT in the new patch any way. It looks cleaner.

Quoted 1 line

You may also want to preserve the __user casts, otherwise

Not sure where it is. But I need to verify this patch on Monday. I don't have an AMD 64 at home.

Thanks.

Chris

include/linux/compat_ioctl.h +3 -0
Index: linux-2.5/include/linux/compat_ioctl.h
===================================================================
@@ -692,6 +692,9 @@
 COMPATIBLE_IOCTL(USBDEVFS_CONNECTINFO)
 COMPATIBLE_IOCTL(USBDEVFS_HUB_PORTINFO)
 COMPATIBLE_IOCTL(USBDEVFS_RESET)
+COMPATIBLE_IOCTL(USBDEVFS_SUBMITURB32)
+COMPATIBLE_IOCTL(USBDEVFS_REAPURB32)
+COMPATIBLE_IOCTL(USBDEVFS_REAPURBNDELAY32)
 COMPATIBLE_IOCTL(USBDEVFS_CLEAR_HALT)
 /* MTD */
 COMPATIBLE_IOCTL(MEMGETINFO)
include/linux/usbdevice_fs.h +20 -1
Index: linux-2.5/include/linux/usbdevice_fs.h
===================================================================
@@ -32,6 +32,7 @@
 #define _LINUX_USBDEVICE_FS_H
 
 #include <linux/types.h>
+#include <linux/compat.h>
 
 /* --------------------------------------------------------------------- */
 
@@ -123,6 +124,22 @@
 	char port [127];	/* e.g. port 3 connects to device 27 */
 };
 
+struct usbdevfs_urb32 {
+	unsigned char type;
+	unsigned char endpoint;
+	compat_int_t status;
+	compat_uint_t flags;
+	compat_caddr_t buffer;
+	compat_int_t buffer_length;
+	compat_int_t actual_length;
+	compat_int_t start_frame;
+	compat_int_t number_of_packets;
+	compat_int_t error_count;
+	compat_uint_t signr;
+	compat_caddr_t usercontext; /* unused */
+	struct usbdevfs_iso_packet_desc iso_frame_desc[0];
+};
+
 #define USBDEVFS_CONTROL           _IOWR('U', 0, struct usbdevfs_ctrltransfer)
 #define USBDEVFS_BULK              _IOWR('U', 2, struct usbdevfs_bulktransfer)
 #define USBDEVFS_RESETEP           _IOR('U', 3, unsigned int)
@@ -130,9 +147,12 @@
 #define USBDEVFS_SETCONFIGURATION  _IOR('U', 5, unsigned int)
 #define USBDEVFS_GETDRIVER         _IOW('U', 8, struct usbdevfs_getdriver)
 #define USBDEVFS_SUBMITURB         _IOR('U', 10, struct usbdevfs_urb)
+#define USBDEVFS_SUBMITURB32       _IOR('U', 10, struct usbdevfs_urb32)
 #define USBDEVFS_DISCARDURB        _IO('U', 11)
 #define USBDEVFS_REAPURB           _IOW('U', 12, void *)
+#define USBDEVFS_REAPURB32         _IOW('U', 12, u32)
 #define USBDEVFS_REAPURBNDELAY     _IOW('U', 13, void *)
+#define USBDEVFS_REAPURBd��NDELAY32   _IOW('U', 13, u32)
 #define USBDEVFS_DISCSIGNAL        _IOR('U', 14, struct usbdevfs_disconnectsignal)
 #define USBDEVFS_CLAIMINTERFACE    _IOR('U', 15, unsigned int)
 #define USBDEVFS_RELEASEINTERFACE  _IOR('U', 16, unsigned int)
@@ -143,5 +163,4 @@
 #define USBDEVFS_CLEAR_HALT        _IOR('U', 21, unsigned int)
 #define USBDEVFS_DISCONNECT        _IO('U', 22)
 #define USBDEVFS_CONNECT           _IO('U', 23)
-
 #endif /* _LINUX_USBDEVICE_FS_H */
fs/compat_ioctl.c +0 -73

76 lines not on this page · raw message

d��-
- kurb->usercontext = 0; /* unused currently */
-
- return 0;
-}
-
-/* Just put back the values which usbdevfs actually changes. */ -static int put_urb32(struct usbdevfs_urb *kurb,
- struct usbdevfs_urb32 *uurb)
-{
- if (put_user(kurb->status, &uurb->status) ||
- __put_user(kurb->actual_length, &uurb->actual_length) ||
- __put_user(kurb->error_count, &uurb->error_count))
- return -EFAULT;
-
- if (kurb->number_of_packets != 0) {
- int i;
-
- for (i = 0; i < kurb->number_of_packets; i++) {
- if (__put_user(kurb->iso_frame_desc[i].actual_length,
- &uurb->iso_frame_desc[i].actual_length) ||
- __put_user(kurb->iso_frame_desc[i].status,
- &uurb->iso_frame_desc[i].status))
- return -EFAULT;
- }
- }
-
- return 0;
-}
-
-static int get_urb32_isoframes(struct usbdevfs_urb *kurb,
- struct usbdevfs_urb32 *uurb)
-{
- unsigned int totlen;
- int i;
-
- if (kurb->type != USBDEVFS_URB_TYPE_ISO) {
- kurb->number_of_packets = 0;
- return 0;
- }
-
- if (kurb->number_of_packets < 1 ||
- kurb->number_of_packets > 128)
- return -EINVAL;
-
- if (copy_from_user(&kurb->iso_frame_desc[0],

-			   &uurb->iso_frame_desc[0],
-			   sizeof(struct usbdevfs_iso_packet_desc) *
-			   kurb->number_of_packets))

- return -EFAULT;
-
- totlen = 0;
- for (i = 0; i < kurb->number_of_packets; i++) {
- unsigned int this_len;
-
- this_len = kurb->iso_frame_desc[i].length;
- if (this_len > 1023)
- return -EINVAL;
-
- totlen += this_len;
- }
-
- if (totlen > 32768)
- return -EINVAL;
-
- kurb->buffer_length = totlen;
-
- return 0;
-}
-
-static int do_usbdevfs_urb(unsigned int fd, unsigned int cmd, unsigned long arg) -{
- struct usbdevfs_urb *kurb;
- struct usbdevfs_urb32 *uurb;
- mm_segment_t old_fs;
- __u32 udata;
- void *uptr, *kptr;
- unsigned int buflen;
- int err;
-
- uurb = compat_ptr(arg);
-
- err = -ENOMEM;
- kurb = kmalloc(sizeof(struct usbdevfs_urb) +
- (sizeof(struct usbdevfs_iso_packet_desc) * 128),
- GFP_KERNEL);
- if (!kurb)
- goto out;
-
- err = -EFAULT;
- if (get_urb32(kurb, uurb))
- goto out;
-
- err = get_urb32_isoframes(kurb, uurb);
- if (err)
- goto out;
-
- err = -EFAULT;
- if (__get_user(udata, &uurb->buffer))
- goto out;
- uptr = compat_ptr(udata);
-
- buflen = kurb->buffer_length;
- err = verify_area(VERIFY_WRITE, uptr, buflen);
- if (err)
- goto out;
-

- old_fs = get_fs();
- set_fs(KERNEL_DS);
- err = sys_ioctl(fd, USBDEVFS_SUBMITURB, (unsigned long) kurb);
- set_fs(old_fs);
-
- if (err >= 0) {
- /* RED-PEN Shit, this doesn't work for async URBs :-( XXX */
- if (put_urb32(kurb, uurb)) {
- err = -EFAULT;
- }
- }
-
-out:
- kfree(kurb);
- return err;
-}
-#endif
-
-#define USBDEVFS_REAPURB32 _IOW('U', 12, u32)
-#define USBDEVFS_REAPURBNDELAY32 _IOW('U', 13, u32)
-
-static int do_usbdevfs_reapurb(unsigned int fd, unsigned int cmd, unsigned long arg) -{

-        mm_segment_t old_fs;
-        void *kptr;
-        int err;
-
-        old_fs = get_fs();
-        set_fs(KERNEL_DS);
-        err = sys_ioctl(fd,

- (cmd == USBDEVFS_REAPURB32 ?
- USBDEVFS_REAPURB :
- USBDEVFS_REAPURBNDELAY),
- (unsigned long) &kptr);
- set_fs(old_fs);
-
- if (err >= 0 &&
- put_user((u32)(u64)kptr, (u32 __user *)compat_ptr(arg)))
- err = -EFAULT;
-
- return err;
-}
+/*
+ * The USBDEVFS_SUBMITURB, USBDEVFS_REAPURB and USBDEVFS_REAPURBNDELAY
+ * is handle in usbdevfs core. -Christopher Li
+ */

 struct usbdevfs_disconnectsignal32 {
         compat_int_t signr;

@@ -3332,9 +3114,6 @@
/* Usbdevfs */
HANDLE_IOCTL(USBDEVFS_CONTROL32, do_usbdevfs_control) HANDLE_IOCTL(USBDEVFS_BULK32, do_usbdevfs_bulk)
-/*HANDLE_IOCTL(USBDEVFS_SUBMITURB32, do_usbdevfs_urb)*/ -HANDLE_IOCTL(USBDEVFS_REAPURB32, do_usbdevfs_reapurb) -HANDLE_IOCTL(USBDEVFS_REAPURBNDELAY32, do_usbdevfs_reapurb)
HANDLE_IOCTL(USBDEVFS_DISCSIGNAL32, do_usbdevfs_discsignal) /* i2c */
HANDLE_IOCTL(I2C_FUNCS, w_long)

drivers/usb/core/devio.c +183 -66

414 lines not on this page · raw message