Skip to contentSkip to conversation

perf: Relax privilege restriction on AMD IBS (v2)

PATCH In review Namhyung Kim 4 participants 11 messages Aug 30, 2024 – Sep 4, 2024 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf: Relax privilege restriction on AMD IBS (v2)

Hello,

This is RFC v2 to allow AMD IBS to regular users on the default settings where kernel-level profiling is disabled. Currently AMD IBS doesn't allow any kind of exclusion in the event attribute. But users needs to set attr.exclude_kernel to open an event on such an environment.

v2) changes

* add PERF_FORMAT_DROPPED
* account dropped sw events and from BPF handler
* use precise RIP from IBS record

v1) https://lore.kernel.org/lkml/[email protected]/

While IBS doesn't support hardware level privilege filters, the kernel can allow the event and drop samples belongs to the kernel like in the software events.

In order to count those dropped samples correctly, I'd propose a new read format PERF_FORMAT_DROPPED same as we did for the lost samples. With this, it can count dropped samples in the software events and from the BPF overflow handler as well.

Let me know what you think.

Thanks,
Namhyung

Namhyung Kim (4):
perf/core: Add PERF_FORMAT_DROPPED
perf/core: Export perf_exclude_event() perf/core: Account dropped samples from BPF perf/x86: Relax privilege filter restriction on AMD IBS

 arch/x86/events/amd/ibs.c       | 13 +++++++++++--
 include/linux/perf_event.h      |  3 +++
 include/uapi/linux/perf_event.h |  5 ++++-
 kernel/events/core.c            | 27 ++++++++++++++++++++++-----
 4 files changed, 40 insertions(+), 8 deletions(-)
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
author maintainer
#1 lore raw

PATCH 1/4 perf/core: Add PERF_FORMAT_DROPPED

When a perf_event is dropped due to some kind of (SW-based) filter, it won't generate sample data. For example, software events drops samples when it doesn't match to privilege from exclude_{user,kernel}.

In order to account such dropped samples, add a new counter in the perf_event, and let users can read(2) the number with the new PERF_FORMAT_DROPPED like the lost sample count.

---
 include/linux/perf_event.h      |  1 +
 include/uapi/linux/perf_event.h |  5 ++++-
 kernel/events/core.c            | 12 ++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
include/linux/perf_event.h +1 -0
index 701549967c18..955d39543398 100644
@@ -809,6 +809,7 @@ struct perf_event {
 	u64				id;
 
 	atomic64_t			lost_samples;
+	atomic64_t			dropped_samples;
 
 	u64				(*clock)(void);
 	perf_overflow_handler_t		overflow_handler;
include/uapi/linux/perf_event.h +4 -1
index 58daf6156fd0..6f19d4f74823 100644
@@ -347,6 +347,7 @@ enum {
  *	  { u64		time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
  *	  { u64		id;           } && PERF_FORMAT_ID
  *	  { u64		lost;         } && PERF_FORMAT_LOST
+ *	  { u64		dropped;      } && PERF_FORMAT_DROPPED
  *	} && !PERF_FORMAT_GROUP
  *
  *	{ u64		nr;
@@ -355,6 +356,7 @@ enum {
  *	  { u64		value;
  *	    { u64	id;           } && PERF_FORMAT_ID
  *	    { u64	lost;         } && PERF_FORMAT_LOST
+ *	    { u64	dropped;      } && PERF_FORMAT_DROPPED
  *	  }		cntr[nr];
  *	} && PERF_FORMAT_GROUP
  * };
@@ -365,8 +367,9 @@ enum perf_event_read_format {
 	PERF_FORMAT_ID				= 1U << 2,
 	PERF_FORMAT_GROUP			= 1U << 3,
 	PERF_FORMAT_LOST			= 1U << 4,
+	PERF_FORMAT_DROPPED			= 1U << 5,
 
-	PERF_FORMAT_MAX = 1U << 5,		/* non-ABI */
+	PERF_FORMAT_MAX = 1U << 6,		/* non-ABI */
 };
 
 #define PERF_ATTR_SIZE_VER0	64	/* sizeof first published struct */
kernel/events/core.c +12 -0
index c6a720f41225..4d72538628ee 100644
@@ -5679,6 +5679,8 @@ static int __perf_read_group_add(struct perf_event *leader,
 		values[n++] = primary_event_id(leader);
 	if (read_format & PERF_FORMAT_LOST)
 		values[n++] = atomic64_read(&leader->lost_samples);
+	if (read_format & PERF_FORMAT_DROPPED)
+		values[n++] = atomic64_read(&leader->dropped_samples);
 
 	for_each_sibling_event(sub, leader) {
 		values[n++] += perf_event_count(sub, false);
@@ -5686,6 +5688,8 @@ static int __perf_read_group_add(struct perf_event *leader,
 			values[n++] = primary_event_id(sub);
 		if (read_format & PERF_FORMAT_LOST)
 			values[n++] = atomic64_read(&sub->lost_samples);
+		if (read_format & PERF_FORMAT_DROPPED)
+			values[n++] = atomic64_read(&sub->dropped_samples);
 	}
 
 unlock:
@@ -5751,6 +5755,8 @@ static int perf_read_one(struct perf_event *event,
 		values[n++] = primary_event_id(event);
 	if (read_format & PERF_FORMAT_LOST)
 		values[n++] = atomic64_read(&event->lost_samples);
+	if (read_format & PERF_FORMAT_DROPPED)
+		values[n++] = atomic64_read(&event->dropped_samples);
 
 	if (copy_to_user(buf, values, n * sizeof(u64)))
 		return -EFAULT;
@@ -7348,6 +7354,8 @@ static void perf_output_read_one(struct perf_output_handle *handle,
 		values[n++] = primary_event_id(event);
 	if (read_format & PERF_FORMAT_LOST)
 		values[n++] = atomic64_read(&event->lost_samples);
+	if (read_format & PERF_FORMAT_DROPPED)
+		values[n++] = atomic64_read(&event->dropped_samples);
 
 	__output_copy(handle, values, n * sizeof(u64));
 }
@@ -7386,6 +7394,8 @@ static void perf_output_read_group(struct perf_output_handle *handle,
 		values[n++] = primary_event_id(leader);
 	if (read_format & PERF_FORMAT_LOST)
 		values[n++] = atomic64_read(&leader->lost_samples);
+	if (read_format & PERF_FORMAT_DROPPED)
+		values[n++] = atomic64_read(&leader->dropped_samples);
 
 	__output_copy(handle, values, n * sizeof(u64));
 
@@ -7401,6 +7411,8 @@ static void perf_output_read_group(struct perf_output_handle *handle,
 			values[n++] = primary_event_id(sub);
 		if (read_format & PERF_FORMAT_LOST)
 			values[n++] = atomic64_read(&sub->lost_samples);
+		if (read_format & PERF_FORMAT_DROPPED)
+			values[n++] = atomic64_read(&sub->dropped_samples);
 
 		__output_copy(handle, values, n * sizeof(u64));
 	}
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
author maintainer
#2 lore raw

PATCH 2/4 perf/core: Export perf_exclude_event()

In reply to Namhyung Kim

And increase the dropped_sample count when it returns 1. Now it can track how many samples are dropped due to the privilege filters in software events.

---
 include/linux/perf_event.h |  2 ++
 kernel/events/core.c       | 11 +++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)
include/linux/perf_event.h +2 -0
index 955d39543398..c93673a8d66a 100644
@@ -1634,6 +1634,8 @@ static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
 	return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
 }
 
+extern int perf_exclude_event(struct perf_event *event, struct pt_regs *regs);
+
 extern void perf_event_init(void);
 extern void perf_tp_event(u16 event_type, u64 count, void *record,
 			  int entry_size, struct pt_regs *regs,
kernel/events/core.c +7 -4
index 4d72538628ee..8250e76f6335 100644
@@ -9978,18 +9978,21 @@ static void perf_swevent_event(struct perf_event *event, u64 nr,
 	perf_swevent_overflow(event, 0, data, regs);
 }
 
-static int perf_exclude_event(struct perf_event *event,
-			      struct pt_regs *regs)
+int perf_exclude_event(struct perf_event *event, struct pt_regs *regs)
 {
 	if (event->hw.state & PERF_HES_STOPPED)
 		return 1;
 
 	if (regs) {
-		if (event->attr.exclude_user && user_mode(regs))
+		if (event->attr.exclude_user && user_mode(regs)) {
+			atomic64_inc(&event->dropped_samples);
 			return 1;
+		}
 
-		if (event->attr.exclude_kernel && !user_mode(regs))
+		if (event->attr.exclude_kernel && !user_mode(regs)) {
+			atomic64_inc(&event->dropped_samples);
 			return 1;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
#3 lore raw

Re: [RFC/PATCH 2/4] perf/core: Export perf_exclude_event()

kernel test robot noticed the following build errors:

Hi Namhyung,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on tip/perf/core]
[also build test ERROR on perf-tools/perf-tools acme/perf/core linus/master v6.11-rc6 next-20240903] [cannot apply to perf-tools-next/perf-tools-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Namhyung-Kim/perf-core-Add-PERF_FORMAT_DROPPED/20240831-073000
base:   tip/perf/core
patch link:    https://lore.kernel.org/r/20240830232910.1839548-3-namhyung%40kernel.org
patch subject: [RFC/PATCH 2/4] perf/core: Export perf_exclude_event()
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20240904/[email protected]/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 05f5a91d00b02f4369f46d076411c700755ae041)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240904/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/s390/kernel/perf_cpum_sf.c:13:
In file included from include/linux/perf_event.h:25:
In file included from arch/s390/include/asm/perf_event.h:14:
In file included from include/linux/device.h:32:
In file included from include/linux/device/driver.h:21:
In file included from include/linux/module.h:19:
In file included from include/linux/elf.h:6:
In file included from arch/s390/include/asm/elf.h:181:
In file included from arch/s390/include/asm/mmu_context.h:11:
In file included from arch/s390/include/asm/pgalloc.h:18:
In file included from include/linux/mm.h:2228:
include/linux/vmstat.h:500:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     500 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     501 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:507:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     507 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     508 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:514:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     514 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:519:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     519 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     520 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:528:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     528 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     529 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   In file included from arch/s390/kernel/perf_cpum_sf.c:25:
   In file included from include/linux/io.h:14:
   In file included from arch/s390/include/asm/io.h:93:
   include/asm-generic/io.h:548:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     548 |         val = __raw_readb(PCI_IOBASE + addr);
         |                           ~~~~~~~~~~ ^
   include/asm-generic/io.h:561:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     561 |         val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
      37 | #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
         |                                                           ^
   include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
     102 | #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
         |                                                      ^
   In file included from arch/s390/kernel/perf_cpum_sf.c:25:
   In file included from include/linux/io.h:14:
   In file included from arch/s390/include/asm/io.h:93:
   include/asm-generic/io.h:574:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     574 |         val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
      35 | #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
         |                                                           ^
   include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
     115 | #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
         |                                                      ^
   In file included from arch/s390/kernel/perf_cpum_sf.c:25:
   In file included from include/linux/io.h:14:
   In file included from arch/s390/include/asm/io.h:93:
   include/asm-generic/io.h:585:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     585 |         __raw_writeb(value, PCI_IOBASE + addr);
         |                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:595:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     595 |         __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:605:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     605 |         __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:693:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     693 |         readsb(PCI_IOBASE + addr, buffer, count);
         |                ~~~~~~~~~~ ^
   include/asm-generic/io.h:701:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     701 |         readsw(PCI_IOBASE + addr, buffer, count);
         |                ~~~~~~~~~~ ^
   include/asm-generic/io.h:709:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     709 |         readsl(PCI_IOBASE + addr, buffer, count);
         |                ~~~~~~~~~~ ^
   include/asm-generic/io.h:718:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     718 |         writesb(PCI_IOBASE + addr, buffer, count);
         |                 ~~~~~~~~~~ ^
   include/asm-generic/io.h:727:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     727 |         writesw(PCI_IOBASE + addr, buffer, count);
         |                 ~~~~~~~~~~ ^
   include/asm-generic/io.h:736:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     736 |         writesl(PCI_IOBASE + addr, buffer, count);
         |                 ~~~~~~~~~~ ^
>> arch/s390/kernel/perf_cpum_sf.c:1086:12: error: static declaration of 'perf_exclude_event' follows non-static declaration
    1086 | static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
         |            ^
   include/linux/perf_event.h:1637:12: note: previous declaration is here
    1637 | extern int perf_exclude_event(struct perf_event *event, struct pt_regs *regs);
         |            ^
>> arch/s390/kernel/perf_cpum_sf.c:1169:39: error: too many arguments to function call, expected 2, have 3
    1169 |         if (perf_exclude_event(event, &regs, sde_regs))
         |             ~~~~~~~~~~~~~~~~~~               ^~~~~~~~
   include/linux/perf_event.h:1637:12: note: 'perf_exclude_event' declared here
    1637 | extern int perf_exclude_event(struct perf_event *event, struct pt_regs *regs);
         |            ^                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

17 warnings and 2 errors generated.

vim +/perf_exclude_event +1086 arch/s390/kernel/perf_cpum_sf.c

8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1076  
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1077  /* perf_exclude_event() - Filter event
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1078   * @event:	The perf event
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1079   * @regs:	pt_regs structure
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1080   * @sde_regs:	Sample-data-entry (sde) regs structure
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1081   *
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1082   * Filter perf events according to their exclude specification.
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1083   *
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1084   * Return non-zero if the event shall be excluded.
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1085   */
dd127b3b977b81 Hendrik Brueckner     2013-12-12 @1086  static int perf_exclude_event(struct perf_event *event, struct pt_regs *regs,
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1087  			      struct perf_sf_sde_regs *sde_regs)
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1088  {
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1089  	if (event->attr.exclude_user && user_mode(regs))
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1090  		return 1;
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1091  	if (event->attr.exclude_kernel && !user_mode(regs))
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1092  		return 1;
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1093  	if (event->attr.exclude_guest && sde_regs->in_guest)
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1094  		return 1;
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1095  	if (event->attr.exclude_host && !sde_regs->in_guest)
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1096  		return 1;
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1097  	return 0;
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1098  }
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1099  
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1100  /* perf_push_sample() - Push samples to perf
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1101   * @event:	The perf event
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1102   * @sample:	Hardware sample data
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1103   *
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1104   * Use the hardware sample data to create perf event sample.  The sample
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1105   * is the pushed to the event subsystem and the function checks for
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1106   * possible event overflows.  If an event overflow occurs, the PMU is
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1107   * stopped.
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1108   *
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1109   * Return non-zero if an event overflow occurred.
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1110   */
3d43b981eb841a Pu Hou                2017-05-19  1111  static int perf_push_sample(struct perf_event *event,
3d43b981eb841a Pu Hou                2017-05-19  1112  			    struct hws_basic_entry *basic)
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1113  {
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1114  	int overflow;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1115  	struct pt_regs regs;
443e802bab1691 Hendrik Brueckner     2013-12-12  1116  	struct perf_sf_sde_regs *sde_regs;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1117  	struct perf_sample_data data;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1118  
7e75fc3ff4cffd Hendrik Brueckner     2013-12-13  1119  	/* Setup perf sample */
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1120  	perf_sample_data_init(&data, 0, event->hw.last_period);
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1121  
443e802bab1691 Hendrik Brueckner     2013-12-12  1122  	/* Setup pt_regs to look like an CPU-measurement external interrupt
443e802bab1691 Hendrik Brueckner     2013-12-12  1123  	 * using the Program Request Alert code.  The regs.int_parm_long
443e802bab1691 Hendrik Brueckner     2013-12-12  1124  	 * field which is unused contains additional sample-data-entry related
443e802bab1691 Hendrik Brueckner     2013-12-12  1125  	 * indicators.
443e802bab1691 Hendrik Brueckner     2013-12-12  1126  	 */
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1127  	memset(&regs, 0, sizeof(regs));
443e802bab1691 Hendrik Brueckner     2013-12-12  1128  	regs.int_code = 0x1407;
443e802bab1691 Hendrik Brueckner     2013-12-12  1129  	regs.int_parm = CPU_MF_INT_SF_PRA;
443e802bab1691 Hendrik Brueckner     2013-12-12  1130  	sde_regs = (struct perf_sf_sde_regs *) &regs.int_parm_long;
443e802bab1691 Hendrik Brueckner     2013-12-12  1131  
3d43b981eb841a Pu Hou                2017-05-19  1132  	psw_bits(regs.psw).ia	= basic->ia;
3d43b981eb841a Pu Hou                2017-05-19  1133  	psw_bits(regs.psw).dat	= basic->T;
3d43b981eb841a Pu Hou                2017-05-19  1134  	psw_bits(regs.psw).wait = basic->W;
3d43b981eb841a Pu Hou                2017-05-19  1135  	psw_bits(regs.psw).pstate = basic->P;
3d43b981eb841a Pu Hou                2017-05-19  1136  	psw_bits(regs.psw).as	= basic->AS;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1137  
e22cf8ca6f75a6 Christian Borntraeger 2015-10-06  1138  	/*
c19805f870c1fa Christian Borntraeger 2016-11-08  1139  	 * Use the hardware provided configuration level to decide if the
c19805f870c1fa Christian Borntraeger 2016-11-08  1140  	 * sample belongs to a guest or host. If that is not available,
c19805f870c1fa Christian Borntraeger 2016-11-08  1141  	 * fall back to the following heuristics:
c19805f870c1fa Christian Borntraeger 2016-11-08  1142  	 * A non-zero guest program parameter always indicates a guest
c19805f870c1fa Christian Borntraeger 2016-11-08  1143  	 * sample. Some early samples or samples from guests without
b1685ab9bd3ae1 Christian Borntraeger 2016-02-29  1144  	 * lpp usage would be misaccounted to the host. We use the asn
c19805f870c1fa Christian Borntraeger 2016-11-08  1145  	 * value as an addon heuristic to detect most of these guest samples.
df26c2e87e6cf3 Martin Schwidefsky    2017-04-04  1146  	 * If the value differs from 0xffff (the host value), we assume to
df26c2e87e6cf3 Martin Schwidefsky    2017-04-04  1147  	 * be a KVM guest.
443e802bab1691 Hendrik Brueckner     2013-12-12  1148  	 */
3d43b981eb841a Pu Hou                2017-05-19  1149  	switch (basic->CL) {
c19805f870c1fa Christian Borntraeger 2016-11-08  1150  	case 1: /* logical partition */
c19805f870c1fa Christian Borntraeger 2016-11-08  1151  		sde_regs->in_guest = 0;
c19805f870c1fa Christian Borntraeger 2016-11-08  1152  		break;
c19805f870c1fa Christian Borntraeger 2016-11-08  1153  	case 2: /* virtual machine */
c19805f870c1fa Christian Borntraeger 2016-11-08  1154  		sde_regs->in_guest = 1;
c19805f870c1fa Christian Borntraeger 2016-11-08  1155  		break;
c19805f870c1fa Christian Borntraeger 2016-11-08  1156  	default: /* old machine, use heuristics */
3d43b981eb841a Pu Hou                2017-05-19  1157  		if (basic->gpp || basic->prim_asn != 0xffff)
443e802bab1691 Hendrik Brueckner     2013-12-12  1158  			sde_regs->in_guest = 1;
c19805f870c1fa Christian Borntraeger 2016-11-08  1159  		break;
c19805f870c1fa Christian Borntraeger 2016-11-08  1160  	}
443e802bab1691 Hendrik Brueckner     2013-12-12  1161  
544e8dd7a8e49d Hendrik Brueckner     2016-03-08  1162  	/*
544e8dd7a8e49d Hendrik Brueckner     2016-03-08  1163  	 * Store the PID value from the sample-data-entry to be
544e8dd7a8e49d Hendrik Brueckner     2016-03-08  1164  	 * processed and resolved by cpumsf_output_event_pid().
544e8dd7a8e49d Hendrik Brueckner     2016-03-08  1165  	 */
544e8dd7a8e49d Hendrik Brueckner     2016-03-08  1166  	data.tid_entry.pid = basic->hpp & LPP_PID_MASK;
544e8dd7a8e49d Hendrik Brueckner     2016-03-08  1167  
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1168  	overflow = 0;
dd127b3b977b81 Hendrik Brueckner     2013-12-12 @1169  	if (perf_exclude_event(event, &regs, sde_regs))
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1170  		goto out;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1171  	if (perf_event_overflow(event, &data, &regs)) {
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1172  		overflow = 1;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1173  		event->pmu->stop(event, 0);
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1174  	}
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1175  	perf_event_update_userpage(event);
dd127b3b977b81 Hendrik Brueckner     2013-12-12  1176  out:
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1177  	return overflow;
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1178  }
8c069ff4bd6063 Hendrik Brueckner     2013-12-12  1179  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#4 lore raw

PATCH 3/4 perf/core: Account dropped samples from BPF

In reply to Namhyung Kim

Like in the software events, the BPF overflow handler can drop samples by returning 0. Let's count the dropped samples here too.

---
 kernel/events/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/events/core.c +3 -1
index 8250e76f6335..ba1f6b51ea26 100644
@@ -9808,8 +9808,10 @@ static int __perf_event_overflow(struct perf_event *event,
 
 	ret = __perf_event_account_interrupt(event, throttle);
 
-	if (event->prog && !bpf_overflow_handler(event, data, regs))
+	if (event->prog && !bpf_overflow_handler(event, data, regs)) {
+		atomic64_inc(&event->dropped_samples);
 		return ret;
+	}
 
 	/*
 	 * XXX event_limit might not quite work as expected on inherited
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
author maintainer
#5 lore raw

PATCH 4/4 perf/x86: Relax privilege filter restriction on AMD IBS

In reply to Namhyung Kim

While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.

This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.

This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.

Cc Ravi Bangoria <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 arch/x86/events/amd/ibs.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/events/amd/ibs.c +11 -2
index e91970b01d62..e40e2255239a 100644
@@ -290,6 +290,11 @@ static int perf_ibs_init(struct perf_event *event)
 	if (has_branch_stack(event))
 		return -EOPNOTSUPP;
 
+	/* handle exclude_{user,kernel} in the IRQ handler */
+	if (event->attr.exclude_hv || event->attr.exclude_idle ||
+	    event->attr.exclude_host || event->attr.exclude_guest)
+		return -EINVAL;
+
 	ret = validate_group(event);
 	if (ret)
 		return ret;
@@ -667,7 +672,6 @@ static struct perf_ibs perf_ibs_fetch = {
 		.start		= perf_ibs_start,
 		.stop		= perf_ibs_stop,
 		.read		= perf_ibs_read,
-		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
 	},
 	.msr			= MSR_AMD64_IBSFETCHCTL,
 	.config_mask		= IBS_FETCH_CONFIG_MASK,
@@ -691,7 +695,6 @@ static struct perf_ibs perf_ibs_op = {
 		.start		= perf_ibs_start,
 		.stop		= perf_ibs_stop,
 		.read		= perf_ibs_read,
-		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
 	},
 	.msr			= MSR_AMD64_IBSOPCTL,
 	.config_mask		= IBS_OP_CONFIG_MASK,
@@ -1111,6 +1114,12 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 		regs.flags |= PERF_EFLAGS_EXACT;
 	}
 
+	if (perf_exclude_event(event, &regs)) {
+		throttle = perf_event_account_interrupt(event);
+		atomic64_inc(&event->dropped_samples);
+		goto out;
+	}
+
 	if (event->attr.sample_type & PERF_SAMPLE_RAW) {
 		raw = (struct perf_raw_record){
 			.frag = {
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
maintainer
#6 lore raw

Re: [RFC/PATCH 4/4] perf/x86: Relax privilege filter restriction on AMD IBS

Quoted 7 lines · Namhyung Kim

While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.

Quoted 5 lines

This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.

Urgh.... this is really rather bad. And I'm sure a bunch of people are going to be spending a lot of time trying to figure out why their results don't make sense.

I realize that having entry hooks to disable/enable the counters is also not going to happen, this has a ton of problems too.

Also, that PMU passthrough patch set has guest hooks, so you can actually do the exclude_host/guest nonsense with those, right?

Quoted 59 lines

This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.

Cc Ravi Bangoria <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 arch/x86/events/amd/ibs.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/events/amd/ibs.c +11 -2
index e91970b01d62..e40e2255239a 100644
@@ -290,6 +290,11 @@ static int perf_ibs_init(struct perf_event *event)
 	if (has_branch_stack(event))
 		return -EOPNOTSUPP;
 
+	/* handle exclude_{user,kernel} in the IRQ handler */
+	if (event->attr.exclude_hv || event->attr.exclude_idle ||
+	    event->attr.exclude_host || event->attr.exclude_guest)
+		return -EINVAL;
+
 	ret = validate_group(event);
 	if (ret)
 		return ret;
@@ -667,7 +672,6 @@ static struct perf_ibs perf_ibs_fetch = {
 		.start		= perf_ibs_start,
 		.stop		= perf_ibs_stop,
 		.read		= perf_ibs_read,
-		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
 	},
 	.msr			= MSR_AMD64_IBSFETCHCTL,
 	.config_mask		= IBS_FETCH_CONFIG_MASK,
@@ -691,7 +695,6 @@ static struct perf_ibs perf_ibs_op = {
 		.start		= perf_ibs_start,
 		.stop		= perf_ibs_stop,
 		.read		= perf_ibs_read,
-		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
 	},
 	.msr			= MSR_AMD64_IBSOPCTL,
 	.config_mask		= IBS_OP_CONFIG_MASK,
@@ -1111,6 +1114,12 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
 		regs.flags |= PERF_EFLAGS_EXACT;
 	}
 
+	if (perf_exclude_event(event, &regs)) {
+		throttle = perf_event_account_interrupt(event);
+		atomic64_inc(&event->dropped_samples);
+		goto out;
+	}
+
 	if (event->attr.sample_type & PERF_SAMPLE_RAW) {
 		raw = (struct perf_raw_record){
 			.frag = {
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
author maintainer
#7 lore raw

Re: [RFC/PATCH 4/4] perf/x86: Relax privilege filter restriction on AMD IBS

Quoted 15 lines · Peter Zijlstra
Quoted 7 lines · Namhyung Kim

While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.

Quoted 5 lines

This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.

Quoted 3 lines

Urgh.... this is really rather bad. And I'm sure a bunch of people are going to be spending a lot of time trying to figure out why their results don't make sense.

I agree it can be confusing but there are use cases where regular users want IBS information like memory data source, data address and so on. Also I realized that software events like cpu-clock use the same logic to discard samples by privilege mode already.

Quoted 3 lines

I realize that having entry hooks to disable/enable the counters is also not going to happen, this has a ton of problems too.

Do you mean kernel/user mode change hook? I guess it'd be too costly.

Quoted 3 lines

Also, that PMU passthrough patch set has guest hooks, so you can actually do the exclude_host/guest nonsense with those, right?

Oh.. this patch is about exclude_user/kernel not host/guest. Anyway it'd be great if IBS could support the guest hooks and allow the exclude bits.

Thanks,
Namhyung

Quoted 60 lines
Quoted 60 lines

This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS.

Cc Ravi Bangoria <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 arch/x86/events/amd/ibs.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/events/amd/ibs.c +11 -3
index e91970b01d62..e40e2255239a 100644
@@ -290,6 +290,11 @@ static int perf_ibs_init(struct perf_event *event)
      if (has_branch_stack(event))
              return -EOPNOTSUPP;
 
+     /* handle exclude_{user,kernel} in the IRQ handler */
+     if (event->attr.exclude_hv || event->attr.exclude_idle ||
+         event->attr.exclude_host || event->attr.exclude_guest)
+             return -EINVAL;
+
      ret = validate_group(event);
      if (ret)
              return ret;
@@ -667,7 +672,6 @@ static struct perf_ibs perf_ibs_fetch = {
              .start          = perf_ibs_start,
              .stop           = perf_ibs_stop,
              .read           = perf_ibs_read,
-             .capabilities   = PERF_PMU_CAP_NO_EXCLUDE,
      },
      .msr                    = MSR_AMD64_IBSFETCHCTL,
      .config_mask            = IBS_FETCH_CONFIG_MASK,
@@ -691,7 +695,6 @@ static struct perf_ibs perf_ibs_op = {
              .start          = perf_ibs_start,
              .stop           = perf_ibs_stop,
              .read           = perf_ibs_read,
-             .capabilities   = PERF_PMU_CAP_NO_EXCLUDE,
      },
      .msr                    = MSR_AMD64_IBSOPCTL,
      .config_mask            = IBS_OP_CONFIG_MASK,
@@ -1111,6 +1114,12 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
              regs.flags |= PERF_EFLAGS_EXACT;
      }
 
+     if (perf_exclude_event(event, &regs)) {
+             throttle = perf_event_account_interrupt(event);
+             atomic64_inc(&event->dropped_samples);
+             goto out;
+     }
+
      if (event->attr.sample_type & PERF_SAMPLE_RAW) {
              raw = (struct perf_raw_record){
                      .frag = {
--

2.46.0.469.g59c65b2a67-goog

maintainer
#8 lore raw

Re: [RFC/PATCH 4/4] perf/x86: Relax privilege filter restriction on AMD IBS

Quoted 16 lines · Namhyung Kim
Quoted 15 lines · Peter Zijlstra
Quoted 12 lines · Namhyung Kim

While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.

This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.

Quoted 6 lines
Quoted 3 lines

Urgh.... this is really rather bad. And I'm sure a bunch of people are going to be spending a lot of time trying to figure out why their results don't make sense.

I agree it can be confusing but there are use cases where regular users want IBS information like memory data source, data address and so on.

Sure, but I'm a bit worried about users not being aware of this trickery. This makes IBS events that have exclude_kernel=1 behave significantly different from those that don't have it.

At the very least you should kill the IBS forward in amd_pmu_hw_config() when this is active. But perhaps we should also emit a one time KERN_INFO message when such an event gets created?

Quoted 2 lines

Also I realized that software events like cpu-clock use the same logic to discard samples by privilege mode already.

Right, but everybody expects the software things to suck :-) And they always suck, unconditionally.

While the IBS thing only sucks when you use exclude_[user,kernel] things. Stealth suckage is bad and enrages people.

Quoted 4 lines
Quoted 2 lines

I realize that having entry hooks to disable/enable the counters is also not going to happen, this has a ton of problems too.

Do you mean kernel/user mode change hook? I guess it'd be too costly.

Yep, insanely expensive :-)

Quoted 6 lines
Quoted 2 lines

Also, that PMU passthrough patch set has guest hooks, so you can actually do the exclude_host/guest nonsense with those, right?

Oh.. this patch is about exclude_user/kernel not host/guest. Anyway it'd be great if IBS could support the guest hooks and allow the exclude bits.

Right, but since your other patchset about disabling exclude_guest because IBS don't support it I figured I'd mention that it would be fairly simple to fix.

A message that is not in the archive (sent off-list or not yet received): CAM9d7ch8fwk-o7W6KrTgtJ5n8-oVMGqzxvW_zd_hrcWFoE2AHg@mail.gmail.com

#10 lore raw

Re: [RFC/PATCH 4/4] perf/x86: Relax privilege filter restriction on AMD IBS

Hi Namhyung,

Quoted 30 lines · Namhyung Kim
Quoted 29 lines · Peter Zijlstra
Quoted 16 lines · Namhyung Kim
Quoted 15 lines · Peter Zijlstra
Quoted 12 lines · Namhyung Kim

While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue.

This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users.

Quoted 6 lines
Quoted 3 lines

Urgh.... this is really rather bad. And I'm sure a bunch of people are going to be spending a lot of time trying to figure out why their results don't make sense.

I agree it can be confusing but there are use cases where regular users want IBS information like memory data source, data address and so on.

Sure, but I'm a bit worried about users not being aware of this trickery. This makes IBS events that have exclude_kernel=1 behave significantly different from those that don't have it.

Quoted 7 lines
Quoted 3 lines

At the very least you should kill the IBS forward in amd_pmu_hw_config() when this is active. But perhaps we should also emit a one time KERN_INFO message when such an event gets created?

What about adding a pseudo config or format for a special event that allows exclude_kernel=1 and drops samples? Maybe we could use attr.config = 0xffffffff or attr.config=1 or something.

attr.config is directly mapped to bitmaps of IBS_OP_CTL. We can't abuse reserved bits also since they might become valid control bits in future.

Maybe use config2 or config3 which are ignored by IBS driver currently.

Thanks,
Ravi

maintainer
#11 lore raw

Re: [RFC/PATCH 4/4] perf/x86: Relax privilege filter restriction on AMD IBS

In reply to a message not in the archive
Quoted 4 lines · Namhyung Kim
Quoted 2 lines

While the IBS thing only sucks when you use exclude_[user,kernel] things. Stealth suckage is bad and enrages people.

Right, let's make it explicit like the above.

Given Ravi's concern, perhaps a whole second PMU instance 'ibs_swfilt' or so? Might have more problems than its worth though, but perhaps something worth exploring for a bit.