Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp676088iog; Mon, 13 Jun 2022 10:23:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vSLbNHQCEEFK9rSi4BKkmU8nLqLmVxfnfvDX4QtAHgfKVF3ESsASRWnBulZKxiH5jh3MLG X-Received: by 2002:a17:90a:5409:b0:1e8:664e:41dd with SMTP id z9-20020a17090a540900b001e8664e41ddmr833070pjh.173.1655141021129; Mon, 13 Jun 2022 10:23:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655141021; cv=none; d=google.com; s=arc-20160816; b=e1oCufNdpOipUhPobHiSx1tDvp6dXgV+8szc9DC9/YXEsv/63o6O8gdNadFjuSVcv3 ZZk5sOKzZdrQBhQZBN5aVDh6vYGHdgtguVje4OVb3IQZ53xWXMm7MVQHRAUCfRvSQDd+ FvZDKwtwmtpbIgh9kkNH6cqv/2YrMBx9RJYLuLgOG5tMqPTtjHYuyBp8v5oCjgDw0XBX G512bkL7Obew42wddMWbkzaba6MGu6C19iEGKhifX+MkViMsqHuGU/m5D+wQyyWACz18 EmVj2mzwgqlifo+V+N5iWf1NqcX8MQ1CI7kQ9UIBhE3qmfBlXMViUPVpsHyK/QXlmwpv o6SA== 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=mt3fuP+cCSiA3z/WonoxJoieU0BtWY4bTV+4sIenAvw=; b=TXt57vSU10FPPBumxyyn9aT4i3g06bSBtTzucMAPB1v1DgBtW/tlXL3pzMlRe6mVJQ oQwjukvub7flfWX/qfzbXkzdKm/2C7qvGyzehXtaYRnThSFWKw7j4gl5uUbi/OA8eg5Y nIixoB6bjhSgHH3koJfVfNBYBSn+CtXylQufatcshz1wcRsVcfC6iK1q8FUjJLLdQb0O C1hxPUqwNhtwNe4K6E/QPV7IQjpbn8ahwqYZx5F4YeUATzuc8qt51RwyNv3VVqShozLn /XIqhuwiNRdDS/4XqtMCVnMAL9ilQLJ29Mw+x/vH3mOpjsanmR7yU7zr2ah/1AKUqTYS JWqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=iuiTqtcM; 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 u5-20020a17090341c500b001678adeceecsi11517027ple.384.2022.06.13.10.23.26; Mon, 13 Jun 2022 10:23:41 -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=casper.20170209 header.b=iuiTqtcM; 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 S237008AbiFMQwF (ORCPT + 99 others); Mon, 13 Jun 2022 12:52:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236579AbiFMQvu (ORCPT ); Mon, 13 Jun 2022 12:51:50 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1469B1C2045 for ; Mon, 13 Jun 2022 07:37:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=mt3fuP+cCSiA3z/WonoxJoieU0BtWY4bTV+4sIenAvw=; b=iuiTqtcML6fcps3YzLvQaYNOUK 8UvXhncw5yePv7qZ8anU8Js6hub5k6UZ1rT+3bFZRkxy0HVa4EOrSP1FesCLokSGLokv0IauUIoeG R9Es3/oUj1agM3e9FWdBHawZvy5S/XO/fDMzFdeLD1x0QlusDYGqClbMzELqfQQewPw29kN/dBzEE JWnSlhi/syYVUv95kO8CElU/V1JxoR9ppeBXY0TxoVIbLky5ARD4mBF4Bf9aDFvxJrE8t6MdOfd6+ +1nwzBMBPifi1Mj/6FKcOGpRwGPYRyrkliqXZN79TJfF8K6d2V7Tlz6KcjsIuf1qSPkdUzWkqhmyQ K35bAUSA==; Received: from dhcp-077-249-017-003.chello.nl ([77.249.17.3] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0lBW-00Gv2G-NR; Mon, 13 Jun 2022 14:36:55 +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 5190F302E11; Mon, 13 Jun 2022 16:36:54 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 3A2442029F884; Mon, 13 Jun 2022 16:36:54 +0200 (CEST) Date: Mon, 13 Jun 2022 16:36:54 +0200 From: Peter Zijlstra To: Ravi Bangoria Cc: acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, songliubraving@fb.com, eranian@google.com, alexey.budankov@linux.intel.com, ak@linux.intel.com, mark.rutland@arm.com, megha.dey@intel.com, frederic@kernel.org, maddy@linux.ibm.com, irogers@google.com, kim.phillips@amd.com, linux-kernel@vger.kernel.org, santosh.shukla@amd.com Subject: Re: [RFC v2] perf: Rewrite core context handling Message-ID: References: <20220113134743.1292-1-ravi.bangoria@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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,T_SCC_BODY_TEXT_LINE 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 Mon, Jun 13, 2022 at 04:35:11PM +0200, Peter Zijlstra wrote: > > > Right, so sorry for being incredibly tardy on this. Find below the > patch fwd ported to something recent. > > I'll reply to this with fixes and comments. You write: >> A simple perf stat/record/top survives with the patch but machine >> crashes with first run of perf test (stale cpc->task_epc causing the >>crash). Lockdep is also screaming a lot :) > @@ -7669,20 +7877,15 @@ static void perf_event_addr_filters_exec > void perf_event_exec(void) > { > struct perf_event_context *ctx; > - int ctxn; > - > - for_each_task_context_nr(ctxn) { > - perf_event_enable_on_exec(ctxn); > - perf_event_remove_on_exec(ctxn); > > - rcu_read_lock(); > - ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); > - if (ctx) { > - perf_iterate_ctx(ctx, perf_event_addr_filters_exec, > - NULL, true); > - } > - rcu_read_unlock(); > + rcu_read_lock(); > + ctx = rcu_dereference(current->perf_event_ctxp); > + if (ctx) { > + perf_event_enable_on_exec(ctx); > + perf_event_remove_on_exec(ctx); > + perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true); > } > + rcu_read_unlock(); > } > > struct remote_output { The above goes *bang* because perf_event_remove_on_exec() will take a mutex, which isn't allowed under rcu_read_lock(). The below cures. --- --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4384,8 +4384,6 @@ static void perf_event_remove_on_exec(st unsigned long flags; bool modified = false; - perf_pin_task_context(current); - mutex_lock(&ctx->mutex); if (WARN_ON_ONCE(ctx->task != current)) @@ -4406,13 +4404,11 @@ static void perf_event_remove_on_exec(st raw_spin_lock_irqsave(&ctx->lock, flags); if (modified) clone_ctx = unclone_ctx(ctx); - --ctx->pin_count; raw_spin_unlock_irqrestore(&ctx->lock, flags); unlock: mutex_unlock(&ctx->mutex); - put_ctx(ctx); if (clone_ctx) put_ctx(clone_ctx); } @@ -7878,14 +7874,16 @@ void perf_event_exec(void) { struct perf_event_context *ctx; - rcu_read_lock(); - ctx = rcu_dereference(current->perf_event_ctxp); - if (ctx) { - perf_event_enable_on_exec(ctx); - perf_event_remove_on_exec(ctx); - perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true); - } - rcu_read_unlock(); + ctx = perf_pin_task_context(current); + if (!ctx) + return; + + perf_event_enable_on_exec(ctx); + perf_event_remove_on_exec(ctx); + perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true); + + perf_unpin_context(ctx); + put_ctx(ctx); } struct remote_output {