Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp424570pxa; Fri, 14 Aug 2020 07:54:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3g2gPHHAM0d6cAhKiFPSLb+XFgGtZ0s+hZlJ7hkGYszWpmCnzpXCyDOWJPMTt9t9epYyb X-Received: by 2002:aa7:cd46:: with SMTP id v6mr2467680edw.21.1597416894659; Fri, 14 Aug 2020 07:54:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597416894; cv=none; d=google.com; s=arc-20160816; b=aXSh42bDSV0MaOIoVm4daA9rsyJQ0ilzvcTnGw9rOEIawbuNYzchj02XmOqbyP+mSm iAmVsb8fNPgf988Z6IfZtUWgISGzJ5vOQOE6JHfmzVAyplhoe//IfMWhcSmBhzdUgH7x p9x0oax7j+EwiFq5qu9LNrVYPU9PKsGnEwM0p9hseewi2WhCI6OK6oVfQ0mvEIHTqgX4 bc1iFsS9Ftz6E7FcLJdkPwD7UCy38q/CNWhNR5280M+pHIlSurFBHj+G48+ilJv6zXP7 7IXBVg/uggCm+lM6jbuQfqKlYdHt2KaY/GNBZQPEtxHKdrRoceHeGK/II0TP+fsiiq/j 0Z1w== 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=Amv9QIiHfjtn876TD4rggI0HRc+PqFCwXxxaILu+uJE=; b=V0NpQnFgRK/U0pWTdy0bBPR47QvpMSXKyIQP2U32C1Gh7EHHXx4kbN4U2wDddxDoLF HXeJn25U3cB8ZBWcjxfB/xiWHP8BcYcKpSrN36eyI6EKrePlXBA+9oxh8wxCvwxejbyK WERMKE/XUSgTiGibuJe6Ea9LM5eOwwhySNNbpYTSzM46f2AkrpFeA+3Juue/b0mvkmBH qScLb0EKcxKBz991bk9yg0Gr2mwdYOooT0fIW9YqDgfL4j8NqIDhUsFXz8fw2NzfrnqT aldY8f22k0xztc9qb5A9sfD5YYtn7gurholiBZRG4OBQKXEuA97xz+CR7DK6NlanXM5n c0kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=oQUuPT7L; 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 bd9si5469468edb.340.2020.08.14.07.54.32; Fri, 14 Aug 2020 07:54:54 -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=oQUuPT7L; 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 S1728853AbgHNOoN (ORCPT + 99 others); Fri, 14 Aug 2020 10:44:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727074AbgHNOoM (ORCPT ); Fri, 14 Aug 2020 10:44:12 -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 23D12C061384 for ; Fri, 14 Aug 2020 07:44:12 -0700 (PDT) Received: by mail-vs1-xe42.google.com with SMTP id p8so4752882vsm.12 for ; Fri, 14 Aug 2020 07:44:12 -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=Amv9QIiHfjtn876TD4rggI0HRc+PqFCwXxxaILu+uJE=; b=oQUuPT7L0xdiY8xjAMJQ87XG8vQcYVqFZwy2yTZmgKZ7gpQCNP+W4dK54ss8qP8pIG LvxugT9QMOVE0N2m0V42xK+3b2aC3e3yeXG+TEI77xHH5usLHfjRPW1oNhv3gzH0Pqoz v2mjuN3J8RHveggm0kxOgNauW1U4mKDLhObdw= 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=Amv9QIiHfjtn876TD4rggI0HRc+PqFCwXxxaILu+uJE=; b=k4nH696V2iuTYq6hF/4cYEE4G5iXpYAtfuyxvWV68MXtaHMd3x4lQwe3sLaUkg0u1F 2EFBNq3e3tbw5NLydh4ZLpe4EI5GglTHvXN1FHvdtSS9W1qckJ9oh+TH83NqfOHVVt9h 7zvQWYWHu/jz54YajBa3aG0388pT+FC4IW9WdzQ6/kvNdi2oj1CQl6tGM6KXjQoWi8qU uZcxDRchXt8I5rgpvsuWtSaT0oqNRhCOKsu/kTYdNWVsBm7qXYKP5mw0DXQuMIXvDbar 5nfNP+8h69Fhrsx3K3dn5lfjmW3UwQaqgJbg6Bx0n5hMN+cHVbyLybDuQrFEzc7cvlac YCqg== X-Gm-Message-State: AOAM5305l53vZhRleJbjHPht+rYJcGvCEUZcO/P9MEyeKjcSGg/hBIKZ pafQYaYN07sHn1Glj488GwikX5HKm6P9xw== X-Received: by 2002:a67:f893:: with SMTP id h19mr1724259vso.158.1597416250654; Fri, 14 Aug 2020 07:44:10 -0700 (PDT) Received: from mail-vs1-f43.google.com (mail-vs1-f43.google.com. [209.85.217.43]) by smtp.gmail.com with ESMTPSA id k28sm1504772vko.3.2020.08.14.07.44.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Aug 2020 07:44:09 -0700 (PDT) Received: by mail-vs1-f43.google.com with SMTP id i129so4767423vsi.3 for ; Fri, 14 Aug 2020 07:44:09 -0700 (PDT) X-Received: by 2002:a67:d714:: with SMTP id p20mr1801455vsj.119.1597416248940; Fri, 14 Aug 2020 07:44:08 -0700 (PDT) MIME-Version: 1.0 References: <1595333413-30052-1-git-send-email-sumit.garg@linaro.org> <1595333413-30052-3-git-send-email-sumit.garg@linaro.org> In-Reply-To: From: Doug Anderson Date: Fri, 14 Aug 2020 07:43:57 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC 2/5] serial: core: Add framework to allow NMI aware serial drivers 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 Fri, Aug 14, 2020 at 4:17 AM Sumit Garg wrote: > > On Thu, 13 Aug 2020 at 20:08, Doug Anderson wrote: > > > > Hi, > > > > On Thu, Aug 13, 2020 at 7:19 AM Sumit Garg wrote: > > > > > > On Thu, 13 Aug 2020 at 05:29, Doug Anderson wrote: > > > > > > > > Hi, > > > > > > > > On Tue, Jul 21, 2020 at 5:11 AM Sumit Garg wrote: > > > > > > > > > > Add NMI framework APIs in serial core which can be leveraged by serial > > > > > drivers to have NMI driven serial transfers. These APIs are kept under > > > > > CONFIG_CONSOLE_POLL as currently kgdb initializing uart in polling mode > > > > > is the only known user to enable NMI driven serial port. > > > > > > > > > > The general idea is to intercept RX characters in NMI context, if those > > > > > are specific to magic sysrq then allow corresponding handler to run in > > > > > NMI context. Otherwise defer all other RX and TX operations to IRQ work > > > > > queue in order to run those in normal interrupt context. > > > > > > > > > > Also, since magic sysrq entry APIs will need to be invoked from NMI > > > > > context, so make those APIs NMI safe via deferring NMI unsafe work to > > > > > IRQ work queue. > > > > > > > > > > Signed-off-by: Sumit Garg > > > > > --- > > > > > drivers/tty/serial/serial_core.c | 120 ++++++++++++++++++++++++++++++++++++++- > > > > > include/linux/serial_core.h | 67 ++++++++++++++++++++++ > > > > > 2 files changed, 185 insertions(+), 2 deletions(-) > > > > > > > > > > diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c > > > > > index 57840cf..6342e90 100644 > > > > > --- a/drivers/tty/serial/serial_core.c > > > > > +++ b/drivers/tty/serial/serial_core.c > > > > > @@ -3181,8 +3181,14 @@ static bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch) > > > > > return true; > > > > > } > > > > > > > > > > +#ifdef CONFIG_CONSOLE_POLL > > > > > + if (in_nmi()) > > > > > + irq_work_queue(&port->nmi_state.sysrq_toggle_work); > > > > > + else > > > > > + schedule_work(&sysrq_enable_work); > > > > > +#else > > > > > schedule_work(&sysrq_enable_work); > > > > > - > > > > > +#endif > > > > > > > > It should be a very high bar to have #ifdefs inside functions. I > > > > don't think this meets it. Instead maybe something like this > > > > (untested and maybe slightly wrong syntax, but hopefully makes > > > > sense?): > > > > > > > > Outside the function: > > > > > > > > #ifdef CONFIG_CONSOLE_POLL > > > > #define queue_port_nmi_work(port, work_type) > > > > irq_work_queue(&port->nmi_state.work_type) > > > > #else > > > > #define queue_port_nmi_work(port, work_type) > > > > #endif > > > > > > > > ...and then: > > > > > > > > if (IS_ENABLED(CONFIG_CONSOLE_POLL) && in_nmi()) > > > > queue_port_nmi_work(port, sysrq_toggle_work); > > > > else > > > > schedule_work(&sysrq_enable_work); > > > > > > > > --- > > > > > > > > The whole double-hopping is really quite annoying. I guess > > > > schedule_work() can't be called from NMI context but can be called > > > > from IRQ context? So you need to first transition from NMI context to > > > > IRQ context and then go and schedule the work? Almost feels like we > > > > should just fix schedule_work() to do this double-hop for you if > > > > called from NMI context. Seems like you could even re-use the list > > > > pointers in the work_struct to keep the queue of people who need to be > > > > scheduled from the next irq_work? Worst case it seems like you could > > > > add a schedule_work_nmi() that would do all the hoops for you. ...but > > > > I also know very little about NMI so maybe I'm being naive. > > > > > > > > > > Thanks for this suggestion and yes indeed we could make > > > schedule_work() NMI safe and in turn get rid of all this #ifdefs. Have > > > a look at below changes: > > > > > > diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h > > > index 26de0ca..1daf1b4 100644 > > > --- a/include/linux/workqueue.h > > > +++ b/include/linux/workqueue.h > > > @@ -14,6 +14,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > > > > struct workqueue_struct; > > > > > > @@ -106,6 +107,7 @@ struct work_struct { > > > #ifdef CONFIG_LOCKDEP > > > struct lockdep_map lockdep_map; > > > #endif > > > + struct irq_work iw; > > > > Hrm, I was thinking you could just have a single queue per CPU then > > you don't need to add all this extra data to every single "struct > > work_struct". I was thinking you could use the existing list node in > > the "struct work_struct" to keep track of the list of things. ...but > > maybe my idea this isn't actually valid because the linked list might > > be in use if we're scheduling work that's already pending / running? > > > > In any case, I worry that people won't be happy with the extra > > overhead per "struct work_struct". Can we reduce it at all? It still > > does feel like you could get by with a single global queue and thus > > you wouldn't need to store the function pointer and flags with every > > "struct work_struct", right? So all you'd need is a single pointer > > for the linked list? I haven't actually tried implementing this, > > though, so I could certainly be wrong. > > Let me try to elaborate here: > > Here we are dealing with 2 different layers of deferring work, one is > irq_work (NMI safe) using "struct irq_work" and other is normal > workqueue (NMI unsafe) using "struct work_struct". > > So when we are in NMI context, the only option is to use irq_work to > defer work and need to pass reference to "struct irq_work". Now in > following irq_work function: > > +void queue_work_nmi(struct irq_work *iw) > +{ > + struct work_struct *work = container_of(iw, struct work_struct, iw); > + > + queue_work(system_wq, work); > +} > +EXPORT_SYMBOL(queue_work_nmi); > > we can't find a reference to "struct work_struct" until there is 1:1 > mapping with "struct irq_work". So we require a way to establish this > mapping and having "struct irq_work" as part of "struct work_struct" > tries to achieve that. If you have any better way to achieve this, I > can use that instead. So I guess the two options to avoid the overhead are: 1. Create a new struct: struct nmi_queuable_work_struct { struct work_struct work; struct irq_work iw; }; Then the overhead is only needed for those that want this functionality. Those people would need to use a variant nmi_schedule_work() which, depending on in_nmi(), would either schedule it directly or use the extra work. Looks like Daniel already responded and suggested this. 2. Something that duplicates the code of at least part of irq_work and therefore saves the need to store the function pointer. Think of it this way: if you made a whole copy of irq_work that was hardcoded to just call the function you wanted then you wouldn't need to store a function pointer. This is, of course, excessive. I was trying to figure out if you could do less by only copying the NMI-safe linked-list manipulation, but this is probably impossible and not worth it anyway. -Doug