Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1055015lqo; Thu, 9 May 2024 03:33:00 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU/l/F/Pe0JIgqd18f6vEh3LzOirtyAHfDyTd++4oe0rvxHb4xtbxDLwZKiHdeTLn5YnrTAr7R1DHnoUk1B6glbqpTfFQNGzH/Um6jiZQ== X-Google-Smtp-Source: AGHT+IGPn/NXUeDw0Hbe/Pgap/XhNLduRuGTzZPSgl/T4SaOkBQgDsfmz3rtY1UC00OjSygBAukh X-Received: by 2002:a17:90a:d195:b0:2a2:7a00:f101 with SMTP id 98e67ed59e1d1-2b616be63bbmr4502052a91.47.1715250778715; Thu, 09 May 2024 03:32:58 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2b670e34472si1223410a91.34.2024.05.09.03.32.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 03:32:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174403-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-174403-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174403-linux.lists.archive=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 580C2283653 for ; Thu, 9 May 2024 10:32:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8AE3912E1EB; Thu, 9 May 2024 10:32:51 +0000 (UTC) Received: from air.basealt.ru (air.basealt.ru [194.107.17.39]) (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 46FF31361; Thu, 9 May 2024 10:32:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.107.17.39 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715250771; cv=none; b=lCDRimTWjRsD+VVrLmgp48QuLb5GytHXDTAeM6lmC/McFMR7PtUm9Jeg3WIft7D3XHo3AeyLO6frcEqj3AtyTT3E7/1PRWBkxYJk6+Bzk0/qML3jeGTMJvq2KwnciXi2KRkW7mcZU/CTGdweDbcvd2fe5ujguQ3I3ZB8U3RhvZM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715250771; c=relaxed/simple; bh=2lxyu2hLVfeAwz1VtGgAmtW/z0G0oGotVbQhWW5bTso=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Jagkx0wvz0M53ziV4uk1OvRGRuRMwnQIE0ZYd/FP7Kp6XCvVwRg13OkE6EbqWqVpRq81zw2y+/J8COLPNlEaFinyY6uUfu3VJ0Epoi8871pgRSZHmUqLIsalPU+K+H28RhFjpcsMa/KxBEiG5NIIBaadTKwTgMbUYtNg3hecKLw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org; spf=pass smtp.mailfrom=altlinux.org; arc=none smtp.client-ip=194.107.17.39 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=altlinux.org Received: by air.basealt.ru (Postfix, from userid 490) id BA0D42F20231; Thu, 9 May 2024 10:32:44 +0000 (UTC) X-Spam-Level: Received: from [192.168.0.102] (unknown [178.76.204.78]) by air.basealt.ru (Postfix) with ESMTPSA id 073EF2F20226; Thu, 9 May 2024 10:32:43 +0000 (UTC) Message-ID: Date: Thu, 9 May 2024 13:32:43 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH] tty: Fix possible deadlock in tty_buffer_flush Content-Language: en-US To: Jiri Slaby , kovalev@altlinux.org, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Cc: lvc-project@linuxtesting.org, dutyrok@altlinux.org, oficerovas@altlinux.org, stable@vger.kernel.org References: <20240508093005.1044815-1-kovalev@altlinux.org> From: Vasiliy Kovalev In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 09.05.2024 09:41, Jiri Slaby wrote: > On 08. 05. 24, 11:30, kovalev@altlinux.org wrote: >> From: Vasiliy Kovalev >> >> A possible scenario in which a deadlock may occur is as follows: >> >> flush_to_ldisc() { >> >>    mutex_lock(&buf->lock); >> >>    tty_port_default_receive_buf() { >>      tty_ldisc_receive_buf() { >>        n_tty_receive_buf2() { >>     n_tty_receive_buf_common() { >>       n_tty_receive_char_special() { >>         isig() { >>           tty_driver_flush_buffer() { >>         pty_flush_buffer() { >>           tty_buffer_flush() { >> >>             mutex_lock(&buf->lock); (DEADLOCK) >> >> flush_to_ldisc() and tty_buffer_flush() functions they use the same mutex >> (&buf->lock), but not necessarily the same struct tty_bufhead object. > > "not necessarily" -- so does it mean that it actually can happen (and we > should fix it) or not at all (and we should annotate the mutex)? During debugging, when running the reproducer multiple times, I failed to catch a situation where these mutexes have the same address in memory in the above call scenario, so I'm not sure that such a situation is possible. But earlier, a thread is triggered that accesses the same structure (and mutex), so LOCKDEP tools throw a warning: thread 0: flush_to_ldisc() { mutex_lock(&buf->lock) // Address mutex == 0xA n_tty_receive_buf_common(); mutex_unlock(&buf->lock) // Address mutex == 0xA } thread 1: flush_to_ldisc() { mutex_lock(&buf->lock) // Address mutex == 0xB n_tty_receive_buf_common() { isig() { tty_driver_flush_buffer() { pty_flush_buffer() { tty_buffer_flush() { mutex_lock(&buf->lock) // Address mutex == 0xA -> throw Warning // successful continuation .. } >> However, you should probably use a separate mutex for the >> tty_buffer_flush() function to exclude such a situation. > ... > >> Cc: stable@vger.kernel.org > > What commit does this fix? I will assume that the commit of introducing mutexes in these functions: e9975fdec013 ("tty: Ensure single-threaded flip buffer consumer with mutex") >> --- a/drivers/tty/tty_buffer.c >> +++ b/drivers/tty/tty_buffer.c >> @@ -226,7 +226,7 @@ void tty_buffer_flush(struct tty_struct *tty, >> struct tty_ldisc *ld) >>       atomic_inc(&buf->priority); >> -    mutex_lock(&buf->lock); >> +    mutex_lock(&buf->flush_mtx); > > Hmm, how does this protect against concurrent buf pickup. We free it > here and the racing thread can start using it, or? Yes, assuming that such a scenario is possible.. Otherwise, if such a scenario is not possible and the patch is inappropriate, then you need to mark this mutex in some way to tell lockdep tools to ignore this place.. >>       /* paired w/ release in __tty_buffer_request_room; ensures there >> are >>        * no pending memory accesses to the freed buffer >>        */ > > thanks, -- Regards, Vasiliy Kovalev