Linux ACPI
 help / color / mirror / Atom feed
From: Wupeng Ma <mawupeng1@huawei.com>
To: <rafael@kernel.org>, <tony.luck@intel.com>, <bp@alien8.de>,
	<guohanjun@huawei.com>, <mchehab@kernel.org>,
	<xueshuai@linux.alibaba.com>, <lenb@kernel.org>,
	<jonathan.cameron@huawei.com>, <jane.chu@oracle.com>,
	<jarkko@kernel.org>, <wangkefeng.wang@huawei.com>
Cc: <mawupeng1@huawei.com>, <linux-kernel@vger.kernel.org>,
	<linux-acpi@vger.kernel.org>
Subject: [PATCH] ACPI: APEI: check return value of task_work_add to prevent memory leaks
Date: Fri, 17 Apr 2026 14:50:21 +0800	[thread overview]
Message-ID: <20260417065021.4067113-1-mawupeng1@huawei.com> (raw)

task_work_add() can fail with -ESRCH if the target task is exiting.
When it fails, the caller must handle the error and free any allocated
resources.

ghes_do_memory_failure() allocates a twcb structure from ghes_estatus_pool
before calling task_work_add(). If task_work_add() fails, twcb is leaked.

This can happen due to a race during task exit:

  do_exit()
    exit_mm()           # current->mm cleared
    exit_task_work()    # task->task_works = &work_exited

ghes_do_memory_failure() checks current->mm before allocating twcb,
but exit_task_work() may run before task_work_add() completes.  At that
point task->task_works == &work_exited, causing task_work_add() to fail.

Fixes the leak by checking the return value and freeing twcb on failure.

Fixes: c1f1fda14137 ("ACPI: APEI: handle synchronous exceptions in task work")
Signed-off-by: Wupeng Ma <mawupeng1@huawei.com>
---
 drivers/acpi/apei/ghes.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
index 8acd2742bb27d..4ffe65ecf4a87 100644
--- a/drivers/acpi/apei/ghes.c
+++ b/drivers/acpi/apei/ghes.c
@@ -520,8 +520,11 @@ static bool ghes_do_memory_failure(u64 physical_addr, int flags)
 		twcb->pfn = pfn;
 		twcb->flags = flags;
 		init_task_work(&twcb->twork, memory_failure_cb);
-		task_work_add(current, &twcb->twork, TWA_RESUME);
-		return true;
+		if (!task_work_add(current, &twcb->twork, TWA_RESUME))
+			return true;
+
+		gen_pool_free(ghes_estatus_pool, (unsigned long)twcb, sizeof(*twcb));
+		return false;
 	}
 
 	memory_failure_queue(pfn, flags);
-- 
2.43.0


             reply	other threads:[~2026-04-17  7:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-17  6:50 Wupeng Ma [this message]
2026-04-21  9:02 ` [PATCH] ACPI: APEI: check return value of task_work_add to prevent memory leaks Hanjun Guo
2026-04-21  9:18   ` mawupeng
2026-04-29  8:16     ` Hanjun Guo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260417065021.4067113-1-mawupeng1@huawei.com \
    --to=mawupeng1@huawei.com \
    --cc=bp@alien8.de \
    --cc=guohanjun@huawei.com \
    --cc=jane.chu@oracle.com \
    --cc=jarkko@kernel.org \
    --cc=jonathan.cameron@huawei.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=rafael@kernel.org \
    --cc=tony.luck@intel.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=xueshuai@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox