From c0dc72bad9cf21071f5e4005de46f7c8b67a138a Mon Sep 17 00:00:00 2001 From: Sebastien Dugue Date: Thu, 20 May 2010 15:58:22 -0700 Subject: mlx4_core: Fix possible chunk sg list overflow in mlx4_alloc_icm() If the number of sg entries in the ICM chunk reaches MLX4_ICM_CHUNK_LEN, we must set chunk to NULL even for coherent mappings so that the next time through the loop will allocate another chunk. Otherwise we'll overflow the sg list the next time through the loop. This will lead to memory corruption if this case is hit. mthca does not have this bug. Signed-off-by: Sebastien Dugue Cc: Signed-off-by: Roland Dreier --- drivers/net/mlx4/icm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'drivers/net/mlx4') diff --git a/drivers/net/mlx4/icm.c b/drivers/net/mlx4/icm.c index 57288ca1395f..ef62f1749b8a 100644 --- a/drivers/net/mlx4/icm.c +++ b/drivers/net/mlx4/icm.c @@ -175,9 +175,10 @@ struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages, if (chunk->nsg <= 0) goto fail; + } + if (chunk->npages == MLX4_ICM_CHUNK_LEN) chunk = NULL; - } npages -= 1 << cur_order; } else { -- cgit v1.2.2 From c050def076bfbc4513ee961c77fde6ba3d401158 Mon Sep 17 00:00:00 2001 From: Roland Dreier Date: Thu, 20 May 2010 15:58:22 -0700 Subject: mlx4_core: Clean up mlx4_alloc_icm() a bit Handle the allocation error case first, so that we don't have further nested if for handling the common case of success. Signed-off-by: Roland Dreier --- drivers/net/mlx4/icm.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'drivers/net/mlx4') diff --git a/drivers/net/mlx4/icm.c b/drivers/net/mlx4/icm.c index ef62f1749b8a..b07e4dee80aa 100644 --- a/drivers/net/mlx4/icm.c +++ b/drivers/net/mlx4/icm.c @@ -163,29 +163,30 @@ struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages, ret = mlx4_alloc_icm_pages(&chunk->mem[chunk->npages], cur_order, gfp_mask); - if (!ret) { - ++chunk->npages; - - if (coherent) - ++chunk->nsg; - else if (chunk->npages == MLX4_ICM_CHUNK_LEN) { - chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, - chunk->npages, - PCI_DMA_BIDIRECTIONAL); - - if (chunk->nsg <= 0) - goto fail; - } + if (ret) { + if (--cur_order < 0) + goto fail; + else + continue; + } - if (chunk->npages == MLX4_ICM_CHUNK_LEN) - chunk = NULL; + ++chunk->npages; - npages -= 1 << cur_order; - } else { - --cur_order; - if (cur_order < 0) + if (coherent) + ++chunk->nsg; + else if (chunk->npages == MLX4_ICM_CHUNK_LEN) { + chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, + chunk->npages, + PCI_DMA_BIDIRECTIONAL); + + if (chunk->nsg <= 0) goto fail; } + + if (chunk->npages == MLX4_ICM_CHUNK_LEN) + chunk = NULL; + + npages -= 1 << cur_order; } if (!coherent && chunk) { -- cgit v1.2.2