Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3199453pxb; Mon, 9 Nov 2020 05:25:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxaE5TkeZmXajiswS8t9w+CDbOUz5bTmCbTF8P/QeBi9SHS0D4+Zzj11jVvHILqmH28F4vi X-Received: by 2002:a50:bb66:: with SMTP id y93mr15500807ede.244.1604928344223; Mon, 09 Nov 2020 05:25:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604928344; cv=none; d=google.com; s=arc-20160816; b=iMkcjIKDuv7/RcAILBPAd7nE0uN+xqpsCaTuypr1YMrstjBai1ffeQ0aWj5orWGNqD G2SmBrGf502z6wsn82jnzjOJPwImhWq9k0pjAv2JkiEtkjN47FHhdVMmS8aOWekNEU83 n1WL4/LDsCbh0lObjLWRVC8i0sYssFI7DkmjsGLTu9X5q/iJq1j7wJW/nHZNvrku+tZd 7jSYDohgz/BdeEhtV9hLHtRmtmJshOhpUKNooLEmOAqEkrgwNUcDN6QonEuUDEk20C+f O644ee0fJ9ulP6d82acmlgoX96AV21mS4j9POCdcx28/dAOhsOynECqfG9oiaPpaiqbX fY1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BUoMggWo7SX9gktNAvM+n8g3KolGSPf/O3aj9TwBK3c=; b=bG2w3g+9gOMMaV+NMyiKKIGXF7AYQcGS0gG/pY6MKFANo6/W2IQEUKljL51q9sqEon +i9oXPw8jjnI/pQos5zAVosLWZ6FrC5gqTN5vishz3vOTlKfH0IMGTpQfJkaRbp0+1ni RxH7nzTCJ2jdYnxGPw5pl+pHu+ybU1Y+TFxqlm1tMPlkEjHdbsb3AWbtBs1lxz4u5b/1 ElQ+waBIgNiA4H6LZ6rPvhv7l2TJoamiwSIgGkq6gAjO3K0lqrVrs/4QvLzD5l3ksavZ AMtB/1m/9wlyjyS62nbxY+J3wBg9fvMKEDvGzG1iG+yZ0eKUKAxmlJCMCOuGXhErtz2o l3uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=keEKfwF+; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x14si7023286ejs.714.2020.11.09.05.25.21; Mon, 09 Nov 2020 05:25: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=@kernel.org header.s=default header.b=keEKfwF+; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387640AbgKINUY (ORCPT + 99 others); Mon, 9 Nov 2020 08:20:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:47942 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387586AbgKINUW (ORCPT ); Mon, 9 Nov 2020 08:20:22 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 45E902076E; Mon, 9 Nov 2020 13:20:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604928021; bh=ctnOPLLHJhy1jQ6TOWEhGszCvAvPxwWh8+vmBkc5cnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=keEKfwF+X8gMzyv+WS01t3AgQfK/tUMkZLW6FQmYyGYXCBc+8EEAMy34RxWMHeUuz oCMF5LrcbnwcW12znfYNbhkiCO7f7/s/lrm5JCf+mldVtq3G/dpnMZbcvqh+4UjlpC cYpBW2jmzrukZsxumrLwZ1lXI2PLgwcb49Wecn50= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Gratian Crisan , Mike Galbraith , Thomas Gleixner Subject: [PATCH 5.9 070/133] futex: Handle transient "ownerless" rtmutex state correctly Date: Mon, 9 Nov 2020 13:55:32 +0100 Message-Id: <20201109125034.101580315@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125030.706496283@linuxfoundation.org> References: <20201109125030.706496283@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Galbraith commit 9f5d1c336a10c0d24e83e40b4c1b9539f7dba627 upstream. 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 T1 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 Link: https://lore.kernel.org/r/87sg9pkvf7.fsf@nanos.tec.linutronix.de Signed-off-by: Greg Kroah-Hartman --- kernel/futex.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) --- a/kernel/futex.c +++ b/kernel/futex.c @@ -2380,10 +2380,22 @@ retry: } /* - * 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)) { + err = -EAGAIN; + goto handle_err; + } } else { WARN_ON_ONCE(argowner != current); if (oldowner == current) {