Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp1763301pxt; Sun, 8 Aug 2021 00:25:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzU5l2B2trsxrz3R819T/k8u2SmUN3M53Sy4x/0MVGnQNVbTUYDMdvbQF3X/VUIeerVLJJB X-Received: by 2002:a5e:a908:: with SMTP id c8mr255483iod.116.1628407501821; Sun, 08 Aug 2021 00:25:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628407501; cv=none; d=google.com; s=arc-20160816; b=JPnqaGczJSIeoOHZFu6RZE90W/xq07Agqt0CWViy9ZknNSBU6f4nPZotx2ykU4W0UH Rd/Q9ORR4UfyvQKnpCWQDvzBW2UTQdKQ2Mkr0y8C8lt0l/KgBFUSDMuHdv+z4sdfVKjz XCZS4jYL9VA6En7qtG22zsmRuC80V2x2iNEzFaPAW5pXARN5fHzoBjxSSPqGRphEUHR3 EeRSziX1jl/KPt+BLsvCN7i0/4Wnk9Os75p8DJbRBHVCMNOGJk2FndK8r1a4+CLL0izb CmxAF6sGmQ3wUh237n8v8xP4m8bFgNQwBayA18Zw74mSbCrsIXhhLGoRNFo/zfd4C5xY txRw== 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=USMJhwUXc0G+6wMX0x231ogawshmGpr6FQtpJlwA8Wo=; b=SBNCQ4hFeSbUSUecKnM4sAHWyrp9/2nnAEGJ0isBL00kLBHL4TbSG8jFl1TrELTrer CwasSbCdizmb7nQBKXZ/T/YJpqXs7QCQhLZZvSEZE3wkbg/ouuRdg3JsBGSMd0AQ9Yjt tyU/Q/JAsPk2DbEUOiHPY1+iy/nxem22aiOMo3KBqOjjg5raP+BgArfjHsihYNeZyl75 72bHjy4moZcOQP4k6+Jb40/kxYJACFWOyupsoUYygMLvQdisM+SkcTh4J42UjQ1nfT1n 1ywKjTSqdKGvw8SMEOAdTb9L/uRRFagCqcO5NVFCmU69fCwlRqVvog/oLpy3RY34NrD1 ZHsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dZYBZM22; 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=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 d10si14338379jaq.37.2021.08.08.00.24.51; Sun, 08 Aug 2021 00:25:01 -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=pass header.i=@linuxfoundation.org header.s=korg header.b=dZYBZM22; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231673AbhHHHYI (ORCPT + 99 others); Sun, 8 Aug 2021 03:24:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:55452 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231576AbhHHHXf (ORCPT ); Sun, 8 Aug 2021 03:23:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1E8DE60C40; Sun, 8 Aug 2021 07:23:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628407396; bh=t2x4MMJGjpeLVqHrlnTStvWC0xWniWrXx/JRaH3tJuY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dZYBZM22HNC8iHtWlupw5xzk0yNDySCzbQl3XVmSyy6XlWQl3x0+e93hBb13T9zFk Hs3Nc+NUr3kTlPTHnC93q38j189PY4lVPxThKjqkFQ9jKvrGv2f41Mk9crgRHwoe+A fUkfqCu5senawOPhvfTIL3RGk5RsbNX8aZZ2NiZU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Gratian Crisan , Mike Galbraith , Thomas Gleixner , Zhen Lei , Joe Korty Subject: [PATCH 4.4 08/11] futex: Handle transient "ownerless" rtmutex state correctly Date: Sun, 8 Aug 2021 09:22:43 +0200 Message-Id: <20210808072217.606471864@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210808072217.322468704@linuxfoundation.org> References: <20210808072217.322468704@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 [ Upstream commit 9f5d1c336a10c0d24e83e40b4c1b9539f7dba627 ] 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: Zhen Lei Acked-by: Joe Korty 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 @@ -2437,10 +2437,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_fault; + } } else { WARN_ON_ONCE(argowner != current); if (oldowner == current) {