Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp696091ybz; Fri, 1 May 2020 07:00:06 -0700 (PDT) X-Google-Smtp-Source: APiQypI679o+3rSDyKkdwj1P/fnjm8GVDxNiE2NsXl3NsCDwlOA8NQSoOiTA6B1udZuX4hZznI2d X-Received: by 2002:a05:6402:30bb:: with SMTP id df27mr3444003edb.267.1588341606279; Fri, 01 May 2020 07:00:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588341606; cv=none; d=google.com; s=arc-20160816; b=WkGCiOaUUdflzx7JQuB84x6zZ2Hapvvkmd7m+XhToGLrIGVons0xI4tqrJkl+0QkTH pvogZkPizvCZuOOt4t7yfnfSHAChIDh34B3n3Anpf5hm67Z/4b/Fmd9/NAWjC58qT8my ysRx5Zv22wGc3w0fSHUYlBwv71Yv1qJPU7ZKzIhRLeUnEd/uyYKAQhnWXNLxGNvjssnY ayHhxrS86pIBWZ777CsDHgODSeQHGx+z15qs/5OCVQe0lbdSWwd9e5FVteKkBmFRCYz6 KLwJUQARgSiV+SdW60FeWMGA3I5ty63kX4tHgpYfC1cQQAyn2+24+q3vPULd/xhkt9Jf bATg== 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=CwB8SxSSgbYRqBwwUAkIWpdYejHT7kvKRjFIv7ghtlc=; b=RfVSXb2Mu6X4sjGGYsdh7ntTwnG9BLY3B5gVBBefsB/4BDjOiEdhP0+ZKjISDiuMPm uBcWdBb3EL9DGa7UGMV2FvI+F2bjpACahNvGwJpwp6+KensjJuZcv8KfZQeJ6S2bUL2n QBMPs0V933IC41wiRPgBcDs5sZdhenAVoWr6fgWWoNYrRrE7RG245h9Vf0MDIPBmrSRw gwx10PQcCS6dyKjGFL2MHw3jzWAkCgTyL6XDeVmZx+i8cWBXW3r887/bd2OBvKeK/Z3a 1QogJBUZz1INWdtX5TqC3LEYys0vu7RN8Xhufk8DwH7wfvtMCipgjYdrDOxSleCxll6I pvPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Zv2DUkeX; 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 a11si1564531edx.62.2020.05.01.06.59.42; Fri, 01 May 2020 07:00:06 -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=Zv2DUkeX; 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 S1730259AbgEANcL (ORCPT + 99 others); Fri, 1 May 2020 09:32:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1729353AbgEANcG (ORCPT ); Fri, 1 May 2020 09:32:06 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40510C061A0E for ; Fri, 1 May 2020 06:32:06 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id z6so6342715wml.2 for ; Fri, 01 May 2020 06:32:06 -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=CwB8SxSSgbYRqBwwUAkIWpdYejHT7kvKRjFIv7ghtlc=; b=Zv2DUkeX0KLYIj51PZCGPiis13gH28irsAEo3T69bAM2sws/ALOnM9thSDsY6gT3V1 oVXhXrxAjKRN23sgEFKZQ9pz9KA6bEthwgNAWdgX60GselC+r04QNerVpTnj14R9aODI raczwgYvDHF4z015qXs+dQh0HxLXopjDOY4daNDxMu6eJBsgMuwyBLAiAIgBPGhWVDCQ SMY3gVjIB4jSDTYghOJjk+nD6RFc0vycq9GbTlZUmtICrD/ImCajAjuHnoBMuTWeS0Ld /CasZuOzbRVvH4grnHn8i1jSu8PJWpkg12tu0QVgVg0Z/2PBSa0Bwa3VxQ0kBxd8zj0p rR+A== 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=CwB8SxSSgbYRqBwwUAkIWpdYejHT7kvKRjFIv7ghtlc=; b=QooiAFL3zxhW3IMW9vuTSWjLbuUk8EBbNBYFfsApxc2dt6AFVGXAbvMQ1RA2ZY9gpr 3fBZI14ONG4tXSTnF9xYPNr1X5Dl+4iLDf0QJahDNnED6+bSV7skNlr22SIF5UY/Qavr oa3rdJPx2/9F0YnMiQ9a2l+Nq72YcREyliySql4KXnFg1Hcg8fcmKzY4QFLtQhZFTdqF UXeJxqrqi6+yPWb6xYLB4EoG0lr+M8c1KMMBE7gHV2lbPMlrYYw38KS0aVQnfq4WYXAk Na83ci02oTW+8CkinRJYudwBTy0cfIGdBzoJeHBSSmwU+W1xNHdBWu3XmaJqK3XU1YNm mlAA== X-Gm-Message-State: AGi0Pubp54OwWeeA170Q04PwK0P9bZUuvXuLKQm8Dqh0Hm8vtoO3UpD9 sKQrnGX52UmGzNUK8619lO5gmQ== X-Received: by 2002:a1c:9d0d:: with SMTP id g13mr4251299wme.102.1588339924824; Fri, 01 May 2020 06:32:04 -0700 (PDT) Received: from wychelm.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id t17sm4293272wro.2.2020.05.01.06.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 May 2020 06:32:03 -0700 (PDT) Date: Fri, 1 May 2020 14:32:02 +0100 From: Daniel Thompson To: Douglas Anderson Cc: Jason Wessel , sumit.garg@linaro.org, Greg Kroah-Hartman , Jiri Slaby , kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Subject: Re: [PATCH] kgdboc: Be a bit more robust about handling earlycon leaving Message-ID: <20200501133202.GA2402281@wychelm.lan> References: <20200430095819.1.Id37f71c69eb21747b9d9e2c7c242be130814b362@changeid> <20200501114943.ioetuxe24gi27bll@holly.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200501114943.ioetuxe24gi27bll@holly.lan> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 01, 2020 at 12:49:43PM +0100, Daniel Thompson wrote: > On Thu, Apr 30, 2020 at 09:59:09AM -0700, Douglas Anderson wrote: > > The original implementation of kgdboc_earlycon included a comment > > about how it was impossible to get notified about the boot console > > going away without making changes to the Linux core. Since folks > > often don't want to change the Linux core for kgdb's purposes, the > > kgdboc_earlycon implementation did a bit of polling to figure out when > > the boot console went away. > > > > It turns out, though, that it is possible to get notified about the > > boot console going away. The solution is either clever or a hack > > depending on your viewpoint. ...or, perhaps, a clever hack. All we > > need to do is head-patch the "exit" routine of the boot console. We > > know that "struct console" must be writable because it has a "next" > > pointer in it, so we can just put our own exit routine in, do our > > stuff, and then call back to the original. > > I think I'm in the hack camp on this one! > > > > This works great to get notified about the boot console going away. > > The (slight) problem is that in the context of the boot console's exit > > routine we can't call tty_find_polling_driver(). > > I presume this is something to do with the tty_mutex? > > > > We solve this by > > kicking off some work on the system_wq when we get notified and this > > works pretty well. > > There are some problems with the workqueue approach. ... so did a couple of experiments to avoid the workqueue. It occured to me that, since we have interfered with deinit() then the console hasn't actually been uninitialized meaning we could still use it. This does exposes us to risks similar to keep_bootcon but in exchange there is no window where kgdb is broken (and no need to panic). My prototype is minimal but I did wonder about ripping out all the code to defend against removal of the earlycon and simply keep the earlycon around until a new kgdbio handler is installed. Daniel. diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c index 596213272ec3..05d58f9f38b1 100644 --- a/drivers/tty/serial/kgdboc.c +++ b/drivers/tty/serial/kgdboc.c @@ -46,6 +46,7 @@ static struct platform_device *kgdboc_pdev; #ifdef CONFIG_KGDB_SERIAL_CONSOLE static struct kgdb_io kgdboc_earlycon_io_ops; struct console *earlycon; +static int (*earlycon_orig_exit)(struct console *con); #else /* ! CONFIG_KGDB_SERIAL_CONSOLE */ #define earlycon NULL #endif /* ! CONFIG_KGDB_SERIAL_CONSOLE */ @@ -478,25 +479,28 @@ static void kgdboc_earlycon_put_char(u8 chr) earlycon->write(earlycon, &chr, 1); } -static void kgdboc_earlycon_pre_exp_handler(void) +static int kgdboc_earlycon_deferred_exit(struct console *con) { /* - * We don't get notified when the boot console is unregistered. - * Double-check when we enter the debugger. Unfortunately we - * can't really unregister ourselves now, so we panic. We rely - * on kgdb's ability to detect re-entrancy to make the panic - * take effect. - * - * NOTE: if you're here in the lull when the real console has - * replaced the boot console but our init hasn't run yet it's - * possible that the "keep_bootcon" argument may help. + * restoring the exit function ensures the earlycon is + * deinitialized when/if we find a suitable tty */ - if (earlycon && !is_earlycon_still_valid()) - panic("KGDB earlycon vanished and nothing replaced it\n"); + con->exit = earlycon_orig_exit; + + return 0; } static void kgdboc_earlycon_deinit(void) { + if (!earlycon) + return; + + if (earlycon->exit == kgdboc_earlycon_deferred_exit) { + earlycon->exit = earlycon_orig_exit; + } else if (earlycon->exit) { + earlycon->exit(earlycon); + } + earlycon = NULL; } @@ -504,7 +508,6 @@ static struct kgdb_io kgdboc_earlycon_io_ops = { .name = "kgdboc_earlycon", .read_char = kgdboc_earlycon_get_char, .write_char = kgdboc_earlycon_put_char, - .pre_exception = kgdboc_earlycon_pre_exp_handler, .deinit = kgdboc_earlycon_deinit, .is_console = true, }; @@ -562,6 +565,9 @@ static int __init kgdboc_earlycon_init(char *opt) return 0; } + earlycon_orig_exit = con->exit; + con->exit = kgdboc_earlycon_deferred_exit; + return 0; } early_param("kgdboc_earlycon", kgdboc_earlycon_init);