probe.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. static umode_t
  7. not_visible(struct kobject *kobj, struct attribute *attr, int i)
  8. {
  9. return 0;
  10. }
  11. /*
  12. * Accepts msr[] array with non populated entries as long as either
  13. * msr[i].msr is 0 or msr[i].grp is NULL. Note that the default sysfs
  14. * visibility is visible when group->is_visible callback is set.
  15. */
  16. unsigned long
  17. perf_msr_probe(struct perf_msr *msr, int cnt, bool zero, void *data)
  18. {
  19. unsigned long avail = 0;
  20. unsigned int bit;
  21. u64 val;
  22. if (cnt >= BITS_PER_LONG)
  23. return 0;
  24. for (bit = 0; bit < cnt; bit++) {
  25. if (!msr[bit].no_check) {
  26. struct attribute_group *grp = msr[bit].grp;
  27. u64 mask;
  28. /* skip entry with no group */
  29. if (!grp)
  30. continue;
  31. grp->is_visible = not_visible;
  32. /* skip unpopulated entry */
  33. if (!msr[bit].msr)
  34. continue;
  35. if (msr[bit].test && !msr[bit].test(bit, data))
  36. continue;
  37. /* Virt sucks; you cannot tell if a R/O MSR is present :/ */
  38. if (rdmsrl_safe(msr[bit].msr, &val))
  39. continue;
  40. mask = msr[bit].mask;
  41. if (!mask)
  42. mask = ~0ULL;
  43. /* Disable zero counters if requested. */
  44. if (!zero && !(val & mask))
  45. continue;
  46. grp->is_visible = NULL;
  47. }
  48. avail |= BIT(bit);
  49. }
  50. return avail;
  51. }
  52. EXPORT_SYMBOL_GPL(perf_msr_probe);