From patchwork Wed Dec 21 18:46:30 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: 9483639 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 D3FA5601D3 for ; Wed, 21 Dec 2016 18:47:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C8F9A28419 for ; Wed, 21 Dec 2016 18:47:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BDD6828458; Wed, 21 Dec 2016 18:47:22 +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 72B0228419 for ; Wed, 21 Dec 2016 18:47:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 217566F0D4; Wed, 21 Dec 2016 18:47:04 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x243.google.com (mail-wm0-x243.google.com [IPv6:2a00:1450:400c:c09::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id 72BCA6E0F0 for ; Wed, 21 Dec 2016 18:46:51 +0000 (UTC) Received: by mail-wm0-x243.google.com with SMTP id m203so32030048wma.3 for ; Wed, 21 Dec 2016 10:46:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PBNPGRumQcLOyHqo2enwYHefuDUK5X6qiIzHXoBVS1g=; b=l27hhKtFKuzh0MCQWHsco9klwno7B5PQ40Jd3d5c1mN6GCFay9GSbTDnJZiaG3ZpT3 6ZjoYhyigUaKYRu3PaEFARMaqGpf6mwWdirRsGPUWJib2Afe8fKDh/BL4sKg2hXpuDus 3tyEnYo1ESqDNz1QhX79qHKrGb6lwn8jcI2wuke4ECTfkdy9rsDEW/EypGl3wio7++OQ pM4my9TWpEJO0JRNQoWvzwKqqKYwGBFHWfSA+YW8rzrEyTEhEPi3objFy338VxG2eC+R jnqO1m3zvjJrOwUzSZD1eXh4+fGAXS28xnY/Kcbh4eDqp+HtcYpvcaS8GVzFM9/dtj1F B1oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PBNPGRumQcLOyHqo2enwYHefuDUK5X6qiIzHXoBVS1g=; b=XVkqQtWZDpDpDaxcdJKlAIQcoNqO7fJxetsZuZB1L4oGoYywMptjR3bQ20ZZstoW0B 0OeMUztVx6Yr+jWzrM0yE7DDSzjGTZMTpMErrk4s5I7AvJVED4oENUQ/wtFNpHmPeSAS 1JgR16FJH9YXrkbfdjT9dnjWDBQ1y3GmMZFx1HVnmnRPRW6izUgcTA8WHdUrj+/fjfCq HH95x6J7d3sANxkvO8QkIzq+I97MWKGkHGWCNvYeTrqEaLsC8anpSBB3sakmxrhxIgiT LcfHrm7K3ikTlhbtD+Jksh3eC5qfojdQkOR9XBrvbRaQMj4mqHhEtUvQ194iUmrD0fHe apww== X-Gm-Message-State: AIkVDXK4AwIFfG+R0Fs2Si6+LnhRzo6blB6zFEJFKTxMJDL+3ehyVJYlmbHCmgyerW6ZMw== X-Received: by 10.28.144.70 with SMTP id s67mr6020446wmd.138.1482346009740; Wed, 21 Dec 2016 10:46:49 -0800 (PST) Received: from cassiopeia.fritz.box ([2001:a61:110a:1701:2511:6687:1ba5:b45b]) by smtp.gmail.com with ESMTPSA id ke6sm31840526wjb.21.2016.12.21.10.46.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 21 Dec 2016 10:46:49 -0800 (PST) From: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= To: linux-kernel@vger.kernel.org Subject: [PATCH v3 02/12] locking/mutex: Fix a race with handoffs and interruptible waits Date: Wed, 21 Dec 2016 19:46:30 +0100 Message-Id: <1482346000-9927-3-git-send-email-nhaehnle@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1482346000-9927-1-git-send-email-nhaehnle@gmail.com> References: <1482346000-9927-1-git-send-email-nhaehnle@gmail.com> MIME-Version: 1.0 Cc: Peter Zijlstra , Ingo Molnar , dri-devel@lists.freedesktop.org, =?UTF-8?q?Nicolai=20H=C3=A4hnle?= 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 There's a possible race where the waiter in front of us leaves the wait list due to a signal, and the current owner subsequently hands the lock off to us even though we never observed ourselves at the front of the list. Set the task state before checking our position in the list, so that the race is handled by falling through the next schedule(). Found by inspection. Cc: Peter Zijlstra Cc: Ingo Molnar Cc: dri-devel@lists.freedesktop.org Signed-off-by: Nicolai Hähnle --- kernel/locking/mutex.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 9b34961..c02c566 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -697,17 +697,18 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, spin_unlock_mutex(&lock->wait_lock, flags); schedule_preempt_disabled(); - if (!first && __mutex_waiter_is_first(lock, &waiter)) { - first = true; - __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); - } - set_task_state(task, state); /* * Here we order against unlock; we must either see it change * state back to RUNNING and fall through the next schedule(), * or we must see its unlock and acquire. */ + + if (!first && __mutex_waiter_is_first(lock, &waiter)) { + first = true; + __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); + } + if ((first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, true)) || __mutex_trylock(lock, first)) break;