: Proposed fix to the I2O problems with > 4GB space
PATCH 1/1 : Proposed fix to the I2O problems with > 4GB space
From: Alan Cox <[email protected]>
The I2O ioctls assume 32bits. In itself that is fine as they are old cards and nobody uses 64bit. However on LKML it was noted this assumption is also made for some ioctl allocated memory and is unsafe on 64bit systems.
Fixing this is a mess. It turns out there is tons of crap buried in a header file that does racy 32/64bit filtering on the masks.
So we:
- Verify all callers of the racy code can sleep (i2o_dma_[re]alloc)
- Move the code into a new i2o/memory.c file
- Remove the gfp_mask argument so nobody can try and misuse the function
- Wrap a mutex around the problem area (a single mutex is easy to do and none of this is performance relevant)
- Switch the remaining problem kmalloc holdout to use i2o_dma_alloc
There is a remaining race but that causes 32bit allocations to occur very rarely during block I/O when 64bit could be used. I don't think that is worth the cost of fixing as the impact is just a negligible performance loss.
(but could do with more testing, especially on 64bit > 4GB boxes with 64bit capable I2O cards)
---
drivers/message/i2o/Makefile | 2 drivers/message/i2o/device.c | 2 drivers/message/i2o/exec-osm.c | 4 drivers/message/i2o/i2o_config.c | 22 +-- drivers/message/i2o/iop.c | 2 drivers/message/i2o/memory.c | 315 ++++++++++++++++++++++++++++++++++++++ drivers/message/i2o/pci.c | 16 +- include/linux/i2o.h | 291 +---------------------------------- 8 files changed, 350 insertions(+), 304 deletions(-) create mode 100644 drivers/message/i2o/memory.c
drivers/message/i2o/Makefile +1 -1
@@ -5,7 +5,7 @@ # In the future, some of these should be built conditionally. # -i2o_core-y += iop.o driver.o device.o debug.o pci.o exec-osm.o +i2o_core-y += iop.o driver.o device.o debug.o pci.o exec-osm.o memory.o i2o_bus-y += bus-osm.o i2o_config-y += config-osm.o obj-$(CONFIG_I2O) += i2o_core.o
drivers/message/i2o/device.c +1 -1
@@ -437,7 +437,7 @@ int i2o_parm_issue(struct i2o_device *i2o_dev, int cmd, void *oplist, res.virt =NULL ; -if (i2o_dma_alloc (dev, &res, reslen,GFP_KERNEL )) +if (i2o_dma_alloc (dev, &res, reslen))return -ENOMEM ; msg =i2o_msg_get_wait (c,I2O_TIMEOUT_MESSAGE_GET );
drivers/message/i2o/exec-osm.c +2 -2
@@ -388,8 +388,8 @@ static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind) dev = &c->pdev->dev; -if (i2o_dma_realloc - (dev, &c->dlct,le32_to_cpu (sb->expected_lct_size),GFP_KERNEL )) +if (i2o_dma_realloc (dev, &c->dlct, +le32_to_cpu (sb->expected_lct_size)))return -ENOMEM ; msg =i2o_msg_get_wait (c,I2O_TIMEOUT_MESSAGE_GET );
drivers/message/i2o/i2o_config.c +11 -11
@@ -780,12 +780,11 @@ static int i2o_cfg_passthru(unsigned long arg)u32 size =0 ;u32 reply_size =0 ;u32 rcode =0 ; -void *sg_list[SG_TABLESIZE ]; +struct i2o_dma sg_list[SG_TABLESIZE ];u32 sg_offset =0 ;u32 sg_count =0 ;int sg_index =0 ;u32 i =0 ; -void *p =NULL ;i2o_status_block *sb;struct i2o_message *msg;unsigned int iop;
@@ -842,6 +841,7 @@ static int i2o_cfg_passthru(unsigned long arg)memset (sg_list,0 ,sizeof (sg_list[0 ]) *SG_TABLESIZE );if (sg_offset) {struct sg_simple_element *sg; +struct i2o_dma *p;if (sg_offset *4 >= size) { rcode = -EFAULT ;
@@ -871,22 +871,23 @@ static int i2o_cfg_passthru(unsigned long arg) goto sg_list_cleanup; } sg_size = sg[i].flag_count &0xffffff ; + p = &(sg_list[sg_index]); +if (i2o_dma_alloc (&c->pdev->dev, p, sg_size, +PCI_DMA_BIDIRECTIONAL )) {/* Allocate memory for the transfer */ - p =kmalloc (sg_size,GFP_KERNEL ); -if (!p) {printk (KERN_DEBUG "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n" , c->name, sg_size, i, sg_count); rcode = -ENOMEM ; goto sg_list_cleanup; } - sg_list[sg_index++] = p;// sglist indexed with input frame, not our internal frame. + sg_index++;/* Copy in the user's SG buffer if necessary */ if (sg[i]. flag_count &0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {// TODO 64bit fix if (copy_from_user - (p, (void __user *)sg[i].addr_bus, + (p->virt, (void __user *)sg[i].addr_bus, sg_size)) { printk(KERN_DEBUG "%s: Could not copy SG buf %d FROM user\n",
@@ -895,8 +896,7 @@ static int i2o_cfg_passthru(unsigned long arg) goto sg_list_cleanup; } } -//TODO 64bit fix - sg[i].addr_bus =virt_to_bus (p); + sg[i].addr_bus = p->phys; } }
@@ -908,7 +908,7 @@ static int i2o_cfg_passthru(unsigned long arg) }if (sg_offset) { -u32 rmsg[128 ]; +u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE ];/* Copy back the Scatter Gather buffers back to user space */ u32 j;// TODO 64bit fix
@@ -942,7 +942,7 @@ static int i2o_cfg_passthru(unsigned long arg) sg_size = sg[j].flag_count &0xffffff ;// TODO 64bit fix if (copy_to_user - ((void __user *)sg[j].addr_bus, sg_list[j], + ((void __user *)sg[j].addr_bus, sg_list[j].virt, sg_size)) { printk(KERN_WARNING "%s: Could not copy %p TO user %x\n",
@@ -973,7 +973,7 @@ sg_list_cleanup: }for (i =0 ; i < sg_index; i++) -kfree (sg_list[i]); +i2o_dma_free (&c->pdev->dev, &sg_list[i]); cleanup:kfree (reply);
drivers/message/i2o/iop.c +1 -1
@@ -1004,7 +1004,7 @@ static int i2o_hrt_get(struct i2o_controller *c) size = hrt->num_entries * hrt->entry_len <<2 ;if (size > c->hrt.len) { -if (i2o_dma_realloc (dev, &c->hrt, size,GFP_KERNEL )) +if (i2o_dma_realloc (dev, &c->hrt, size))return -ENOMEM ;else hrt = c->hrt.virt;
drivers/message/i2o/memory.c new file +315 -0
315 lines not on this page · raw message
drivers/message/i2o/pci.c +7 -9
@@ -186,31 +186,29 @@ static int __devinit i2o_pci_alloc(struct i2o_controller *c) } } -if (i2o_dma_alloc (dev, &c->status,8 ,GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->status,8 )) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc (dev, &c->hrt,sizeof (i2o_hrt),GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->hrt,sizeof (i2o_hrt))) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc (dev, &c->dlct,8192 ,GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->dlct,8192 )) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc (dev, &c->status_block,sizeof (i2o_status_block), -GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->status_block,sizeof (i2o_status_block))) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc - (dev, &c->out_queue, -I2O_MAX_OUTBOUND_MSG_FRAMES *I2O_OUTBOUND_MSG_FRAME_SIZE * -sizeof (u32),GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->out_queue, +I2O_MAX_OUTBOUND_MSG_FRAMES *I2O_OUTBOUND_MSG_FRAME_SIZE * +sizeof (u32))) {i2o_pci_free (c);return -ENOMEM ; }
include/linux/i2o.h +12 -279
307 lines not on this page · raw message
Quoted 17 lines · On Fri, 25 Jul 2008 13:16:37 +0100 Alan Cox <[email protected]> wrote:
From: Alan Cox <[email protected]>
The I2O ioctls assume 32bits. In itself that is fine as they are old cards and nobody uses 64bit. However on LKML it was noted this assumption is also made for some ioctl allocated memory and is unsafe on 64bit systems.
Fixing this is a mess. It turns out there is tons of crap buried in a header file that does racy 32/64bit filtering on the masks.
So we:
- Verify all callers of the racy code can sleep (i2o_dma_[re]alloc)
- Move the code into a newi2o/memory.cfile
- Remove thegfp_maskargument so nobody can try and misuse the function
- Wrap a mutex around the problem area (a single mutex is easy to do and none of this is performance relevant)
- Switch the remaining problem kmalloc holdout to usei2o_dma_alloc
Quoted 6 lines
There is a remaining race but that causes 32bit allocations to occur very rarely during block I/O when 64bit could be used. I don't think that is worth the cost of fixing as the impact is just a negligible performance loss.
Signed-off-by Alan Cox <[email protected]>
I think you sent the wrong version of this patch - i2o_dma_alloc() is all mucked up. Sometimes takes three args, sometimes four. When it's four the fourth arg is sometimes a gfp_t, sometimes a PCI_DMA_foo.
It fell afoul of the dma_mapping_error() interface change too.
Quoted 2 lines
(but could do with more testing, especially on 64bit > 4GB boxes with 64bit capable I2O cards)
A bit of git-trolling indicates that Vasily might be able to help out with a bit of testing?
Andrew, Alan,
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Fri, 25 Jul 2008 13:16:37 +0100 Alan Cox <[email protected]> wrote:
(but could do with more testing, especially on 64bit > 4GB boxes with 64bit capable I2O cards)
A bit of git-trolling indicates that Vasily might be able to help out with a bit of testing?
One of our test nodes uses i2o controller. Now this node have 6 Gb memory and quite a long time it works well under 64-bit RHEL5-based kernel. I'm not sure is i2o card really 64-bit capable, but dmesg shows that it uses 64-bit dma.
I'm able to run any tests on this node, however it can take some time.
Could you please explain what tests you want to perform?
Thank you,
Vasily Averin
Quoted 3 lines
One of our test nodes uses i2o controller. Now this node have 6 Gb memory and quite a long time it works well under 64-bit RHEL5-based kernel. I'm not sure is i2o card really 64-bit capable, but dmesg shows that it uses 64-bit dma.
Do you use the i2o config interfaces and driver ?
Quoted 5 lines · Alan Cox
Quoted 3 lines
One of our test nodes uses i2o controller. Now this node have 6 Gb memory and quite a long time it works well under 64-bit RHEL5-based kernel. I'm not sure is i2o card really 64-bit capable, but dmesg shows that it uses 64-bit dma.
Do you use the i2o config interfaces and driver ?
When I loaded i2o_config module and read all entries in /proc/i2o/ directory the node hung.
Do you mean that you have fixed this issue?
thank you,
Vasily Averin
Quoted 11 lines · On Tue, 29 Jul 2008 13:23:01 +0400 Vasily Averin <[email protected]> wrote:
Quoted 5 lines · Alan Cox
Quoted 3 lines
One of our test nodes uses i2o controller. Now this node have 6 Gb memory and quite a long time it works well under 64-bit RHEL5-based kernel. I'm not sure is i2o card really 64-bit capable, but dmesg shows that it uses 64-bit dma.
Do you use the i2o config interfaces and driver ?
When I loaded
i2o_configmodule and read all entries in/proc/i2o/directory the node hung.Do you mean that you have fixed this issue?
I think I have yes
Quoted 4 lines
all mucked up. Sometimes takes three args, sometimes four. When it's four the fourth arg is sometimes a
gfp_t, sometimes aPCI_DMA_foo.It fell afoul of the
dma_mapping_error()interface change too.
Probably forgot to refresh the patch series... must be age ;)
I'll take a look at that next week
Quoted 5 lines
I think you sent the wrong version of this patch -
i2o_dma_alloc()is all mucked up. Sometimes takes three args, sometimes four. When it's four the fourth arg is sometimes agfp_t, sometimes aPCI_DMA_foo.It fell afoul of the
dma_mapping_error()interface change too.
i2o: Fix 32/64bit DMA locking
From: Alan Cox <[email protected]>
The I2O ioctls assume 32bits. In itself that is fine as they are old cards and nobody uses 64bit. However on LKML it was noted this assumption is also made for allocated memory and is unsafe on 64bit systems.
Fixing this is a mess. It turns out there is tons of crap buried in a header file that does racy 32/64bit filtering on the masks.
So we:
- Verify all callers of the racy code can sleep (i2o_dma_[re]alloc)
- Move the code into a new i2o/memory.c file
- Remove the gfp_mask argument so nobody can try and misuse the function
- Wrap a mutex around the problem area (a single mutex is easy to do and none of this is performance relevant)
- Switch the remaining problem kmalloc holdout to use i2o_dma_alloc
---
drivers/media/video/planb.c | 0 drivers/media/video/planb.h | 0 drivers/media/video/saa7196.h | 0 drivers/media/video/videodev.c | 0 drivers/message/i2o/Makefile | 2 drivers/message/i2o/device.c | 2 drivers/message/i2o/exec-osm.c | 4 drivers/message/i2o/i2o_config.c | 31 ++-- drivers/message/i2o/iop.c | 2 drivers/message/i2o/memory.c | 313 ++++++++++++++++++++++++++++++++++++++ drivers/message/i2o/pci.c | 16 +- include/linux/i2o.h | 292 +---------------------------------- include/media/audiochip.h | 0 13 files changed, 351 insertions(+), 311 deletions(-) delete mode 100644 drivers/media/video/planb.c delete mode 100644 drivers/media/video/planb.h delete mode 100644 drivers/media/video/saa7196.h delete mode 100644 drivers/media/video/videodev.c create mode 100644 drivers/message/i2o/memory.c delete mode 100644 include/media/audiochip.h
drivers/media/video/planb.c deleted +0 -0
drivers/media/video/planb.h deleted +0 -0
drivers/media/video/saa7196.h deleted +0 -0
drivers/media/video/videodev.c deleted +0 -0
drivers/message/i2o/Makefile +1 -1
@@ -5,7 +5,7 @@ # In the future, some of these should be built conditionally. # -i2o_core-y += iop.o driver.o device.o debug.o pci.o exec-osm.o +i2o_core-y += iop.o driver.o device.o debug.o pci.o exec-osm.o memory.o i2o_bus-y += bus-osm.o i2o_config-y += config-osm.o obj-$(CONFIG_I2O) += i2o_core.o
drivers/message/i2o/device.c +1 -1
@@ -467,7 +467,7 @@ int i2o_parm_issue(struct i2o_device *i2o_dev, int cmd, void *oplist, res.virt =NULL ; -if (i2o_dma_alloc (dev, &res, reslen,GFP_KERNEL )) +if (i2o_dma_alloc (dev, &res, reslen))return -ENOMEM ; msg =i2o_msg_get_wait (c,I2O_TIMEOUT_MESSAGE_GET );
drivers/message/i2o/exec-osm.c +2 -2
@@ -388,8 +388,8 @@ static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind) dev = &c->pdev->dev; -if (i2o_dma_realloc - (dev, &c->dlct,le32_to_cpu (sb->expected_lct_size),GFP_KERNEL )) +if (i2o_dma_realloc (dev, &c->dlct, +le32_to_cpu (sb->expected_lct_size)))return -ENOMEM ; msg =i2o_msg_get_wait (c,I2O_TIMEOUT_MESSAGE_GET );
drivers/message/i2o/i2o_config.c +14 -17
@@ -260,7 +260,7 @@ static int i2o_cfg_swdl(unsigned long arg)if (IS_ERR (msg))return PTR_ERR (msg); -if (i2o_dma_alloc (&c->pdev->dev, &buffer, fragsize,GFP_KERNEL )) { +if (i2o_dma_alloc (&c->pdev->dev, &buffer, fragsize)) {i2o_msg_nop (c, msg);return -ENOMEM ; }
@@ -339,7 +339,7 @@ static int i2o_cfg_swul(unsigned long arg)if (IS_ERR (msg))return PTR_ERR (msg); -if (i2o_dma_alloc (&c->pdev->dev, &buffer, fragsize,GFP_KERNEL )) { +if (i2o_dma_alloc (&c->pdev->dev, &buffer, fragsize)) {i2o_msg_nop (c, msg);return -ENOMEM ; }
@@ -634,9 +634,7 @@ static int i2o_cfg_passthru32(struct file *file, unsigned cmnd, sg_size = sg[i].flag_count &0xffffff ; p = &(sg_list[sg_index]);/* Allocate memory for the transfer */ -if (i2o_dma_alloc - (&c->pdev->dev, p, sg_size, -PCI_DMA_BIDIRECTIONAL )) { +if (i2o_dma_alloc (&c->pdev->dev, p, sg_size)) {printk (KERN_DEBUG "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n" , c->name, sg_size, i, sg_count);
@@ -780,12 +778,11 @@ static int i2o_cfg_passthru(unsigned long arg)u32 size =0 ;u32 reply_size =0 ;u32 rcode =0 ; -void *sg_list[SG_TABLESIZE ]; +struct i2o_dma sg_list[SG_TABLESIZE ];u32 sg_offset =0 ;u32 sg_count =0 ;int sg_index =0 ;u32 i =0 ; -void *p =NULL ;i2o_status_block *sb;struct i2o_message *msg;unsigned int iop;
@@ -842,6 +839,7 @@ static int i2o_cfg_passthru(unsigned long arg)memset (sg_list,0 ,sizeof (sg_list[0 ]) *SG_TABLESIZE );if (sg_offset) {struct sg_simple_element *sg; +struct i2o_dma *p;if (sg_offset *4 >= size) { rcode = -EFAULT ;
@@ -871,22 +869,22 @@ static int i2o_cfg_passthru(unsigned long arg) goto sg_list_cleanup; } sg_size = sg[i].flag_count &0xffffff ; + p = &(sg_list[sg_index]); +if (i2o_dma_alloc (&c->pdev->dev, p, sg_size)) {/* Allocate memory for the transfer */ - p =kmalloc (sg_size,GFP_KERNEL ); -if (!p) {printk (KERN_DEBUG "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n" , c->name, sg_size, i, sg_count); rcode = -ENOMEM ; goto sg_list_cleanup; } - sg_list[sg_index++] = p;// sglist indexed with input frame, not our internal frame. + sg_index++;/* Copy in the user's SG buffer if necessary */ if (sg[i]. flag_count &0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {// TODO 64bit fix if (copy_from_user - (p, (void __user *)sg[i].addr_bus, + (p->virt, (void __user *)sg[i].addr_bus, sg_size)) { printk(KERN_DEBUG "%s: Could not copy SG buf %d FROM user\n",
@@ -895,8 +893,7 @@ static int i2o_cfg_passthru(unsigned long arg) goto sg_list_cleanup; } } -//TODO 64bit fix - sg[i].addr_bus =virt_to_bus (p); + sg[i].addr_bus = p->phys; } }
@@ -908,7 +905,7 @@ static int i2o_cfg_passthru(unsigned long arg) }if (sg_offset) { -u32 rmsg[128 ]; +u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE ];/* Copy back the Scatter Gather buffers back to user space */ u32 j;// TODO 64bit fix
@@ -942,11 +939,11 @@ static int i2o_cfg_passthru(unsigned long arg) sg_size = sg[j].flag_count &0xffffff ;// TODO 64bit fix if (copy_to_user - ((void __user *)sg[j].addr_bus, sg_list[j], + ((void __user *)sg[j].addr_bus, sg_list[j].virt, sg_size)) {printk (KERN_WARNING "%s: Could not copy %p TO user %x\n" , - c->name, sg_list[j], + c->name, sg_list[j].virt, sg[j].addr_bus); rcode = -EFAULT ; goto sg_list_cleanup;
@@ -973,7 +970,7 @@ sg_list_cleanup: }for (i =0 ; i < sg_index; i++) -kfree (sg_list[i]); +i2o_dma_free (&c->pdev->dev, &sg_list[i]); cleanup:kfree (reply);
drivers/message/i2o/iop.c +1 -1
@@ -1004,7 +1004,7 @@ static int i2o_hrt_get(struct i2o_controller *c) size = hrt->num_entries * hrt->entry_len <<2 ;if (size > c->hrt.len) { -if (i2o_dma_realloc (dev, &c->hrt, size,GFP_KERNEL )) +if (i2o_dma_realloc (dev, &c->hrt, size))return -ENOMEM ;else hrt = c->hrt.virt;
drivers/message/i2o/memory.c new file +313 -0
314 lines not on this page · raw message
drivers/message/i2o/pci.c +7 -9
@@ -186,31 +186,29 @@ static int __devinit i2o_pci_alloc(struct i2o_controller *c) } } -if (i2o_dma_alloc (dev, &c->status,8 ,GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->status,8 )) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc (dev, &c->hrt,sizeof (i2o_hrt),GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->hrt,sizeof (i2o_hrt))) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc (dev, &c->dlct,8192 ,GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->dlct,8192 )) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc (dev, &c->status_block,sizeof (i2o_status_block), -GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->status_block,sizeof (i2o_status_block))) {i2o_pci_free (c);return -ENOMEM ; } -if (i2o_dma_alloc - (dev, &c->out_queue, -I2O_MAX_OUTBOUND_MSG_FRAMES *I2O_OUTBOUND_MSG_FRAME_SIZE * -sizeof (u32),GFP_KERNEL )) { +if (i2o_dma_alloc (dev, &c->out_queue, +I2O_MAX_OUTBOUND_MSG_FRAMES *I2O_OUTBOUND_MSG_FRAME_SIZE * +sizeof (u32))) {i2o_pci_free (c);return -ENOMEM ; }
include/linux/i2o.h +12 -280
308 lines not on this page · raw message