xref: /kernel/linux/linux-5.10/arch/x86/events/probe.c (revision 8c2ecf20)
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/export.h>
3#include <linux/types.h>
4#include <linux/bits.h>
5#include "probe.h"
6
7static umode_t
8not_visible(struct kobject *kobj, struct attribute *attr, int i)
9{
10	return 0;
11}
12
13/*
14 * Accepts msr[] array with non populated entries as long as either
15 * msr[i].msr is 0 or msr[i].grp is NULL. Note that the default sysfs
16 * visibility is visible when group->is_visible callback is set.
17 */
18unsigned long
19perf_msr_probe(struct perf_msr *msr, int cnt, bool zero, void *data)
20{
21	unsigned long avail = 0;
22	unsigned int bit;
23	u64 val;
24
25	if (cnt >= BITS_PER_LONG)
26		return 0;
27
28	for (bit = 0; bit < cnt; bit++) {
29		if (!msr[bit].no_check) {
30			struct attribute_group *grp = msr[bit].grp;
31
32			/* skip entry with no group */
33			if (!grp)
34				continue;
35
36			grp->is_visible = not_visible;
37
38			/* skip unpopulated entry */
39			if (!msr[bit].msr)
40				continue;
41
42			if (msr[bit].test && !msr[bit].test(bit, data))
43				continue;
44			/* Virt sucks; you cannot tell if a R/O MSR is present :/ */
45			if (rdmsrl_safe(msr[bit].msr, &val))
46				continue;
47			/* Disable zero counters if requested. */
48			if (!zero && !val)
49				continue;
50
51			grp->is_visible = NULL;
52		}
53		avail |= BIT(bit);
54	}
55
56	return avail;
57}
58EXPORT_SYMBOL_GPL(perf_msr_probe);
59