← Documents Documentation/arch/powerpc/pmu-ebb.rst GitHub 원문 ↗

Linux 6.18.37 · Architecture

PMU Event Based Branches

Perf event를 PMU EBB로 생성·schedule하고 userspace handler에서 처리하는 제한과 lifecycle입니다.

Source pathDocumentation/arch/powerpc/pmu-ebb.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

pmu-ebb.rst:1-138

EBB는 PMU exception을 kernel interrupt path 대신 현재 process의 userspace handler로 전달합니다. Event는 pinned/exclusive이고 일반 perf event와 공존하지 않으며 register 보존은 handler 책임입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ========================
2 PMU Event Based Branches
3 ========================
4
5 Event Based Branches (EBBs) are a feature which allows the hardware to
6 branch directly to a specified user space address when certain events occur.
7
8 The full specification is available in Power ISA v2.07:
9
10 https://www.power.org/documentation/power-isa-version-2-07/
11
12 One type of event for which EBBs can be configured is PMU exceptions. This
13 document describes the API for configuring the Power PMU to generate EBBs,
14 using the Linux perf_events API.
15
16
17 Terminology
18 -----------
19
20 Throughout this document we will refer to an "EBB event" or "EBB events". This
21 just refers to a struct perf_event which has set the "EBB" flag in its
22 attr.config. All events which can be configured on the hardware PMU are
23 possible "EBB events".
24
25
26 Background
27 ----------
28
29 When a PMU EBB occurs it is delivered to the currently running process. As such
30 EBBs can only sensibly be used by programs for self-monitoring.
31
32 It is a feature of the perf_events API that events can be created on other
33 processes, subject to standard permission checks. This is also true of EBB
34 events, however unless the target process enables EBBs (via mtspr(BESCR)) no
35 EBBs will ever be delivered.
36
37 This makes it possible for a process to enable EBBs for itself, but not
38 actually configure any events. At a later time another process can come along
39 and attach an EBB event to the process, which will then cause EBBs to be
40 delivered to the first process. It's not clear if this is actually useful.
41
42
43 When the PMU is configured for EBBs, all PMU interrupts are delivered to the
44 user process. This means once an EBB event is scheduled on the PMU, no non-EBB
45 events can be configured. This means that EBB events can not be run
46 concurrently with regular 'perf' commands, or any other perf events.
47
48 It is however safe to run 'perf' commands on a process which is using EBBs. The
49 kernel will in general schedule the EBB event, and perf will be notified that
50 its events could not run.
51
52 The exclusion between EBB events and regular events is implemented using the
53 existing "pinned" and "exclusive" attributes of perf_events. This means EBB
54 events will be given priority over other events, unless they are also pinned.
55 If an EBB event and a regular event are both pinned, then whichever is enabled
56 first will be scheduled and the other will be put in error state. See the
57 section below titled "Enabling an EBB event" for more information.
58
59
60 Creating an EBB event
61 ---------------------
62
63 To request that an event is counted using EBB, the event code should have bit
64 63 set.
65
66 EBB events must be created with a particular, and restrictive, set of
67 attributes - this is so that they interoperate correctly with the rest of the
68 perf_events subsystem.
69
70 An EBB event must be created with the "pinned" and "exclusive" attributes set.
71 Note that if you are creating a group of EBB events, only the leader can have
72 these attributes set.
73
74 An EBB event must NOT set any of the "inherit", "sample_period", "freq" or
75 "enable_on_exec" attributes.
76
77 An EBB event must be attached to a task. This is specified to perf_event_open()
78 by passing a pid value, typically 0 indicating the current task.
79
80 All events in a group must agree on whether they want EBB. That is all events
81 must request EBB, or none may request EBB.
82
83 EBB events must specify the PMC they are to be counted on. This ensures
84 userspace is able to reliably determine which PMC the event is scheduled on.
85
86
87 Enabling an EBB event
88 ---------------------
89
90 Once an EBB event has been successfully opened, it must be enabled with the
91 perf_events API. This can be achieved either via the ioctl() interface, or the
92 prctl() interface.
93
94 However, due to the design of the perf_events API, enabling an event does not
95 guarantee that it has been scheduled on the PMU. To ensure that the EBB event
96 has been scheduled on the PMU, you must perform a read() on the event. If the
97 read() returns EOF, then the event has not been scheduled and EBBs are not
98 enabled.
99
100 This behaviour occurs because the EBB event is pinned and exclusive. When the
101 EBB event is enabled it will force all other non-pinned events off the PMU. In
102 this case the enable will be successful. However if there is already an event
103 pinned on the PMU then the enable will not be successful.
104
105
106 Reading an EBB event
107 --------------------
108
109 It is possible to read() from an EBB event. However the results are
110 meaningless. Because interrupts are being delivered to the user process the
111 kernel is not able to count the event, and so will return a junk value.
112
113
114 Closing an EBB event
115 --------------------
116
117 When an EBB event is finished with, you can close it using close() as for any
118 regular event. If this is the last EBB event the PMU will be deconfigured and
119 no further PMU EBBs will be delivered.
120
121
122 EBB Handler
123 -----------
124
125 The EBB handler is just regular userspace code, however it must be written in
126 the style of an interrupt handler. When the handler is entered all registers
127 are live (possibly) and so must be saved somehow before the handler can invoke
128 other code.
129
130 It's up to the program how to handle this. For C programs a relatively simple
131 option is to create an interrupt frame on the stack and save registers there.
132
133 Fork
134 ----
135
136 EBB events are not inherited across fork. If the child process wishes to use
137 EBBs it should open a new event for itself. Similarly the EBB state in
138 BESCR/EBBHR/EBBRR is cleared across fork().
139

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

PMU Event Based Branch

1-16

Event Based Branch(EBB)는 특정 event 발생 시 hardware가 지정된 userspace address로 직접 branch하는 Power ISA v2.07 기능입니다.

전체 specification은 `https://www.power.org/documentation/power-isa-version-2-07/`에 있으며, 이 문서는 Linux `perf_events` API로 Power PMU exception을 EBB로 생성하는 방법을 설명합니다.

용어

17-25

`EBB event`는 `attr.config`에 EBB flag를 set한 `struct perf_event`를 뜻합니다. Hardware PMU에서 구성할 수 있는 event라면 모두 EBB event 후보가 될 수 있습니다.

동작 배경과 일반 perf event 배제

26-59

PMU EBB는 현재 실행 중인 process에 전달되므로 실질적으로 self-monitoring program에서만 사용할 수 있습니다.

Permission check를 통과하면 다른 process에 EBB event를 만들 수도 있지만 target이 `mtspr(BESCR)`로 EBB를 enable하지 않으면 전달되지 않습니다. Target이 먼저 EBB를 enable하고 나중에 다른 process가 event를 attach하는 구성도 가능하지만 유용성은 분명하지 않습니다.

PMU가 EBB용으로 구성되면 모든 PMU interrupt가 user process로 전달됩니다. EBB event가 PMU에 schedule된 동안 non-EBB event는 구성할 수 없으므로 일반 `perf` command나 다른 perf event와 동시에 실행할 수 없습니다.

EBB 사용 process를 대상으로 `perf`를 실행하는 것 자체는 안전합니다. Kernel은 보통 EBB event를 우선 schedule하고 perf에는 event가 실행되지 못했다고 알립니다.

이 배제는 기존 `pinned`와 `exclusive` attribute로 구현됩니다. 두 event가 모두 pinned라면 먼저 enable된 event가 schedule되고 다른 event는 error state가 됩니다.

EBB event 생성 조건

60-86

Event code의 bit 63을 set하면 EBB counting을 요청합니다.

조건요구 사항
Event codeBit 63 set
Scheduling`pinned`와 `exclusive` set
Event groupGroup이면 leader만 pinned/exclusive set
금지 attribute`inherit`, `sample_period`, `freq`, `enable_on_exec`를 set하면 안 됨
Target`perf_event_open()`에 pid를 전달해 task에 attach, 보통 현재 task인 0
Group agreementGroup의 모든 event가 EBB를 요청하거나 모두 요청하지 않아야 함
CounterUserspace가 위치를 확실히 알 수 있도록 사용할 PMC를 명시

EBB event 활성화와 scheduling 확인

87-105

Event를 성공적으로 open한 뒤 `ioctl()` 또는 `prctl()` interface로 enable합니다.

Perf API에서 enable 성공은 PMU scheduling을 보장하지 않습니다. Event를 `read()`하여 EOF가 반환되면 PMU에 schedule되지 않았고 EBB도 enabled가 아닙니다.

EBB event는 pinned/exclusive이므로 enable 시 다른 non-pinned event를 PMU에서 밀어낼 수 있습니다. 이미 pinned event가 있으면 enable은 실제 scheduling에 실패합니다.

EBB event lifecycle
`perf_event_open()``ioctl()`/`prctl()` enable`read()` scheduling 확인PMU EBBUserspace handler`close()`
`read()` = EOFNot scheduled / EBB disabled

생성 조건 검증부터 PMU scheduling 확인, handler delivery와 close까지 이어집니다.

EBB event 읽기

106-113

EBB event에 `read()`를 호출할 수는 있지만 count 결과는 의미가 없습니다. Interrupt가 user process로 직접 전달되어 kernel이 event를 count할 수 없으므로 junk value를 반환합니다.

EBB event 닫기

114-121

일반 perf event처럼 `close()`로 종료합니다. 마지막 EBB event를 닫으면 PMU가 deconfigure되고 더 이상 PMU EBB가 전달되지 않습니다.

EBB handler

122-132

EBB handler는 일반 userspace code지만 interrupt handler 방식으로 작성해야 합니다. 진입 시 모든 register가 live일 수 있으므로 다른 code를 호출하기 전에 보존해야 합니다.

C program에서는 stack에 interrupt frame을 만들고 register를 저장하는 방식이 비교적 단순합니다.

Fork 동작

133-138

EBB event는 `fork()`를 통해 inherit되지 않습니다. Child process는 자체 event를 새로 열어야 하며 `BESCR`, `EBBHR`, `EBBRR`의 EBB state도 fork 때 clear됩니다.