Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp912388pxa; Wed, 12 Aug 2020 17:02:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxszXGuLh5yPGxxujSrJ5PvT5koQSANgcGgGIwxksnhgh0qi8r4UrSW/QOqk2r4Z8WZkfqp X-Received: by 2002:a17:906:4d89:: with SMTP id s9mr2182621eju.365.1597276928645; Wed, 12 Aug 2020 17:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597276928; cv=none; d=google.com; s=arc-20160816; b=uVi4yMr+9ZvNQFU4TCTY1zooF1+t/YmzqCOQtmS7C6StJdsP5XzRpWy/fWfEwsEIn6 ZhWZlFMH0WRpUCRTIwAih9QS+fj/YddnG2aLZu2LbyAhHE/7QWZdnvmbB8d8bDlPm7XG H2vI84ptAilKX7iZxVmYY7/KXrtxdpEx/Am+g/z5Vm798o//zaWFcBnC8E5zmoPsN0+T 4SHfMGVIsMeEACFyxprR8Qg/7iTVrX4yLZvm7N3L01S8sTy7sNERpodkJVxucprpo50W 2NnoCCkTUliLz3f34mscwVESzjFp8Jjw+c2bA3k99940M/IzVJLBvX+Vr1He7k1KNWA5 u6pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=YZ0DPU+7FEgTW1kdKvFkaE475aoyAvXltQCD2JpTtaU=; b=Z6MRMm85uI4rVgen8wT71lQsJOgyr+TSc0ePDEVYeG0drgMByzN+bO4dH7l+R5N1t2 KtXm6mk6J4ggVNk9AbDLBFmv1u/5/7o7q/3gIb11Ot1ZarQmQnKY/KXC+x5LCuAYRU1c l+TVt3y4QU1WJO5sqGLyXqQ7ZzZi9YnKKQxFQ25kXU2ZZbPy9hcpDMRlT+nzSncjS4xx 8X0Vm0uNxkYH0jbFjYLL+HDmOflIKqfb58RMwNXGgnyvo7Crx0YnVhGtOR7ru760yacn BfL/HlBNjCDO0s3HaO0xq1wcA8j8S47NXWIiIEGepx5urEwyPEV5VGopgtEqhkecQO7A M2Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cADRa2L+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m29si2618078edb.298.2020.08.12.17.01.45; Wed, 12 Aug 2020 17:02:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cADRa2L+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726611AbgHMAAG (ORCPT + 99 others); Wed, 12 Aug 2020 20:00:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726518AbgHMAAD (ORCPT ); Wed, 12 Aug 2020 20:00:03 -0400 Received: from mail-vs1-xe42.google.com (mail-vs1-xe42.google.com [IPv6:2607:f8b0:4864:20::e42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92434C061384 for ; Wed, 12 Aug 2020 17:00:02 -0700 (PDT) Received: by mail-vs1-xe42.google.com with SMTP id i129so2027226vsi.3 for ; Wed, 12 Aug 2020 17:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=YZ0DPU+7FEgTW1kdKvFkaE475aoyAvXltQCD2JpTtaU=; b=cADRa2L+LNtmOC8nGDW4NEBh1LKPUNQBVbSJ2lRlPO7/ex575XERs6bkt8ye0WQ36q nKfPHAfk093ngCTZeIOX3r4GXkJbFYEonLHqKYPfvuBmjEDniJ+DDci92BugIbisNdPp AwJF9BUpjxQHhED+vrE0T9EHVEbtGvjX8N8kc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=YZ0DPU+7FEgTW1kdKvFkaE475aoyAvXltQCD2JpTtaU=; b=fQ19ueLs+0t2XQnkEQinBULdC3sPVKiFws9EJvg5hoRtQ9CnN62FO8r+G6cyuvhJN5 8pa5EySG1H2oC0dDLU80LfSIu0/yybx6n+jna+50JkEXHlIVkH4DS3YUtUuKWKyFC0wb VHs1QOwfGR3A4R5BBoMgXOE2FqMYECF8N7QtVF8V4WdYVHpNZ3WvReomGXDhAeQ13gqw YgA8Er8vWzzkORSTZ+NEeN9FZ0m3Asc8Uq0AyFp1m67BdDrSfncgivznar9NYX9t5sXV AXliAUf+s9HsPsPwm65MZihLWqMTGNkkFoHPbZGevlKLKwA4R724NcvachBFflp4rlV/ Nw6A== X-Gm-Message-State: AOAM532N8ovb0V4TIZ6oPVzjMIY0cyog0cqIkhaOK3vfUMQzmoGZ4VkH xKudNFJnAEAIXDHtI8PgzcTXgrc+qMs= X-Received: by 2002:a67:3015:: with SMTP id w21mr1241595vsw.99.1597276801375; Wed, 12 Aug 2020 17:00:01 -0700 (PDT) Received: from mail-vk1-f175.google.com (mail-vk1-f175.google.com. [209.85.221.175]) by smtp.gmail.com with ESMTPSA id 61sm32083uau.16.2020.08.12.17.00.00 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 12 Aug 2020 17:00:00 -0700 (PDT) Received: by mail-vk1-f175.google.com with SMTP id x187so900127vkc.1 for ; Wed, 12 Aug 2020 17:00:00 -0700 (PDT) X-Received: by 2002:a1f:fc02:: with SMTP id a2mr1543414vki.65.1597276799887; Wed, 12 Aug 2020 16:59:59 -0700 (PDT) MIME-Version: 1.0 References: <1595333413-30052-1-git-send-email-sumit.garg@linaro.org> <1595333413-30052-2-git-send-email-sumit.garg@linaro.org> In-Reply-To: <1595333413-30052-2-git-send-email-sumit.garg@linaro.org> From: Doug Anderson Date: Wed, 12 Aug 2020 16:59:48 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC 1/5] tty/sysrq: Make sysrq handler NMI aware To: Sumit Garg Cc: Greg Kroah-Hartman , Daniel Thompson , linux-serial@vger.kernel.org, kgdb-bugreport@lists.sourceforge.net, Jiri Slaby , Russell King - ARM Linux , Jason Wessel , LKML , Linux ARM Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Tue, Jul 21, 2020 at 5:10 AM Sumit Garg wrote: > > In a future patch we will add support to the serial core to make it > possible to trigger a magic sysrq from an NMI context. Prepare for this > by marking some sysrq actions as NMI safe. Safe actions will be allowed > to run from NMI context whilst that cannot run from an NMI will be queued > as irq_work for later processing. > > A particular sysrq handler is only marked as NMI safe in case the handler > isn't contending for any synchronization primitives as in NMI context > they are expected to cause deadlocks. Note that the debug sysrq do not > contend for any synchronization primitives. It does call kgdb_breakpoint() > to provoke a trap but that trap handler should be NMI safe on > architectures that implement an NMI. > > Signed-off-by: Sumit Garg > --- > drivers/tty/sysrq.c | 33 ++++++++++++++++++++++++++++++++- > include/linux/sysrq.h | 1 + > kernel/debug/debug_core.c | 1 + > 3 files changed, 34 insertions(+), 1 deletion(-) > > diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c > index 7c95afa9..8017e33 100644 > --- a/drivers/tty/sysrq.c > +++ b/drivers/tty/sysrq.c > @@ -50,6 +50,8 @@ > #include > #include > #include > +#include > +#include > > #include > #include > @@ -111,6 +113,7 @@ static const struct sysrq_key_op sysrq_loglevel_op = { > .help_msg = "loglevel(0-9)", > .action_msg = "Changing Loglevel", > .enable_mask = SYSRQ_ENABLE_LOG, > + .nmi_safe = true, > }; > > #ifdef CONFIG_VT > @@ -157,6 +160,7 @@ static const struct sysrq_key_op sysrq_crash_op = { > .help_msg = "crash(c)", > .action_msg = "Trigger a crash", > .enable_mask = SYSRQ_ENABLE_DUMP, > + .nmi_safe = true, > }; > > static void sysrq_handle_reboot(int key) > @@ -170,6 +174,7 @@ static const struct sysrq_key_op sysrq_reboot_op = { > .help_msg = "reboot(b)", > .action_msg = "Resetting", > .enable_mask = SYSRQ_ENABLE_BOOT, > + .nmi_safe = true, > }; > > const struct sysrq_key_op *__sysrq_reboot_op = &sysrq_reboot_op; > @@ -217,6 +222,7 @@ static const struct sysrq_key_op sysrq_showlocks_op = { > .handler = sysrq_handle_showlocks, > .help_msg = "show-all-locks(d)", > .action_msg = "Show Locks Held", > + .nmi_safe = true, > }; > #else > #define sysrq_showlocks_op (*(const struct sysrq_key_op *)NULL) > @@ -289,6 +295,7 @@ static const struct sysrq_key_op sysrq_showregs_op = { > .help_msg = "show-registers(p)", > .action_msg = "Show Regs", > .enable_mask = SYSRQ_ENABLE_DUMP, > + .nmi_safe = true, > }; > > static void sysrq_handle_showstate(int key) > @@ -326,6 +333,7 @@ static const struct sysrq_key_op sysrq_ftrace_dump_op = { > .help_msg = "dump-ftrace-buffer(z)", > .action_msg = "Dump ftrace buffer", > .enable_mask = SYSRQ_ENABLE_DUMP, > + .nmi_safe = true, > }; > #else > #define sysrq_ftrace_dump_op (*(const struct sysrq_key_op *)NULL) > @@ -538,6 +546,23 @@ static void __sysrq_put_key_op(int key, const struct sysrq_key_op *op_p) > sysrq_key_table[i] = op_p; > } > > +#define SYSRQ_NMI_FIFO_SIZE 64 > +static DEFINE_KFIFO(sysrq_nmi_fifo, int, SYSRQ_NMI_FIFO_SIZE); A 64-entry FIFO seems excessive. Quite honestly even a FIFO seems a bit excessive and it feels like if two sysrqs were received in super quick succession that it would be OK to just process the first one. I guess if it simplifies the processing to have a FIFO then it shouldn't hurt, but no need for 64 entries. > +static void sysrq_do_nmi_work(struct irq_work *work) > +{ > + const struct sysrq_key_op *op_p; > + int key; > + > + while (kfifo_out(&sysrq_nmi_fifo, &key, 1)) { > + op_p = __sysrq_get_key_op(key); > + if (op_p) > + op_p->handler(key); > + } Do you need to manage "suppress_printk" in this function? Do you need to call rcu_sysrq_start() and rcu_read_lock()? If so, how do you prevent racing between the mucking we're doing with these things and the mucking that the NMI does with them? > +} > + > +static DEFINE_IRQ_WORK(sysrq_nmi_work, sysrq_do_nmi_work); > + > void __handle_sysrq(int key, bool check_mask) > { > const struct sysrq_key_op *op_p; > @@ -568,7 +593,13 @@ void __handle_sysrq(int key, bool check_mask) > if (!check_mask || sysrq_on_mask(op_p->enable_mask)) { > pr_info("%s\n", op_p->action_msg); > console_loglevel = orig_log_level; > - op_p->handler(key); > + > + if (in_nmi() && !op_p->nmi_safe) { > + kfifo_in(&sysrq_nmi_fifo, &key, 1); Rather than kfifo_in() and kfifo_out(), I think you can use kfifo_put() and kfifo_get(). As I understand it those just get/put one element which is what you want. > + irq_work_queue(&sysrq_nmi_work); Wishful thinking, but (as far as I can tell) irq_work_queue() only queues work on the CPU running the NMI. I don't have lots of NMI experience, but any chance there is a variant that will queue work on any CPU? Then sysrq handlers that aren't NMI aware will be more likely to work. > + } else { > + op_p->handler(key); > + } > } else { > pr_info("This sysrq operation is disabled.\n"); > console_loglevel = orig_log_level; > diff --git a/include/linux/sysrq.h b/include/linux/sysrq.h > index 3a582ec..630b5b9 100644 > --- a/include/linux/sysrq.h > +++ b/include/linux/sysrq.h > @@ -34,6 +34,7 @@ struct sysrq_key_op { > const char * const help_msg; > const char * const action_msg; > const int enable_mask; > + const bool nmi_safe; > }; > > #ifdef CONFIG_MAGIC_SYSRQ > diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c > index 9e59347..2b51173 100644 > --- a/kernel/debug/debug_core.c > +++ b/kernel/debug/debug_core.c > @@ -943,6 +943,7 @@ static const struct sysrq_key_op sysrq_dbg_op = { > .handler = sysrq_handle_dbg, > .help_msg = "debug(g)", > .action_msg = "DEBUG", > + .nmi_safe = true, > }; > #endif > > -- > 2.7.4 >