SLAB vs. pci_alloc_xxx in usb-uhci patch
Hi, everyone:
When I turn FORCE_DEBUG on in mm/slab.c, usb-uhci driver stops working. It turned out that DMA headers must be aligned on 16. Slab poisoning violates that assumption.
I have come up with a fix which USB folks did not like, but they did not object to discussion on linux-kernel. Here is it.
The current code does something like this:
struct dmahdr {
__u32 head;
};
struct desc {
struct dmahdr h;
struct desc *next;
int last_used;
};
struct desc *p;
unsigned long busaddr;
p = kmalloc_z(sizeof(struct desc), GFP_SOMETHING);
busaddr = virt_to_bus(p);
writel(busaddr, ioremap_cookie + UHCI_SOME_REGISTER);I changed it to this:
struct dmahdr {
__u32 head;
};
struct desc {
struct dmahdr *hp;
struct desc *next;
int last_used;
};
struct desc *p;
void *dp;
unsigned long busaddr;
p = kmalloc_z(sizeof(struct desc), GFP_SOMETHING);
dp = kmalloc_z(sizeof(struct dmahdr) + 15, GFP_SOMETHING);
dp = (dp + 15) & ~15;
p->hp = dp;
busaddr = virt_to_bus(p->hp);
writel(busaddr, ioremap_cookie + UHCI_SOME_REGISTER);This way, after testing, we may replace second kmalloc and virt_to_bus with pci_alloc_consistent.
Actual patch is more complicated due to checks, zeroing, and so on. It is attached below.
Here is a mail that sums up the disagreement:
Date: Sun, 04 Mar 2001 13:10:26 -0800
From: David Brownell <[email protected]>
Subject: Re: [linux-usb-devel] Patch for usb-uhci and poisoned slab (2.4.2-ac7)
To: Peter Zaitcev <[email protected]>
Cc: [email protected]
Quoted 2 lines
I found that usb-uhci fails when
FORCE_DEBUGis set inmm/slab.cbecause it expects 16 bytes alignment for structures it allocates.
And mm/slab.c changes semantics when CONFIG_SLAB_DEBUG is set: it ignores SLAB_HWCACHE_ALIGN. That seems more like the root cause of the problem to me!
It's a lot simpler to patch mm/slab.c so its semantics don't change. That is, don't resolve clashes between HWCACHE_ALIGN and automagic redzoning in favor of redzoning any more.
Quoted 4 lines
I did not go all the way to using
pci_alloc_single,pci_alloc_consistentand friends, because I am not too sure in my hand, and also because I believe in gradual change (in this case).
That big a patch is rather non-gradual ... :-)
I think that the pci_alloc_consistent patch that Johannes sent by for "uhci.c" would be a better approach. Though I'd like to see that be more general ... say, making mm/slab.c know about such things. Add a simple abstraction, and that should be it -- right? :-)
- Dave
I see the Dave's argument as 1. Slab must honor HWCACHE_ALIGN when debugged; 2. My patch is too big for too little gain. It is understandable, but I find it hard to agree. First, mixing the controller imposed alignment with cache alignment is quite misleading. Second, is more "phylosophical" - yes I can work without global poisoning. I just do want to use it. I fancy forced slab poisoning, is it so wrong?
I need our benevolent dictatiorship to judge. Or else ... umm... well, I guess, nothing will happen, we would just have broken code as we always did :0
-- Pete
drivers/usb/usb-uhci-debug.h +49 -45
@@ -1,25 +1,25 @@ -#ifdef DEBUG +#ifdef DEBUG_DUMP static void __attribute__((__unused__))uhci_show_qh (puhci_desc_t qh) {if (qh->type !=QH_TYPE ) {dbg ("qh has not QH_TYPE" );return ; } -dbg ("QH @ %p/%08lX:" , qh,virt_to_bus (qh)); +dbg ("QH @ %p->%p/%08lX:" , qh, qh->hwp,virt_to_bus (qh->hwp)); -if (qh->hw.qh.head &UHCI_PTR_TERM ) +if (qh->hwp->qh.head &UHCI_PTR_TERM )dbg (" Head Terminate" );else dbg (" Head: %s @ %08X" , - (qh->hw.qh.head &UHCI_PTR_QH ?"QH" :"TD" ), - qh->hw.qh.head & ~UHCI_PTR_BITS ); + (qh->hwp->qh.head &UHCI_PTR_QH ?"QH" :"TD" ), + qh->hwp->qh.head & ~UHCI_PTR_BITS ); -if (qh->hw.qh.element &UHCI_PTR_TERM ) +if (qh->hwp->qh.element &UHCI_PTR_TERM )dbg (" Element Terminate" );else dbg (" Element: %s @ %08X" , - (qh->hw.qh.element &UHCI_PTR_QH ?"QH" :"TD" ), - qh->hw.qh.element & ~UHCI_PTR_BITS ); + (qh->hwp->qh.element &UHCI_PTR_QH ?"QH" :"TD" ), + qh->hwp->qh.element & ~UHCI_PTR_BITS ); }#endif
@@ -27,7 +27,7 @@ {char *spid; -switch (td->hw.td.info &0xff ) { +switch (td->hwp->td.info &0xff ) {case USB_PID_SETUP : spid ="SETUP" ;break ;
@@ -42,50 +42,52 @@break ; } -warn (" TD @ %p/%08lX, MaxLen=%02x DT%d EP=%x Dev=%x PID=(%s) buf=%08x" , - td,virt_to_bus (td), - td->hw.td.info >>21 , - ((td->hw.td.info >>19 ) &1 ), - (td->hw.td.info >>15 ) &15 , - (td->hw.td.info >>8 ) &127 , +warn (" TD @ %p->%p/%08lX, MaxLen=%02x DT%d EP=%x Dev=%x PID=(%s) buf=%08x" , + td, td->hwp,virt_to_bus (td->hwp), + td->hwp->td.info >>21 , + ((td->hwp->td.info >>19 ) &1 ), + (td->hwp->td.info >>15 ) &15 , + (td->hwp->td.info >>8 ) &127 , spid, - td->hw.td.buffer); + td->hwp->td.buffer);warn (" Len=%02x e%d %s%s%s%s%s%s%s%s%s%s" , - td->hw.td.status &0x7ff , - ((td->hw.td.status >>27 ) &3 ), - (td->hw.td.status &TD_CTRL_SPD ) ?"SPD " :"" , - (td->hw.td.status &TD_CTRL_LS ) ?"LS " :"" , - (td->hw.td.status &TD_CTRL_IOC ) ?"IOC " :"" , - (td->hw.td.status &TD_CTRL_ACTIVE ) ?"Active " :"" , - (td->hw.td.status &TD_CTRL_STALLED ) ?"Stalled " :"" , - (td->hw.td.status &TD_CTRL_DBUFERR ) ?"DataBufErr " :"" , - (td->hw.td.status &TD_CTRL_BABBLE ) ?"Babble " :"" , - (td->hw.td.status &TD_CTRL_NAK ) ?"NAK " :"" , - (td->hw.td.status &TD_CTRL_CRCTIMEO ) ?"CRC/Timeo " :"" , - (td->hw.td.status &TD_CTRL_BITSTUFF ) ?"BitStuff " :"" + td->hwp->td.status &0x7ff , + ((td->hwp->td.status >>27 ) &3 ), + (td->hwp->td.status &TD_CTRL_SPD ) ?"SPD " :"" , + (td->hwp->td.status &TD_CTRL_LS ) ?"LS " :"" , + (td->hwp->td.status &TD_CTRL_IOC ) ?"IOC " :"" , + (td->hwp->td.status &TD_CTRL_ACTIVE ) ?"Active " :"" , + (td->hwp->td.status &TD_CTRL_STALLED ) ?"Stalled " :"" , + (td->hwp->td.status &TD_CTRL_DBUFERR ) ?"DataBufErr " :"" , + (td->hwp->td.status &TD_CTRL_BABBLE ) ?"Babble " :"" , + (td->hwp->td.status &TD_CTRL_NAK ) ?"NAK " :"" , + (td->hwp->td.status &TD_CTRL_CRCTIMEO ) ?"CRC/Timeo " :"" , + (td->hwp->td.status &TD_CTRL_BITSTUFF ) ?"BitStuff " :"" ); -if (td->hw.td.link &UHCI_PTR_TERM ) +if (td->hwp->td.link &UHCI_PTR_TERM )warn (" TD Link Terminate" );else warn (" Link points to %s @ %08x, %s" , - (td->hw.td.link &UHCI_PTR_QH ?"QH" :"TD" ), - td->hw.td.link & ~UHCI_PTR_BITS , - (td->hw.td.link &UHCI_PTR_DEPTH ?"Depth first" :"Breadth first" )); + (td->hwp->td.link &UHCI_PTR_QH ?"QH" :"TD" ), + td->hwp->td.link & ~UHCI_PTR_BITS , + (td->hwp->td.link &UHCI_PTR_DEPTH ?"Depth first" :"Breadth first" )); } -#ifdef DEBUG +#ifdef DEBUG_DUMP static void __attribute__((__unused__))uhci_show_td_queue (puhci_desc_t td) { -//dbg("uhci_show_td_queue %p (%08lX):", td, virt_to_bus (td)); +uhci_desc_u_t *h; +//dbg("uhci_show_td_queue %p (%08lX):", td, virt_to_bus (td->hwp)); while (1 ) {uhci_show_td (td); -if (td->hw.td.link &UHCI_PTR_TERM ) +if (td->hwp->td.link &UHCI_PTR_TERM )break ; -if (td !=bus_to_virt (td->hw.td.link & ~UHCI_PTR_BITS )) - td =bus_to_virt (td->hw.td.link & ~UHCI_PTR_BITS ); -else { -dbg ("td points to itself!" ); + h =bus_to_virt (td->hwp->td.link & ~UHCI_PTR_BITS ); +if (td->hwp != h) { + td = h->td.backp; + }else { +dbg ("td %p points to itself!" , td);break ; } }
@@ -94,21 +96,23 @@static void __attribute__((__unused__))uhci_show_queue (puhci_desc_t qh) {uhci_desc_t *start_qh=qh; +uhci_desc_u_t *h;dbg ("uhci_show_queue %p:" , qh);while (1 ) {uhci_show_qh (qh); -if (!(qh->hw.qh.element &UHCI_PTR_TERM )) -uhci_show_td_queue (bus_to_virt (qh->hw.qh.element & ~UHCI_PTR_BITS )); +if (!(qh->hwp->qh.element &UHCI_PTR_TERM )) +uhci_show_td_queue (((uhci_desc_u_t *)bus_to_virt (qh->hwp->qh.element & ~UHCI_PTR_BITS ))->td.backp); -if (qh->hw.qh.head &UHCI_PTR_TERM ) +if (qh->hwp->qh.head &UHCI_PTR_TERM )break ; -if (qh !=bus_to_virt (qh->hw.qh.head & ~UHCI_PTR_BITS )) - qh =bus_to_virt (qh->hw.qh.head & ~UHCI_PTR_BITS ); + h =bus_to_virt (qh->hwp->qh.head & ~UHCI_PTR_BITS ); +if (qh->hwp != h) + qh = h->qh.backp;else { -dbg ("qh points to itself!" ); +dbg ("qh %p points to itself!" , qh);break ; }
drivers/usb/usb-uhci.c +139 -93
560 lines not on this page · raw message
drivers/usb/usb-uhci.h +22 -7
@@ -100,7 +100,7 @@#define uhci_status_bits (ctrl_sts) (ctrl_sts & 0xFE0000)#define uhci_actual_length (ctrl_sts) ((ctrl_sts + 1) & TD_CTRL_ACTLEN_MASK)/* 1-based */ -#define uhci_ptr_to_virt (x) bus_to_virt(x & ~UHCI_PTR_BITS) +/* #define uhci_ptr_to_virt(x) bus_to_virt(x & ~UHCI_PTR_BITS) */ /* unused */ /* * for TD <flags>:
@@ -124,6 +124,10 @@/* ------------------------------------------------------------------------------------ New TD/QH-structures ------------------------------------------------------------------------------------ */ + +/* One size for TD & QH. Align to 16 eats any gains of two sizes. Less fragmented, too. */ +#define UHCI_HWDESC_SZ 16 +typedef enum {TD_TYPE ,QH_TYPE }uhci_desc_type_t ;
@@ -133,18 +137,29 @@__u32 status;__u32 info;__u32 buffer; -} uhci_td_t, *puhci_td_t; +#ifdef DEBUG_DUMP +struct uhci_desc *backp; +#endif +} uhci_td_t, *puhci_td_t;/* __attribute__((packed)) perhaps? XXX */ typedef struct {__u32 head;__u32 element;/* Queue element pointer */ +#ifdef DEBUG_DUMP +__u32 fill_08; +__u32 fill_0c; +struct uhci_desc *backp; +#endif }uhci_qh_t , *puhci_qh_t ; -typedef struct { - union { - uhci_td_t td; - uhci_qh_t qh; - } hw; +typedef union uhci_desc_u {/* uncached, dma-able part of descriptor */ +uhci_td_t td; +uhci_qh_t qh; +}uhci_desc_u_t ; + +typedef struct uhci_desc { /* cached, or software, descriptor */ + uhci_desc_u_t *hwp; + void *mmp; uhci_desc_type_t type; struct list_head horizontal; struct list_head vertical;