Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5290749ybl; Tue, 10 Dec 2019 03:42:33 -0800 (PST) X-Google-Smtp-Source: APXvYqy15hGwoi7z2cfsPlYshx+JlLUpVU2c4d6AYY1qPebXwrwRIwckHmkAvUPMM3C1TPhxpGx9 X-Received: by 2002:a05:6830:1e37:: with SMTP id t23mr25683731otr.16.1575978153849; Tue, 10 Dec 2019 03:42:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575978153; cv=none; d=google.com; s=arc-20160816; b=sLQyC1itf5NXKVwByrRUHAYEzl7I2vMchiVcB0Lk+XPE/MKIDmKgngDYz0VGB4Fntk SToD/GndWf4Mm15Z9GkpDTEFC83Ah5CJFrgGhRxilQQov8ED/lPt/yRYI3QG29YJHule Fh6/MYVkMY3dF910lyLUPZxrlqXSM9oMfLIUNJyqAB0SBHAFq/XAa1EojcX8jeczHfJa 6IO7bPbhDklYYZJkw58BiVqrQFz8DbEz6q8AqREYzvFc4zh7CD9Xg5L3UXTN2M/FcR2h JMEanhsVwzB8RIA7PTye+WcTYPZnJ5SSC+1To5pNxp3+bon8xgfYHEwkijyEVT6GZkrb b8ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=CCNCtyIpljxGbglOKFnRn5m+itMarmJl7+8snb+zxSE=; b=bho7u+CZYVFGQzaOWs0bikBFKjUg+GmOMebIZ6IM1+DUu3JTfZa8hySGHGMHPOvfTB gCgJHJpxB92Ds1UwotvU3rI+K9t1g8OxhRIjaCr4VKLyQ5J12LVLoDt4AsfGYDr5cCHK 4U6QwTuib6/ZccowcnWK8Q5kBYwW0BFDg2Ih0EVn211851eBGM+x6u+qe4i2tSITb0aB w3Cz3YNzSxNcvkKHuYLermEqxl0sFRTnNZOnneUjV64a7aFyaPGJ4c7M8uMzm4FI6Hm0 nYE6Hrk8EIm/vGY4friSRM6la9H52Mv3rycWc4HvSAwCDVeIV5fa2A6a71FuUic9LpU0 hwuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PBybQrbT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f65si1687058oib.0.2019.12.10.03.42.21; Tue, 10 Dec 2019 03:42:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PBybQrbT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727562AbfLJLlw (ORCPT + 99 others); Tue, 10 Dec 2019 06:41:52 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40412 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727249AbfLJLlv (ORCPT ); Tue, 10 Dec 2019 06:41:51 -0500 Received: by mail-wm1-f66.google.com with SMTP id t14so2745553wmi.5 for ; Tue, 10 Dec 2019 03:41:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=CCNCtyIpljxGbglOKFnRn5m+itMarmJl7+8snb+zxSE=; b=PBybQrbTFnbA6gaSClKcbfpJusLBtFBY3aqxZYLgVvz8j2iCSQxIDJTk8vRJdyX5IQ yC70f61tkLJPxnwNmw7KQ2A/w7luHlokBJxIi754fSUB76TUsD/z6FHiR12gz7wU3+ss GnXisc0hHIgmFv4DZw2cfU7U+QuQbMtE4HsyuJS0iUOteQrsFHJB13L+dqngPzLKfu+s Nnj0G3ywBceqZSP+bYXPQlUms1lhNJC9uL2dwy6mEpSIJibGX8ROmEcLmMQ/mNFPqoWf 62vJNmPFjihxUuiTOZ1UnpDe2D1gt24ZR5vGd4OYo8gkggv72rrquEdWS2NdaTNuHPst mWUw== 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:user-agent; bh=CCNCtyIpljxGbglOKFnRn5m+itMarmJl7+8snb+zxSE=; b=kvbM7PA47HunbQG3RkpPiNpJqF7gZ96wUJWYgQJf1D01SoHYZBD5VGWFb+GvQUaXOA +eok/q5suTRFzjS/h/3SJH63nGg30na+b2yJPL4Hbuk2z+aRKSgB8ZyC38F2GCHZKT+R 819pqGHrSzqP4iFrzZgdbJsUUcQFbGod258zF36uH8uyhVz9A8Dvr09bYpQidFGbJYQV ohFoqhRryc7CwVc5PLeQoYIuIDEtNC163nL1NoJyo3KmjnP91FqOg1MvJ1bBz1JWJWuZ IjSVk+11gA9H2Gk9czXDkdXSSRB+pAaAbBa3ZE3BhT1rzybSWS66owGCmkTODxmYi2IW KV2Q== X-Gm-Message-State: APjAAAX7DFmZ5T4rCVU5Hyqwp0Q9+eDjvvHRvTkvQRxKfkhb+ziu+QOt pOl0EUNq1X0loG66b587rSQ= X-Received: by 2002:a7b:c778:: with SMTP id x24mr4664994wmk.119.1575978110010; Tue, 10 Dec 2019 03:41:50 -0800 (PST) Received: from debian ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id z83sm1612038wmg.2.2019.12.10.03.41.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 10 Dec 2019 03:41:49 -0800 (PST) Date: Tue, 10 Dec 2019 11:41:47 +0000 From: Sudip Mukherjee To: Jiri Slaby Cc: Greg Kroah-Hartman , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] tty: use tty_init_dev_retry() to workaround a race condition Message-ID: <20191210114147.ivple4ccr4bj6c4h@debian> References: <20191121152239.28405-1-sudipm.mukherjee@gmail.com> <20191121152239.28405-2-sudipm.mukherjee@gmail.com> <20191121164138.GD651886@kroah.com> <20191121210155.limd7v6cpd5yz2e7@debian> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="xyokhrw7wuwg5wwa" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --xyokhrw7wuwg5wwa Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi Jiri, On Fri, Nov 22, 2019 at 10:05:09AM +0100, Jiri Slaby wrote: > On 21. 11. 19, 22:01, Sudip Mukherjee wrote: > > Hi Greg, > > > > On Thu, Nov 21, 2019 at 05:41:38PM +0100, Greg Kroah-Hartman wrote: > >> On Thu, Nov 21, 2019 at 03:22:39PM +0000, Sudip Mukherjee wrote: > >>> There seems to be a race condition in tty drivers and I could see on > >>> many boot cycles a NULL pointer dereference as tty_init_dev() tries to > >>> do 'tty->port->itty = tty' even though tty->port is NULL. > >>> > >>> uart_add_one_port() registers the console, as soon as it registers, the > >>> userspace tries to use it and that leads to tty_open() but > >>> uart_add_one_port() has not yet done tty_port_link_device() and so > >>> tty->port is not yet configured when control reaches tty_init_dev(). > >> > >> Shouldn't we do tty_port_link_device() before uart_add_one_port() to > >> remove that race? Once you register the console, yes, tty_open() can > >> happen, so the driver had better be ready to go at that point in time. > >> > > > > But tty_port_link_device() is done by uart_add_one_port() itself. > > After registering the console uart_add_one_port() will call > > tty_port_register_device_attr_serdev() and tty_port_link_device() is > > called from this. Thats still tty core. > > Interferences of console vs tty code are ugly. Does it help to simply > put tty_port_link_device to uart_add_one_port before uart_configure_port? sorry for the late response, got busy with an out-of-tree driver. It fixes the problem if I put tty_port_link_device() before uart_configure_port(). Please check the attached patch and that completely fixes the problem. Do you want me to send a proper patch for it or do you want me to check more into it? -- Regards Sudip --xyokhrw7wuwg5wwa Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename=patch diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index 351843f847c0..006d478a63be 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -2820,6 +2820,7 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) if (uport->cons && uport->dev) of_console_check(uport->dev->of_node, uport->cons->name, uport->line); + tty_port_link_device(port, drv->tty_driver, uport->line); uart_configure_port(drv, state, uport); port->console = uart_console(uport); diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c index a9e12b3bc31d..a4f85fc75539 100644 --- a/drivers/tty/tty_port.c +++ b/drivers/tty/tty_port.c @@ -89,7 +89,8 @@ void tty_port_link_device(struct tty_port *port, { if (WARN_ON(index >= driver->num)) return; - driver->ports[index] = port; + if (!driver->ports[index]) + driver->ports[index] = port; } EXPORT_SYMBOL_GPL(tty_port_link_device); --xyokhrw7wuwg5wwa--