Skip to contentSkip to conversation

io-wq: fix memory leak in create_io_worker()

PATCH In review Hao Xu 2 participants 4 messages Sep 9, 2021

Subsystems (2)
lore
Share & tools
lore

Hao Xu

author
#0 lore raw

PATCH 1/1 io-wq: fix memory leak in create_io_worker()

We should free memory the variable worker point to in fail path.

Reported-by [email protected]
Fixes 3146cba99aa2 ("io-wq: make worker creation resilient against signals")
---
 fs/io-wq.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
fs/io-wq.c +10 -10
index d80e4a735677..382efca4812b 100644
@@ -737,15 +737,8 @@ static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
 	__set_current_state(TASK_RUNNING);
 
 	worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
-	if (!worker) {
-fail:
-		atomic_dec(&acct->nr_running);
-		raw_spin_lock(&wqe->lock);
-		acct->nr_workers--;
-		raw_spin_unlock(&wqe->lock);
-		io_worker_ref_put(wq);
-		return false;
-	}
+	if (!worker)
+		goto fail;
 
 	refcount_set(&worker->ref, 1);
 	worker->wqe = wqe;
@@ -759,7 +752,14 @@ static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
 	if (!IS_ERR(tsk)) {
 		io_init_new_worker(wqe, worker, tsk);
 	} else if (!io_should_retry_thread(PTR_ERR(tsk))) {
-		goto fail;
+		kfree(worker);
+fail:
+		atomic_dec(&acct->nr_running);
+		raw_spin_lock(&wqe->lock);
+		acct->nr_workers--;
+		raw_spin_unlock(&wqe->lock);
+		io_worker_ref_put(wq);
+		return false;
 	} else {
 		INIT_WORK(&worker->work, io_workqueue_create);
 		schedule_work(&worker->work);
Signature · 1 line
2.24.4
maintainer
#1 lore raw
Quoted 1 line · Hao Xu

We should free memory the variable worker point to in fail path.

I think this one is missing a few paths where it can also happen, once punted.

Signature · 1 line
Jens Axboe
author
#2 lore raw
Quoted 5 lines · Jens Axboe
Quoted 1 line · Hao Xu

We should free memory the variable worker point to in fail path.

I think this one is missing a few paths where it can also happen, once punted.

True. I browse the code again and I think Qiang.zhang's patch should be fine.

Quoted 1 line
maintainer
#3 lore raw
Quoted 8 lines · Hao Xu
Quoted 5 lines · Jens Axboe
Quoted 1 line · Hao Xu

We should free memory the variable worker point to in fail path.

I think this one is missing a few paths where it can also happen, once punted.

True. I browse the code again and I think Qiang.zhang's patch should be fine.

Yes agree, that one looks complete. I have applied it.

Signature · 1 line
Jens Axboe