nvme: fix memory leak for power latency tolerance req
PATCH 1/1 nvme: fix memory leak for power latency tolerance req
nvme_init_ctrl() initialize latency tolerance controls and kzalloc structdev_pm_qos_request 'req' in nvme_init_ctrl(). The only way to free the 'req' is nvme_init_identify(), when apst is disable and prev_apst is enable.
But, prev_apst is disable when device probe, and we will miss to free it when the device does't support APST.
To fix the problem, we call dev_pm_qos_update_user_latency_tolerance() to nvme_init_identify() only when the device actually supports APST. And it needs to be hidden when remove nvme ctrl.
--- drivers/nvme/host/core.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/nvme/host/core.c +24 -10
@@ -2485,6 +2485,21 @@ static int nvme_get_effects_log(struct nvme_ctrl *ctrl)return ret; } +static void nvme_expose_latency_tolerance (struct nvme_ctrl *ctrl) +{ + ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; +dev_pm_qos_update_user_latency_tolerance (ctrl->device, +min (default_ps_max_latency_us, (unsigned long )S32_MAX )); + +dev_pm_qos_expose_latency_tolerance (ctrl->device); +} + +static void nvme_hide_latency_tolerance (struct nvme_ctrl *ctrl) +{ + ctrl->device->power.set_latency_tolerance =NULL ; +dev_pm_qos_hide_latency_tolerance (ctrl->device); +} +/* * Initialize the cached copies of the Identify data and various controller * register in our nvme_ctrl structure. This should be called as soon as
@@ -2643,9 +2658,9 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)return ret;if (ctrl->apst_enabled && !prev_apst_enabled) -dev_pm_qos_expose_latency_tolerance (ctrl->device); +nvme_expose_latency_tolerance (ctrl);else if (!ctrl->apst_enabled && prev_apst_enabled) -dev_pm_qos_hide_latency_tolerance (ctrl->device); +nvme_hide_latency_tolerance (ctrl); ret =nvme_configure_apst (ctrl);if (ret <0 )
@@ -3687,6 +3702,13 @@ EXPORT_SYMBOL_GPL(nvme_start_ctrl);void nvme_uninit_ctrl (struct nvme_ctrl *ctrl) { +/* +* If userspace latency tolerance have been exposed, +* we need to hide it. +*/ +if (ctrl->device->power.set_latency_tolerance) +nvme_hide_latency_tolerance (ctrl); +cdev_device_del (&ctrl->cdev, ctrl->device); }EXPORT_SYMBOL_GPL (nvme_uninit_ctrl);
@@ -3773,14 +3795,6 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,if (ret) goto out_free_name; -/* -* Initialize latency tolerance controls. The sysfs files won't -* be visible to userspace unless the device actually supports APST. -*/ - ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; -dev_pm_qos_update_user_latency_tolerance (ctrl->device, -min (default_ps_max_latency_us, (unsigned long )S32_MAX )); -return 0 ; out_free_name:kfree_const (ctrl->device->kobj.name);
Signature · 1 line
2.16.2.dirty
PATCH 1/1 nvme: fix memory leak for power latency tolerance req
Hi,
Quoted 77 lines · Yufen Yu
nvme_init_ctrl()initialize latency tolerance controls and kzallocstructdev_pm_qos_request'req' innvme_init_ctrl(). The only way to free the 'req' isnvme_init_identify(), when apst is disable andprev_apstis enable.But,
prev_apstis disable when device probe, and we will miss to free it when the device does't support APST.To fix the problem, we call
dev_pm_qos_update_user_latency_tolerance()tonvme_init_identify()only when the device actually supports APST. And it needs to be hidden when remove nvme ctrl.Fixes c5552fde102fc("nvme: Enable autonomous power state transitions")Signed-off-by Yufen Yu <yuyufen at huawei.com>--- drivers/nvme/host/core.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)drivers/nvme/host/core.c +24
-3@@ -2485,6 +2485,21 @@ static int nvme_get_effects_log(struct nvme_ctrl *ctrl)return ret; } +static void nvme_expose_latency_tolerance (struct nvme_ctrl *ctrl) +{ + ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; +dev_pm_qos_update_user_latency_tolerance (ctrl->device, +min (default_ps_max_latency_us, (unsigned long )S32_MAX )); + +dev_pm_qos_expose_latency_tolerance (ctrl->device); +} + +static void nvme_hide_latency_tolerance (struct nvme_ctrl *ctrl) +{ + ctrl->device->power.set_latency_tolerance =NULL ; +dev_pm_qos_hide_latency_tolerance (ctrl->device); +} +/* * Initialize the cached copies of the Identify data and various controller * register in our nvme_ctrl structure. This should be called as soon as @@ -2643,9 +2658,9 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)return ret;if (ctrl->apst_enabled && !prev_apst_enabled) -dev_pm_qos_expose_latency_tolerance (ctrl->device); +nvme_expose_latency_tolerance (ctrl);else if (!ctrl->apst_enabled && prev_apst_enabled) -dev_pm_qos_hide_latency_tolerance (ctrl->device); +nvme_hide_latency_tolerance (ctrl); ret =nvme_configure_apst (ctrl);if (ret <0 )@@ -3687,6 +3702,13 @@ EXPORT_SYMBOL_GPL(nvme_start_ctrl);void nvme_uninit_ctrl (struct nvme_ctrl *ctrl) { +/* +* If userspace latency tolerance have been exposed, +* we need to hide it. +*/ +if (ctrl->device->power.set_latency_tolerance) +nvme_hide_latency_tolerance (ctrl); +cdev_device_del (&ctrl->cdev, ctrl->device); }EXPORT_SYMBOL_GPL (nvme_uninit_ctrl);@@ -3773,14 +3795,6 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,if (ret) goto out_free_name; -/*
drivers/nvme/host/core.c int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, · line 3800
Quoted 10 lines
drivers/nvme/host/core.c +0
-7-* Initialize latency tolerance controls. The sysfs files won't -* be visible to userspace unless the device actually supports APST. -*/ - ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance; -dev_pm_qos_update_user_latency_tolerance (ctrl->device, -min (default_ps_max_latency_us, (unsigned long )S32_MAX )); -return 0 ; out_free_name:kfree_const (ctrl->device->kobj.name);
Before patch running v5.1
$ cat /sys/kernel/debug/kmemleak
unreferenced object 0xffff924143eebfc0 (size 64):
comm "kworker/1:2", pid 418, jiffies 4294679082 (age 275.968s)
hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
[<000000001db7845a>] dev_pm_qos_update_user_latency_tolerance+0x63/0xe0
[<000000008e36e7c0>] nvme_init_ctrl+0x2ec/0x310 [nvme_core]
[<000000000177edcb>] nvme_probe+0x224/0x3e0 [nvme]
[<00000000d2397746>] local_pci_probe+0x42/0x80
[<0000000068201d8c>] work_for_cpu_fn+0x16/0x20
[<00000000ccf116ec>] process_one_work+0x1a1/0x3a0
[<000000007b61fbe2>] worker_thread+0x1c9/0x380
[<0000000073ebfed5>] kthread+0x10c/0x130
[<000000007f82e90d>] ret_from_fork+0x35/0x40After patch no more memory leak as indicated above.
Thanks,
David
PATCH 1/1 nvme: fix memory leak for power latency tolerance req
In reply to Yufen YuIt's perfectly safe to call dev_pm_qos_hide_latency_tolerance even if we never exposed it, so I think you're making this more complicated than necessary. Would the following one-liner work for you instead?
---
drivers/nvme/host/core.c +1 -0
@@ -3697,6 +3697,7 @@ EXPORT_SYMBOL_GPL(nvme_start_ctrl);void nvme_uninit_ctrl (struct nvme_ctrl *ctrl) { +dev_pm_qos_hide_latency_tolerance (ctrl->device);cdev_device_del (&ctrl->cdev, ctrl->device); }EXPORT_SYMBOL_GPL (nvme_uninit_ctrl);
--
PATCH 1/1 nvme: fix memory leak for power latency tolerance req
Quoted 10 lines · Keith Busch
It's perfectly safe to call
dev_pm_qos_hide_latency_toleranceeven if we never exposed it, so I think you're making this more complicated than necessary. Would the following one-liner work for you instead?---
drivers/nvme/host/core.c +0
-0
drivers/nvme/host/core.c EXPORT_SYMBOL_GPL(nvme_start_ctrl); · line 3702
Quoted 6 lines
drivers/nvme/host/core.c +1
-0@@ -3697,6 +3697,7 @@ EXPORT_SYMBOL_GPL(nvme_start_ctrl);void nvme_uninit_ctrl (struct nvme_ctrl *ctrl) { +dev_pm_qos_hide_latency_tolerance (ctrl->device);cdev_device_del (&ctrl->cdev, ctrl->device); }
Yes, you are right.
I have worried that it will create a new 'req' if it has been free and set 'NULL'.
Thus, I try to hide it based on device->power.set_latency_tolerance.
Actually, I ignore the fact that dev_pm_qos_hide_latency_tolerance() will pass '-1' todev_pm_qos_update_user_latency_tolerance(). It will not create a new 'req'.
I will send patch v2 with the suggestion.
Yufen
Thanks a lot
Quoted 5 lines
drivers/nvme/host/core.c +0
-1EXPORT_SYMBOL_GPL (nvme_uninit_ctrl); --.