Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2432059rwb; Mon, 19 Sep 2022 05:11:21 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6gcuXRDZ72WxrVb2oEU+nRNgyGRTNZb2g/ysWCY0mjKe6TVrm6/iaehzQ9G3cRFCMIpEVG X-Received: by 2002:a17:906:ef90:b0:77f:8f0d:e925 with SMTP id ze16-20020a170906ef9000b0077f8f0de925mr12282673ejb.622.1663589481450; Mon, 19 Sep 2022 05:11:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663589481; cv=none; d=google.com; s=arc-20160816; b=EN5PYN6KTTePVj9uQqzmnypijsqVobk/8274p6YRzqCCmXvZDTAq/7KrK1OzTIx1TO Eum6MD/btIFsszQM31GPix08IwT5Ao3/uPYj8lELBejpe3kYcWNmOmzL6Zi0c+iaZ+mq XdADujz3/uQP04edWbqktmyXJiMtpGyVdivD6brpqAxqsksgMxriocCK6lpPBK/hb3ya +u1PDqyGKjO41G2XoNOvHBR/fPAOz6wJr3rPbDLCWqNbGGWrWpxEFh0j/lt6zbPAe4o9 FYw9Dy7hjVzZd4o0ORB5gLOPFEJgHSTXxpYvi9Pit51/D5OcPtqFyvLrN+ZrN0DYoNSK c3hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/3DW1goWuoKer9u+A3DtQ9jlkLup9NoDTU3kbas6vLw=; b=pbdUjOSFYnd6bNn4W6hK8WkNWD436iQTciwZIL/gnpzgLn++z8b3AWt3Wx4tejS5d9 BtW5eOIFun5e4kHvefjITPegqzJwYhp+1or7PKdRBjVYgqLhemUoZyN6s0bNV2ZBaSa9 fe6omF7GCeq+7/KQLcyb8e4fQCYIauxMFF4RH4vtM/R78mtKO5sTpKpBfcRxjJb0XtbB q+CyqhsML0vdtszJ1c9wfMVeilRtP28OxZyGHF7s6yyn0bIZRsjKfqJIKCdCkUSnIYIM 44FmKnCbJux9T3Ugmk5jakxZMEHlcwRXVNBjzE/71b/FiUnsAVYcIHCVTFJCadGZGm97 DDjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="N9OI/owu"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c62-20020a509fc4000000b0044f0dc2cbd6si9061579edf.378.2022.09.19.05.10.55; Mon, 19 Sep 2022 05:11:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="N9OI/owu"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229910AbiISLqA (ORCPT + 99 others); Mon, 19 Sep 2022 07:46:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbiISLp6 (ORCPT ); Mon, 19 Sep 2022 07:45:58 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 913012AE12; Mon, 19 Sep 2022 04:45:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=/3DW1goWuoKer9u+A3DtQ9jlkLup9NoDTU3kbas6vLw=; b=N9OI/owuvMz9blgRWzlwoesiBL OTC+fa+ODBPVgmLMgQ+BbcchiiwfIK71hMcuQQ1exYfDdG+L+PV0252Bzkx8/NfHoYqnyjBCQcU0K Hiqvvma4rZxDVrJiYuF4eY1QUO9nVdMXSzCXM7gFrWnbJGpWu6mktfJg90PZ3o6PRURA6uk92gw7W TnmuKIhYU4DtdvaModSpQKsDcwvrWs5JJP0UrYzxBH7y8NlrAuQ4xu/pPTtZQQUbgdDwO89FYgRyW KOvsYNhKvWKCi769nlTYbEtvGdPmNvQRZvjkrtSZgkKO1MAUWiRx38i5+n76BrTrVF0zftvQ0EJvY M7V1lIVQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1oaFDX-00E4Q6-V2; Mon, 19 Sep 2022 11:45:40 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 8EE30300342; Mon, 19 Sep 2022 13:45:38 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 7480F2B6335FA; Mon, 19 Sep 2022 13:45:38 +0200 (CEST) Date: Mon, 19 Sep 2022 13:45:38 +0200 From: Peter Zijlstra To: Duoming Zhou Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, tglx@linutronix.de, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com Subject: Re: [PATCH] perf/x86/rapl: fix deadlock in rapl_pmu_event_stop Message-ID: References: <20220917144729.118500-1-duoming@zju.edu.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220917144729.118500-1-duoming@zju.edu.cn> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Sep 17, 2022 at 10:47:29PM +0800, Duoming Zhou wrote: > There is a deadlock in rapl_pmu_event_stop(), the process is > shown below: > > (thread 1) | (thread 2) > rapl_pmu_event_stop() | rapl_hrtimer_handle() > ... | if (!pmu->n_active) > raw_spin_lock_irqsave() //(1) | ... > ... | > hrtimer_cancel() | raw_spin_lock_irqsave() //(2) > (block forever) > > We hold pmu->lock in position (1) and use hrtimer_cancel() to wait > rapl_hrtimer_handle() to stop, but rapl_hrtimer_handle() also need > pmu->lock in position (2). As a result, the rapl_pmu_event_stop() > will be blocked forever. > > This patch extracts hrtimer_cancel() from the protection of > raw_spin_lock_irqsave(). As a result, the rapl_hrtimer_handle() could > obtain the pmu->lock. In order to prevent race conditions, we put > "if (!pmu->n_active)" in rapl_hrtimer_handle() under the protection > of raw_spin_lock_irqsave(). > > Fixes: 65661f96d3b3 ("perf/x86: Add RAPL hrtimer support") > Signed-off-by: Duoming Zhou > --- > arch/x86/events/rapl.c | 9 ++++++--- > 1 file changed, 6 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c > index 77e3a47af5a..97c71538d01 100644 > --- a/arch/x86/events/rapl.c > +++ b/arch/x86/events/rapl.c > @@ -219,11 +219,11 @@ static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer) > struct perf_event *event; > unsigned long flags; > > + raw_spin_lock_irqsave(&pmu->lock, flags); > + > if (!pmu->n_active) > return HRTIMER_NORESTART; Except now you return with the lock held... > > - raw_spin_lock_irqsave(&pmu->lock, flags); > - > list_for_each_entry(event, &pmu->active_list, active_entry) > rapl_event_update(event); > > @@ -281,8 +281,11 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode) > if (!(hwc->state & PERF_HES_STOPPED)) { > WARN_ON_ONCE(pmu->n_active <= 0); > pmu->n_active--; > - if (pmu->n_active == 0) > + if (!pmu->n_active) { > + raw_spin_unlock_irqrestore(&pmu->lock, flags); > hrtimer_cancel(&pmu->hrtimer); > + raw_spin_lock_irqsave(&pmu->lock, flags); Doing a lock-break makes the nr_active and list_del thing non-atomic, breaking the whole purpose of the lock. > + } > > list_del(&event->active_entry); Now; did you actually observe this deadlock or is this a code-reading exercise? If you saw an actual deadlock, was cpu-hotplug involved?