Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3407857pxf; Mon, 29 Mar 2021 01:07:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4TkLCkN5jmsVeDLpnZPB/b/QdRhEHthV+2FGW+7V3ekoUh2fKEs+M10fda1uiJDmk9WBv X-Received: by 2002:a17:906:6c93:: with SMTP id s19mr27069330ejr.151.1617005223728; Mon, 29 Mar 2021 01:07:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617005223; cv=none; d=google.com; s=arc-20160816; b=ZxpyilDLVtVGW05P3iSVnpr2IJsA5aLnCpQoWicOVqs+Mn2KdQE9YtdIF/2DD3AZGk +o5YzM8IaCfYl288TE0IPoZiMary3kX7n0rKO6hcaHS6AAx8Lyhl7b+4EoFCUDh1Ov57 OW49CqFEEcpCt/73KrrbdG8ULYEMx7UroJGylgc4iR1ik9hYeT4IhEkZXCMnphn8lyg2 yTJBTqYqPagB5rd5I3kiCav2IYbK22XtzfZVl3T7pQsxDbK4paXWowMfk1V4hSyvaLAz e7EDKw3yBprEdCTb9OAlECN2cWq4FMTRnmmR7PSUJ9R2RHH43FrFHNxCYnKWeObfVF1d nAhg== 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=sQU8ZdxC2qrKAefgldLmsReSNQjsFybMbVNWdYpptKQ=; b=SW/6faYBb0+uTU6Z+5C64+wL1+WQlfUMgSnQwUzSllSiEpu6sFWnwb7+1Da3zBFQ5w MHX5m8YU4bO34HsYrIwy+ItJJZpt8qTJX+nNKuqchn1fu1PKSZkmbNc4tAn6Hl68x2A4 RjmKtm2LlFARegBH61Ys9ov6Qc1Y1XwE2pjVR6jYC/yZdG5giRrraTM1FPxtL/JoGB8G x17ScLXeTaVw99FZmXx4JVzzrrac0K9umblEcuGKt/BxProPLvRTfhWVb6igpHGRjlCR RgvA96uQU7svNE4OhZBWiaqZw62BT800PDBelfiCM/V2A/qMXz3PNh0wuMpGpS58Pc0K IVtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Kp27MxaM; 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 r22si12523485edy.138.2021.03.29.01.06.40; Mon, 29 Mar 2021 01:07:03 -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=Kp27MxaM; 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 S231971AbhC2IF4 (ORCPT + 99 others); Mon, 29 Mar 2021 04:05:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:46442 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231683AbhC2IDf (ORCPT ); Mon, 29 Mar 2021 04:03:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C0DDD61976; Mon, 29 Mar 2021 08:03:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617005015; bh=cLvc1Hf0v65YzDrHgoD7P8Caz0A9l7JkjQlHRmCaVSo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kp27MxaMcyqFOy1idJrSRcX8AygWIqtY3zUpvhIZ/qIqLvEennQixIlEWhTHoCAhF xSNRMcEoIvXsPf8RAZSm3nkivckIp6AsdQHnu7m5KZn6VSP1+W6db1DGb8SFNMNH6v oURQc4yc7OI1rFzdjxTwXt7bcuk6UTmLEcKWO8nA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Gratian Crisan , Mike Galbraith , Thomas Gleixner , Ben Hutchings Subject: [PATCH 4.9 49/53] futex: Handle transient "ownerless" rtmutex state correctly Date: Mon, 29 Mar 2021 09:58:24 +0200 Message-Id: <20210329075609.128212818@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210329075607.561619583@linuxfoundation.org> References: <20210329075607.561619583@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 Signed-off-by: Ben Hutchings 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 @@ -2497,10 +2497,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) {