Page MenuHomeFreeBSD

D60568.1795519704.diff
No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None

D60568.1795519704.diff

diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -3477,7 +3477,7 @@
uint32_t caps, flags;
u_int cpu;
int adjri, n;
- int error;
+ int alloc_error, error;
class = pa->pm_class;
caps = pa->pm_caps;
@@ -3580,6 +3580,25 @@
"Requested PMC capabilities %#jx are not supported",
(uintmax_t)caps));
+ /*
+ * Branch records that may include kernel addresses disclose kernel
+ * layout and control flow. Treat them like system-wide PMCs, even
+ * for a thread PMC attached to the caller's own process.
+ */
+ if ((caps & PMC_CAP_LBR) != 0 &&
+ (caps & (PMC_CAP_USER | PMC_CAP_SYSTEM)) != PMC_CAP_USER) {
+ if (jailed(td->td_ucred))
+ return (EXTERROR(EPERM,
+ "Kernel branch records are not allowed in a jail"));
+ if (!pmc_unprivileged_syspmcs) {
+ error = priv_check(td, PRIV_PMC_SYSTEM);
+ if (error != 0)
+ return (EXTERROR(error,
+ "Kernel branch records require privilege; "
+ "use the usr qualifier"));
+ }
+ }
+
PMCDBG4(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d", pa->pm_ev,
caps, mode, cpu);
@@ -3606,6 +3625,8 @@
} else
pmc->pm_sc.pm_initial = pa->pm_count;
+ error = EINVAL;
+
/* switch thread to CPU 'cpu' */
pmc_save_cpu_binding(&pb);
@@ -3627,7 +3648,11 @@
!PMC_IS_SHAREABLE_PMC(cpu, n))
continue;
- if (pcd->pcd_allocate_pmc(cpu, adjri, pmc, pa) == 0) {
+ alloc_error = pcd->pcd_allocate_pmc(cpu, adjri, pmc,
+ pa);
+ if ((caps & PMC_CAP_LBR) != 0 && alloc_error != EINVAL)
+ error = alloc_error;
+ if (alloc_error == 0) {
/* Success. */
break;
}
@@ -3641,8 +3666,11 @@
!pmc_can_allocate_rowindex(p, n, PMC_CPU_ANY))
continue;
- if (pcd->pcd_allocate_pmc(td->td_oncpu, adjri, pmc,
- pa) == 0) {
+ alloc_error = pcd->pcd_allocate_pmc(td->td_oncpu, adjri,
+ pmc, pa);
+ if ((caps & PMC_CAP_LBR) != 0 && alloc_error != EINVAL)
+ error = alloc_error;
+ if (alloc_error == 0) {
/* Success. */
break;
}
@@ -3658,8 +3686,8 @@
pmc_destroy_pmc_descriptor(pmc);
/* Preserve a more specific error from the class allocator. */
if ((td->td_pflags2 & TDP2_EXTERR) != 0)
- return (EINVAL);
- return (EXTERROR(EINVAL,
+ return (error);
+ return (EXTERROR(error,
"No PMC row accepted the allocation request"));
}
@@ -3673,8 +3701,9 @@
if ((pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW)) != 0)
pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
- /* All system mode sampling PMCs require a log file. */
- if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
+ /* LBR records cannot be delivered through profil(2), even for self. */
+ if (PMC_IS_SAMPLING_MODE(mode) &&
+ (PMC_IS_SYSTEM_MODE(mode) || (caps & PMC_CAP_LBR) != 0))
pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
/*
@@ -5224,14 +5253,15 @@
* If this is a process-mode PMC that is attached to
* its owner, and if the PC is in user mode, update
* profiling statistics like timer-based profiling
- * would have done.
+ * would have done. LBR samples always use the log.
*
* Otherwise, this is either a sampling-mode PMC that
* is attached to a different process than its owner,
* or a system-wide sampling PMC. Dispatch a log
* entry to the PMC's owner process.
*/
- if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
+ if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) != 0 &&
+ (pm->pm_caps & PMC_CAP_LBR) == 0) {
if (ps->ps_flags & PMC_CC_F_USERSPACE) {
uint32_t pcidx;

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 24, 11:28 AM (12 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40816613
Default Alt Text
D60568.1795519704.diff (3 KB)

Event Timeline