habanalabs: Disable file operations after device is removed
authorTomer Tayar <ttayar@habana.ai>
Mon, 1 Feb 2021 17:44:34 +0000 (19:44 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 10 Mar 2021 08:16:09 +0000 (09:16 +0100)
A device can be removed from the PCI subsystem while a process holds the
file descriptor opened.
In such a case, the driver attempts to kill the process, but as it is
still possible that the process will be alive after this step, the
device removal will complete, and we will end up with a process object
that points to a device object which was already released.

To prevent the usage of this released device object, disable the
following file operations for this process object, and avoid the cleanup
steps when the file descriptor is eventually closed.
The latter is just a best effort, as memory leak will occur.

Signed-off-by: Tomer Tayar <ttayar@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/misc/habanalabs/common/device.c
drivers/misc/habanalabs/common/habanalabs_ioctl.c

index 9ecd805..334009e 100644 (file)
@@ -93,12 +93,19 @@ void hl_hpriv_put(struct hl_fpriv *hpriv)
 static int hl_device_release(struct inode *inode, struct file *filp)
 {
        struct hl_fpriv *hpriv = filp->private_data;
+       struct hl_device *hdev = hpriv->hdev;
+
+       filp->private_data = NULL;
+
+       if (!hdev) {
+               pr_crit("Closing FD after device was removed. Memory leak will occur and it is advised to reboot.\n");
+               put_pid(hpriv->taskpid);
+               return 0;
+       }
 
        hl_cb_mgr_fini(hpriv->hdev, &hpriv->cb_mgr);
        hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);
 
-       filp->private_data = NULL;
-
        hl_hpriv_put(hpriv);
 
        return 0;
@@ -107,16 +114,19 @@ static int hl_device_release(struct inode *inode, struct file *filp)
 static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
 {
        struct hl_fpriv *hpriv = filp->private_data;
-       struct hl_device *hdev;
+       struct hl_device *hdev = hpriv->hdev;
 
        filp->private_data = NULL;
 
-       hdev = hpriv->hdev;
+       if (!hdev) {
+               pr_err("Closing FD after device was removed\n");
+               goto out;
+       }
 
        mutex_lock(&hdev->fpriv_list_lock);
        list_del(&hpriv->dev_node);
        mutex_unlock(&hdev->fpriv_list_lock);
-
+out:
        put_pid(hpriv->taskpid);
 
        kfree(hpriv);
@@ -136,8 +146,14 @@ static int hl_device_release_ctrl(struct inode *inode, struct file *filp)
 static int hl_mmap(struct file *filp, struct vm_area_struct *vma)
 {
        struct hl_fpriv *hpriv = filp->private_data;
+       struct hl_device *hdev = hpriv->hdev;
        unsigned long vm_pgoff;
 
+       if (!hdev) {
+               pr_err_ratelimited("Trying to mmap after device was removed! Please close FD\n");
+               return -ENODEV;
+       }
+
        vm_pgoff = vma->vm_pgoff;
        vma->vm_pgoff = HL_MMAP_OFFSET_VALUE_GET(vm_pgoff);
 
@@ -885,6 +901,16 @@ wait_for_processes:
        return -EBUSY;
 }
 
+static void device_disable_open_processes(struct hl_device *hdev)
+{
+       struct hl_fpriv *hpriv;
+
+       mutex_lock(&hdev->fpriv_list_lock);
+       list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node)
+               hpriv->hdev = NULL;
+       mutex_unlock(&hdev->fpriv_list_lock);
+}
+
 /*
  * hl_device_reset - reset the device
  *
@@ -1558,8 +1584,10 @@ void hl_device_fini(struct hl_device *hdev)
                HL_PENDING_RESET_LONG_SEC);
 
        rc = device_kill_open_processes(hdev, HL_PENDING_RESET_LONG_SEC);
-       if (rc)
+       if (rc) {
                dev_crit(hdev->dev, "Failed to kill all open processes\n");
+               device_disable_open_processes(hdev);
+       }
 
        hl_cb_pool_fini(hdev);
 
index 03af61c..083a309 100644 (file)
@@ -5,6 +5,8 @@
  * All Rights Reserved.
  */
 
+#define pr_fmt(fmt)    "habanalabs: " fmt
+
 #include <uapi/misc/habanalabs.h>
 #include "habanalabs.h"
 
@@ -682,6 +684,11 @@ long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
        const struct hl_ioctl_desc *ioctl = NULL;
        unsigned int nr = _IOC_NR(cmd);
 
+       if (!hdev) {
+               pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
+               return -ENODEV;
+       }
+
        if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
                ioctl = &hl_ioctls[nr];
        } else {
@@ -700,6 +707,11 @@ long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg)
        const struct hl_ioctl_desc *ioctl = NULL;
        unsigned int nr = _IOC_NR(cmd);
 
+       if (!hdev) {
+               pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
+               return -ENODEV;
+       }
+
        if (nr == _IOC_NR(HL_IOCTL_INFO)) {
                ioctl = &hl_ioctls_control[nr];
        } else {