Skip to content

Commit 0b75cb5

Browse files
Tomer Tayarogabbay
authored andcommitted
accel/habanalabs: export dma-buf only if size/offset multiples of PAGE_SIZE
It is currently allowed for a user to export dma-buf with size and offset that are not multiples of PAGE_SIZE. The exported memory is mapped for the importer device, and there it will be rounded to PAGE_SIZE, leading to actually exporting more than the user intended to. To make the user be aware of it, accept only size and offset which are multiple of PAGE_SIZE. Signed-off-by: Tomer Tayar <ttayar@habana.ai> Reviewed-by: Oded Gabbay <ogabbay@kernel.org> Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
1 parent efbca04 commit 0b75cb5

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

drivers/accel/habanalabs/common/memory.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,16 +1877,16 @@ static int export_dmabuf(struct hl_ctx *ctx,
18771877

18781878
static int validate_export_params_common(struct hl_device *hdev, u64 device_addr, u64 size)
18791879
{
1880-
if (!IS_ALIGNED(device_addr, PAGE_SIZE)) {
1880+
if (!PAGE_ALIGNED(device_addr)) {
18811881
dev_dbg(hdev->dev,
1882-
"exported device memory address 0x%llx should be aligned to 0x%lx\n",
1882+
"exported device memory address 0x%llx should be aligned to PAGE_SIZE 0x%lx\n",
18831883
device_addr, PAGE_SIZE);
18841884
return -EINVAL;
18851885
}
18861886

1887-
if (size < PAGE_SIZE) {
1887+
if (!size || !PAGE_ALIGNED(size)) {
18881888
dev_dbg(hdev->dev,
1889-
"exported device memory size %llu should be equal to or greater than %lu\n",
1889+
"exported device memory size %llu should be a multiple of PAGE_SIZE %lu\n",
18901890
size, PAGE_SIZE);
18911891
return -EINVAL;
18921892
}
@@ -1937,6 +1937,13 @@ static int validate_export_params(struct hl_device *hdev, u64 device_addr, u64 s
19371937
if (rc)
19381938
return rc;
19391939

1940+
if (!PAGE_ALIGNED(offset)) {
1941+
dev_dbg(hdev->dev,
1942+
"exported device memory offset %llu should be a multiple of PAGE_SIZE %lu\n",
1943+
offset, PAGE_SIZE);
1944+
return -EINVAL;
1945+
}
1946+
19401947
if ((offset + size) > phys_pg_pack->total_size) {
19411948
dev_dbg(hdev->dev, "offset %#llx and size %#llx exceed total map size %#llx\n",
19421949
offset, size, phys_pg_pack->total_size);

0 commit comments

Comments
 (0)