From patchwork Thu Feb 4 17:28:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 376314 Delivered-To: patch@linaro.org Received: by 2002:a02:b18a:0:0:0:0:0 with SMTP id t10csp1492367jah; Thu, 4 Feb 2021 09:47:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJxqetnXl8haw92kKl3gF0kCmY53WJOU8j/Koihkj+oXpFOjjYjUz7SIhMcdEN6ETKI6GJYP X-Received: by 2002:a05:6402:1bc7:: with SMTP id ch7mr144916edb.124.1612460857616; Thu, 04 Feb 2021 09:47:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612460857; cv=none; d=google.com; s=arc-20160816; b=CVKm9NwXf1L9SNtYRLRnPfyjOz1xF3tDeA5DgP1ydNWembiXVa8W3k6Vul+mSZEhlk EVHhIEls8X3Ym5xTT4NKvezHe1QIr/CVwYGGEedMlBfC6k0L+VXm4/AQM6HbzrI6m8e3 bXAbRUsHOMFuxC0mYfS9ntYM/DXXQM3epVgkKZHVdWST3BWl4VQl12E0ciEkHTiX8Qgk IKPa62+iHW0uP028FhShnmjLK0nY44ktke4fg0uovlZ72qngYY07oM0KOHBMKJW6xRq0 8u8U79nkdHOKjj/ffjPGgglC0E5MXu2zXdoV1ooBmmRMVZ6bd4PTaw3jpD7xEpuS7sbe JtPw== 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=L9WDlXzldPwLcNIVpjw64K4i0YIfrHdhJn+ngLH0dQE=; b=G60HecH6PEP5AZR/afYJxsqLqKKQY3lqS59Fno4/M1AbWvdvauh3uJ1QqPzWQMuJe9 D8vVzudrL/j5pNo12LrfBcnJh4Uzn1oXjvDQDIkdpN4rLna3uIOAwckYYr3mFTC1Nlmb Y2zoYOebeht0r2IvTq5FX5/aVPWpI1XxARtU5y0bfc75tpiG6CTALkmtU7YUkV6kM6Ta jXCQKZHhtjp9/ulYIgRMDOw9SRNUc4rM25xZeK+q0qXkGVJPFoRj/hVIt+cF+jWjoZmn jHRdV9PxyGtdfo2vfG6PhjXUvtbWJAvnLVVAv67W/huVpLb3rFVJ/npZAj2Z//+ErgI/ ToEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NTLTlxqX; 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 z17si3584606ejc.207.2021.02.04.09.47.35; Thu, 04 Feb 2021 09:47:37 -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=NTLTlxqX; 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 S237290AbhBDRqY (ORCPT + 13 others); Thu, 4 Feb 2021 12:46:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238530AbhBDRaP (ORCPT ); Thu, 4 Feb 2021 12:30:15 -0500 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29E48C061797 for ; Thu, 4 Feb 2021 09:29:16 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id 7so4551333wrz.0 for ; Thu, 04 Feb 2021 09:29:16 -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=L9WDlXzldPwLcNIVpjw64K4i0YIfrHdhJn+ngLH0dQE=; b=NTLTlxqXUs9SwMrPjNG0ZyDO1lAKLCgMYooX9qcFgzVmPo3prVb0uDJfMjfEPYaDKf eICcyeIfhn1SxbnsPc1KoqwxPsJ7LtyDgF/CXrcobUa7U48X/CA+c6ztlB30K/95J/aF FIzbfbLlRlV5sBJtt+M/KHkhPm3LWeGSm9ibbFnPIWvNcO3hqGAfMj96/ddmWtyz+wmi 6gClkTxXEdLakFp8MoKH4rHXNB39SCj0BPgO7OfoQZlh+T7oo3bZa10H+1Ya7zFEAiOG 0Qs/outahWmrt6sW48Gg/9lehbF3NbsHfNUC2MVmG7yuflHTvPlwJthaIp+Y9wicMBfT iD2A== 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=L9WDlXzldPwLcNIVpjw64K4i0YIfrHdhJn+ngLH0dQE=; b=KB76HkRCIke0xA9oR4bfPlLnzI5lBGKlt9dJkutYSrSj08527Ksr7hMrSR4B6lxAbs yTwExnNBbcSxBltPVUfURYeaTif0wIdwvyrPPIAk+O8DdQ5L4/4STnfXt6myJohZ2w9c hm2gHM8njPmWitPqSEaFlUqo/2Wkp3WfIM5K4pzpG+n/zZBNGFf9dqCbjHPt1Dq2Bhur 3F0nCZqbDAN8K8Py1TwNI/jN0KisuB3cI++kRwbRosAskRo6szEDEpSc3BDHm+A347YS TA2xr1DtnaQzxotSlPowEZ6d4sS1NzAPguOaRSxIlh5rC36yQOnklUplSPjxb+/INPh6 /gCg== X-Gm-Message-State: AOAM532KTMB2lI4wLId29ACaLHD7rtwgWoiGNqnuXCVbhMYUeQEKY8jN +HWDRJXXHZy84u94zEcActX78XBJiy+F1Q== X-Received: by 2002:adf:a2ca:: with SMTP id t10mr454572wra.370.1612459754633; Thu, 04 Feb 2021 09:29:14 -0800 (PST) Received: from dell.default ([91.110.221.188]) by smtp.gmail.com with ESMTPSA id j7sm9641334wrp.72.2021.02.04.09.29.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 09:29:14 -0800 (PST) From: Lee Jones To: stable@vger.kernel.org Cc: Thomas Gleixner , Peter Zijlstra , Lee Jones Subject: [PATCH 06/10] futex: Provide and use pi_state_update_owner() Date: Thu, 4 Feb 2021 17:28:59 +0000 Message-Id: <20210204172903.2860981-7-lee.jones@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210204172903.2860981-1-lee.jones@linaro.org> References: <20210204172903.2860981-1-lee.jones@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Thomas Gleixner [ Upstream commit c5cade200ab9a2a3be9e7f32a752c8d86b502ec7 ] Updating pi_state::owner is done at several places with the same code. Provide a function for it and use that at the obvious places. This is also a preparation for a bug fix to avoid yet another copy of the same code or alternatively introducing a completely unpenetratable mess of gotos. Originally-by: Peter Zijlstra Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Cc: stable@vger.kernel.org Signed-off-by: Lee Jones --- kernel/futex.c | 64 ++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 31 deletions(-) -- 2.25.1 diff --git a/kernel/futex.c b/kernel/futex.c index a247942d69799..1390ffa874a6b 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -835,6 +835,29 @@ static struct futex_pi_state * alloc_pi_state(void) return pi_state; } +static void pi_state_update_owner(struct futex_pi_state *pi_state, + struct task_struct *new_owner) +{ + struct task_struct *old_owner = pi_state->owner; + + lockdep_assert_held(&pi_state->pi_mutex.wait_lock); + + if (old_owner) { + raw_spin_lock(&old_owner->pi_lock); + WARN_ON(list_empty(&pi_state->list)); + list_del_init(&pi_state->list); + raw_spin_unlock(&old_owner->pi_lock); + } + + if (new_owner) { + raw_spin_lock(&new_owner->pi_lock); + WARN_ON(!list_empty(&pi_state->list)); + list_add(&pi_state->list, &new_owner->pi_state_list); + pi_state->owner = new_owner; + raw_spin_unlock(&new_owner->pi_lock); + } +} + /* * Must be called with the hb lock held. */ @@ -1427,26 +1450,16 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this, else ret = -EINVAL; } - if (ret) { - raw_spin_unlock(&pi_state->pi_mutex.wait_lock); - return ret; - } - - raw_spin_lock_irq(&pi_state->owner->pi_lock); - WARN_ON(list_empty(&pi_state->list)); - list_del_init(&pi_state->list); - raw_spin_unlock_irq(&pi_state->owner->pi_lock); - raw_spin_lock_irq(&new_owner->pi_lock); - WARN_ON(!list_empty(&pi_state->list)); - list_add(&pi_state->list, &new_owner->pi_state_list); - pi_state->owner = new_owner; - raw_spin_unlock_irq(&new_owner->pi_lock); - - /* - * We've updated the uservalue, this unlock cannot fail. - */ - deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q); + if (!ret) { + /* + * This is a point of no return; once we modified the uval + * there is no going back and subsequent operations must + * not fail. + */ + pi_state_update_owner(pi_state, new_owner); + deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q); + } raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); spin_unlock(&hb->lock); @@ -2318,19 +2331,8 @@ retry: * We fixed up user space. Now we need to fix the pi_state * itself. */ - if (pi_state->owner != NULL) { - raw_spin_lock_irq(&pi_state->owner->pi_lock); - WARN_ON(list_empty(&pi_state->list)); - list_del_init(&pi_state->list); - raw_spin_unlock_irq(&pi_state->owner->pi_lock); - } - - pi_state->owner = newowner; + pi_state_update_owner(pi_state, newowner); - raw_spin_lock_irq(&newowner->pi_lock); - WARN_ON(!list_empty(&pi_state->list)); - list_add(&pi_state->list, &newowner->pi_state_list); - raw_spin_unlock_irq(&newowner->pi_lock); return 0; /*