From: Dmitry Vyukov <dvyukov@google.com>
To: Weizhao Ouyang <ouyangweizhao@zeku.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Alexander Potapenko <glider@google.com>,
Andrey Konovalov <andreyknvl@gmail.com>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
kasan-dev@googlegroups.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org,
Weizhao Ouyang <o451686892@gmail.com>,
Shuai Yuan <yuanshuai@zeku.com>, Peng Ren <renlipeng@zeku.com>
Subject: Re: [PATCH v2] kasan: fix deadlock in start_report()
Date: Thu, 9 Feb 2023 09:55:39 +0100 [thread overview]
Message-ID: <CACT4Y+Zrz4KOU82jjEperYOM0sEp6TCmgse4XVMPkwAkS+dXrA@mail.gmail.com> (raw)
In-Reply-To: <20230209031159.2337445-1-ouyangweizhao@zeku.com>
On Thu, 9 Feb 2023 at 04:27, Weizhao Ouyang <ouyangweizhao@zeku.com> wrote:
>
> From: Weizhao Ouyang <o451686892@gmail.com>
>
> From: Shuai Yuan <yuanshuai@zeku.com>
>
> Calling start_report() again between start_report() and end_report()
> will result in a race issue for the report_lock. In extreme cases this
> problem arose in Kunit tests in the hardware tag-based Kasan mode.
>
> For example, when an invalid memory release problem is found,
> kasan_report_invalid_free() will print error log, but if an MTE exception
> is raised during the output log, the kasan_report() is called, resulting
> in a deadlock problem. The kasan_depth not protect it in hardware
> tag-based Kasan mode.
I think checking report_suppressed() would be cleaner and simpler than
ignoring all trylock failures. If trylock fails, it does not mean that
the current thread is holding it. We of course could do a custom lock
which stores current->tid in the lock word, but it looks effectively
equivalent to checking report_suppressed().
> Signed-off-by: Shuai Yuan <yuanshuai@zeku.com>
> Reviewed-by: Weizhao Ouyang <ouyangweizhao@zeku.com>
> Reviewed-by: Peng Ren <renlipeng@zeku.com>
> ---
> Changes in v2:
> -- remove redundant log
>
> mm/kasan/report.c | 25 ++++++++++++++++++++-----
> 1 file changed, 20 insertions(+), 5 deletions(-)
>
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index 22598b20c7b7..aa39aa8b1855 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -166,7 +166,7 @@ static inline void fail_non_kasan_kunit_test(void) { }
>
> static DEFINE_SPINLOCK(report_lock);
>
> -static void start_report(unsigned long *flags, bool sync)
> +static bool start_report(unsigned long *flags, bool sync)
> {
> fail_non_kasan_kunit_test();
> /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
> @@ -175,8 +175,13 @@ static void start_report(unsigned long *flags, bool sync)
> lockdep_off();
> /* Make sure we don't end up in loop. */
> kasan_disable_current();
> - spin_lock_irqsave(&report_lock, *flags);
> + if (!spin_trylock_irqsave(&report_lock, *flags)) {
> + lockdep_on();
> + kasan_enable_current();
> + return false;
> + }
> pr_err("==================================================================\n");
> + return true;
> }
>
> static void end_report(unsigned long *flags, void *addr)
> @@ -468,7 +473,10 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_ty
> if (unlikely(!report_enabled()))
> return;
>
> - start_report(&flags, true);
> + if (!start_report(&flags, true)) {
> + pr_err("%s: report ignore\n", __func__);
> + return;
> + }
>
> memset(&info, 0, sizeof(info));
> info.type = type;
> @@ -503,7 +511,11 @@ bool kasan_report(unsigned long addr, size_t size, bool is_write,
> goto out;
> }
>
> - start_report(&irq_flags, true);
> + if (!start_report(&irq_flags, true)) {
> + ret = false;
> + pr_err("%s: report ignore\n", __func__);
> + goto out;
> + }
>
> memset(&info, 0, sizeof(info));
> info.type = KASAN_REPORT_ACCESS;
> @@ -536,7 +548,10 @@ void kasan_report_async(void)
> if (unlikely(!report_enabled()))
> return;
>
> - start_report(&flags, false);
> + if (!start_report(&flags, false)) {
> + pr_err("%s: report ignore\n", __func__);
> + return;
> + }
> pr_err("BUG: KASAN: invalid-access\n");
> pr_err("Asynchronous fault: no details available\n");
> pr_err("\n");
> --
> 2.25.1
>
> --
> You received this message because you are subscribed to the Google Groups "kasan-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20230209031159.2337445-1-ouyangweizhao%40zeku.com.
next parent reply other threads:[~2023-02-09 8:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20230209031159.2337445-1-ouyangweizhao@zeku.com>
2023-02-09 8:55 ` Dmitry Vyukov [this message]
[not found] ` <93b94f59016145adbb1e01311a1103f8@zeku.com>
2023-02-09 10:44 ` Dmitry Vyukov
2023-02-09 22:54 ` Andrey Konovalov
[not found] ` <b058a424e46d4f94a1f2fdc61292606b@zeku.com>
2023-02-15 13:22 ` 袁帅(Shuai Yuan)
2023-02-27 2:13 ` Andrey Konovalov
2023-02-28 16:09 ` Catalin Marinas
2023-02-28 21:50 ` Andrey Konovalov
2023-03-01 16:59 ` Catalin Marinas
2023-03-10 23:42 ` Andrey Konovalov
2023-03-15 16:14 ` Catalin Marinas
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=CACT4Y+Zrz4KOU82jjEperYOM0sEp6TCmgse4XVMPkwAkS+dXrA@mail.gmail.com \
--to=dvyukov@google.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=o451686892@gmail.com \
--cc=ouyangweizhao@zeku.com \
--cc=renlipeng@zeku.com \
--cc=ryabinin.a.a@gmail.com \
--cc=vincenzo.frascino@arm.com \
--cc=yuanshuai@zeku.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