Skip to content

Commit 25521fd

Browse files
fancergregkh
authored andcommitted
block: sed-opal: kmalloc the cmd/resp buffers
[ Upstream commit f829230 ] In accordance with [1] the DMA-able memory buffers must be cacheline-aligned otherwise the cache writing-back and invalidation performed during the mapping may cause the adjacent data being lost. It's specifically required for the DMA-noncoherent platforms [2]. Seeing the opal_dev.{cmd,resp} buffers are implicitly used for DMAs in the NVME and SCSI/SD drivers in framework of the nvme_sec_submit() and sd_sec_submit() methods respectively they must be cacheline-aligned to prevent the denoted problem. One of the option to guarantee that is to kmalloc the buffers [2]. Let's explicitly allocate them then instead of embedding into the opal_dev structure instance. Note this fix was inspired by the commit c94b7f9 ("nvme-hwmon: kmalloc the NVME SMART log buffer"). [1] Documentation/core-api/dma-api.rst [2] Documentation/core-api/dma-api-howto.rst Fixes: 455a7b2 ("block: Add Sed-opal library") Signed-off-by: Serge Semin <Sergey.Semin@baikalelectronics.ru> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20221107203944.31686-1-Sergey.Semin@baikalelectronics.ru Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2f21d65 commit 25521fd

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

block/sed-opal.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ struct opal_dev {
8888
u64 lowest_lba;
8989

9090
size_t pos;
91-
u8 cmd[IO_BUFFER_LENGTH];
92-
u8 resp[IO_BUFFER_LENGTH];
91+
u8 *cmd;
92+
u8 *resp;
9393

9494
struct parsed_resp parsed;
9595
size_t prev_d_len;
@@ -2134,6 +2134,8 @@ void free_opal_dev(struct opal_dev *dev)
21342134
return;
21352135

21362136
clean_opal_dev(dev);
2137+
kfree(dev->resp);
2138+
kfree(dev->cmd);
21372139
kfree(dev);
21382140
}
21392141
EXPORT_SYMBOL(free_opal_dev);
@@ -2146,17 +2148,39 @@ struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
21462148
if (!dev)
21472149
return NULL;
21482150

2151+
/*
2152+
* Presumably DMA-able buffers must be cache-aligned. Kmalloc makes
2153+
* sure the allocated buffer is DMA-safe in that regard.
2154+
*/
2155+
dev->cmd = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2156+
if (!dev->cmd)
2157+
goto err_free_dev;
2158+
2159+
dev->resp = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2160+
if (!dev->resp)
2161+
goto err_free_cmd;
2162+
21492163
INIT_LIST_HEAD(&dev->unlk_lst);
21502164
mutex_init(&dev->dev_lock);
21512165
dev->data = data;
21522166
dev->send_recv = send_recv;
21532167
if (check_opal_support(dev) != 0) {
21542168
pr_debug("Opal is not supported on this device\n");
2155-
kfree(dev);
2156-
return NULL;
2169+
goto err_free_resp;
21572170
}
21582171

21592172
return dev;
2173+
2174+
err_free_resp:
2175+
kfree(dev->resp);
2176+
2177+
err_free_cmd:
2178+
kfree(dev->cmd);
2179+
2180+
err_free_dev:
2181+
kfree(dev);
2182+
2183+
return NULL;
21602184
}
21612185
EXPORT_SYMBOL(init_opal_dev);
21622186

0 commit comments

Comments
 (0)