Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommu/vt-d: Support cpumask for IOMMU perfmon

The perf subsystem assumes that all counters are by default per-CPU. So
the user space tool reads a counter from each CPU. However, the IOMMU
counters are system-wide and can be read from any CPU. Here we use a CPU
mask to restrict counting to one CPU to handle the issue. (with CPU
hotplug notifier to choose a different CPU if the chosen one is taken
off-line).

The CPU is exposed to /sys/bus/event_source/devices/dmar*/cpumask for
the user space perf tool.

Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Link: https://lore.kernel.org/r/20230128200428.1459118-6-kan.liang@linux.intel.com
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>

authored by

Kan Liang and committed by
Joerg Roedel
46284c6c 7232ab8b

+115 -9
+8
Documentation/ABI/testing/sysfs-bus-event_source-devices-iommu
··· 27 27 filter_pasid = "config2:0-21" - PASID filter 28 28 filter_ats = "config2:24-28" - Address Type filter 29 29 filter_page_table = "config2:32-36" - Page Table Level filter 30 + 31 + What: /sys/bus/event_source/devices/dmar*/cpumask 32 + Date: Jan 2023 33 + KernelVersion: 6.3 34 + Contact: Kan Liang <kan.liang@linux.intel.com> 35 + Description: Read-only. This file always returns the CPU to which the 36 + IOMMU pmu is bound for access to all IOMMU pmu performance 37 + monitoring events.
+106 -9
drivers/iommu/intel/perfmon.c
··· 34 34 .attrs = attrs_empty, 35 35 }; 36 36 37 + static cpumask_t iommu_pmu_cpu_mask; 38 + 39 + static ssize_t 40 + cpumask_show(struct device *dev, struct device_attribute *attr, char *buf) 41 + { 42 + return cpumap_print_to_pagebuf(true, buf, &iommu_pmu_cpu_mask); 43 + } 44 + static DEVICE_ATTR_RO(cpumask); 45 + 46 + static struct attribute *iommu_pmu_cpumask_attrs[] = { 47 + &dev_attr_cpumask.attr, 48 + NULL 49 + }; 50 + 51 + static struct attribute_group iommu_pmu_cpumask_attr_group = { 52 + .attrs = iommu_pmu_cpumask_attrs, 53 + }; 54 + 37 55 static const struct attribute_group *iommu_pmu_attr_groups[] = { 38 56 &iommu_pmu_format_attr_group, 39 57 &iommu_pmu_events_attr_group, 58 + &iommu_pmu_cpumask_attr_group, 40 59 NULL 41 60 }; 42 61 ··· 698 679 iommu->pmu = NULL; 699 680 } 700 681 701 - void iommu_pmu_register(struct intel_iommu *iommu) 682 + static int iommu_pmu_cpu_online(unsigned int cpu) 702 683 { 703 - if (!iommu->pmu) 684 + if (cpumask_empty(&iommu_pmu_cpu_mask)) 685 + cpumask_set_cpu(cpu, &iommu_pmu_cpu_mask); 686 + 687 + return 0; 688 + } 689 + 690 + static int iommu_pmu_cpu_offline(unsigned int cpu) 691 + { 692 + struct dmar_drhd_unit *drhd; 693 + struct intel_iommu *iommu; 694 + int target; 695 + 696 + if (!cpumask_test_and_clear_cpu(cpu, &iommu_pmu_cpu_mask)) 697 + return 0; 698 + 699 + target = cpumask_any_but(cpu_online_mask, cpu); 700 + 701 + if (target < nr_cpu_ids) 702 + cpumask_set_cpu(target, &iommu_pmu_cpu_mask); 703 + else 704 + target = -1; 705 + 706 + rcu_read_lock(); 707 + 708 + for_each_iommu(iommu, drhd) { 709 + if (!iommu->pmu) 710 + continue; 711 + perf_pmu_migrate_context(&iommu->pmu->pmu, cpu, target); 712 + } 713 + rcu_read_unlock(); 714 + 715 + return 0; 716 + } 717 + 718 + static int nr_iommu_pmu; 719 + 720 + static int iommu_pmu_cpuhp_setup(struct iommu_pmu *iommu_pmu) 721 + { 722 + int ret; 723 + 724 + if (nr_iommu_pmu++) 725 + return 0; 726 + 727 + ret = cpuhp_setup_state(CPUHP_AP_PERF_X86_IOMMU_PERF_ONLINE, 728 + "driver/iommu/intel/perfmon:online", 729 + iommu_pmu_cpu_online, 730 + iommu_pmu_cpu_offline); 731 + if (ret) 732 + nr_iommu_pmu = 0; 733 + 734 + return ret; 735 + } 736 + 737 + static void iommu_pmu_cpuhp_free(struct iommu_pmu *iommu_pmu) 738 + { 739 + if (--nr_iommu_pmu) 704 740 return; 705 741 706 - if (__iommu_pmu_register(iommu)) { 707 - pr_err("Failed to register PMU for iommu (seq_id = %d)\n", 708 - iommu->seq_id); 709 - free_iommu_pmu(iommu); 710 - } 742 + cpuhp_remove_state(CPUHP_AP_PERF_X86_IOMMU_PERF_ONLINE); 743 + } 744 + 745 + void iommu_pmu_register(struct intel_iommu *iommu) 746 + { 747 + struct iommu_pmu *iommu_pmu = iommu->pmu; 748 + 749 + if (!iommu_pmu) 750 + return; 751 + 752 + if (__iommu_pmu_register(iommu)) 753 + goto err; 754 + 755 + if (iommu_pmu_cpuhp_setup(iommu_pmu)) 756 + goto unregister; 757 + 758 + return; 759 + 760 + unregister: 761 + perf_pmu_unregister(&iommu_pmu->pmu); 762 + err: 763 + pr_err("Failed to register PMU for iommu (seq_id = %d)\n", iommu->seq_id); 764 + free_iommu_pmu(iommu); 711 765 } 712 766 713 767 void iommu_pmu_unregister(struct intel_iommu *iommu) 714 768 { 715 - if (iommu->pmu) 716 - perf_pmu_unregister(&iommu->pmu->pmu); 769 + struct iommu_pmu *iommu_pmu = iommu->pmu; 770 + 771 + if (!iommu_pmu) 772 + return; 773 + 774 + iommu_pmu_cpuhp_free(iommu_pmu); 775 + perf_pmu_unregister(&iommu_pmu->pmu); 717 776 }
+1
include/linux/cpuhotplug.h
··· 221 221 CPUHP_AP_PERF_X86_CQM_ONLINE, 222 222 CPUHP_AP_PERF_X86_CSTATE_ONLINE, 223 223 CPUHP_AP_PERF_X86_IDXD_ONLINE, 224 + CPUHP_AP_PERF_X86_IOMMU_PERF_ONLINE, 224 225 CPUHP_AP_PERF_S390_CF_ONLINE, 225 226 CPUHP_AP_PERF_S390_SF_ONLINE, 226 227 CPUHP_AP_PERF_ARM_CCI_ONLINE,