io-uring.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] io-wq: fix memory leak in create_io_worker()
@ 2021-09-09  4:05 Hao Xu
  2021-09-09 12:57 ` Jens Axboe
  0 siblings, 1 reply; 6+ messages in thread
From: Hao Xu @ 2021-09-09  4:05 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

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

Reported-by: syzbot+65454c239241d3d647da@syzkaller.appspotmail.com
Fixes: 3146cba99aa2 ("io-wq: make worker creation resilient against signals")
Signed-off-by: Hao Xu <haoxu@linux.alibaba.com>
---
 fs/io-wq.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index d80e4a735677..382efca4812b 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -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);
-- 
2.24.4


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] io-wq: fix memory leak in create_io_worker()
  2021-09-09  4:05 [PATCH] io-wq: fix memory leak in create_io_worker() Hao Xu
@ 2021-09-09 12:57 ` Jens Axboe
  2021-09-09 13:00   ` Hao Xu
  0 siblings, 1 reply; 6+ messages in thread
From: Jens Axboe @ 2021-09-09 12:57 UTC (permalink / raw)
  To: Hao Xu; +Cc: io-uring, Pavel Begunkov, Joseph Qi

On 9/8/21 10:05 PM, Hao Xu wrote:
> 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.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] io-wq: fix memory leak in create_io_worker()
  2021-09-09 12:57 ` Jens Axboe
@ 2021-09-09 13:00   ` Hao Xu
  2021-09-09 15:41     ` Jens Axboe
  0 siblings, 1 reply; 6+ messages in thread
From: Hao Xu @ 2021-09-09 13:00 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring, Pavel Begunkov, Joseph Qi

在 2021/9/9 下午8:57, Jens Axboe 写道:
> On 9/8/21 10:05 PM, Hao Xu wrote:
>> 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.
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] io-wq: fix memory leak in create_io_worker()
  2021-09-09 13:00   ` Hao Xu
@ 2021-09-09 15:41     ` Jens Axboe
  0 siblings, 0 replies; 6+ messages in thread
From: Jens Axboe @ 2021-09-09 15:41 UTC (permalink / raw)
  To: Hao Xu; +Cc: io-uring, Pavel Begunkov, Joseph Qi

On 9/9/21 7:00 AM, Hao Xu wrote:
> 在 2021/9/9 下午8:57, Jens Axboe 写道:
>> On 9/8/21 10:05 PM, Hao Xu wrote:
>>> 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.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] io-wq: fix memory leak in create_io_worker()
  2021-09-09 11:58 qiang.zhang
@ 2021-09-09 12:57 ` Jens Axboe
  0 siblings, 0 replies; 6+ messages in thread
From: Jens Axboe @ 2021-09-09 12:57 UTC (permalink / raw)
  To: qiang.zhang, asml.silence; +Cc: io-uring, linux-kernel

On 9/9/21 5:58 AM, qiang.zhang@windriver.com wrote:
> From: "Qiang.zhang" <qiang.zhang@windriver.com>
> 
> BUG: memory leak
> unreferenced object 0xffff888126fcd6c0 (size 192):
>   comm "syz-executor.1", pid 11934, jiffies 4294983026 (age 15.690s)
>   backtrace:
>     [<ffffffff81632c91>] kmalloc_node include/linux/slab.h:609 [inline]
>     [<ffffffff81632c91>] kzalloc_node include/linux/slab.h:732 [inline]
>     [<ffffffff81632c91>] create_io_worker+0x41/0x1e0 fs/io-wq.c:739
>     [<ffffffff8163311e>] io_wqe_create_worker fs/io-wq.c:267 [inline]
>     [<ffffffff8163311e>] io_wqe_enqueue+0x1fe/0x330 fs/io-wq.c:866
>     [<ffffffff81620b64>] io_queue_async_work+0xc4/0x200 fs/io_uring.c:1473
>     [<ffffffff8162c59c>] __io_queue_sqe+0x34c/0x510 fs/io_uring.c:6933
>     [<ffffffff8162c7ab>] io_req_task_submit+0x4b/0xa0 fs/io_uring.c:2233
>     [<ffffffff8162cb48>] io_async_task_func+0x108/0x1c0 fs/io_uring.c:5462
>     [<ffffffff816259e3>] tctx_task_work+0x1b3/0x3a0 fs/io_uring.c:2158
>     [<ffffffff81269b43>] task_work_run+0x73/0xb0 kernel/task_work.c:164
>     [<ffffffff812dcdd1>] tracehook_notify_signal include/linux/tracehook.h:212 [inline]
>     [<ffffffff812dcdd1>] handle_signal_work kernel/entry/common.c:146 [inline]
>     [<ffffffff812dcdd1>] exit_to_user_mode_loop kernel/entry/common.c:172 [inline]
>     [<ffffffff812dcdd1>] exit_to_user_mode_prepare+0x151/0x180 kernel/entry/common.c:209
>     [<ffffffff843ff25d>] __syscall_exit_to_user_mode_work kernel/entry/common.c:291 [inline]
>     [<ffffffff843ff25d>] syscall_exit_to_user_mode+0x1d/0x40 kernel/entry/common.c:302
>     [<ffffffff843fa4a2>] do_syscall_64+0x42/0xb0 arch/x86/entry/common.c:86
>     [<ffffffff84600068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
> 
> when create_io_thread() return error, and not retry, the worker object
> need to be freed.

Thanks, this one looks complete.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] io-wq: fix memory leak in create_io_worker()
@ 2021-09-09 11:58 qiang.zhang
  2021-09-09 12:57 ` Jens Axboe
  0 siblings, 1 reply; 6+ messages in thread
From: qiang.zhang @ 2021-09-09 11:58 UTC (permalink / raw)
  To: axboe, asml.silence; +Cc: io-uring, linux-kernel

From: "Qiang.zhang" <qiang.zhang@windriver.com>

BUG: memory leak
unreferenced object 0xffff888126fcd6c0 (size 192):
  comm "syz-executor.1", pid 11934, jiffies 4294983026 (age 15.690s)
  backtrace:
    [<ffffffff81632c91>] kmalloc_node include/linux/slab.h:609 [inline]
    [<ffffffff81632c91>] kzalloc_node include/linux/slab.h:732 [inline]
    [<ffffffff81632c91>] create_io_worker+0x41/0x1e0 fs/io-wq.c:739
    [<ffffffff8163311e>] io_wqe_create_worker fs/io-wq.c:267 [inline]
    [<ffffffff8163311e>] io_wqe_enqueue+0x1fe/0x330 fs/io-wq.c:866
    [<ffffffff81620b64>] io_queue_async_work+0xc4/0x200 fs/io_uring.c:1473
    [<ffffffff8162c59c>] __io_queue_sqe+0x34c/0x510 fs/io_uring.c:6933
    [<ffffffff8162c7ab>] io_req_task_submit+0x4b/0xa0 fs/io_uring.c:2233
    [<ffffffff8162cb48>] io_async_task_func+0x108/0x1c0 fs/io_uring.c:5462
    [<ffffffff816259e3>] tctx_task_work+0x1b3/0x3a0 fs/io_uring.c:2158
    [<ffffffff81269b43>] task_work_run+0x73/0xb0 kernel/task_work.c:164
    [<ffffffff812dcdd1>] tracehook_notify_signal include/linux/tracehook.h:212 [inline]
    [<ffffffff812dcdd1>] handle_signal_work kernel/entry/common.c:146 [inline]
    [<ffffffff812dcdd1>] exit_to_user_mode_loop kernel/entry/common.c:172 [inline]
    [<ffffffff812dcdd1>] exit_to_user_mode_prepare+0x151/0x180 kernel/entry/common.c:209
    [<ffffffff843ff25d>] __syscall_exit_to_user_mode_work kernel/entry/common.c:291 [inline]
    [<ffffffff843ff25d>] syscall_exit_to_user_mode+0x1d/0x40 kernel/entry/common.c:302
    [<ffffffff843fa4a2>] do_syscall_64+0x42/0xb0 arch/x86/entry/common.c:86
    [<ffffffff84600068>] entry_SYSCALL_64_after_hwframe+0x44/0xae

when create_io_thread() return error, and not retry, the worker object
need to be freed.

Reported-by: syzbot+65454c239241d3d647da@syzkaller.appspotmail.com
Signed-off-by: Qiang.zhang <qiang.zhang@windriver.com>
---
 fs/io-wq.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/io-wq.c b/fs/io-wq.c
index 35e7ee26f7ea..27fa0506c1a6 100644
--- a/fs/io-wq.c
+++ b/fs/io-wq.c
@@ -709,6 +709,7 @@ static void create_worker_cont(struct callback_head *cb)
 		}
 		raw_spin_unlock(&wqe->lock);
 		io_worker_ref_put(wqe->wq);
+		kfree(worker);
 		return;
 	}
 
@@ -725,6 +726,7 @@ static void io_workqueue_create(struct work_struct *work)
 	if (!io_queue_worker_create(worker, acct, create_worker_cont)) {
 		clear_bit_unlock(0, &worker->create_state);
 		io_worker_release(worker);
+		kfree(worker);
 	}
 }
 
@@ -759,6 +761,7 @@ 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))) {
+		kfree(worker);
 		goto fail;
 	} else {
 		INIT_WORK(&worker->work, io_workqueue_create);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-09-09 15:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-09  4:05 [PATCH] io-wq: fix memory leak in create_io_worker() Hao Xu
2021-09-09 12:57 ` Jens Axboe
2021-09-09 13:00   ` Hao Xu
2021-09-09 15:41     ` Jens Axboe
2021-09-09 11:58 qiang.zhang
2021-09-09 12:57 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).