From patchwork Mon Feb 1 15:12:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 374265 Delivered-To: patch@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp1383148jah; Mon, 1 Feb 2021 07:16:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJwBKn+8TL2I2eL0ra49zNPzPX4R/BqhYSRRqLs0rxUGuhIRiXTL2v8gv8sV5tZsBOOSzf66 X-Received: by 2002:aa7:d692:: with SMTP id d18mr5737596edr.327.1612192570449; Mon, 01 Feb 2021 07:16:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612192570; cv=none; d=google.com; s=arc-20160816; b=Sx1yvxzSDXTx4sO7QkHDvKE+UN0LJVxlxLlALOWJflevEXa2mWya8jpMuL/I7to8fx CI/KGBR570Zmt3HhVzPnlDopOAG/ekX1f+ijuLwtRNwWhYFjhvhnV+EsyZZS1l7gfjHX 5pRPiSaFn6i3NsW62Jhj0BPhKbqjU9Y8ksEoEstFq41HIGHdK0BogjvNfWdXtakndpzf L5TIdsfQCSncUIT0tDnWzgBYfLzSLjiPP2TpZ6hwDb9u7jPq73E39HAmOCEN0vu9XdbS 2oaFqptXNCcNrJd30YaTCBWN98AYPWnk+C8Hu75SAb9mq9F8HFZwG1Z1s7tPDmP5GY39 t0Sw== 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=q27H37/zpcTikuASjIMD4FhxtWUcta8dDgeh38mJ6rY=; b=QGkoRkD+9QfD4Tn91Pkzu0SC6va6ZOYBdAGO7bbt6VjOusEcbqsdWHgpmj7XtyDRCm V3CaNdwzqGREc2+DpBbrc15RXeEgTls55cvxxufY7o9hpGB3fdBlwCBEqHr+KRXIbwQm qOrHcUeKpcsTkSEqCL6Wb9brM0D313ZM6faf0wkDuwZjZL6me6TwfsxRSMOmOYTFcuaj Ya0Nep8/GQoMXGW2yfoYmoQ8/kyYp0q8D/k/qeLCDfy1QjHAGaeatsSdB48Avk1znqBC V0mYvP6FnnwOOhrSRcPstKFAVvuU2GLr6E51fwEkGJms0Yq6oIXelWs+0FROawrjgLNP l+eA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q4MQbzN7; 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 f29si11574766edj.297.2021.02.01.07.16.10; Mon, 01 Feb 2021 07:16:10 -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=Q4MQbzN7; 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 S231390AbhBAPPB (ORCPT + 13 others); Mon, 1 Feb 2021 10:15:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231463AbhBAPO5 (ORCPT ); Mon, 1 Feb 2021 10:14:57 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C18FC061794 for ; Mon, 1 Feb 2021 07:13:46 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id g10so16980654wrx.1 for ; Mon, 01 Feb 2021 07:13:46 -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=q27H37/zpcTikuASjIMD4FhxtWUcta8dDgeh38mJ6rY=; b=Q4MQbzN7KagSSyZ68E9POBDyRqxUcOspqukH42Stuk/h19Vv+2ghdy8TUd6Yhtw+sJ fJPNPL1vInwJXsofZ+4Py7dCY4waVqj6sjKpzQ+Eu1RcsQHD1LxQAzAx59jMjWyvz6yN cdVNCz0j661b86g4pKYv9U5Pg6ffG9Bjxzk26TljQ4lc1oR6cb434Jkn2JLQeI1lUFz9 wbkXTykPYT2TuEvOYP+NrPd99OEaAe+VZsmpwDIgLBYypY8FhbxdvYt5LpCglEbZKYlh bI9pHuUUlsjYf8lGiIs1WyE847ZLZVfGTd25NrhlkG/jx7dV9EI8A864c8+9mC41qxlo dyCA== 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=q27H37/zpcTikuASjIMD4FhxtWUcta8dDgeh38mJ6rY=; b=nfcTX7yCyAkv1b4M9K1B5muxATTNq6/2D0nxhj/raM3b6j+U61WUV5fslSssO757WL Zz27ly0XH17QD3zqIA3+0TkFiZ0nVdMrGI+lpXBWLvnqYCSwtd0DEzsk1AHe1g3QCdwb ePf2cVdiysK5bPVTPH0TSfAckWoryghThI4GwHCB6fTr5MRJwmlYXx2IARhUswkcCH1A snBlqjYqh6Xro1dBxbANzm0A3Q8fI0YGK+z7uH4EC3fmfDFHLYToK7nprzE/UajZrxCr c97DpTHy6/CguwhtXXCBMVaP2j+Xmc5kT8jNlEy0QBnd0MKWhd4+h1Q4+z6Hny4OpJQH 0bCA== X-Gm-Message-State: AOAM531kHml/x8Xugk2eBdsMXUJKgkhKXB1KQ90Poyfs1iYm1ZnXzDOu aOD/qqK0ANnxmMqUXdcek9l36Q6Hgz63asOR X-Received: by 2002:a5d:47ae:: with SMTP id 14mr18321414wrb.378.1612192424811; Mon, 01 Feb 2021 07:13:44 -0800 (PST) Received: from dell.default ([91.110.221.188]) by smtp.gmail.com with ESMTPSA id 192sm23323381wme.27.2021.02.01.07.13.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 07:13:44 -0800 (PST) From: Lee Jones To: stable@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Greg Kroah-Hartman , Lee Jones Subject: [PATCH 10/12] futex: Add mutex around futex exit Date: Mon, 1 Feb 2021 15:12:12 +0000 Message-Id: <20210201151214.2193508-11-lee.jones@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210201151214.2193508-1-lee.jones@linaro.org> References: <20210201151214.2193508-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 Signed-off-by: Lee Jones --- 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 aba34bba5e9e3..8c10e97f94fea 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1704,6 +1704,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 feef5ce071aa5..d21b151216aa3 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -3271,11 +3271,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. * @@ -3299,6 +3310,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)