habanalabs: leave space for 2xMSG_PROT in CB
authorOded Gabbay <oded.gabbay@gmail.com>
Fri, 17 Apr 2020 09:12:13 +0000 (12:12 +0300)
committerOded Gabbay <oded.gabbay@gmail.com>
Tue, 19 May 2020 11:48:41 +0000 (14:48 +0300)
The user must leave space for 2xMSG_PROT in the external CB, so adjust the
define of max size accordingly. The driver, however, can still create a CB
with the maximum size of 2MB. Therefore, we need to add a check
specifically for the user requested size.

Reviewed-by: Tomer Tayar <ttayar@habana.ai>
Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
drivers/misc/habanalabs/command_buffer.c
include/uapi/misc/habanalabs.h

index 53fddbd8e693938cd47724ca1813a9b87555b661..6cb92efce4d94a5ccdf65a4920c68ff5c508459c 100644 (file)
@@ -105,10 +105,9 @@ int hl_cb_create(struct hl_device *hdev, struct hl_cb_mgr *mgr,
                goto out_err;
        }
 
-       if (cb_size > HL_MAX_CB_SIZE) {
-               dev_err(hdev->dev,
-                       "CB size %d must be less then %d\n",
-                       cb_size, HL_MAX_CB_SIZE);
+       if (cb_size > SZ_2M) {
+               dev_err(hdev->dev, "CB size %d must be less than %d\n",
+                       cb_size, SZ_2M);
                rc = -EINVAL;
                goto out_err;
        }
@@ -211,7 +210,7 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
 {
        union hl_cb_args *args = data;
        struct hl_device *hdev = hpriv->hdev;
-       u64 handle;
+       u64 handle = 0;
        int rc;
 
        if (hl_device_disabled_or_in_reset(hdev)) {
@@ -223,15 +222,26 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
 
        switch (args->in.op) {
        case HL_CB_OP_CREATE:
-               rc = hl_cb_create(hdev, &hpriv->cb_mgr, args->in.cb_size,
-                                       &handle, hpriv->ctx->asid);
+               if (args->in.cb_size > HL_MAX_CB_SIZE) {
+                       dev_err(hdev->dev,
+                               "User requested CB size %d must be less than %d\n",
+                               args->in.cb_size, HL_MAX_CB_SIZE);
+                       rc = -EINVAL;
+               } else {
+                       rc = hl_cb_create(hdev, &hpriv->cb_mgr,
+                                               args->in.cb_size, &handle,
+                                               hpriv->ctx->asid);
+               }
+
                memset(args, 0, sizeof(*args));
                args->out.cb_handle = handle;
                break;
+
        case HL_CB_OP_DESTROY:
                rc = hl_cb_destroy(hdev, &hpriv->cb_mgr,
                                        args->in.cb_handle);
                break;
+
        default:
                rc = -ENOTTY;
                break;
index 4d593050c42b6c7ca2e21ba6cd0e851839167254..523e511e6cffcf1756dc7a9a661d2ff0a21c6703 100644 (file)
@@ -209,7 +209,8 @@ struct hl_info_args {
 /* Opcode to destroy previously created command buffer */
 #define HL_CB_OP_DESTROY       1
 
-#define HL_MAX_CB_SIZE         0x200000        /* 2MB */
+/* 2MB minus 32 bytes for 2xMSG_PROT */
+#define HL_MAX_CB_SIZE         (0x200000 - 32)
 
 struct hl_cb_in {
        /* Handle of CB or 0 if we want to create one */