Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1370953pxb; Wed, 4 Nov 2020 07:15:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7Ye3hLXapEhTGtyCW50DtEW32sKZR4ek9Kw7sG16aNamH/0tvV7hPRMs4pfUG6sFftkn6 X-Received: by 2002:a17:906:1c0f:: with SMTP id k15mr24537978ejg.343.1604502944086; Wed, 04 Nov 2020 07:15:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604502944; cv=none; d=google.com; s=arc-20160816; b=JgpxrZzIIPnQnFDyIborGae+ruxwKNCV7k/S6cke+SWn/XObAx6htsr3kZEvdqFFOA AoFaa0hGN91Er8DgrCcsb9c5eZL+xw8axGtPCrrIG/NRjRv5YQBfFwFiNXVWoGI0yNOz Hkc6eRiuf0Mts5cbJpAbPbQwuzeJLSPGbI1u/vioO7bGD0vEzeSp+z1gTLKXWhqFL9s+ EVjp6asmloi5wq2xRkZXjoUP9kenZAXayqi9tu10i6RtBXKQ6H8mkPXR2Ijr5His77CV lSur+dBnZchFhOWMCLjUyD9voBN7qldKd9WfFNtaxvXfNb7rdL/tBkfr1iUEPKbsv3Cz 4RGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=SHy+znafm/VyROcHvLpycULzJCGKvQvcCR6rmrfdldQ=; b=wl588/OWiBKvBOLO0DYdizSLZB0D9VMio2vqcVsnfiKNqD+SuHP/Vi/obJ5bbi6TT9 9awdFaI5TiF3xY65upPUEEPRhsswdP/AN6kvwVmB5VdEQfTj3HJz59HUjLAUxnQbUbW+ 67kJXq7x2m4DdHmNDaXEgvqSSjDKHSEgK4CoAwzxiO+qztZOGcnGPcXZ0jdSnYxHNHJ1 nWLRFS7OCr+xLtpcpA26m/tyWLhzQAKxT/IvVkBLvtDx6isi+b7nsYh/6r9YZd0LcQZU 9OMO9yAllhTAvNKXfRieyKgVFYOiJycII550nin8stG/jj0kqmuasKMUJqy+L4dldJF0 iqOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=e6U8IuJx; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n4si1623335ejk.692.2020.11.04.07.15.20; Wed, 04 Nov 2020 07:15:44 -0800 (PST) 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=@linutronix.de header.s=2020 header.b=e6U8IuJx; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730274AbgKDPMt (ORCPT + 99 others); Wed, 4 Nov 2020 10:12:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726801AbgKDPMt (ORCPT ); Wed, 4 Nov 2020 10:12:49 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9A98C0613D3; Wed, 4 Nov 2020 07:12:48 -0800 (PST) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1604502765; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SHy+znafm/VyROcHvLpycULzJCGKvQvcCR6rmrfdldQ=; b=e6U8IuJxtG+Q+uz7AeunWosCEZjyHrzQPNRHrfw60TfFZl9Fq44C2WMnk0kWXuPiT+FMHB +86HzLSQuhEFRo7B9OaGDu4QjmOrU+PVT7nt8AIaMai6tEcNAJIKzjlT0ZEitPk5OpUKAs AdzaeW7vnR14gRag9mouf6v/hq4m33IjlkDNBp/wrQ7OcbJnHcQQoqUROhrc6X7ScE4uT+ pRxcbKBCqPCRDhATA99z1H97mgJaiHIJzbdoTA49eLUf1vTW6qxu6XDegiTa0dDXHJqde+ NrYMdH9cqbso6flrWubT6ztoZxNYddBYJiCic/etk/uUsHAHBMCRuoX9+IPvJQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1604502765; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SHy+znafm/VyROcHvLpycULzJCGKvQvcCR6rmrfdldQ=; b=IF9+NXuFztp+x16ZEFmqfhirR3N2axqAA1SitxW4k5Am6sms24TnYzKRehJ06bQm9++OjY HF/z/NVBiFTio7Dg== To: Mike Galbraith , Gratian Crisan , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, linux-rt-users@vger.kernel.org, Brandon Streiff , Ingo Molnar , Darren Hart , James Minor , Sebastian Andrzej Siewior Subject: [PATCH] futex: Handle transient "ownerless" rtmutex state correctly In-Reply-To: References: <87a6w6x7bb.fsf@ni.com> <878sbixbk4.fsf@ni.com> <2376f4e71c638aee215a4911e5efed14c5adf56e.camel@gmx.de> <5f536491708682fc3b86cb5b7bc1e05ffa3521e7.camel@gmx.de> <874km5mnbf.fsf@nanos.tec.linutronix.de> <871rh9mkvr.fsf@nanos.tec.linutronix.de> <87v9ell0cn.fsf@nanos.tec.linutronix.de> Date: Wed, 04 Nov 2020 16:12:44 +0100 Message-ID: <87sg9pkvf7.fsf@nanos.tec.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Galbraith Gratian managed to trigger the BUG_ON(!newowner) in fixup_pi_state_owner(). This is one possible chain of events leading to this: Task Prio Operation T1 120 lock(F) T2 120 lock(F) -> blocks (top waiter) T3 50 (RT) lock(F) -> boosts T3 and blocks (new top waiter) XX timeout/ -> wakes T2 signal T1 50 unlock(F) -> wakes T3 (rtmutex->owner == NULL, waiter bit is set) T2 120 cleanup -> try_to_take_mutex() fails because T3 is the top waiter and the lower priority T2 cannot steal the lock. -> fixup_pi_state_owner() sees newowner == NULL -> BUG_ON() The comment states that this is invalid and rt_mutex_real_owner() must return a non NULL owner when the trylock failed, but in case of a queued and woken up waiter rt_mutex_real_owner() == NULL is a valid transient state. The higher priority waiter has simply not yet managed to take over the rtmutex. The BUG_ON() is therefore wrong and this is just another retry condition in fixup_pi_state_owner(). Drop the locks, so that T3 can make progress, and then try the fixup again. Gratian provided a great analysis, traces and a reproducer. The analysis is to the point, but it confused the hell out of that tglx dude who had to page in all the futex horrors again. Condensed version is above. [ tglx: Wrote comment and changelog ] Fixes: c1e2f0eaf015 ("futex: Avoid violating the 10th rule of futex") Reported-by: Gratian Crisan Signed-off-by: Mike Galbraith Signed-off-by: Thomas Gleixner Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/87a6w6x7bb.fsf@ni.com --- kernel/futex.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) --- a/kernel/futex.c +++ b/kernel/futex.c @@ -2380,10 +2380,22 @@ static int fixup_pi_state_owner(u32 __us } /* - * Since we just failed the trylock; there must be an owner. + * The trylock just failed, so either there is an owner or + * there is a higher priority waiter than this one. */ newowner = rt_mutex_owner(&pi_state->pi_mutex); - BUG_ON(!newowner); + /* + * If the higher priority waiter has not yet taken over the + * rtmutex then newowner is NULL. We can't return here with + * that state because it's inconsistent vs. the user space + * state. So drop the locks and try again. It's a valid + * situation and not any different from the other retry + * conditions. + */ + if (unlikely(!newowner)) { + ret = -EAGAIN; + goto handle_err; + } } else { WARN_ON_ONCE(argowner != current); if (oldowner == current) {