Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp138323rdh; Tue, 6 Feb 2024 23:22:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNt228CZ8SahEH+5OhX9cux4qRrp64M6W5wyaGZefIC52X5xJqdT5upy70V+vSHi+3P6zR X-Received: by 2002:ac8:44aa:0:b0:42c:3708:bdca with SMTP id a10-20020ac844aa000000b0042c3708bdcamr3149326qto.43.1707290552967; Tue, 06 Feb 2024 23:22:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707290552; cv=pass; d=google.com; s=arc-20160816; b=nThegSsV7btdUONWp/f0aR9o74BL1EMbwkTvYPMVUdjp4aFujcRib8RMkze0cc9xRS /B1/nzoDd2KasHB7RLc/KaAZzBUak+pWSFEOKmRjE0fOszZ8k84lZZ3nbwNBrD4KaAMd y/E1M3upmsU1wnuxoMMVlDDPSDF0XN/fvst8mlJBydZ22Y2+V4QVLRj02KgJEwpVhtSD VWVn+F881PYgeXkA86qDJ1arxGUnkGMDjUS8UZfa7ub5CVt46fFShgqgwBz4vuUzEX9H yauhRmfA4/SV1gYV5MK9Fm39S/Kgnt5ScoMyPMsHCqKgC6DNhml+56YCMHX9Rks20urM YZCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:date:message-id:from :references:to:subject:cc; bh=PD5+sRSKUp0OlFmxCdZ2okKsKx+pxxWrprWA05f2Dm0=; fh=Ce/0v7wLHI8JD5av11t5ocSJARAW6omSQQDkjVrPgR4=; b=OkoSCQD3cTNwjyEaCv8VH74V5HKZ23RhLO4/xcb+yIWhuLjHxRYhXkme2YiZ19r4S3 PlkthoKnYSCCNOaWAgnLwpEnAYDEZLnqjE6YAPW5tUcJ5mQ9OLRbR0B/G7gejr/lg1Zv 3TGh0/0e6wHxjBHMM11iL+LX/t8S80GtYe8zLKZIzl7G+j+SEABSRX9ZTjRHYrKCbQ46 z87AjfDtKdFb5NqxYlsg0opRr8uM72wXyiPAuYkEZvABzn9nMIzifiV/SwqsV0cfslcx eZTvWk7tXQ8AwHVHGRRhaX6V1qOnmxBSdbSipsYPLft2+4RnkaCaE4IvpKt0181lPOty miug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-56064-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56064-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com X-Forwarded-Encrypted: i=2; AJvYcCUo0kSWQH4DnZCOIFsliC+EwPmJL96kS9+zF8y809675kPve/vmI9HGG7iOUt9YhMTLHf8oMQJnwicwmVT9OfQY6rHrSSP8O3wctWndcg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id c21-20020ac85a95000000b0042aabb03753si620467qtc.48.2024.02.06.23.22.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 23:22:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56064-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-56064-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56064-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 9667F1C22870 for ; Wed, 7 Feb 2024 07:22:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CE49D1F606; Wed, 7 Feb 2024 07:22:25 +0000 (UTC) Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36FF41F5F0; Wed, 7 Feb 2024 07:22:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707290545; cv=none; b=j1T96wd+O0pZqkyhQOeerdLUYoGsed3tLHI/awhmZ8efW/W6thRqaeEZwc5/10b2WFCDFkmcssUnGsQCO1BWo7U/wLWnOluNBKI6qmgbSScPLvVRiq0OLHR/UxV3zwZ0k4H6iJq94IfbiMqRZ3omQr+LP5j/K3afL8qS0MdyIRs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707290545; c=relaxed/simple; bh=VVvRFG/eiBuKl5tvWu7HLckqb1fOLvTugxdSX+bh1nQ=; h=CC:Subject:To:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=sRKk+B9YIks+7Mh3AhlSUa5DSaflzYGQ7JNVcgOR+nLekzGTl3322sHqgqMRdsUMQl4QqNMOaNtSO8cE31HNamMHZOKKJBjVUundbFr+lbFj9N8T2leUQM5kdDdhLZVZQEQbq0w3ETqv0S3XVeWvip2L8KTrnKH0aoraio/64rI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4TVBN62G5mz1gydY; Wed, 7 Feb 2024 15:20:22 +0800 (CST) Received: from canpemm500009.china.huawei.com (unknown [7.192.105.203]) by mail.maildlp.com (Postfix) with ESMTPS id 983AB1400CF; Wed, 7 Feb 2024 15:22:18 +0800 (CST) Received: from [10.67.121.177] (10.67.121.177) by canpemm500009.china.huawei.com (7.192.105.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 7 Feb 2024 15:22:18 +0800 CC: , , , , , , , , , , , Subject: Re: [PATCH v2] serial: port: Don't suspend if the port is still busy To: Andy Shevchenko References: <20240206073322.5560-1-yangyicong@huawei.com> From: Yicong Yang Message-ID: <9e3d8daf-6715-bb37-125a-4141a9460417@huawei.com> Date: Wed, 7 Feb 2024 15:22:17 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500009.china.huawei.com (7.192.105.203) Hi Andy, On 2024/2/6 21:09, Andy Shevchenko wrote: > On Tue, Feb 06, 2024 at 03:33:22PM +0800, Yicong Yang wrote: >> From: Yicong Yang >> >> We accidently met the issue that the bash prompt is not shown after the >> previous command done and until the next input if there's only one CPU >> (In our issue other CPUs are isolated by isolcpus=). Further analysis >> shows it's because the port entering runtime suspend even if there's >> still pending chars in the buffer and the pending chars will only be >> processed in next device resuming. We are using amba-pl011 and the >> problematic flow is like below: >> >> Bash                                         kworker >> tty_write() >>   file_tty_write() >>     n_tty_write() >>       uart_write() >>         __uart_start() >>           pm_runtime_get() // wakeup waker >>             queue_work() >>                                     pm_runtime_work() >>                                                rpm_resume() >>                                                 status = RPM_RESUMING >>                                                 serial_port_runtime_resume() >>                                                   port->ops->start_tx() >>                                                     pl011_tx_chars() >>                                                       uart_write_wakeup() >>         […] >>         __uart_start() >>           pm_runtime_get() < 0 // because runtime status = RPM_RESUMING >>                                // later data are not commit to the port driver >>                                                 status = RPM_ACTIVE >>                                                 rpm_idle() -> rpm_suspend() >> >> This patch tries to fix this by checking the port busy before entering >> runtime suspending. A runtime_suspend callback is added for the port >> driver. When entering runtime suspend the callback is invoked, if there's >> still pending chars in the buffer then flush the buffer. > > ... > >> +static int serial_port_runtime_suspend(struct device *dev) >> +{ >> + struct serial_port_device *port_dev = to_serial_base_port_device(dev); >> + struct uart_port *port; >> + unsigned long flags; >> + int ret = 0; >> + >> + port = port_dev->port; >> + >> + if (port->flags & UPF_DEAD) >> + return ret; >> + >> + uart_port_lock_irqsave(port, &flags); >> + if (__serial_port_busy(port)) { >> + port->ops->start_tx(port); > >> + pm_runtime_mark_last_busy(dev); > > Do you think we need to call this under a lock? > I just put this close to the ops->start_tx() where I used the device. Yes I have no strong reason to put it in/with the lock region, but pm_runtime_mark_last_busy() should be no costy and safe enough to put it in the spinlock region. Any thoughts? >> + ret = -EBUSY; >> + } >> + uart_port_unlock_irqrestore(port, flags); >> + >> + return ret; >> +} > > With the above I would rather write it as > > static int __serial_port_busy(struct uart_port *port) > { > if (uart_tx_stopped(port)) > return 0; > > if (uart_circ_chars_pending(&port->state->xmit) > return -EBUSY; I'm not sure but EBUSY seems not quite match here. EBUSY for "Device or resource busy" so the device probably cannot be used but we're testing whether the port is busy here. Hope I understand it correctly. > > return 0; > } > > static int serial_port_runtime_suspend(struct device *dev) > { > int ret; > ... > uart_port_lock_irqsave(port, &flags); > ret = __serial_port_busy(port); > if (ret) > port->ops->start_tx(port); > uart_port_unlock_irqrestore(port, flags); > > if (ret) > pm_runtime_mark_last_busy(dev); > > return ret; > } > > It also seems aligned with the resume implementation above. > > ... > > For the consistency's sake the resume can be refactored as > > static int serial_port_runtime_resume(struct device *dev) > { > ... > int ret; > ... > ret = __serial_port_busy(port); > if (ret) > ... > } > > but this can be done later. > I agree the refactoring should go to a separate patch. But it doesn't seem to be more simpler or readable comparing to the current implementation? Just want to narrowing the spinlock region? Thanks.