Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp420817pxa; Fri, 14 Aug 2020 07:49:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/ry60jhg3jkx+3/ipDp+UCis2Bwvv3LGxqaIFAFLrcbrJdD/5h7VcsiQnnDr/PcmBpx0z X-Received: by 2002:a05:6402:508:: with SMTP id m8mr2465281edv.279.1597416559930; Fri, 14 Aug 2020 07:49:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597416559; cv=none; d=google.com; s=arc-20160816; b=npn99vi3iX0aJZN2GOpd+FVVnmVFV610dB40UkLdlDJgWqZtOEsOVvu60H47IqT9g+ L+chLEuNtIKmMivbZsm2f4TpVkrJrQSVankwS7c4zCylokF7vZKiIMnJZ6y2HPPRHAQn EoEWBX5CjVSk/fZxRwdsrZN5iVDgIqcGrxv+ubxgTuzDSDdwHxviQpwMdbGbDzVOYeGs HBb8sqdTB4djqFHcj/XXfNddGpxpKPlOzJEIo1i+YhjlkdpmlFJmun6yfiyPZ6wqDRvf F5BCkjkB/L3wtNAzeFiKQCFR3jNHk5cAE0svZTawD12Z2S8U52vnfdcBMkaSfmEk2QDr ILpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject:dkim-signature; bh=ef5LJ2yNYl4zwywECTQBptoCdcYzbdTtvUSNV41zWDg=; b=hYls6VOjl46q0I/aeEsvWqU5K4UD9nRJHHogFqgwZPT6cOeWkxBQGhYv+DIEKGNM8G jOFMTAkSODa0yndBvq+Z/+g3Zm3pwuP2ntV2Q8Rb7C490uzIUYocMwWyBdnGM5gNosLN SOQfyMIchimZx5rxMDIyVhnnMYp+R+jRAz1F3U1Nl8/A/W/PhJuNn83SVFDO4yIULtKC G2ps3tY2rXjpBm6pDeCsoOk6jxFf4PpfgDyjeEKv8xOp+zPq6R0b9lZL728JUrfa9RXB 0O+LhQnqCaPw4IJ1J3YHvfEsw1w92tGJqdjiFQGB095jXf66jx7563sk289n6+ec1oqK LBZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=iTVjZ5Gk; 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 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.48.57; Fri, 14 Aug 2020 07:49:19 -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=fail header.i=@gmail.com header.s=20161025 header.b=iTVjZ5Gk; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726967AbgHNOJw (ORCPT + 99 others); Fri, 14 Aug 2020 10:09:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726185AbgHNOJu (ORCPT ); Fri, 14 Aug 2020 10:09:50 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2E65C061384; Fri, 14 Aug 2020 07:09:49 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id o13so4609747pgf.0; Fri, 14 Aug 2020 07:09:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ef5LJ2yNYl4zwywECTQBptoCdcYzbdTtvUSNV41zWDg=; b=iTVjZ5GkBLeIKvJ9aan7yTGC1RFIXnvbgiim6Rnc09bpm8Mu49zksTCXZ5j5ZlAfaH qHsQ2oGnMZgNCs2QnbaTLvVSBCgto0z9ZwFKaU1D1OMXgPCBL3SeHMJTj6ai7DbeT1tc y2rT24azx4IfhOv2FR51qiLvdaQ5fycLAeFzt+BQc1SRMdfG9ySM4fyw75Ri8a9mLfwG kcM8ysqqy7b8Y+5oJVPN+Te2XMZMbgl6QeE7eOkTyTgPIX9OrQrKcQ1xlWvHRbHJc/R2 D0+UpCXQOtGQ9baQ0T/QdYNyTDSuN1O7JR2+bZ46sDC8CCUi0Hak3G5GATmdCT1AGZA9 uddQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=ef5LJ2yNYl4zwywECTQBptoCdcYzbdTtvUSNV41zWDg=; b=IfV0m5UeTB6acikaeGm3I8sIuY4up7GPt8PEth9OYHg6ZumtCPCax9BoUYYuaebTU9 B0vi3IX8Vy5nMuTCI83tX4CQwZ4sDa+dUKPzcLiqNm70kZlb1bNwo8P7ZgEk/6qiqOuz aDheEfPIjGdfom7EIvO4/BmpzLv5mDIhCNV/vufZCrnyv/UiDOputs3SQgXg+fCmyAnr KjQyLVWG32kAkOqwzKUWUtpFigb0Imv3X1/ljT+uAfXzn9zGBKnJQpRjurleGMmV+gki iihixpr8WbTDpE3GxvQeVICJ651mG9H2/Tj01Qb6ZcobDYCCFgPsn2aySWB0WxvrVo5K 994A== X-Gm-Message-State: AOAM532NrerB1tLA2WufPnRJVLIPed8PkkwVuNkUjlPj1F685Wg5xwF0 p5Ukz3vdlB6d5nG7GY/hTExJPslaFhQ= X-Received: by 2002:a62:f904:: with SMTP id o4mr2072547pfh.14.1597414189025; Fri, 14 Aug 2020 07:09:49 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id f63sm8627038pjk.53.2020.08.14.07.09.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Aug 2020 07:09:45 -0700 (PDT) Subject: Re: [PATCH] uart:8250: change lock order in serial8250_do_startup() To: Sergey Senozhatsky , Greg KH Cc: Andy Shevchenko , Tony Lindgren , Kurt Kanzenbach , Raul Rangel , Petr Mladek , Steven Rostedt , John Ogness , linux-kernel , linux-serial@vger.kernel.org References: <20200814013802.357412-1-sergey.senozhatsky@gmail.com> From: Guenter Roeck Autocrypt: addr=linux@roeck-us.net; keydata= xsFNBE6H1WcBEACu6jIcw5kZ5dGeJ7E7B2uweQR/4FGxH10/H1O1+ApmcQ9i87XdZQiB9cpN RYHA7RCEK2dh6dDccykQk3bC90xXMPg+O3R+C/SkwcnUak1UZaeK/SwQbq/t0tkMzYDRxfJ7 nyFiKxUehbNF3r9qlJgPqONwX5vJy4/GvDHdddSCxV41P/ejsZ8PykxyJs98UWhF54tGRWFl 7i1xvaDB9lN5WTLRKSO7wICuLiSz5WZHXMkyF4d+/O5ll7yz/o/JxK5vO/sduYDIlFTvBZDh gzaEtNf5tQjsjG4io8E0Yq0ViobLkS2RTNZT8ICq/Jmvl0SpbHRvYwa2DhNsK0YjHFQBB0FX IdhdUEzNefcNcYvqigJpdICoP2e4yJSyflHFO4dr0OrdnGLe1Zi/8Xo/2+M1dSSEt196rXaC kwu2KgIgmkRBb3cp2vIBBIIowU8W3qC1+w+RdMUrZxKGWJ3juwcgveJlzMpMZNyM1jobSXZ0 VHGMNJ3MwXlrEFPXaYJgibcg6brM6wGfX/LBvc/haWw4yO24lT5eitm4UBdIy9pKkKmHHh7s jfZJkB5fWKVdoCv/omy6UyH6ykLOPFugl+hVL2Prf8xrXuZe1CMS7ID9Lc8FaL1ROIN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Ldertmw7eyAYOo4RuWJguYMg5DRnBk WkRwrSuCn7UG+qVWZeKEsFKFOkynOs3pVbcbq1pxbhk3TRWCGRU5JolI4ohy/7JV1TVbjiDI HP/aVnm6NC8of26P40Pg8EdAhajZnHHjA7FrJXsy3cyIGqvg9os4rNkUWmrCfLLsZDHD8FnU mDW4+i+XlNFUPUYMrIKi9joBhu18ssf5i5Q= Message-ID: <2a70c05e-4a0a-40f7-bc8b-29cd75be6d06@roeck-us.net> Date: Fri, 14 Aug 2020 07:09:43 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200814013802.357412-1-sergey.senozhatsky@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 8/13/20 6:38 PM, Sergey Senozhatsky wrote: > We have a number of "uart.port->desc.lock vs desc.lock->uart.port" > lockdep reports coming from 8250 driver; this causes a bit of trouble > to people, so let's fix it. > > The problem is reverse lock order in two different call paths: > > chain #1: > > serial8250_do_startup() > spin_lock_irqsave(&port->lock); > disable_irq_nosync(port->irq); > raw_spin_lock_irqsave(&desc->lock) > > chain #2: > > __report_bad_irq() > raw_spin_lock_irqsave(&desc->lock) > for_each_action_of_desc() > printk() > spin_lock_irqsave(&port->lock); > > Fix this by changing the order of locks in serial8250_do_startup(): > do disable_irq_nosync() first, which grabs desc->lock, and grab > uart->port after that, so that chain #1 and chain #2 have same lock > order. > > Full lockdep splat: > > ====================================================== > WARNING: possible circular locking dependency detected > 5.4.39 #55 Not tainted > ------------------------------------------------------ > swapper/0/0 is trying to acquire lock: > ffffffffab65b6c0 (console_owner){-...}, at: console_lock_spinning_enable+0x31/0x57 > > but task is already holding lock: > ffff88810a8e34c0 (&irq_desc_lock_class){-.-.}, at: __report_bad_irq+0x5b/0xba > > which lock already depends on the new lock. > > the existing dependency chain (in reverse order) is: > > -> #2 (&irq_desc_lock_class){-.-.}: > _raw_spin_lock_irqsave+0x61/0x8d > __irq_get_desc_lock+0x65/0x89 > __disable_irq_nosync+0x3b/0x93 > serial8250_do_startup+0x451/0x75c > uart_startup+0x1b4/0x2ff > uart_port_activate+0x73/0xa0 > tty_port_open+0xae/0x10a > uart_open+0x1b/0x26 > tty_open+0x24d/0x3a0 > chrdev_open+0xd5/0x1cc > do_dentry_open+0x299/0x3c8 > path_openat+0x434/0x1100 > do_filp_open+0x9b/0x10a > do_sys_open+0x15f/0x3d7 > kernel_init_freeable+0x157/0x1dd > kernel_init+0xe/0x105 > ret_from_fork+0x27/0x50 > > -> #1 (&port_lock_key){-.-.}: > _raw_spin_lock_irqsave+0x61/0x8d > serial8250_console_write+0xa7/0x2a0 > console_unlock+0x3b7/0x528 > vprintk_emit+0x111/0x17f > printk+0x59/0x73 > register_console+0x336/0x3a4 > uart_add_one_port+0x51b/0x5be > serial8250_register_8250_port+0x454/0x55e > dw8250_probe+0x4dc/0x5b9 > platform_drv_probe+0x67/0x8b > really_probe+0x14a/0x422 > driver_probe_device+0x66/0x130 > device_driver_attach+0x42/0x5b > __driver_attach+0xca/0x139 > bus_for_each_dev+0x97/0xc9 > bus_add_driver+0x12b/0x228 > driver_register+0x64/0xed > do_one_initcall+0x20c/0x4a6 > do_initcall_level+0xb5/0xc5 > do_basic_setup+0x4c/0x58 > kernel_init_freeable+0x13f/0x1dd > kernel_init+0xe/0x105 > ret_from_fork+0x27/0x50 > > -> #0 (console_owner){-...}: > __lock_acquire+0x118d/0x2714 > lock_acquire+0x203/0x258 > console_lock_spinning_enable+0x51/0x57 > console_unlock+0x25d/0x528 > vprintk_emit+0x111/0x17f > printk+0x59/0x73 > __report_bad_irq+0xa3/0xba > note_interrupt+0x19a/0x1d6 > handle_irq_event_percpu+0x57/0x79 > handle_irq_event+0x36/0x55 > handle_fasteoi_irq+0xc2/0x18a > do_IRQ+0xb3/0x157 > ret_from_intr+0x0/0x1d > cpuidle_enter_state+0x12f/0x1fd > cpuidle_enter+0x2e/0x3d > do_idle+0x1ce/0x2ce > cpu_startup_entry+0x1d/0x1f > start_kernel+0x406/0x46a > secondary_startup_64+0xa4/0xb0 > > other info that might help us debug this: > > Chain exists of: > console_owner --> &port_lock_key --> &irq_desc_lock_class > > Possible unsafe locking scenario: > > CPU0 CPU1 > ---- ---- > lock(&irq_desc_lock_class); > lock(&port_lock_key); > lock(&irq_desc_lock_class); > lock(console_owner); > > *** DEADLOCK *** > > 2 locks held by swapper/0/0: > #0: ffff88810a8e34c0 (&irq_desc_lock_class){-.-.}, at: __report_bad_irq+0x5b/0xba > #1: ffffffffab65b5c0 (console_lock){+.+.}, at: console_trylock_spinning+0x20/0x181 > > stack backtrace: > CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.4.39 #55 > Hardware name: XXXXXX > Call Trace: > > dump_stack+0xbf/0x133 > ? print_circular_bug+0xd6/0xe9 > check_noncircular+0x1b9/0x1c3 > __lock_acquire+0x118d/0x2714 > lock_acquire+0x203/0x258 > ? console_lock_spinning_enable+0x31/0x57 > console_lock_spinning_enable+0x51/0x57 > ? console_lock_spinning_enable+0x31/0x57 > console_unlock+0x25d/0x528 > ? console_trylock+0x18/0x4e > vprintk_emit+0x111/0x17f > ? lock_acquire+0x203/0x258 > printk+0x59/0x73 > __report_bad_irq+0xa3/0xba > note_interrupt+0x19a/0x1d6 > handle_irq_event_percpu+0x57/0x79 > handle_irq_event+0x36/0x55 > handle_fasteoi_irq+0xc2/0x18a > do_IRQ+0xb3/0x157 > common_interrupt+0xf/0xf > > > Signed-off-by: Sergey Senozhatsky Tested-by: Guenter Roeck > --- > drivers/tty/serial/8250/8250_port.c | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c > index 09475695effd..67f1a4f31093 100644 > --- a/drivers/tty/serial/8250/8250_port.c > +++ b/drivers/tty/serial/8250/8250_port.c > @@ -2275,6 +2275,11 @@ int serial8250_do_startup(struct uart_port *port) > > if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) { > unsigned char iir1; > + bool irq_shared = up->port.irqflags & IRQF_SHARED; > + > + if (irq_shared) > + disable_irq_nosync(port->irq); > + > /* > * Test for UARTs that do not reassert THRE when the > * transmitter is idle and the interrupt has already > @@ -2284,8 +2289,6 @@ int serial8250_do_startup(struct uart_port *port) > * allow register changes to become visible. > */ > spin_lock_irqsave(&port->lock, flags); > - if (up->port.irqflags & IRQF_SHARED) > - disable_irq_nosync(port->irq); > > wait_for_xmitr(up, UART_LSR_THRE); > serial_port_out_sync(port, UART_IER, UART_IER_THRI); > @@ -2297,9 +2300,9 @@ int serial8250_do_startup(struct uart_port *port) > iir = serial_port_in(port, UART_IIR); > serial_port_out(port, UART_IER, 0); > > - if (port->irqflags & IRQF_SHARED) > - enable_irq(port->irq); > spin_unlock_irqrestore(&port->lock, flags); > + if (irq_shared) > + enable_irq(port->irq); > > /* > * If the interrupt is not reasserted, or we otherwise >