From patchwork Mon Feb 1 10:01:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 374227 Delivered-To: patch@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp1120324jah; Mon, 1 Feb 2021 02:04:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJyiSHK3XtOlTOAys3GucmOyd8iBaRycicnhDr4mOl2gpTMKr6+stp6Gts3KURODpj+CNbn7 X-Received: by 2002:a05:6402:b6f:: with SMTP id cb15mr17704131edb.277.1612173897825; Mon, 01 Feb 2021 02:04:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612173897; cv=none; d=google.com; s=arc-20160816; b=Ut52qIzb/AYRrTDgFPFdSRb8xuBOT3J8pbDdVYTpUzdEPlwUkAz3cU+O6adIgUBXTq VogTZMEtfkZPv9+WzYO8soP1uuMkqvC8gmvl3MXMk1DZ6Y3FKYBz7a480STDTYiLtu4u A8mXwLKZKHgqaIDn3gcqPTbAu20myhgaYWZ28TanOnvjsQVZejh2dvmR6Sd1Jbzd6AXu ZXGWVDGz6V7Kc4QQifNMu/G+EQdN7Wvg3l7A7OkWKOFGpRKA3MGOz1bGdci03BhloaSx HUBsA0+m+DTQrd6zkVEg+FBRhnVmZ6cNCTv6Kb04qaRGbg0pP6XUrkBuO6b85sn38AJk 1xDQ== 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 :dkim-signature; bh=aVoAzw89+a17gR7Osu1Lbh2IFkjRoOStrGb7pIoW1Us=; b=krIP0RePAvW2eIDNCh5SLr0i2dKlxtqsF+OXNhIMEjXMEdC4IrKg3N6Z/CLI8jdxPn iFHZAzj/o/JUts0NZ2X4mTcjKqpSp7iqTOk7iKyhkJTsp/6iHHYP3hGTf9nWkf9mf0xa Qwis7pP77TikTslv5/xojQ3f3Ci87LZZta81sFKTaJbbHES/1JCOAdl4bm92Sr6NxP5r Y1G2PGT36DJL3C9pYpaQ22JFEbcoLFeKhXoogr3CJDNuf9rlTTIAV4hPY0zX07J6t/sT HWGJX/FstMAwQJAwDNhYADNZMCy9N5MVgjhiZpjB0IOjRjfPLNaaae7w4VcJG2dQbwOC uNdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rT0MzlDN; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m6si13120787ejl.643.2021.02.01.02.04.57; Mon, 01 Feb 2021 02:04:57 -0800 (PST) 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; dkim=pass header.i=@linaro.org header.s=google header.b=rT0MzlDN; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232752AbhBAKDg (ORCPT + 13 others); Mon, 1 Feb 2021 05:03:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233011AbhBAKDf (ORCPT ); Mon, 1 Feb 2021 05:03:35 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F1EEC061794 for ; Mon, 1 Feb 2021 02:02:24 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id g10so15885102wrx.1 for ; Mon, 01 Feb 2021 02:02:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aVoAzw89+a17gR7Osu1Lbh2IFkjRoOStrGb7pIoW1Us=; b=rT0MzlDNrgArkLuFqncWDG0LBLW5ELPTy8ULBD15CvQRx8+gcP9t/8VSy4EdLromj5 VLWa0yGdxhZvLzYGMBGTxARnLG7JRg0pO/diAysLoxXlkwIluPn/TxfajZNlK9OaRZjW Bq0n8CXM5Jr3Wh03b/nmxT46Vb6ADpXcMP7WsSxXsTog9MwJWPvRqDMoBbwk5BtSJp/3 LhbteVvA8AW5Qthp8ahEpGZosxjiK/8CZ1qui/upDqbq7GVzf937jKiuwH0dp8PQaVG8 QNo5CkqMyupCYN5QWguuDrrq/NkhQ1b7PGkZ4L0GLY6lGLZmf7DuL1Q2OX45Q9H/A8CS Pt5w== 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=aVoAzw89+a17gR7Osu1Lbh2IFkjRoOStrGb7pIoW1Us=; b=L5VjWkcd2hrlei68RVdGnIZWqbbSGUQC65LZVyit7/xfBycAslqhJ1fZ82pLzK4/Q8 J4zpDYNsPI5WD5jo2QB5J9XOduQS1lKAJO/UUxn8V7RCnj5pH7esqf0g28CLSJtpV7U2 IzkS20cz6x/aOknivkO6KGoqQGLl4gBE8s8lMzajcJI3a5Gu2aTSJO9y3MBDOwpB6KsW giOCW8jiDGsPNK06pq+TzViMasXy8JggA8F/jVSPG7ehbxx3klHAFu+JO7xqtg/mQq6f dHJh7FuSLQmtDbrbu+zAFZYCbCUFJUsWMT+9yGtznHrCuWbTtoQacRIhEYSud5UAYjZl gGbQ== X-Gm-Message-State: AOAM533INe74joDy+eyGZTD2u9xFsQUk6MufbswmEDYQTjsoFF0T7mHS T8Qu6K2u4Kyyfm89n7I5tA8sPF7TLyPhZHRP X-Received: by 2002:adf:e48b:: with SMTP id i11mr17343924wrm.406.1612173742392; Mon, 01 Feb 2021 02:02:22 -0800 (PST) Received: from dell.default ([91.110.221.188]) by smtp.gmail.com with ESMTPSA id p15sm26151387wrt.15.2021.02.01.02.02.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 02:02:21 -0800 (PST) From: Lee Jones To: stable@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Greg Kroah-Hartman Subject: [PATCH 10/12] futex: Add mutex around futex exit Date: Mon, 1 Feb 2021 10:01:41 +0000 Message-Id: <20210201100143.2028618-11-lee.jones@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210201100143.2028618-1-lee.jones@linaro.org> References: <20210201100143.2028618-1-lee.jones@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Thomas Gleixner commit 3f186d974826847a07bc7964d79ec4eded475ad9 upstream. The mutex will be used in subsequent changes to replace the busy looping of a waiter when the futex owner is currently executing the exit cleanup to prevent a potential live lock. Signed-off-by: Thomas Gleixner Reviewed-by: Ingo Molnar Acked-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20191106224556.845798895@linutronix.de Signed-off-by: Greg Kroah-Hartman --- include/linux/futex.h | 1 + include/linux/sched.h | 1 + kernel/futex.c | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) -- 2.25.1 diff --git a/include/linux/futex.h b/include/linux/futex.h index 805508373fcea..0f294ae63c78c 100644 --- a/include/linux/futex.h +++ b/include/linux/futex.h @@ -70,6 +70,7 @@ static inline void futex_init_task(struct task_struct *tsk) INIT_LIST_HEAD(&tsk->pi_state_list); tsk->pi_state_cache = NULL; tsk->futex_state = FUTEX_STATE_OK; + mutex_init(&tsk->futex_exit_mutex); } void futex_exit_recursive(struct task_struct *tsk); diff --git a/include/linux/sched.h b/include/linux/sched.h index fcbe5904cbd97..f094882822a63 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1815,6 +1815,7 @@ struct task_struct { #endif struct list_head pi_state_list; struct futex_pi_state *pi_state_cache; + struct mutex futex_exit_mutex; unsigned int futex_state; #endif #ifdef CONFIG_PERF_EVENTS diff --git a/kernel/futex.c b/kernel/futex.c index 50f61d0e51b59..e7798ef3b4b71 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -3306,11 +3306,22 @@ static void futex_cleanup(struct task_struct *tsk) */ void futex_exit_recursive(struct task_struct *tsk) { + /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */ + if (tsk->futex_state == FUTEX_STATE_EXITING) + mutex_unlock(&tsk->futex_exit_mutex); tsk->futex_state = FUTEX_STATE_DEAD; } static void futex_cleanup_begin(struct task_struct *tsk) { + /* + * Prevent various race issues against a concurrent incoming waiter + * including live locks by forcing the waiter to block on + * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in + * attach_to_pi_owner(). + */ + mutex_lock(&tsk->futex_exit_mutex); + /* * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock. * @@ -3334,6 +3345,11 @@ static void futex_cleanup_end(struct task_struct *tsk, int state) * take another loop until it becomes visible. */ tsk->futex_state = state; + /* + * Drop the exit protection. This unblocks waiters which observed + * FUTEX_STATE_EXITING to reevaluate the state. + */ + mutex_unlock(&tsk->futex_exit_mutex); } void futex_exec_release(struct task_struct *tsk)