Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp78335ybs; Tue, 26 May 2020 04:13:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyv3SMKydPbZJDIfStXPQApMnjQFrf9bZKYMV9LrkzrgDOww2xaLDHL9GuaIPYerfC53f+5 X-Received: by 2002:a17:906:2c08:: with SMTP id e8mr596569ejh.385.1590491618400; Tue, 26 May 2020 04:13:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590491618; cv=none; d=google.com; s=arc-20160816; b=N3scJLSgC++GcS5EdiAgecbiozGue2hUGXuXUhAcwymBao48zaRq/1vOca/ksXKVJF kWt4Z10nzohqK9gEJ7A1ddlEBL5HSggrzUTd4CmcKeD1pgJ+GCrWM0uy3M8dQ2wkrR28 L/+EDgRXpTZ0Tmu2XHu/hbS/L9IDVEuq185utvgF3opNor4AAoQQOQoXhJsDI6VauLeN Be7MUTDvM8NowXnRdE3ZvwXr4WvPkCJnOggGki5SWS7D4v5i/uJ8CzSLq162MFyNRY+V nuTrTreswtgvNUvgxLaz5EOnGvmdj6XBVr4k3sMAW8zCS4S9ZkRfvavkuRQ7jVHbcfoG l19g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=6RAM8Sj52MEn9Pq3/CfMGq4xvMrRRJlBrM0wddYyE9s=; b=m/52cFh7PY6Iwbdlfo0DQ0QDrjTalxzMq5HU+L9zaRj1Kfames0U4jp0KzpjEUyC08 tBVvlSYDQKMs7ZGlr6fUTJ3XUsi4bATW1veNK0V5/Q4MU9ZYPiV9oLhgkUXAGoouh/cq F53LigYddtZbPhkApVj7joO10SjVC37vkH1Jt4zQW51NLntWl1cexw+fHpmguSoYYKTF yl+AskjSaTOfWgEHsYizT/1T7+n0yk999E2ORb9/gedhlnjfhLrSi8AkxURnEsgIne0n 0Dw/7E8JCAruFgVzThPWgcXaNo5yyYfYwNyOSVdAAL73MFwu2Y/goMjzFEUEEt0bfbN5 QUxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eRChNU0B; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q12si11936234edc.413.2020.05.26.04.13.12; Tue, 26 May 2020 04:13:38 -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=@linaro.org header.s=google header.b=eRChNU0B; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388762AbgEZLK4 (ORCPT + 99 others); Tue, 26 May 2020 07:10:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388683AbgEZLK4 (ORCPT ); Tue, 26 May 2020 07:10:56 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 932F7C03E97E for ; Tue, 26 May 2020 04:10:54 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id e1so19988473wrt.5 for ; Tue, 26 May 2020 04:10:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6RAM8Sj52MEn9Pq3/CfMGq4xvMrRRJlBrM0wddYyE9s=; b=eRChNU0BPToFg/f5+T8hjxG/g/Qj0asd99BaepOtZNY6qh3JapmOtcjj0vCtg0Le7J 8/0hzVo3Pdh09jpgC3fnV1NO+enuLsphHdxU5+jYaJuTpFHpnbyP4mLE4P2nbkW7IjZn XdpaD9mpikrOBNjW6CiccEJqEkQVAThQI3CC+zC80S8bSZPAkHcPQFedtFS3W/6W8s8B gwC/AKo9eb5VTSPOV3NInocOLF3cW2nidD2Zusd7reLENYZkIAJP6GRcgaioaC76zpfU wzANgk2QVhBxEQXk2915zmtpXu+P8qqlSoTrp5+TtRiNJdkz0zP9dNboTjGyfMDtTC/x tUNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6RAM8Sj52MEn9Pq3/CfMGq4xvMrRRJlBrM0wddYyE9s=; b=q5GpKknr8Wv7NiHOviJUulavbKv95lDqhLgep5r68Zwb8OkSO5lJwkG61mXEj3WrXz 8hek7dRKepR+9C6/bN8sbwtK4GbYGxXpc9Ha7mOo4j8RFD20g5zVNCWQcwQ+GsIFyRdm vCefFJfAzEBgcoD5mpl2oWK+jJGKEZENzbSQWhpznbAudzO+T7e831XL0gZTSrI+zjQ6 lKL6OkU/xvTOTy/uzHspsd4Pwwmr53pdDh8/oj0PsCMFXvnlgDvaILe2pRzUhKhbH23T 4h8b4xS5bvEBFvvTMLgJGyjVvjfBhI2aB0q0iPwGgXtzJstosJrAKTpcp/tQyyMIKILN nP8g== X-Gm-Message-State: AOAM532Nmyf6G2/DIFwUXwgyiz15J10lDMtbjE1GTN/WBiUwgkIkDxYx SW0TdizyhkPkdM/xaVvwitJutg== X-Received: by 2002:adf:df03:: with SMTP id y3mr11531546wrl.376.1590491452888; Tue, 26 May 2020 04:10:52 -0700 (PDT) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id d4sm20656648wre.22.2020.05.26.04.10.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 04:10:52 -0700 (PDT) Date: Tue, 26 May 2020 12:10:50 +0100 From: Daniel Thompson To: Sumit Garg Cc: kgdb-bugreport@lists.sourceforge.net, Jason Wessel , Douglas Anderson , Petr Mladek , Sergey Senozhatsky , Linux Kernel Mailing List Subject: Re: [RFC] kdb: Switch kdb_printf to use safer console poll APIs Message-ID: <20200526111050.qfvdlw3jp2gokktg@holly.lan> References: <1590158071-15325-1-git-send-email-sumit.garg@linaro.org> <20200522160258.yq63iigp74u3ngtn@holly.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 26, 2020 at 01:16:17PM +0530, Sumit Garg wrote: > On Fri, 22 May 2020 at 21:33, Daniel Thompson > wrote: > > > > On Fri, May 22, 2020 at 08:04:31PM +0530, Sumit Garg wrote: > > > In kgdb NMI context, polling driver APIs are more safer to use instead > > > of console APIs since the polling drivers know they will execute from > > > all sorts of crazy places. And for the most common use cases this would > > > also result in no console handler ever being called. So switch to use > > > polling driver APIs in case a particular console supports polling mode. > > > > This comment seems rather half hearted, not least because it doesn't > > explain what the current problem is nor why using the polling API is > > safer. > > > > TBH, some sentences in the above comment were borrowed from your > suggestion here [1]. But I agree that it doesn't portray the complete > picture. So how about: > > ==== > In kgdb NMI context, calling console handlers isn't safe due to locks > used in those handlers which could lead to a deadlock. Although, using > oops_in_progress increases the chance to bypass locks in most console > handlers but it might not be sufficient enough in case a console uses > more locks (VT/TTY is good example). > > So instead switch to use lockless polling driver APIs in case a > particular console supports polling mode which is common for most kdb > use-cases and would result in no console handler ever being called. > ==== Better, although the later paragraph still seems rather vague to me. Compare to: Currently when a driver provides both polling I/O and a console then kdb will output using the console. We can increase robustness by using the currently active polling I/O driver (which should be lockless) instead of the corresponding console. For several common cases (e.g. an embedded system with a single serial port that is used both for console output and debugger I/O) this will result in no console handler being used. > [1] https://lkml.org/lkml/2020/5/20/356 > > > Compare the above against the advice in > > https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes > > and I think it comes up short. Perhaps also consider Ingo Molnar's much > > more concise suggestion on describing changes: > > > > : Please use the customary changelog style we use in the kernel: > > : " Current code does (A), this has a problem when (B). > > : We can improve this doing (C), because (D)." > > -- http://lkml.iu.edu/hypermail//linux/kernel/1311.1/01157.html > > Thanks for the pointers. > > > > > > > > Suggested-by: Daniel Thompson > > > Signed-off-by: Sumit Garg > > > --- > > > kernel/debug/kdb/kdb_io.c | 39 +++++++++++++++++++++++++++++++++------ > > > 1 file changed, 33 insertions(+), 6 deletions(-) > > > > > > diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c > > > index 3a5a068..8e0d581 100644 > > > --- a/kernel/debug/kdb/kdb_io.c > > > +++ b/kernel/debug/kdb/kdb_io.c > > > @@ -24,6 +24,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > #include "kdb_private.h" > > > > > > #define CMD_BUFLEN 256 > > > @@ -699,11 +700,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) > > > } > > > } > > > for_each_console(c) { > > > + int line; > > > + struct tty_driver *p; > > > + > > > if (!(c->flags & CON_ENABLED)) > > > continue; > > > - ++oops_in_progress; > > > - c->write(c, cp, retlen - (cp - kdb_buffer)); > > > - --oops_in_progress; > > > + p = c->device ? c->device(c, &line) : NULL; > > > + if (p && p->ops && p->ops->poll_put_char) { > > > > What prevents this logic from matching an active console that hasn't > > been selected as the polling driver? > > Yes you are correct and it could lead to invoking poll_put_char() > without poll_init(). And we couldn't invoke poll_init() here as that > still comes with locks and could sleep. So one way to overcome this > would be to pass selected polling driver via dbg_io_ops and use > polling APIs only if the underlying console driver matches that > polling driver. Agree. Note that this is all I ever expected to look at when I commented about before. > > > + len = retlen - (cp - kdb_buffer); > > > + cp2 = cp; > > > + while (len--) { > > > + p->ops->poll_put_char(p, line, *cp2); > > > + cp2++; > > > + } > > > > Assuming it is possible to identify the console that matches the > > currently selected polling driver can't we just drop the > > is_console test and get rid of this branch entirely. > > Have a look at my suggested approach above. > > > > > The only reason for the is_console test is to avoid issuing messages > > twice so if we are able to suppress the c->write() for the same UART > > then is_console check becomes pointless and can go. > > I did consider removing is_console check but it looks like it's not > only limited to polling drivers but also used at other places (see [1] > [2]) as well. > > [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/early/ehci-dbgp.c#n1061 > [2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/tty/serial/kgdb_nmi.c#n48 IIUC you mean that the logic to match devices only works for tty drivers and there examples are not tty drivers. This could probably be solved but no need to get too tied in knots. It's fine to keep the is_console check for now. However rather than replicate the polled I/O write code a third and fourth time lets get the I/O logic pulled out into proper functions. Daniel.