From patchwork Mon Nov 28 12:20:07 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: 9449495 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 46695600CB for ; Mon, 28 Nov 2016 12:21:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 367FD26A99 for ; Mon, 28 Nov 2016 12:21:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2AC6127BE5; Mon, 28 Nov 2016 12:21:55 +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 BFAF526A99 for ; Mon, 28 Nov 2016 12:21:54 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DC0576E2D6; Mon, 28 Nov 2016 12:21:53 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wj0-x241.google.com (mail-wj0-x241.google.com [IPv6:2a00:1450:400c:c01::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2B11B6E2BD for ; Mon, 28 Nov 2016 12:20:29 +0000 (UTC) Received: by mail-wj0-x241.google.com with SMTP id jb2so13838493wjb.3 for ; Mon, 28 Nov 2016 04:20:29 -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=49exVXWiuQPTkaSSw2gnwEBj3gutXOCPs+2HLDM1f4o=; b=uFKz7n9QfN8rE8uCTG9JJPAgM8XHvRxqxBJP54UCSkDp3/oabbopyxpBQ2dZ0BU9If sCABF7hCFzEl1dH4NsRmC/k7W9XGkgZQ1yIjEwupdsY6tou2vNIZj2LJbporIg2fGmXO qFZkQ0F/jx+17C4Iv2J7o0ObI9nZm9Jt4ZlDAvwozpx56RA59r4FdaxBExl0UGRd/Zub rv1tFhqvmXK9BfqMC9ihg/9LYqEbQ5crl3fz3cXdC0P2e+3VlXeTvxiJ0Sqehay8Kpuf BhtO090EBiBm0Ih4BuPVc7QzAlyOSuYJGVCJ7vQ6ZW7OKnh0JyWwVDkz/bHL8wDN23bS q0bQ== 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=49exVXWiuQPTkaSSw2gnwEBj3gutXOCPs+2HLDM1f4o=; b=lhsR8j+YjLPpEWOy6uMqHIiCbzOO2VZ7lcy7Y6+RM0OJyjUYhzAwKXreFlW6frBd7R DB1RHoNX6VcusTChNLpK9xf7efJJHmcGHJt9DooKOkTyOHkhbrpQcjdEGO3no884hvqH WtAMYLLDz9f8PV3ZnDsGt9ZBt/QHqopuMrg2UXC+qqk0LtFNI78T5qoINB+c6FbvsxcH Fd9KO5+D2YXTw+xqvcVAwNtpxl2UnTrcPNsGpyK2RxRCE0UcW0VSuzfl/8YJ/KJxwce7 DfsVlYT0ROA2O1CvilKft+HdpoDSTcnonc0vMTfHusqlrE3tQXjd/+3rY7TpclOjsxfl ku1A== X-Gm-Message-State: AKaTC03PgILjDHKVzyaE94KX3MsLeFzt4cnh6BF+0aT4k8GaPcEmNU+oB96RB2lkj1Tlcw== X-Received: by 10.194.85.165 with SMTP id i5mr18692077wjz.147.1480335627547; Mon, 28 Nov 2016 04:20:27 -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.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Nov 2016 04:20:27 -0800 (PST) From: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= To: linux-kernel@vger.kernel.org Subject: [PATCH 06/11] locking/ww_mutex: Notify waiters that have to back off while adding tasks to wait list Date: Mon, 28 Nov 2016 13:20:07 +0100 Message-Id: <1480335612-12069-7-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 While adding our task as a waiter, detect if another task should back off because of us. With this patch, we establish the invariant that the wait list contains at most one (sleeping) waiter with ww_ctx->acquired > 0, and this waiter will be the first waiter with a context. Since only waiters with ww_ctx->acquired > 0 have to back off, this allows us to be much more economical with wakeups. 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 | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 01dcae7..d310703e 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c @@ -609,23 +609,34 @@ void __sched ww_mutex_unlock(struct ww_mutex *lock) EXPORT_SYMBOL(ww_mutex_unlock); static inline int __sched -__ww_mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx) +__ww_mutex_lock_check_stamp(struct mutex *lock, struct mutex_waiter *waiter, + struct ww_acquire_ctx *ctx) { struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx); + struct mutex_waiter *cur; - if (!hold_ctx) - return 0; + if (hold_ctx && __ww_mutex_stamp_after(ctx, hold_ctx)) + goto deadlock; - if (__ww_mutex_stamp_after(ctx, hold_ctx)) { -#ifdef CONFIG_DEBUG_MUTEXES - DEBUG_LOCKS_WARN_ON(ctx->contending_lock); - ctx->contending_lock = ww; -#endif - return -EDEADLK; + /* + * If there is a waiter in front of us that has a context, then its + * stamp is earlier than ours and we must back off. + */ + cur = waiter; + list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) { + if (cur->ww_ctx) + goto deadlock; } return 0; + +deadlock: +#ifdef CONFIG_DEBUG_MUTEXES + DEBUG_LOCKS_WARN_ON(ctx->contending_lock); + ctx->contending_lock = ww; +#endif + return -EDEADLK; } static inline int __sched @@ -654,6 +665,16 @@ __ww_mutex_add_waiter(struct mutex_waiter *waiter, } list_add_tail(&waiter->list, &cur->list); + + /* + * Wake up the waiter so that it gets a chance to back + * off. + */ + if (cur->ww_ctx->acquired > 0) { + debug_mutex_wake_waiter(lock, cur); + wake_up_process(cur->task); + } + return 0; } } @@ -756,7 +777,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, } if (use_ww_ctx && ww_ctx && ww_ctx->acquired > 0) { - ret = __ww_mutex_lock_check_stamp(lock, ww_ctx); + ret = __ww_mutex_lock_check_stamp(lock, &waiter, + ww_ctx); if (ret) goto err; }