Skip to content

Commit f829230

Browse files
fanceraxboe
authored andcommitted
block: sed-opal: kmalloc the cmd/resp buffers
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>
1 parent f02be90 commit f829230

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

block/sed-opal.c

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

8989
size_t pos;
90-
u8 cmd[IO_BUFFER_LENGTH];
91-
u8 resp[IO_BUFFER_LENGTH];
90+
u8 *cmd;
91+
u8 *resp;
9292

9393
struct parsed_resp parsed;
9494
size_t prev_d_len;
@@ -2175,6 +2175,8 @@ void free_opal_dev(struct opal_dev *dev)
21752175
return;
21762176

21772177
clean_opal_dev(dev);
2178+
kfree(dev->resp);
2179+
kfree(dev->cmd);
21782180
kfree(dev);
21792181
}
21802182
EXPORT_SYMBOL(free_opal_dev);
@@ -2187,18 +2189,40 @@ struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
21872189
if (!dev)
21882190
return NULL;
21892191

2192+
/*
2193+
* Presumably DMA-able buffers must be cache-aligned. Kmalloc makes
2194+
* sure the allocated buffer is DMA-safe in that regard.
2195+
*/
2196+
dev->cmd = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2197+
if (!dev->cmd)
2198+
goto err_free_dev;
2199+
2200+
dev->resp = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2201+
if (!dev->resp)
2202+
goto err_free_cmd;
2203+
21902204
INIT_LIST_HEAD(&dev->unlk_lst);
21912205
mutex_init(&dev->dev_lock);
21922206
dev->flags = 0;
21932207
dev->data = data;
21942208
dev->send_recv = send_recv;
21952209
if (check_opal_support(dev) != 0) {
21962210
pr_debug("Opal is not supported on this device\n");
2197-
kfree(dev);
2198-
return NULL;
2211+
goto err_free_resp;
21992212
}
22002213

22012214
return dev;
2215+
2216+
err_free_resp:
2217+
kfree(dev->resp);
2218+
2219+
err_free_cmd:
2220+
kfree(dev->cmd);
2221+
2222+
err_free_dev:
2223+
kfree(dev);
2224+
2225+
return NULL;
22022226
}
22032227
EXPORT_SYMBOL(init_opal_dev);
22042228

0 commit comments

Comments
 (0)