From patchwork Tue Aug 8 06:31:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 132815 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2362434vqr; Tue, 8 Aug 2023 13:00:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEf7djnUwozyBOyWDKM/ehET3g1xkZJqqyl6zISklPa0EXRUZFtqhSvJGiuf+dhKg6mjvVl X-Received: by 2002:a05:6a20:4303:b0:133:38cb:2b93 with SMTP id h3-20020a056a20430300b0013338cb2b93mr903238pzk.9.1691524805828; Tue, 08 Aug 2023 13:00:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691524805; cv=none; d=google.com; s=arc-20160816; b=twIRIhA5IpVuj2OfIIYYikw+gjdSd7BSkUDYaGVXUir8cxSz8SWEhCmkFnxifYFrrx FbSrf6cEff0WV1a75iUBDLIIznBqOH/yv2mUMGw7x8ec77Z+9UH5zWmMKo2yugIwXt80 is+z0x0T8jn8avhSahGy3DX6bWh7MCWkH/tljk8kv3rvXAh7H0JtIUVLnIXSuzMm6Zd8 sjETPsP2B+e5LKRqayOQOzVappzsd1JLpGmdgs4khuu2q8VWcIq6NYTAB+TogjtV10H2 TOqTZq0JaHoP70BMSgSvQw9vQAejA6y9mN4OahESESqCMBcWsEV/9BH+mzULiflyV4un jruQ== 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=QTo5IzW77GAdv4Wun1cjb9NTXHwk49r/0wW/L/RAUGc=; fh=UPwqiesAqvJCK7JcrgEpVEdaX86ysnZ9K1cUIusTBBs=; b=GnipAgCBsV+koyMdNYNYeBChTk5xNbGci/md18iFkytWksiu2QmM7cvBO5/PRa7MPY 4fdpgCDW5jhDtIhYARKju9+XUTxvFvLuvxd6V0yoSxAa1vWRg99XYb0oIdL2XfwMO/6b degtBoHlGZAwcJnXW71Wye3+Y3Ce3TjkepBpvbo+OILceFLgAQF0tM7yYkjCpYPYJhX5 X1Ql/rqPe1DDf+ny8CFMIGZnxqmdluQ5ghpyhcquGX6q1puLY1d+filYOKwG+WFJe3MI b17fAKbS9fjIaHYDMxZNFpPxczyKdItVrxGPx0BmVHShScU8ATDxNT7EzR3oqdycBTuv VeFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nJECElYy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id be9-20020a056a001f0900b006866be8eed2si7606505pfb.137.2023.08.08.12.59.51; Tue, 08 Aug 2023 13:00:05 -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=@intel.com header.s=Intel header.b=nJECElYy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233731AbjHHTDD (ORCPT + 99 others); Tue, 8 Aug 2023 15:03:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233155AbjHHTBu (ORCPT ); Tue, 8 Aug 2023 15:01:50 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8069A8DCDE; Tue, 8 Aug 2023 10:31:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691515899; x=1723051899; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7C56/gt/RGvreA0L41MZxGhb9elLWu8ezfqy+Cf9NVY=; b=nJECElYykVz7TU0yU1QDQ3LXTXHaMzhrIWvCZ3P2QQv7we92N08QA4wf qgyivtU3WqYErA4eu//0Pqa5BGU0HDPXoyk1qTFNw/03++addTZPVoXDv G7fklKwcc85Agya53jNaIJ4eYzpewVxwOkqDq5Nf34Qn+aGIB1FEQuso/ YNyziNu/yEbJjl8W05+5+FyfV4BrB3+z5OieAZNJhnynGN4WYD4DNtfN9 UJ9v9En+XXrfBszzGUOsowIy6HHAuK7+mSYLL0LAaHCkMp64MA0cUUkAI s2V6CFX/7Q6LAG9KwJH7BDP1ZaaMNmBmtem/fpifq+6ad5R3ltKDibrow A==; X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="434581950" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="434581950" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2023 23:26:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="734377665" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="734377665" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga007.fm.intel.com with ESMTP; 07 Aug 2023 23:26:20 -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 RFV v2 04/13] perf/core: Add function perf_event_move_group() Date: Tue, 8 Aug 2023 14:31:02 +0800 Message-Id: <20230808063111.1870070-5-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808063111.1870070-1-dapeng1.mi@linux.intel.com> References: <20230808063111.1870070-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773692315063835366 X-GMAIL-MSGID: 1773692315063835366 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 616391158d7c..15eb82d1a010 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12399,6 +12399,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 * @@ -12414,7 +12456,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; @@ -12646,42 +12688,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