From patchwork Mon Nov 28 12:20:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Nicolai_H=C3=A4hnle?= X-Patchwork-Id: 9449469 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2D7C2600CB for ; Mon, 28 Nov 2016 12:20:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1D55927BFF for ; Mon, 28 Nov 2016 12:20:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1241127C0C; Mon, 28 Nov 2016 12:20:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B39C127BFF for ; Mon, 28 Nov 2016 12:20:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 40BD96E2CE; Mon, 28 Nov 2016 12:20:36 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wj0-x242.google.com (mail-wj0-x242.google.com [IPv6:2a00:1450:400c:c01::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id C7CD76E2B9 for ; Mon, 28 Nov 2016 12:20:24 +0000 (UTC) Received: by mail-wj0-x242.google.com with SMTP id o2so9995503wje.2 for ; Mon, 28 Nov 2016 04:20:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yer458507bc7tWxrnimtdXoXFfxfTW5fs8Zw7Q3Ewi0=; b=PjVK1Q8FobHhao8LcP9hd7wCcvAzf4n/6BAgw+m/NxIgmRsJOMbNCOhK6vNDgLag3T y/uNnLDr/+tJAO51nhKzWPqpMujD5KBAPmTyDyNdVKSFk6sMvDobVL0eagxxQzqcEFBU 59AellC7LfDMJZb9Qor7JFmZf09/maDG5Jc4abvvAAkUEMN+jfQjVXXojFrr1s+9mY6Z ewBfip//wBXM2t0QKy66jGS5gDhjDHaEA8iGzw81Ko/Lsgq/5Br1fGiQyeMdB0VwB05u V9iGeiA1muUXPcAPbyF6Qc/+lQwsUwYtmVJbBeduvDrS0xhKCaUro7xj1DTw32fuwBk+ 9A0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yer458507bc7tWxrnimtdXoXFfxfTW5fs8Zw7Q3Ewi0=; b=OYwhA9El4p4uNIZDM3J52/MzsOKFFluzbIGp8LVPs9ArlQZGMTEHe+dpRq/pkpPu8s HKJzTJZP3g26VrL8ovTmFO9ihT88+YhMGfatRtBHHyBY/zg8voRgaKeihV3xrgdBwmKX z/2PwVKdAdBSP0BGOQM1fbpmWTvYHa/U05ZjU+IgGyu16Cy4RZu5bFVKU9mDDocYuZE8 9fj9hAEdOsnG2zdSsvIFCIDCb+fc7XIRTfodWUfsJH+FcOftA63RIaGNZk+7zOYv1HuD s/egOkSWHDjSxwRqQNMfU8vywFD53UR8gqmjQQuByKsXs8OXTaZxmHW00IarzvnTjMQN OrIg== X-Gm-Message-State: AKaTC00vK/76YGDQPD1Sf5Ndu4TnJu1/TinfmwmsXEAoJNk8ByYoW2UJMCWPVr5oEhp/JA== X-Received: by 10.194.0.167 with SMTP id 7mr17819876wjf.80.1480335623191; Mon, 28 Nov 2016 04:20:23 -0800 (PST) Received: from cassiopeia.fritz.box ([2001:a61:1119:dc01:d88b:9432:f601:27ed]) by smtp.gmail.com with ESMTPSA id a13sm28830511wma.18.2016.11.28.04.20.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Nov 2016 04:20:22 -0800 (PST) From: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= To: linux-kernel@vger.kernel.org Subject: [PATCH 02/11] locking/ww_mutex: Re-check ww->ctx in the inner optimistic spin loop Date: Mon, 28 Nov 2016 13:20:03 +0100 Message-Id: <1480335612-12069-3-git-send-email-nhaehnle@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1480335612-12069-1-git-send-email-nhaehnle@gmail.com> References: <1480335612-12069-1-git-send-email-nhaehnle@gmail.com> MIME-Version: 1.0 Cc: Maarten Lankhorst , =?UTF-8?q?Nicolai=20H=C3=A4hnle?= , Peter Zijlstra , dri-devel@lists.freedesktop.org, Ingo Molnar X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Nicolai Hähnle In the following scenario, thread #1 should back off its attempt to lock ww1 and unlock ww2 (assuming the acquire context stamps are ordered accordingly). Thread #0 Thread #1 --------- --------- successfully lock ww2 set ww1->base.owner attempt to lock ww1 confirm ww1->ctx == NULL enter mutex_spin_on_owner set ww1->ctx What was likely to happen previously is: attempt to lock ww2 refuse to spin because ww2->ctx != NULL schedule() detect thread #0 is off CPU stop optimistic spin return -EDEADLK unlock ww2 wakeup thread #0 lock ww2 Now, we are more likely to see: detect ww1->ctx != NULL stop optimistic spin return -EDEADLK unlock ww2 successfully lock ww2 ... because thread #1 will stop its optimistic spin as soon as possible. The whole scenario is quite unlikely, since it requires thread #1 to get between thread #0 setting the owner and setting the ctx. But since we're idling here anyway, the additional check is basically free. Found by inspection. Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Maarten Lankhorst Cc: Daniel Vetter Cc: Chris Wilson Cc: dri-devel@lists.freedesktop.org Signed-off-by: Nicolai Hähnle --- kernel/locking/mutex.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 9b34961..0afa998 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -350,7 +350,8 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock, * access and not reliable. */ static noinline -bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) +bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, + bool use_ww_ctx, struct ww_acquire_ctx *ww_ctx) { bool ret = true; @@ -373,6 +374,28 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) break; } + if (use_ww_ctx && ww_ctx->acquired > 0) { + struct ww_mutex *ww; + + ww = container_of(lock, struct ww_mutex, base); + + /* + * If ww->ctx is set the contents are undefined, only + * by acquiring wait_lock there is a guarantee that + * they are not invalid when reading. + * + * As such, when deadlock detection needs to be + * performed the optimistic spinning cannot be done. + * + * Check this in every inner iteration because we may + * be racing against another thread's ww_mutex_lock. + */ + if (READ_ONCE(ww->ctx)) { + ret = false; + break; + } + } + cpu_relax(); } rcu_read_unlock(); @@ -460,22 +483,6 @@ static bool mutex_optimistic_spin(struct mutex *lock, for (;;) { struct task_struct *owner; - if (use_ww_ctx && ww_ctx->acquired > 0) { - struct ww_mutex *ww; - - ww = container_of(lock, struct ww_mutex, base); - /* - * If ww->ctx is set the contents are undefined, only - * by acquiring wait_lock there is a guarantee that - * they are not invalid when reading. - * - * As such, when deadlock detection needs to be - * performed the optimistic spinning cannot be done. - */ - if (READ_ONCE(ww->ctx)) - goto fail_unlock; - } - /* * If there's an owner, wait for it to either * release the lock or go to sleep. @@ -487,7 +494,8 @@ static bool mutex_optimistic_spin(struct mutex *lock, break; } - if (!mutex_spin_on_owner(lock, owner)) + if (!mutex_spin_on_owner(lock, owner, use_ww_ctx, + ww_ctx)) goto fail_unlock; }