Skip to content

[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] [Intel] Intel: Backport TPMI based RAPL PMU support for next Intel Xeon Granite Rapids (GNR)#836

Merged
opsiff merged 3 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:RAPL_PMU
Jun 3, 2025
Merged

[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] [Intel] Intel: Backport TPMI based RAPL PMU support for next Intel Xeon Granite Rapids (GNR)#836
opsiff merged 3 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:RAPL_PMU

Conversation

@Avenger-285714
Copy link
Copy Markdown
Member

@Avenger-285714 Avenger-285714 commented Jun 2, 2025

bugzilla: https://bugzilla.openanolis.cn/show_bug.cgi?id=9256

RAPL energy counter MSRs are exposed via perf PMU. But this is done by separate code which is not part of RAPL framework, and it cannot be reused by other RAPL Interface drivers like TPMI RAPL.

Introduce two new APIs for PMU support in RAPL framework. This allows TPMI RAPL PMU support and also makes it possible for future cleanups of MSR RAPL PMU code.

Commits from mainline kernel v6.10-rc1:
963a9ad powercap: intel_rapl_tpmi: Enable PMU support
575024a powercap: intel_rapl: Introduce APIs for PMU support
72b8b94 powercap: intel_rapl: Sort header files

Test:
Before backport, there's no "power/energy-{pkg,psys,ram}" events available on GNR:

$ perf list | grep -i energy
$
After backport on GNR:

$ perf list | grep -i energy
power/energy-pkg/ [Kernel PMU event]
power/energy-psys/ [Kernel PMU event]
power/energy-ram/ [Kernel PMU event]
$
$ perf stat -a -e power/energy-pkg/ -e power/energy-ram/ -e power/energy-psys/ sleep 2

Performance counter stats for 'system wide':

        225.78 Joules power/energy-pkg/
         10.71 Joules power/energy-ram/
          0.05 Joules power/energy-psys/

   2.004303771 seconds time elapsed

$
The backported patches are identical as mainline version, and have been verified on Intel GNR platform.

Known issue:
N/A

Configs:
Note that to make RAPL PMU work, TPMI must be enabled in kernel with configs:
CONFIG_INTEL_TPMI=m
CONFIG_INTEL_RAPL_TPMI=m

Link: https://gitee.com/anolis/cloud-kernel/pulls/3293

Summary by Sourcery

Backport upstream RAPL PMU support into the Linux 6.6 kernel, adding unified perf event exposure for TPMI-based Intel RAPL on next-gen Granite Rapids processors

New Features:

  • Add PMU support to Intel RAPL framework via new APIs, enabling TPMI-based energy counter exposure through perf events
  • Expose perf events power/energy-{pkg,psys,ram,cores,gpu} on Intel Granite Rapids

Enhancements:

  • Backport and integrate mainline RAPL PMU registration, event handling, and sysfs attribute setup
  • Hook PMU add/remove calls into the intel_rapl_tpmi driver probe and remove paths

Tests:

  • Verify perf energy events and power readings on Granite Rapids platform

zhang-rui added 3 commits June 2, 2025 12:07
ANBZ: #9256

commit 72b8b94 upstream.

Sort header files alphabetically.

Intel-SIG: commit 72b8b94 powercap: intel_rapl: Sort header files
Backport TPMI based RAPL PMU support for GNR and future Xeons.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
[ Yunying Sun: amend commit log ]
Signed-off-by: Yunying Sun <yunying.sun@intel.com>
ANBZ: #9256

commit 575024a upstream.

Introduce two new APIs rapl_package_add_pmu()/rapl_package_remove_pmu().

RAPL driver can invoke these APIs to expose its supported energy
counters via perf PMU. The new RAPL PMU is fully compatible with current
MSR RAPL PMU, including using the same PMU name and events
name/id/unit/scale, etc.

For example, use below command
 perf stat -e power/energy-pkg/ -e power/energy-ram/ FOO
to get the energy consumption if power/energy-pkg/ and power/energy-ram/
events are available in the "perf list" output.

This does not introduce any conflict because TPMI RAPL is the only user
of these APIs currently, and it never co-exists with MSR RAPL.

Note that RAPL Packages can be probed/removed dynamically, and the
events supported by each TPMI RAPL device can be different. Thus the
RAPL PMU support is done on demand, which means
1. PMU is registered only if it is needed by a RAPL Package. PMU events
   for unsupported counters are not exposed.
2. PMU is unregistered and registered when a new RAPL Package is probed
   and supports new counters that are not supported by current PMU.
   For example, on a dual-package system using TPMI RAPL, it is possible
   that Package 1 behaves as TPMI domain root and supports Psys domain.
   In this case, register PMU without Psys event when probing Package 0,
   and re-register the PMU with Psys event when probing Package 1.
3. PMU is unregistered when all registered RAPL Packages don't need PMU.

Intel-SIG: commit 575024a powercap: intel_rapl: Introduce APIs for PMU support
Backport TPMI based RAPL PMU support for GNR and future Xeons.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
[ Yunying Sun: amend commit log ]
Signed-off-by: Yunying Sun <yunying.sun@intel.com>
ANBZ: #9256

commit 963a9ad upstream.

Enable RAPL PMU support for TPMI RAPL driver.

Intel-SIG: commit 963a9ad powercap: intel_rapl_tpmi: Enable PMU support
Backport TPMI based RAPL PMU support for GNR and future Xeons.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
[ Yunying Sun: amend commit log ]
Signed-off-by: Yunying Sun <yunying.sun@intel.com>
@Avenger-285714 Avenger-285714 requested a review from Copilot June 2, 2025 04:09
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 2, 2025

Reviewer's Guide

Backports upstream v6.10 TPMI-based RAPL perf PMU support by extending the Intel RAPL framework with new PMU APIs in the common driver, updating headers, and integrating PMU hooks in the TPMI RAPL driver.

Sequence Diagram: Adding PMU Support for a RAPL Package

sequenceDiagram
    participant TPMI_Driver as intel_rapl_tpmi_probe
    participant RAPL_Common as intel_rapl_common.c
    participant Perf_Core as Linux Perf Core

    TPMI_Driver->>+RAPL_Common: rapl_package_add_pmu(rp)
    RAPL_Common->>RAPL_Common: Initialize rp->pmu_data (scale, timer, etc.)
    RAPL_Common->>RAPL_Common: rapl_pmu_update(rp)
    alt PMU needs registration/update
        RAPL_Common->>RAPL_Common: Potentially unregister existing rapl_pmu if different config needed
        RAPL_Common->>Perf_Core: perf_pmu_register(&rapl_pmu.pmu, "power", ...)
        Perf_Core-->>RAPL_Common: Registration status
        RAPL_Common->>RAPL_Common: Update rapl_pmu.registered, rp->has_pmu = true
    end
    RAPL_Common-->>-TPMI_Driver: Status
Loading

Class Diagram: Data Structures for RAPL PMU Support

classDiagram
  class rapl_package {
    <<struct>>
    +bool has_pmu (added)
    +rapl_package_pmu_data pmu_data (added)
    # ... other existing fields (not shown for brevity)
  }

  class rapl_package_pmu_data {
    <<new struct>>
    +u64 scale[RAPL_DOMAIN_MAX]
    +raw_spinlock_t lock
    +int n_active
    +list_head active_list
    +ktime_t timer_interval
    +hrtimer hrtimer
  }

  class rapl_pmu {
    <<new struct / global static>>
    +pmu pmu
    +u64 timer_ms
    +unsigned long domain_map
    +bool registered
  }

  rapl_package "1" -- "1" rapl_package_pmu_data : contains
Loading

File-Level Changes

Change Details Files
Introduce RAPL PMU infrastructure in the common Intel RAPL driver
  • Define rapl_pmu and per-package pmu_data structures under CONFIG_PERF_EVENTS
  • Implement perf_event callbacks for init, add, start, update, stop, del, and hrtimer handling
  • Create PMU registration/update routines with sysfs attribute groups and perf_pmu hooks
drivers/powercap/intel_rapl_common.c
Extend RAPL headers to support PMU data structures and APIs
  • Add struct rapl_package_pmu_data definition and CONFIG_PERF_EVENTS guards
  • Augment rapl_package with has_pmu flag and pmu_data field
  • Declare rapl_package_add_pmu and rapl_package_remove_pmu prototypes and stubs
include/linux/intel_rapl.h
Enable PMU support in the Intel TPMI RAPL driver
  • Invoke rapl_package_add_pmu() in intel_rapl_tpmi_probe
  • Invoke rapl_package_remove_pmu() in intel_rapl_tpmi_remove
drivers/powercap/intel_rapl_tpmi.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from avenger-285714. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Avenger-285714 Avenger-285714 requested a review from opsiff June 2, 2025 04:09
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR backports the TPMI-based RAPL PMU support to the Linux 6.6-y kernel, enabling power/energy events on Intel Xeon Granite Rapids systems. Key changes include:

  • Adding new PMU support APIs in the RAPL framework (in include/linux/intel_rapl.h).
  • Integrating PMU support calls into the TPMI RAPL driver (in drivers/powercap/intel_rapl_tpmi.c).

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
include/linux/intel_rapl.h Introduces new data structures and inline functions for PMU support.
drivers/powercap/intel_rapl_tpmi.c Adds PMU initialization and removal calls in the TPMI RAPL driver.

goto err;
}

rapl_package_add_pmu(trp->rp);
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of rapl_package_add_pmu is not checked. It is recommended to handle errors appropriately and perform cleanup if PMU initialization fails.

Suggested change
rapl_package_add_pmu(trp->rp);
ret = rapl_package_add_pmu(trp->rp);
if (ret) {
dev_err(&auxdev->dev, "Failed to add PMU for Package%d, %d\n", info->package_id, ret);
goto err;
}

Copilot uses AI. Check for mistakes.
{
struct tpmi_rapl_package *trp = auxiliary_get_drvdata(auxdev);

rapl_package_remove_pmu(trp->rp);
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that rapl_package_remove_pmu is only called if the PMU was successfully added. Consider setting and checking a flag to indicate whether PMU initialization succeeded.

Copilot uses AI. Check for mistakes.
char name[PACKAGE_DOMAIN_NAME_LENGTH];
struct rapl_if_priv *priv;
#ifdef CONFIG_PERF_EVENTS
bool has_pmu;
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'has_pmu' flag is declared but never updated. Consider setting this flag after a successful PMU initialization to accurately reflect the package's state.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Avenger-285714 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@opsiff opsiff merged commit c85b8c8 into deepin-community:linux-6.6.y Jun 3, 2025
6 of 7 checks passed
@Avenger-285714 Avenger-285714 deleted the RAPL_PMU branch June 3, 2025 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants