From patchwork Tue Nov 14 21:56:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 165102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a59:b0:164:83eb:24d7 with SMTP id 25csp2226473rwb; Tue, 14 Nov 2023 13:57:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IHpHmjANldNWaZpoyGB/ocS3HhVyy6wpjrF7t31WjgguUeF3T3BeThP2PVXB3oAERRzZqhX X-Received: by 2002:a05:6a00:18a4:b0:6c4:b182:f3d9 with SMTP id x36-20020a056a0018a400b006c4b182f3d9mr11233794pfh.21.1699999024063; Tue, 14 Nov 2023 13:57:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699999024; cv=none; d=google.com; s=arc-20160816; b=Q6ZEhtzwOOyHRyWXYEQjkMtTwhCf6IieOFF1vbvbhPrGe6DtkbNwJZyQJGWnCmZ/l8 L/Qk2j4tD45ikWO2ArEDrW2OqUUib3nrN8SeNifNOD2XKH02QSnEggyO4aYe2gGJ24lz zYRjXa/UoIv9ukdW376Cba4FwdX5KkE4HlKXYZJ10sN+1/4CD4Y8pRfvxhbxxMTNVTra L8n2Ex77uwa7OJY1akZf28//nj5HmjXL/8w23tTrHHqDebNCpIKkKCLeMls1fsRRdE0Z xIPLQNgmJbyfE0dEaV1hve/sgmKv61WEtTCVJ+UyN8UNZ1MsfwfKUtRHEiFnfTAhbV1U 2yAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=uYr1U+hqW8z8NDe7fU2P96uRXyUsLHKUNnItYoIx7xA=; fh=H2MkSknZKy/XcUE0SNWlqZifRHvGVZQUr5cmv0Cbtz8=; b=Fb6KtKgyXboxZEwN6qEyyOe/OuHz72WZax60kRfC+8DG2SvnDgd8BOBspP6iFUrtsg wHwpFSNPncZuy9Y/iP7BpD6+PCHTbFobtXh8wqHIUFev/Y9z0Z9AX0fJlLmhQMl4XwzQ 0NsMYbK60qH9oANfTXvTC9MXSlB1MeKouFA/CJPHy/UhD2Di2cexNW3lXQlBcOSgihd7 3bsxloU9cpFzd2g4j9GEpe5gAbP2JsgopJS9ORsMUivCARyKdJUwPCYPrfyiqtsFv+Vv P7nDIXjV5eRzCCQ0kuQ6YDo9Rzxwlml2mVTwC1LgyM2eqxKTVR/OuMKA3HVHALUlbxfi ksHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Lk1J4vqg; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id bz9-20020a056a02060900b005be3685c1ccsi9686323pgb.567.2023.11.14.13.57.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 13:57:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Lk1J4vqg; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 5148581121E9; Tue, 14 Nov 2023 13:57:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234017AbjKNV46 (ORCPT + 29 others); Tue, 14 Nov 2023 16:56:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232443AbjKNV4z (ORCPT ); Tue, 14 Nov 2023 16:56:55 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70038D6; Tue, 14 Nov 2023 13:56:51 -0800 (PST) Date: Tue, 14 Nov 2023 21:56:49 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699999009; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uYr1U+hqW8z8NDe7fU2P96uRXyUsLHKUNnItYoIx7xA=; b=Lk1J4vqgTEn7LqP2S4wBYEPbbQihzELUr8yGdOzbWjhL8yNQcRosA8J2OJZgW14n1wOZs9 tkPj3eYxG68l8WRGa5zpQC4s9r57RNMwEcxD7EWFT+ojRUjMs2V34mb2CL2SNXo1YeftJe 5ozMosAPkdKpf8YcfRAXQvUx1Tup8WBHetvofTN50OWTCXwVQiuu1pToU4FSO9ffmpVc81 Gwsc+u1lQlfE2hnaiqIrziUwQgfbB1Z5QfvfBk0BY8RwVpp/kv/u3oYHsqELJMwdpOXbrM xYCxol5InH7MLmTNI1sTwUxXxUl3BWJtaaBbdwj6CoAKSuT1KceUulnsADcstg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699999009; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uYr1U+hqW8z8NDe7fU2P96uRXyUsLHKUNnItYoIx7xA=; b=VznPNdtjmX8kkobLTTTJZVUqPIkuoqY4UTRZ6S/sRXg0a7K6AWyMfGnt7KJnwkDgfa8J5p MJ8LI7gE9vv9I+DQ== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/urgent] perf: Fix cpuctx refcounting Cc: "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230612093539.085862001@infradead.org> References: <20230612093539.085862001@infradead.org> MIME-Version: 1.0 Message-ID: <169999900905.391.7191385772913382509.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 14 Nov 2023 13:57:03 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1768491458935085211 X-GMAIL-MSGID: 1782578176379685173 The following commit has been merged into the perf/urgent branch of tip: Commit-ID: cfaf1b99ac72d339c586d053885554dbe169d276 Gitweb: https://git.kernel.org/tip/cfaf1b99ac72d339c586d053885554dbe169d276 Author: Peter Zijlstra AuthorDate: Fri, 09 Jun 2023 12:34:46 +02:00 Committer: Peter Zijlstra CommitterDate: Tue, 14 Nov 2023 22:27:28 +01:00 perf: Fix cpuctx refcounting Audit of the refcounting turned up that perf_pmu_migrate_context() fails to migrate the ctx refcount. Fixes: bd2756811766 ("perf: Rewrite core context handling") Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20230612093539.085862001@infradead.org --- include/linux/perf_event.h | 13 ++++++++----- kernel/events/core.c | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 4b93ff1..d2a15c0 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -843,11 +843,11 @@ struct perf_event { }; /* - * ,-----------------------[1:n]----------------------. - * V V - * perf_event_context <-[1:n]-> perf_event_pmu_context <--- perf_event - * ^ ^ | | - * `--------[1:n]---------' `-[n:1]-> pmu <-[1:n]-' + * ,-----------------------[1:n]------------------------. + * V V + * perf_event_context <-[1:n]-> perf_event_pmu_context <-[1:n]- perf_event + * | | + * `--[n:1]-> pmu <-[1:n]--' * * * struct perf_event_pmu_context lifetime is refcount based and RCU freed @@ -865,6 +865,9 @@ struct perf_event { * ctx->mutex pinning the configuration. Since we hold a reference on * group_leader (through the filedesc) it can't go away, therefore it's * associated pmu_ctx must exist and cannot change due to ctx->mutex. + * + * perf_event holds a refcount on perf_event_context + * perf_event holds a refcount on perf_event_pmu_context */ struct perf_event_pmu_context { struct pmu *pmu; diff --git a/kernel/events/core.c b/kernel/events/core.c index ce984aa..0825098 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4828,6 +4828,11 @@ find_get_pmu_context(struct pmu *pmu, struct perf_event_context *ctx, void *task_ctx_data = NULL; if (!ctx->task) { + /* + * perf_pmu_migrate_context() / __perf_pmu_install_event() + * relies on the fact that find_get_pmu_context() cannot fail + * for CPU contexts. + */ struct perf_cpu_pmu_context *cpc; cpc = per_cpu_ptr(pmu->cpu_pmu_context, event->cpu); @@ -12897,6 +12902,9 @@ static void __perf_pmu_install_event(struct pmu *pmu, int cpu, struct perf_event *event) { struct perf_event_pmu_context *epc; + struct perf_event_context *old_ctx = event->ctx; + + get_ctx(ctx); /* normally find_get_context() */ event->cpu = cpu; epc = find_get_pmu_context(pmu, ctx, event); @@ -12905,6 +12913,11 @@ static void __perf_pmu_install_event(struct pmu *pmu, if (event->state >= PERF_EVENT_STATE_OFF) event->state = PERF_EVENT_STATE_INACTIVE; perf_install_in_context(ctx, event, cpu); + + /* + * Now that event->ctx is updated and visible, put the old ctx. + */ + put_ctx(old_ctx); } static void __perf_pmu_install(struct perf_event_context *ctx, @@ -12943,6 +12956,10 @@ void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) struct perf_event_context *src_ctx, *dst_ctx; LIST_HEAD(events); + /* + * Since per-cpu context is persistent, no need to grab an extra + * reference. + */ src_ctx = &per_cpu_ptr(&perf_cpu_context, src_cpu)->ctx; dst_ctx = &per_cpu_ptr(&perf_cpu_context, dst_cpu)->ctx;