From patchwork Wed Sep 27 03:31:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 145462 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2753811vqu; Wed, 27 Sep 2023 09:36:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTIeHX+ewikKJD2RuAw84q2dLF1FuqpgdQjLJhWW+og8DcdMiJobVWO+GIblOGQoBAodwm X-Received: by 2002:a17:903:22ce:b0:1c3:a814:a12b with SMTP id y14-20020a17090322ce00b001c3a814a12bmr4447778plg.16.1695832610768; Wed, 27 Sep 2023 09:36:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695832610; cv=none; d=google.com; s=arc-20160816; b=slqH2kQN1Vatzp3VNj/1QDpD0pmOD1mB3vEsll6zZF1+j/IfqCkmez6G7snDvjhHzP wL8YbXvqVwoYY6hM0DgojAy+8bK4AV1l3/0AlwIk1/HTWqW2vQmP20ppWEnECsvcuwZ8 rS/Awtp81kfs7WuyWpsgsK/VST21RjNI/AyvQfEblN6sQpoiuxbZp2D1vHJ/bgwVRsHN LP2kBEncLVymOjqucBgwl0IeNh3pBN72FeKbwMRiOm9qH0hleLpAgpKrPFUBpO6HyobG 0s12z3xf2vPQ372bTqrpsfMByA5wj5nBLY0QfvN4TS2FTVuCqultp8y6Yw08NIuFUsbx aAhA== 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=xiNq0QGbeg3EHphJWvq2QGOyxypTdGABpiVv+kHL4Zo=; fh=SYwTdU7AJ6bh+wuJYieKOpYo9FOAEtzrlv0Krk9EVUs=; b=LgN+KLDOF7FJY1KD+pVobGk6vNwWPVneKSquBK6ep5aZn5nqpvj9FwNr2SgWveIsUN UdlzHk/2QWa8gu5YH52HK0T0JhrphjSqfjO+ddKZT6B/ng6uQ+a1tYi1RHpQ3ThI65Ww iYccemYG1CXDoizWxBXC/ciSKRe9bCnNw/ZDmBYl/o0rE5PXECiA6XamhL+/v+uSdmCE p46VuivXV8D4Zdt86vBty1kSJPh+q6Dtd6TRRJmmDZLhrKDXS9hcYNwi6xeDwbCCpp1N YB5a36OZtTdtvVae97vRkidjn+C5wmcjgycOdkXZmbFiO1wM66srxwCdCcfTIkKlVTS5 rd1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=A7ITNZhR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id f5-20020a170902ce8500b001c446f12975si12232185plg.447.2023.09.27.09.36.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 09:36:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=A7ITNZhR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C22D28073218; Tue, 26 Sep 2023 21:25:53 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbjI0EZR (ORCPT + 26 others); Wed, 27 Sep 2023 00:25:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbjI0EXq (ORCPT ); Wed, 27 Sep 2023 00:23:46 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9189C140CD; Tue, 26 Sep 2023 20:24:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695785074; x=1727321074; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kF4DhZt8pErot/RT4yIRmpZuAvHJlxj64wy/EpFCWTQ=; b=A7ITNZhR0WyeYjmCqdxKo8ktMntAcwpCPi3wE3GGELbViwwPr+qW/gDc BQpWTHzRa62rHcAYur94xQmZiByg0egwLfiXvfq8IxbgQqf1XBaviOs9W O5IYnLC+AOWzmZdFyaBvF0udZmgqihdjWCpWqoIAO2JaNvfrRtT9Kj0Us //LXYmyXAtVrkPgdFIiLRWwrzceTMoQNzRuN2TxbgPlF8xmnBq/M0pm5r yE45h6m1rN5xjzBPUx0cAScsIW1ZHI2RN0x2ayNKhzhsmSq7XbW7llN+R 9D8+asnAjTo/9Lkkgq0EbM+bFYlorzrpwHcVqhw0be0WK4BvZEIe479A/ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="366780778" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="366780778" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 20:24:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="864637104" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="864637104" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga002.fm.intel.com with ESMTP; 26 Sep 2023 20:24:29 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Peter Zijlstra , Arnaldo Carvalho de Melo , Kan Liang , Like Xu , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter Cc: kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Lv Zhiyuan , Yang Weijiang , Dapeng Mi , Dapeng Mi Subject: [Patch v4 04/13] perf/core: Add function perf_event_move_group() Date: Wed, 27 Sep 2023 11:31:15 +0800 Message-Id: <20230927033124.1226509-5-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230927033124.1226509-1-dapeng1.mi@linux.intel.com> References: <20230927033124.1226509-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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 (groat.vger.email [0.0.0.0]); Tue, 26 Sep 2023 21:25:53 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778209375611759109 X-GMAIL-MSGID: 1778209375611759109 Extract the group moving code in function sys_perf_event_open() to create a new function perf_event_move_group(). The subsequent change would add a new function perf_event_create_group_kernel_counters() which is used to create group events in kernel space. The function also needs to do same group moving for group leader event just like function sys_perf_event_open() does. So extract the moving code into a separate function to avoid the code duplication. Signed-off-by: Dapeng Mi --- kernel/events/core.c | 82 ++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index d485dac2b55f..953e3d3a1664 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12388,6 +12388,48 @@ static int perf_event_group_leader_check(struct perf_event *group_leader, return 0; } +static void perf_event_move_group(struct perf_event *group_leader, + struct perf_event_pmu_context *pmu_ctx, + struct perf_event_context *ctx) +{ + struct perf_event *sibling; + + perf_remove_from_context(group_leader, 0); + put_pmu_ctx(group_leader->pmu_ctx); + + for_each_sibling_event(sibling, group_leader) { + perf_remove_from_context(sibling, 0); + put_pmu_ctx(sibling->pmu_ctx); + } + + /* + * Install the group siblings before the group leader. + * + * Because a group leader will try and install the entire group + * (through the sibling list, which is still in-tact), we can + * end up with siblings installed in the wrong context. + * + * By installing siblings first we NO-OP because they're not + * reachable through the group lists. + */ + for_each_sibling_event(sibling, group_leader) { + sibling->pmu_ctx = pmu_ctx; + get_pmu_ctx(pmu_ctx); + perf_event__state_init(sibling); + perf_install_in_context(ctx, sibling, sibling->cpu); + } + + /* + * Removing from the context ends up with disabled + * event. What we want here is event in the initial + * startup state, ready to be add into new context. + */ + group_leader->pmu_ctx = pmu_ctx; + get_pmu_ctx(pmu_ctx); + perf_event__state_init(group_leader); + perf_install_in_context(ctx, group_leader, group_leader->cpu); +} + /** * sys_perf_event_open - open a performance event, associate it to a task/cpu * @@ -12403,7 +12445,7 @@ SYSCALL_DEFINE5(perf_event_open, { struct perf_event *group_leader = NULL, *output_event = NULL; struct perf_event_pmu_context *pmu_ctx; - struct perf_event *event, *sibling; + struct perf_event *event; struct perf_event_attr attr; struct perf_event_context *ctx; struct file *event_file = NULL; @@ -12635,42 +12677,8 @@ SYSCALL_DEFINE5(perf_event_open, * where we start modifying current state. */ - if (move_group) { - perf_remove_from_context(group_leader, 0); - put_pmu_ctx(group_leader->pmu_ctx); - - for_each_sibling_event(sibling, group_leader) { - perf_remove_from_context(sibling, 0); - put_pmu_ctx(sibling->pmu_ctx); - } - - /* - * Install the group siblings before the group leader. - * - * Because a group leader will try and install the entire group - * (through the sibling list, which is still in-tact), we can - * end up with siblings installed in the wrong context. - * - * By installing siblings first we NO-OP because they're not - * reachable through the group lists. - */ - for_each_sibling_event(sibling, group_leader) { - sibling->pmu_ctx = pmu_ctx; - get_pmu_ctx(pmu_ctx); - perf_event__state_init(sibling); - perf_install_in_context(ctx, sibling, sibling->cpu); - } - - /* - * Removing from the context ends up with disabled - * event. What we want here is event in the initial - * startup state, ready to be add into new context. - */ - group_leader->pmu_ctx = pmu_ctx; - get_pmu_ctx(pmu_ctx); - perf_event__state_init(group_leader); - perf_install_in_context(ctx, group_leader, group_leader->cpu); - } + if (move_group) + perf_event_move_group(group_leader, pmu_ctx, ctx); /* * Precalculate sample_data sizes; do while holding ctx::mutex such