Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4067038pxb; Mon, 8 Feb 2021 07:11:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJyLjmGMEZHSsKIaTAYfYdo5uOy3eDWzgr7xqmb1Qbwcbdp4m0QpfOAS40nJvDrou4FEcjVf X-Received: by 2002:a17:906:c296:: with SMTP id r22mr17564601ejz.158.1612797110543; Mon, 08 Feb 2021 07:11:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612797110; cv=none; d=google.com; s=arc-20160816; b=HB8GgprjpV1XmWpdEDzy53bMtChb6UR8RphdmyiJB21sFlicF8NeFjQtst/ppbUjOz O518XC3+1bn04hwC9n74Ml78xwGgOaxInZJFaCIGkMufGdjrYdJSGZnzU4P42UJaybDW R3jtC+3UtmpgC5s5SOsGVabzzBPZja5URXNvfuMnjFTF2M69MbSneomRutF3QyMMPvAt cmayuSDyllUwB8uI/t+TInf+c6uMwzpgAbw72smNHKzfk4WaJqLUpcbSVM54ZISSBszY EdfV1Bm9/UD+r3glhUEblLZx4YmziXhLlpxkry9bZQG4wR0qvUEY/8DsNVN1dBPqJGJb VwZg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PBWEVaNBf7dbYpYw3R4WLgMprqVcioNVpi1xOOtdMIU=; b=HzSicIpAKvjaYgMOpTE+fns1pHOHe1SULqBrpCaC42zKsDI9D19h+GHY2HG7pnASnC fAdJrOCMz7CpT/py42ieNsLfUMegpkmqGGmIVcqZF1ZbaI0IUhEpryPre7Ecmg+vFYVq pOUwzhbAZenfsv4rLdHA+sganHidc7l3FvRcsan3pGBNluL7+62L0SIM5KQayOvYnqIn RvajAO4NEdgIZGQnk8B+yVyaLFmzBtLwOPCEZPuVGeivXWqzctUeKdQ5QhEUghDtXdKB /+eVGwleIqm4nxbgw/F4O/gL3jVV0kzasMON9AjOzkdutXQOPURUpxgbTE2/Gqeuf4/J +6dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GKLimW2T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h12si13339315edk.467.2021.02.08.07.11.21; Mon, 08 Feb 2021 07:11:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=GKLimW2T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232844AbhBHPKC (ORCPT + 99 others); Mon, 8 Feb 2021 10:10:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:52064 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232560AbhBHPD3 (ORCPT ); Mon, 8 Feb 2021 10:03:29 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id A302A64E9D; Mon, 8 Feb 2021 15:02:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612796541; bh=D/cuF8HNNnjapB3LbsVqBvyvmr3HSkTynXjdm9FHBwU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GKLimW2TRzrKOqx2RAn48qb4TfJ89d3zfMkQbUS/tiWz4i/TX3fb/NA/fCXjW9AqD LF+NTX9WiX9nZuwU8ECHjDTiAivxYLMpLRgxM1J9aRorI7IJ2C6IgVP8f+2k1X0WOB yzGPIfIzfJKjAm+bEclNVhArOhlmxRZZhi+YFegs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Thomas Gleixner , "Peter Zijlstra (Intel)" , Lee Jones Subject: [PATCH 4.4 07/38] futex: Provide and use pi_state_update_owner() Date: Mon, 8 Feb 2021 16:00:29 +0100 Message-Id: <20210208145805.576321276@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210208145805.279815326@linuxfoundation.org> References: <20210208145805.279815326@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lee Jones 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 Signed-off-by: Greg Kroah-Hartman --- kernel/futex.c | 64 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) --- a/kernel/futex.c +++ b/kernel/futex.c @@ -835,6 +835,29 @@ static struct futex_pi_state * alloc_pi_ 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 *uad 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; /*