mirror of
https://github.com/torvalds/linux.git
synced 2024-12-26 21:02:19 +00:00
habanalabs: Disable file operations after device is removed
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>
This commit is contained in:
parent
27ac5aada0
commit
ffd123fe83
@ -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);
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user