From patchwork Mon Aug 2 13:46:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhen Lei X-Patchwork-Id: 490338 Delivered-To: patch@linaro.org Received: by 2002:a05:6638:1185:0:0:0:0 with SMTP id f5csp1940424jas; Mon, 2 Aug 2021 06:47:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJ3K/+J4PzjD9DtWhkl9LoOhOq7qo+fy7b2BodDSasjR7h3QDIsP2jmQQUEDpzqObo/Nku X-Received: by 2002:a02:cbb1:: with SMTP id v17mr15402778jap.0.1627912079417; Mon, 02 Aug 2021 06:47:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912079; cv=none; d=google.com; s=arc-20160816; b=kQjmX8bo5DVaRljw9OaW/KP0QVJa4m+uYdtolxo4c1OI+6VcR9l7SMBw3UfUGjv0Hb Ip6D0eYhLxc7OE0Dsal83VV2lmdIVhNIQU640INSjQUlsk2OTGb8CwSjBKl7/BuQT1rm 1yZgpVyf+GNX3klT5kFD+pTRHfSu/F4hEXCs/8uE9zWCjX8XYnv6QlYcSrlTDJWgViYZ GPaPCW+Sa20iB7s6Tu8bjnRgGCDKsyEoickluIpG9HD/2EF2H3t5p177T0Zkff5+8rS0 h5QvfyzVtft57BNhWRUWS00TB0x9HVeZr5nrVMSUe0d8z/5AyERr/eCWz0B2SX66fmDb Q8eQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=NsaIXPNVd1SzIFOGt63S8uUawFUvPMvgsNDgL/uhcuQ=; b=lUL9hvHfbo58hl3C08q1ts+smPz69L6EO+BH4Cw0wCanfTUFahxQcbC/0xhzysPyyX s83t78rHCQ+cpT6cJ4ZOHfzHCbYOf3QIJk0kuXfrrHARIU28/Cj8RFslnfOnZJURG13Y qBxH2ThzDEA0Sc0+dO7LayNaunn5uoUQjMIaeblOedVXCbTsMkYzamsbkIyQoIH9/++2 X0tGlOjldyokFKHq0F+WRNr97hRRhP83Sk1RDheRS0KY23nn7wOjwyXqN9N7xlXd7Wvq tBKdW2Z/j4nUBMT9iVKHPcmEMZne/gZIz+WobezmCcNuYwtghTscelqheWf2tGbHFwGn WjCA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y11si15278928ilu.1.2021.08.02.06.47.59; Mon, 02 Aug 2021 06:47:59 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234401AbhHBNsF (ORCPT + 12 others); Mon, 2 Aug 2021 09:48:05 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:7776 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234366AbhHBNrg (ORCPT ); Mon, 2 Aug 2021 09:47:36 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4GdfSc6vxwzYkNQ; Mon, 2 Aug 2021 21:47:12 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 2 Aug 2021 21:47:16 +0800 Received: from thunder-town.china.huawei.com (10.174.179.0) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 2 Aug 2021 21:47:15 +0800 From: Zhen Lei To: Greg Kroah-Hartman , stable CC: Zhen Lei , Anna-Maria Gleixner , Mike Galbraith , Sasha Levin , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , linux-kernel Subject: [PATCH 4.4 08/11] futex: Handle transient "ownerless" rtmutex state correctly Date: Mon, 2 Aug 2021 21:46:21 +0800 Message-ID: <20210802134624.1934-9-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.26.0.windows.1 In-Reply-To: <20210802134624.1934-1-thunder.leizhen@huawei.com> References: <20210802134624.1934-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.179.0] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: stable@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 --- kernel/futex.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) -- 2.26.0.106.g9fadedd diff --git a/kernel/futex.c b/kernel/futex.c index 8f6372d3a1feea0..e7c2e552aef4ae6 100644 --- 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) {