Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3002844iog; Mon, 20 Jun 2022 09:07:58 -0700 (PDT) X-Google-Smtp-Source: AGRyM1urUGbwENWFUenHw9M5rM+Rb5EkWgq0Kqnn3omGF9Uahy6jgt0lAbkuKSGr7YbfkuyinR95 X-Received: by 2002:a05:6402:1694:b0:435:79d2:a441 with SMTP id a20-20020a056402169400b0043579d2a441mr10523217edv.55.1655741278671; Mon, 20 Jun 2022 09:07:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655741278; cv=none; d=google.com; s=arc-20160816; b=Amj7R63Ujr94czOmYEYEcCHT+DXJGazWdQs77Hlavtg5BcF6+TscjT+qcoUcDMxC+e wUaFhP6Uptw6uByDMZwVRO5DaAd7aJ+/DqBZsVIds8T9SyLTAp/VUqKwQztrVSvttJW3 LcwBM1uqwDuUfIwG60mQcpXf+JSlIFXTBsET23ZRduoJzaR5S7OeiwrstRJIvJIZlkLB CZi9KzJzhVDivU66gqIUBHYnhbgZ2g5Ou2vgzm+mkJfUafEyS7iTH2G1PHTbCsxOo/TL sH3x/EeniG74jtxksPQI8y00cgPTvWUQwTquDWcrXrpyTd9wFwMgOXIM99eqVMQ7y5Et jfhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=mw9X237dDhEA8SQhqrDdsGk0AgCqi+ZByddZS6/UV+4=; b=qRD2x+uAjLV6crNd4TtxXl7CkdU1hEnc95d4eLqj9abqj9DzqJ5vQZJ1pIP+MlZmaZ j0xoY3VauMJXNwgBbYp8Vk8wOblQqouHfYQBBAcMD8UQYa6R6Dry5/iaotouXgeTgGKq 8nFLID/gOxJXaxCMwvHGG976ljB5+nRzyaghRkniYcj/JurQnjMI8qhEemyrQu/Qqqzu yMUbGDpxNUCT2FQymXBwWN0wi22uUtI7tafw6eJRKQOXSn+CM57BEFJZpygwZmG8Dun8 ziDtnhKh7qAy9sK10TiDGf3VNYynoLKSKxnaEfmeHe6+st79YyDbhdzDWAhq41eJY7ra 7syQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mihalicyn.com header.s=mihalicyn header.b="eRU88IF/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mihalicyn.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa42-20020a17090786aa00b00715790f718dsi12487696ejc.318.2022.06.20.09.07.31; Mon, 20 Jun 2022 09:07:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@mihalicyn.com header.s=mihalicyn header.b="eRU88IF/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mihalicyn.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241994AbiFTPrP (ORCPT + 99 others); Mon, 20 Jun 2022 11:47:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233412AbiFTPrM (ORCPT ); Mon, 20 Jun 2022 11:47:12 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF94D1AF06 for ; Mon, 20 Jun 2022 08:47:09 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id z11so9539861edp.9 for ; Mon, 20 Jun 2022 08:47:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mihalicyn.com; s=mihalicyn; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=mw9X237dDhEA8SQhqrDdsGk0AgCqi+ZByddZS6/UV+4=; b=eRU88IF/V8eG8wnibAb/BC04X+BsGb2lgYclIT+Z8D4ZCJZhKBS81EgW05qoLhICUi nqbRGYrFYyXPmhQGO//FiA0c+xRZ5m+/++eX0eKWMoZurJ/Jgts81ofRY14KmirSAb/0 XQNoXiL9yphJwKesG1xnnKgsK2Z7yqA+3ieic= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mw9X237dDhEA8SQhqrDdsGk0AgCqi+ZByddZS6/UV+4=; b=BmN7GvRsLM/1C8oO7aLxdsRAZ59Ru89kU1Mldbb+ZBIMjxSCzkOWVzezIly9NCp76h LPF6rKK8ygdlholYGmSJ48KqWcQ/l8afNL2NpblsAQsHr+Vxw91cU+zRpWjST9Nf6HPs +RA6A/vg4Ll+EXKqhIoiVbU8bvK5SSoFPzAOiqlgJYkeGcPeoK2OnjLIHn5uE+YQBabR nTKeLFh7+NZNQ5UmvGL8bQ5EvYLv7/gmnpEpK2qXkkf4AT2O9KrsX1vXFQ1ebFdjZKK8 ToXFXA9/rc9FuW1KUaD3C0yTRIzRmazPvPbeLK6d5wqyUJlkkphBTCsAKvg+petMenv4 TIgg== X-Gm-Message-State: AJIora8+7vNIiFnM/GQzaSt1HPKAuwVG73IkqnISodc3VaXc4k555eGC c6sA4MyQI9vd1yIE0lFJPZFhuXuvu7SdGe9Q85AOyw== X-Received: by 2002:a05:6402:170b:b0:435:5d22:7cd7 with SMTP id y11-20020a056402170b00b004355d227cd7mr22741876edu.383.1655740028462; Mon, 20 Jun 2022 08:47:08 -0700 (PDT) MIME-Version: 1.0 References: <20220618182515.95831-1-minhquangbui99@gmail.com> <258546133.12151.1655739550814.JavaMail.zimbra@efficios.com> In-Reply-To: <258546133.12151.1655739550814.JavaMail.zimbra@efficios.com> From: Alexander Mikhalitsyn Date: Mon, 20 Jun 2022 18:46:57 +0300 Message-ID: Subject: Re: [PATCH] rseq: x86: Fix rseq_cs get cleared when returning from signal handler To: Mathieu Desnoyers Cc: Bui Quang Minh , linux-kernel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86 , "H. Peter Anvin" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , "Paul E. McKenney" , Boqun Feng , Kees Cook , "Chang S. Bae" , "Eric W. Biederman" , Brian Gerst Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Mathieu! It comes from CRIU ;) We have seen periodic failures of our ZDTM test (rseq02): zdtm: add rseq02 transition test with NO_RESTART CS flag https://github.com/checkpoint-restore/criu/commit/db9ec13616625f98db95b00438a370f8a660bfee zdtm: temporary disable rseq02 test https://github.com/checkpoint-restore/criu/commit/ead227994b4918422496e06d3f3802188c091a7a Bui Quang Minh wanted to investigate this problem and come up with this kernel patch. Regards, Alex On Mon, Jun 20, 2022 at 6:39 PM Mathieu Desnoyers wrote: > > ----- On Jun 18, 2022, at 2:25 PM, Bui Quang Minh minhquangbui99@gmail.com wrote: > > > rseq.rseq_cs is not expected to be cleared when we are still in critical > > section. When using rseq with RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL, if the > > signal is delivered, we don't restart to the abort ip but go to signal > > handler then return and continue the critical section execution. While in > > signal handler, if a preemption, migration or another signal is delivered, > > we will fall into rseq_ip_fixup again. At that moment, the IP is in signal > > handler not in critical section so the rseq.rseq_cs is cleared. Later, when > > we return back to critical section from signal handlers, we get the > > rseq.rseq_cs incorrectly cleared. > > > > Another scenario is when a preemption or migration happens, then a signal > > is delivered. When preparing to return to user mode, in > > exit_to_user_mode_loop, we first handle the signal and set return IP to > > signal handler. After that, rseq_handle_notify_resume is called to handle > > the preemption/migration, sees the IP is the address of signal handler not > > in critical section and clear rseq.rseq_cs. > > > > To handle this case, a RSEQ_CS_FLAG_IN_SIGNAL_HANDLER is added. This flag > > is set in rseq.flags (it is easier to read and update rseq.flags than > > rseq.rseq_cs.flags) by the kernel when rseq does not restart on signal. > > When this flag is set, we don't clear rseq.rseq_cs when the IP is outside > > of critical section. The flag is unset in sigreturn path when the last > > signal handler returns back to the critical section. > > Hi, > > You raise valid points. > > May I first ask what is your use-case for RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL ? > > Considering the added per-arch complexity, I think we should consider whether > it's worthwhile to keep supporting this flag, and whether there are actual users > of it out there, or if we should handle this by deprecating this flag. > > Thanks, > > Mathieu > > > > > Reviewed-by: Alexander Mikhalitsyn > > Signed-off-by: Bui Quang Minh > > --- > > arch/x86/kernel/signal.c | 7 ++++ > > include/linux/sched.h | 5 +++ > > include/uapi/linux/rseq.h | 8 +++- > > kernel/rseq.c | 80 ++++++++++++++++++++++++++++++++++++--- > > 4 files changed, 94 insertions(+), 6 deletions(-) > > > > diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c > > index 9c7265b524c7..7aadf69d6849 100644 > > --- a/arch/x86/kernel/signal.c > > +++ b/arch/x86/kernel/signal.c > > @@ -646,6 +646,10 @@ SYSCALL_DEFINE0(sigreturn) > > */ > > if (!restore_sigcontext(regs, &frame->sc, 0)) > > goto badframe; > > + > > + if (rseq_sigreturn(regs)) > > + goto badframe; > > + > > return regs->ax; > > > > badframe: > > @@ -678,6 +682,9 @@ SYSCALL_DEFINE0(rt_sigreturn) > > if (restore_altstack(&frame->uc.uc_stack)) > > goto badframe; > > > > + if (rseq_sigreturn(regs)) > > + goto badframe; > > + > > return regs->ax; > > > > badframe: > > diff --git a/include/linux/sched.h b/include/linux/sched.h > > index c46f3a63b758..656b03232d0a 100644 > > --- a/include/linux/sched.h > > +++ b/include/linux/sched.h > > @@ -2340,6 +2340,8 @@ static inline void rseq_execve(struct task_struct *t) > > t->rseq_event_mask = 0; > > } > > > > +int rseq_sigreturn(struct pt_regs *regs); > > + > > #else > > > > static inline void rseq_set_notify_resume(struct task_struct *t) > > @@ -2365,6 +2367,9 @@ static inline void rseq_fork(struct task_struct *t, > > unsigned long clone_flags) > > static inline void rseq_execve(struct task_struct *t) > > { > > } > > +static inline int rseq_sigreturn(struct pt_regs *regs) > > +{ > > +} > > > > #endif > > > > diff --git a/include/uapi/linux/rseq.h b/include/uapi/linux/rseq.h > > index 77ee207623a9..f946af8e21e6 100644 > > --- a/include/uapi/linux/rseq.h > > +++ b/include/uapi/linux/rseq.h > > @@ -26,6 +26,7 @@ enum rseq_cs_flags_bit { > > RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT = 0, > > RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT = 1, > > RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT = 2, > > + RSEQ_CS_FLAG_IN_SIGNAL_HANDLER_BIT = 3, > > }; > > > > enum rseq_cs_flags { > > @@ -35,6 +36,8 @@ enum rseq_cs_flags { > > (1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT), > > RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE = > > (1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT), > > + RSEQ_CS_FLAG_IN_SIGNAL_HANDLER = > > + (1U << RSEQ_CS_FLAG_IN_SIGNAL_HANDLER_BIT), > > }; > > > > /* > > @@ -115,7 +118,7 @@ struct rseq { > > * Restartable sequences flags field. > > * > > * This field should only be updated by the thread which > > - * registered this data structure. Read by the kernel. > > + * registered this data structure. Read and set by the kernel. > > * Mainly used for single-stepping through rseq critical sections > > * with debuggers. > > * > > @@ -128,6 +131,9 @@ struct rseq { > > * - RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE > > * Inhibit instruction sequence block restart on migration for > > * this thread. > > + * - RSEQ_CS_FLAG_IN_SIGNAL_HANDLER > > + * Mark that this thread is in critical section but currently in > > + * signal handlers, so don't clear the rseq_cs field. > > */ > > __u32 flags; > > } __attribute__((aligned(4 * sizeof(__u64)))); > > diff --git a/kernel/rseq.c b/kernel/rseq.c > > index 97ac20b4f738..a4aca612724b 100644 > > --- a/kernel/rseq.c > > +++ b/kernel/rseq.c > > @@ -172,14 +172,16 @@ static int rseq_get_rseq_cs(struct task_struct *t, struct > > rseq_cs *rseq_cs) > > > > static int rseq_need_restart(struct task_struct *t, u32 cs_flags) > > { > > - u32 flags, event_mask; > > + u32 old_flags, flags, event_mask; > > int ret; > > > > /* Get thread flags. */ > > - ret = get_user(flags, &t->rseq->flags); > > + ret = get_user(old_flags, &t->rseq->flags); > > if (ret) > > return ret; > > > > + flags = old_flags; > > + > > /* Take critical section flags into account. */ > > flags |= cs_flags; > > > > @@ -203,6 +205,21 @@ static int rseq_need_restart(struct task_struct *t, u32 > > cs_flags) > > t->rseq_event_mask = 0; > > preempt_enable(); > > > > + /* > > + * If we don't restart on signal event, set the > > + * RSEQ_CS_FLAG_IN_SIGNAL_HANDLER flag so that later rseq_ip_fixup > > + * doesn't clear rseq_cs pointer as the IP is outside of critical > > + * section. > > + */ > > + if ((flags & RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL) && > > + (event_mask & RSEQ_EVENT_SIGNAL) && > > + !(old_flags & RSEQ_CS_FLAG_IN_SIGNAL_HANDLER)) { > > + old_flags |= RSEQ_CS_FLAG_IN_SIGNAL_HANDLER; > > + ret = put_user(old_flags, &t->rseq->flags); > > + if (ret) > > + return ret; > > + } > > + > > return !!(event_mask & ~flags); > > } > > > > @@ -248,10 +265,23 @@ static int rseq_ip_fixup(struct pt_regs *regs) > > /* > > * Handle potentially not being within a critical section. > > * If not nested over a rseq critical section, restart is useless. > > - * Clear the rseq_cs pointer and return. > > + * In case the RSEQ_CS_FLAG_IN_SIGNAL_HANDLER is set, we are in > > + * signal handlers and later return to critical section so don't > > + * clear rseq_cs pointer. > > + * Otherwise, clear the rseq_cs pointer and return. > > */ > > - if (!in_rseq_cs(ip, &rseq_cs)) > > - return clear_rseq_cs(t); > > + if (!in_rseq_cs(ip, &rseq_cs)) { > > + u32 flags; > > + > > + ret = get_user(flags, &t->rseq->flags); > > + if (ret) > > + return ret; > > + if (flags & RSEQ_CS_FLAG_IN_SIGNAL_HANDLER) > > + return 0; > > + else > > + return clear_rseq_cs(t); > > + } > > + > > ret = rseq_need_restart(t, rseq_cs.flags); > > if (ret <= 0) > > return ret; > > @@ -322,6 +352,46 @@ void rseq_syscall(struct pt_regs *regs) > > > > #endif > > > > +#ifdef CONFIG_RSEQ > > + > > +int rseq_sigreturn(struct pt_regs *regs) > > +{ > > + int ret; > > + struct rseq_cs rseq_cs; > > + struct task_struct *t = current; > > + > > + if (t->rseq) { > > + u32 flags; > > + > > + ret = get_user(flags, &t->rseq->flags); > > + if (ret) > > + return ret; > > + > > + if (flags & RSEQ_CS_FLAG_IN_SIGNAL_HANDLER) { > > + ret = rseq_get_rseq_cs(t, &rseq_cs); > > + if (ret) > > + return ret; > > + > > + /* > > + * If the returned IP is in critical section, it > > + * means we handle all the possible nested signal, > > + * so unset the RSEQ_CS_FLAG_IN_SIGNAL_HANDLER. > > + */ > > + if (in_rseq_cs(regs->ip, &rseq_cs)) { > > + flags &= ~RSEQ_CS_FLAG_IN_SIGNAL_HANDLER; > > + > > + ret = put_user(flags, &t->rseq->flags); > > + if (ret) > > + return ret; > > + } > > + } > > + } > > + > > + return 0; > > +} > > + > > +#endif > > + > > /* > > * sys_rseq - setup restartable sequences for caller thread. > > */ > > -- > > 2.25.1 > > -- > Mathieu Desnoyers > EfficiOS Inc. > http://www.efficios.com