Skip to content

Commit 1acda87

Browse files
author
Robert Richter
committed
oprofile: use new data sample format for ibs
The new ring buffer implementation allows the storage of samples with different size. This patch implements the usage of the new sample format to store ibs samples in the cpu buffer. Until now, writing to the cpu buffer could lead to incomplete sampling sequences since IBS samples were transfered in multiple samples. Due to a full buffer, data could be lost at any time. This can't happen any more since the complete data is reserved in advance and then stored in a single sample. Signed-off-by: Robert Richter <robert.richter@amd.com>
1 parent bd7dc46 commit 1acda87

4 files changed

Lines changed: 76 additions & 137 deletions

File tree

‎arch/x86/oprofile/op_model_amd.c‎

Lines changed: 41 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "op_x86_model.h"
2424
#include "op_counter.h"
25+
#include "../../../drivers/oprofile/cpu_buffer.h"
2526

2627
#define NUM_COUNTERS 4
2728
#define NUM_CONTROLS 4
@@ -60,51 +61,16 @@ static unsigned long reset_value[NUM_COUNTERS];
6061
#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
6162
#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
6263

63-
/* Codes used in cpu_buffer.c */
64-
/* This produces duplicate code, need to be fixed */
65-
#define IBS_FETCH_BEGIN (1UL << 4)
66-
#define IBS_OP_BEGIN (1UL << 5)
67-
6864
/*
6965
* The function interface needs to be fixed, something like add
7066
* data. Should then be added to linux/oprofile.h.
7167
*/
72-
extern void
73-
oprofile_add_ibs_sample(struct pt_regs * const regs,
74-
unsigned int * const ibs_sample, int ibs_code);
75-
76-
struct ibs_fetch_sample {
77-
/* MSRC001_1031 IBS Fetch Linear Address Register */
78-
unsigned int ibs_fetch_lin_addr_low;
79-
unsigned int ibs_fetch_lin_addr_high;
80-
/* MSRC001_1030 IBS Fetch Control Register */
81-
unsigned int ibs_fetch_ctl_low;
82-
unsigned int ibs_fetch_ctl_high;
83-
/* MSRC001_1032 IBS Fetch Physical Address Register */
84-
unsigned int ibs_fetch_phys_addr_low;
85-
unsigned int ibs_fetch_phys_addr_high;
86-
};
68+
extern
69+
void oprofile_add_data(struct op_entry *entry, struct pt_regs * const regs,
70+
unsigned long pc, int code, int size);
8771

88-
struct ibs_op_sample {
89-
/* MSRC001_1034 IBS Op Logical Address Register (IbsRIP) */
90-
unsigned int ibs_op_rip_low;
91-
unsigned int ibs_op_rip_high;
92-
/* MSRC001_1035 IBS Op Data Register */
93-
unsigned int ibs_op_data1_low;
94-
unsigned int ibs_op_data1_high;
95-
/* MSRC001_1036 IBS Op Data 2 Register */
96-
unsigned int ibs_op_data2_low;
97-
unsigned int ibs_op_data2_high;
98-
/* MSRC001_1037 IBS Op Data 3 Register */
99-
unsigned int ibs_op_data3_low;
100-
unsigned int ibs_op_data3_high;
101-
/* MSRC001_1038 IBS DC Linear Address Register (IbsDcLinAd) */
102-
unsigned int ibs_dc_linear_low;
103-
unsigned int ibs_dc_linear_high;
104-
/* MSRC001_1039 IBS DC Physical Address Register (IbsDcPhysAd) */
105-
unsigned int ibs_dc_phys_low;
106-
unsigned int ibs_dc_phys_high;
107-
};
72+
#define IBS_FETCH_SIZE 6
73+
#define IBS_OP_SIZE 12
10874

10975
static int has_ibs; /* AMD Family10h and later */
11076

@@ -197,31 +163,29 @@ static inline int
197163
op_amd_handle_ibs(struct pt_regs * const regs,
198164
struct op_msrs const * const msrs)
199165
{
200-
unsigned int low, high;
201-
struct ibs_fetch_sample ibs_fetch;
202-
struct ibs_op_sample ibs_op;
166+
u32 low, high;
167+
u64 msr;
168+
struct op_entry entry;
203169

204170
if (!has_ibs)
205171
return 1;
206172

207173
if (ibs_config.fetch_enabled) {
208174
rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
209175
if (high & IBS_FETCH_HIGH_VALID_BIT) {
210-
ibs_fetch.ibs_fetch_ctl_high = high;
211-
ibs_fetch.ibs_fetch_ctl_low = low;
212-
rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high);
213-
ibs_fetch.ibs_fetch_lin_addr_high = high;
214-
ibs_fetch.ibs_fetch_lin_addr_low = low;
215-
rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high);
216-
ibs_fetch.ibs_fetch_phys_addr_high = high;
217-
ibs_fetch.ibs_fetch_phys_addr_low = low;
218-
219-
oprofile_add_ibs_sample(regs,
220-
(unsigned int *)&ibs_fetch,
221-
IBS_FETCH_BEGIN);
176+
rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
177+
oprofile_add_data(&entry, regs, msr, IBS_FETCH_CODE,
178+
IBS_FETCH_SIZE);
179+
op_cpu_buffer_add_data(&entry, (u32)msr);
180+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
181+
op_cpu_buffer_add_data(&entry, low);
182+
op_cpu_buffer_add_data(&entry, high);
183+
rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
184+
op_cpu_buffer_add_data(&entry, (u32)msr);
185+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
186+
op_cpu_buffer_write_commit(&entry);
222187

223188
/* reenable the IRQ */
224-
rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
225189
high &= ~IBS_FETCH_HIGH_VALID_BIT;
226190
high |= IBS_FETCH_HIGH_ENABLE;
227191
low &= IBS_FETCH_LOW_MAX_CNT_MASK;
@@ -232,30 +196,29 @@ op_amd_handle_ibs(struct pt_regs * const regs,
232196
if (ibs_config.op_enabled) {
233197
rdmsr(MSR_AMD64_IBSOPCTL, low, high);
234198
if (low & IBS_OP_LOW_VALID_BIT) {
235-
rdmsr(MSR_AMD64_IBSOPRIP, low, high);
236-
ibs_op.ibs_op_rip_low = low;
237-
ibs_op.ibs_op_rip_high = high;
238-
rdmsr(MSR_AMD64_IBSOPDATA, low, high);
239-
ibs_op.ibs_op_data1_low = low;
240-
ibs_op.ibs_op_data1_high = high;
241-
rdmsr(MSR_AMD64_IBSOPDATA2, low, high);
242-
ibs_op.ibs_op_data2_low = low;
243-
ibs_op.ibs_op_data2_high = high;
244-
rdmsr(MSR_AMD64_IBSOPDATA3, low, high);
245-
ibs_op.ibs_op_data3_low = low;
246-
ibs_op.ibs_op_data3_high = high;
247-
rdmsr(MSR_AMD64_IBSDCLINAD, low, high);
248-
ibs_op.ibs_dc_linear_low = low;
249-
ibs_op.ibs_dc_linear_high = high;
250-
rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high);
251-
ibs_op.ibs_dc_phys_low = low;
252-
ibs_op.ibs_dc_phys_high = high;
199+
rdmsrl(MSR_AMD64_IBSOPRIP, msr);
200+
oprofile_add_data(&entry, regs, msr, IBS_OP_CODE,
201+
IBS_OP_SIZE);
202+
op_cpu_buffer_add_data(&entry, (u32)msr);
203+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
204+
rdmsrl(MSR_AMD64_IBSOPDATA, msr);
205+
op_cpu_buffer_add_data(&entry, (u32)msr);
206+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
207+
rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
208+
op_cpu_buffer_add_data(&entry, (u32)msr);
209+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
210+
rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
211+
op_cpu_buffer_add_data(&entry, (u32)msr);
212+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
213+
rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
214+
op_cpu_buffer_add_data(&entry, (u32)msr);
215+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
216+
rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
217+
op_cpu_buffer_add_data(&entry, (u32)msr);
218+
op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
219+
op_cpu_buffer_write_commit(&entry);
253220

254221
/* reenable the IRQ */
255-
oprofile_add_ibs_sample(regs,
256-
(unsigned int *)&ibs_op,
257-
IBS_OP_BEGIN);
258-
rdmsr(MSR_AMD64_IBSOPCTL, low, high);
259222
high = 0;
260223
low &= ~IBS_OP_LOW_VALID_BIT;
261224
low |= IBS_OP_LOW_ENABLE;

‎drivers/oprofile/buffer_sync.c‎

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -318,29 +318,18 @@ static void add_trace_begin(void)
318318

319319
#ifdef CONFIG_OPROFILE_IBS
320320

321-
#define IBS_FETCH_CODE_SIZE 2
322-
#define IBS_OP_CODE_SIZE 5
323-
324-
/*
325-
* Add IBS fetch and op entries to event buffer
326-
*/
327-
static void add_ibs_begin(int cpu, int code, struct mm_struct *mm)
321+
static void add_data(struct op_entry *entry, struct mm_struct *mm)
328322
{
329-
unsigned long pc;
330-
int i, count;
331-
unsigned long cookie = 0;
323+
unsigned long code, pc, val;
324+
unsigned long cookie;
332325
off_t offset;
333-
struct op_entry entry;
334-
struct op_sample *sample;
335326

336-
sample = op_cpu_buffer_read_entry(&entry, cpu);
337-
if (!sample)
327+
if (!op_cpu_buffer_get_data(entry, &code))
328+
return;
329+
if (!op_cpu_buffer_get_data(entry, &pc))
330+
return;
331+
if (!op_cpu_buffer_get_size(entry))
338332
return;
339-
pc = sample->eip;
340-
341-
#ifdef __LP64__
342-
pc += sample->event << 32;
343-
#endif
344333

345334
if (mm) {
346335
cookie = lookup_dcookie(mm, pc, &offset);
@@ -362,24 +351,8 @@ static void add_ibs_begin(int cpu, int code, struct mm_struct *mm)
362351
add_event_entry(code);
363352
add_event_entry(offset); /* Offset from Dcookie */
364353

365-
/* we send the Dcookie offset, but send the raw Linear Add also*/
366-
add_event_entry(sample->eip);
367-
add_event_entry(sample->event);
368-
369-
if (code == IBS_FETCH_CODE)
370-
count = IBS_FETCH_CODE_SIZE; /*IBS FETCH is 2 int64s*/
371-
else
372-
count = IBS_OP_CODE_SIZE; /*IBS OP is 5 int64s*/
373-
374-
for (i = 0; i < count; i++) {
375-
sample = op_cpu_buffer_read_entry(&entry, cpu);
376-
if (!sample)
377-
return;
378-
add_event_entry(sample->eip);
379-
add_event_entry(sample->event);
380-
}
381-
382-
return;
354+
while (op_cpu_buffer_get_data(entry, &val))
355+
add_event_entry(val);
383356
}
384357

385358
#endif
@@ -572,10 +545,8 @@ void sync_buffer(int cpu)
572545
add_user_ctx_switch(new, cookie);
573546
}
574547
#ifdef CONFIG_OPROFILE_IBS
575-
if (flags & IBS_FETCH_BEGIN)
576-
add_ibs_begin(cpu, IBS_FETCH_CODE, mm);
577-
if (flags & IBS_OP_BEGIN)
578-
add_ibs_begin(cpu, IBS_OP_CODE, mm);
548+
if (op_cpu_buffer_get_size(&entry))
549+
add_data(&entry, mm);
579550
#endif
580551
continue;
581552
}

‎drivers/oprofile/cpu_buffer.c‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,38 @@ void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
363363

364364
#ifdef CONFIG_OPROFILE_IBS
365365

366-
void oprofile_add_ibs_sample(struct pt_regs * const regs,
367-
unsigned int * const ibs_sample, int ibs_code)
366+
/*
367+
* Add samples with data to the ring buffer.
368+
*
369+
* Use op_cpu_buffer_add_data(&entry, val) to add data and
370+
* op_cpu_buffer_write_commit(&entry) to commit the sample.
371+
*/
372+
void oprofile_add_data(struct op_entry *entry, struct pt_regs * const regs,
373+
unsigned long pc, int code, int size)
368374
{
375+
struct op_sample *sample;
369376
int is_kernel = !user_mode(regs);
370377
struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
371-
int fail = 0;
372378

373379
cpu_buf->sample_received++;
374380

375-
/* backtraces disabled for ibs */
376-
fail = fail || op_add_code(cpu_buf, 0, is_kernel, current);
381+
/* no backtraces for samples with data */
382+
if (op_add_code(cpu_buf, 0, is_kernel, current))
383+
goto fail;
377384

378-
fail = fail || op_add_sample(cpu_buf, ESCAPE_CODE, ibs_code);
379-
fail = fail || op_add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
380-
fail = fail || op_add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
381-
fail = fail || op_add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
385+
sample = op_cpu_buffer_write_reserve(entry, size + 2);
386+
if (!sample)
387+
goto fail;
388+
sample->eip = ESCAPE_CODE;
389+
sample->event = 0; /* no flags */
382390

383-
if (ibs_code == IBS_OP_BEGIN) {
384-
fail = fail || op_add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
385-
fail = fail || op_add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
386-
fail = fail || op_add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
387-
}
391+
op_cpu_buffer_add_data(entry, code);
392+
op_cpu_buffer_add_data(entry, pc);
393+
394+
return;
388395

389-
if (fail)
390-
cpu_buf->sample_lost_overflow++;
396+
fail:
397+
cpu_buf->sample_lost_overflow++;
391398
}
392399

393400
#endif

‎drivers/oprofile/cpu_buffer.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,5 @@ int op_cpu_buffer_get_data(struct op_entry *entry, unsigned long *val)
115115
#define IS_KERNEL (1UL << 1)
116116
#define TRACE_BEGIN (1UL << 2)
117117
#define USER_CTX_SWITCH (1UL << 3)
118-
#define IBS_FETCH_BEGIN (1UL << 4)
119-
#define IBS_OP_BEGIN (1UL << 5)
120118

121119
#endif /* OPROFILE_CPU_BUFFER_H */

0 commit comments

Comments
 (0)