habanalabs: helper function to validate export params
authorOhad Sharabi <osharabi@habana.ai>
Tue, 29 Nov 2022 07:13:34 +0000 (09:13 +0200)
committerOded Gabbay <ogabbay@kernel.org>
Thu, 26 Jan 2023 08:56:21 +0000 (10:56 +0200)
Validate export parameters in a dedicated function instead of in the
main export flow.
This will be useful later when support to export dmabuf for devices
with virtual memory will be added.

Signed-off-by: Ohad Sharabi <osharabi@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/misc/habanalabs/common/memory.c

index e7c4dd9842ce6b7f9335f9d4d119a199e47bd799..ba7b2ee09408eb30fe8da86e2d3201452b9a85d9 100644 (file)
@@ -1797,36 +1797,10 @@ err_dma_buf_put:
        return rc;
 }
 
-/**
- * export_dmabuf_from_addr() - export a dma-buf object for the given memory
- *                             address and size.
- * @ctx: pointer to the context structure.
- * @device_addr:  device memory physical address.
- * @size: size of device memory.
- * @flags: DMA-BUF file/FD flags.
- * @dmabuf_fd: pointer to result FD that represents the dma-buf object.
- *
- * Create and export a dma-buf object for an existing memory allocation inside
- * the device memory, and return a FD which is associated with the dma-buf
- * object.
- *
- * Return: 0 on success, non-zero for failure.
- */
-static int export_dmabuf_from_addr(struct hl_ctx *ctx, u64 device_addr,
-                                       u64 size, int flags, int *dmabuf_fd)
+static int validate_export_params(struct hl_device *hdev, u64 device_addr, u64 size)
 {
-       struct hl_dmabuf_priv *hl_dmabuf;
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
        u64 bar_address;
-       int rc;
-
-       prop = &hdev->asic_prop;
-
-       if (prop->dram_supports_virtual_memory) {
-               dev_dbg(hdev->dev, "Export not supported for devices with virtual memory\n");
-               return -EOPNOTSUPP;
-       }
 
        if (!IS_ALIGNED(device_addr, PAGE_SIZE)) {
                dev_dbg(hdev->dev,
@@ -1843,26 +1817,61 @@ static int export_dmabuf_from_addr(struct hl_ctx *ctx, u64 device_addr,
        }
 
        if (device_addr < prop->dram_user_base_address ||
-                               device_addr + size > prop->dram_end_address ||
-                               device_addr + size < device_addr) {
+                               (device_addr + size) > prop->dram_end_address ||
+                               (device_addr + size) < device_addr) {
                dev_dbg(hdev->dev,
                        "DRAM memory range 0x%llx (+0x%llx) is outside of DRAM boundaries\n",
                        device_addr, size);
                return -EINVAL;
        }
 
-       bar_address = hdev->dram_pci_bar_start +
-                       (device_addr - prop->dram_base_address);
+       bar_address = hdev->dram_pci_bar_start + (device_addr - prop->dram_base_address);
 
-       if (bar_address + size >
-                       hdev->dram_pci_bar_start + prop->dram_pci_bar_size ||
-                       bar_address + size < bar_address) {
+       if ((bar_address + size) > (hdev->dram_pci_bar_start + prop->dram_pci_bar_size) ||
+                       (bar_address + size) < bar_address) {
                dev_dbg(hdev->dev,
                        "DRAM memory range 0x%llx (+0x%llx) is outside of PCI BAR boundaries\n",
                        device_addr, size);
                return -EINVAL;
        }
 
+       return 0;
+}
+
+/**
+ * export_dmabuf_from_addr() - export a dma-buf object for the given memory
+ *                             address and size.
+ * @ctx: pointer to the context structure.
+ * @device_addr:  device memory physical address.
+ * @size: size of device memory.
+ * @flags: DMA-BUF file/FD flags.
+ * @dmabuf_fd: pointer to result FD that represents the dma-buf object.
+ *
+ * Create and export a dma-buf object for an existing memory allocation inside
+ * the device memory, and return a FD which is associated with the dma-buf
+ * object.
+ *
+ * Return: 0 on success, non-zero for failure.
+ */
+static int export_dmabuf_from_addr(struct hl_ctx *ctx, u64 device_addr,
+                                       u64 size, int flags, int *dmabuf_fd)
+{
+       struct hl_dmabuf_priv *hl_dmabuf;
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop;
+       int rc;
+
+       prop = &hdev->asic_prop;
+
+       if (prop->dram_supports_virtual_memory) {
+               dev_dbg(hdev->dev, "Export not supported for devices with virtual memory\n");
+               return -EOPNOTSUPP;
+       }
+
+       rc = validate_export_params(hdev, device_addr, size);
+       if (rc)
+               return rc;
+
        hl_dmabuf = kzalloc(sizeof(*hl_dmabuf), GFP_KERNEL);
        if (!hl_dmabuf)
                return -ENOMEM;