platform/x86/amd/hsmp: Family 1Ah Model 50h-5Fh HSMP and metrics
Subsystems (4)
v6 cover platform/x86/amd/hsmp: Family 1Ah Model 50h-5Fh HSMP and metrics
Hi,
This series extends the AMD HSMP platform driver and UAPI for Family 1Ah, Model 50h-5Fh: new HSMP message IDs (including PQoS-related IDs in the first patch), Zen 6 metric table layout in UAPI, relaxed response_sz handling against the descriptor table, firmware-reported metric table sizing for ioremap and reads, HSMP_IOCTL_GET_TELEMETRY_DATA on the existing character device for a single-shot full-table copy (avoiding sysfs PAGE_SIZE limits and torn reads), ACPI enablement for protocol version 7 tables, Spectre-v1 hardening on user-controlled indices, and coordinated metric-table access plus ACPI HSMP probe/remove lifetime fixes on the shared global socket array. Documentation is updated in Documentation/arch/x86/amd_hsmp.rst.
v5 submission (lore.kernel.org)
https://lore.kernel.org/platform-driver-x86/[email protected]/T/#t
Changes since v5
* Patch 6/8: Tighten the Spectre-v1 changelog and subject around clamping ioctl and hsmp_send_message() indices; wrap the commit description so scripts/checkpatch.pl stays quiet on line length.
* Patch 8/8: Broaden the final fix beyond sysfs metric locking alone. Serialize metric table refresh and memcpy_fromio(), refcount ACPI HSMP sockets with a probe mutex across init_acpi(), misc_register(), and refcount_inc(), replace devm metric DRAM maps with explicit ioremap()/mutex_init plus hsmp_metric_tbl_unmap_all() on last ACPI remove, add hsmp_acpi_free_sock_alloc_if_idle() for early probe failures, and order legacy platform remove so misc is deregistered before metric unmap (matching ACPI teardown).
The series is bisect-safe: each commit was built with
make M=drivers/platform/x86/amd/hsmp
after the previous commits, and scripts/checkpatch.pl -g <rev> was run on each change (0 errors / 0 warnings).
Thanks,
Muralidhara M K
Muralidhara M K (8):
platform/x86/amd/hsmp: Add HSMP messages for Family 1Ah, Model 50h-5Fh platform/x86/amd/hsmp: Add UAPI structures for Family 1Ah Model
50h-5Fh metrics table
platform/x86/amd/hsmp: Unify response_sz validation to an upper-bound
check
platform/x86/amd/hsmp: Source metric-table size from firmware platform/x86/amd/hsmp: Add IOCTL_GET_TELEMETRY_DATA for metric table
reads
platform/x86/amd/hsmp: Clamp ioctl/send_message indices (Spectre v1) platform/x86/amd/hsmp: Enable HSMP_PROTO_VER7 metric tables on the
ACPI driver via the IOCTL
platform/x86/amd/hsmp: Serialize metric table access and ACPI HSMP
teardown
Documentation/arch/x86/amd_hsmp.rst | 30 ++- arch/x86/include/uapi/asm/amd_hsmp.h | 310 +++++++++++++++++++++++++-- drivers/platform/x86/amd/hsmp/acpi.c | 81 +++++-- drivers/platform/x86/amd/hsmp/hsmp.c | 214 ++++++++++++++++--- drivers/platform/x86/amd/hsmp/hsmp.h | 13 +- drivers/platform/x86/amd/hsmp/plat.c | 1 + 6 files changed, 594 insertions(+), 55 deletions(-)
Signature · 1 line
2.34.1
PATCH v6 1/8 platform/x86/amd/hsmp: Add HSMP messages for Family 1Ah, Model 50h-5Fh
Family 1Ah Model 50h-5Fh firmware exposes new HSMP messages (0x29-0x2A, 0x33-0x3A) for PC6/CC6 control, CCD power/thermal monitoring, DIMM sideband access, floor- and SDPS-limit control, and command-enable discovery. The same firmware extends three existing SET-only messages (HSMP_SET_XGMI_LINK_WIDTH 0x0C, HSMP_SET_DF_PSTATE 0x0D, HSMP_SET_PSTATE_MAX_MIN 0x22) with a read-back path selected by bit[31] of args[0] (0 = set, 1 = get). Add the new IDs, convert the three messages to HSMP_SET_GET, and bump the driver version to 2.6.
Also add PQoS-related HSMP messages HSMP_PQOS_TRAFFIC_PRIORITY (0x3B) and HSMP_PQOS_FLOATING_BW (0x3C) with matching hsmp_msg_desc_table[] descriptors so userspace can reach the new functionality.
Backward compatibility is preserved on prior platforms: new IDs previously occupied HSMP_RSVD slots, and existing userspace that leaves bit[31] = 0 continues to take a pure SET path.
--- arch/x86/include/uapi/asm/amd_hsmp.h | 169 ++++++++++++++++++++++++--- drivers/platform/x86/amd/hsmp/hsmp.h | 2 +- 2 files changed, 156 insertions(+), 15 deletions(-)
arch/x86/include/uapi/asm/amd_hsmp.h +155 -14
@@ -53,9 +53,21 @@ enum hsmp_message_ids {HSMP_SET_XGMI_PSTATE_RANGE ,/* 26h Set xGMI P-state range */ HSMP_CPU_RAIL_ISO_FREQ_POLICY ,/* 27h Get/Set Cpu Iso frequency policy */ HSMP_DFC_ENABLE_CTRL ,/* 28h Enable/Disable DF C-state */ +HSMP_PC6_ENABLE ,/* 29h Get/Set PC6 enable/disable status */ +HSMP_CC6_ENABLE ,/* 2Ah Get/Set CC6 enable/disable status */ HSMP_GET_RAPL_UNITS =0x30 ,/* 30h Get scaling factor for energy */ HSMP_GET_RAPL_CORE_COUNTER ,/* 31h Get core energy counter value */ HSMP_GET_RAPL_PACKAGE_COUNTER ,/* 32h Get package energy counter value */ +HSMP_DIMM_SB_RD ,/* 33h Get DIMM sideband data */ +HSMP_READ_CCD_POWER ,/* 34h Get average CCD power */ +HSMP_READ_TDELTA ,/* 35h Get thermal behaviour */ +HSMP_GET_SVI3_VR_CTRL_TEMP ,/* 36h Get SVI3 VR controller rail temp */ +HSMP_GET_ENABLED_HSMP_CMDS ,/* 37h Get supported HSMP commands */ +HSMP_SET_GET_FLOOR_LIMIT ,/* 38h Get/Set core floor frequency limit */ +HSMP_DIMM_SB_WR ,/* 39h Set DIMM sideband data */ +HSMP_SDPS_LIMIT ,/* 3Ah Get/Set SDPS limit */ +HSMP_PQOS_TRAFFIC_PRIORITY ,/* 3Bh Get/Set traffic priority */ +HSMP_PQOS_FLOATING_BW ,/* 3Ch Get/Set max floating bandwidth */ HSMP_MSG_ID_MAX, };
@@ -170,16 +182,27 @@ static const struct hsmp_msg_desc hsmp_msg_desc_table[] {0, 1, HSMP_GET},/* -* HSMP_SET_XGMI_LINK_WIDTH, num_args = 1, response_sz = 0 -* input: args[0] = min link width[15:8] + max link width[7:0] +* HSMP_SET_XGMI_LINK_WIDTH, num_args = 1, response_sz = 0/1 +* input: args[0] = set/get XGMI Link width[31] (0 = set, 1 = get) + +* min link width[15:8] + max link width[7:0] +* Link width encoding: 0 = x4, 1 = x8, 2 = x16. +* On SET, max must be >= min. On GET, [15:0] are reserved. +* output: args[0] = reserved[31:16] + min link width[15:8] + +* max link width[7:0] */ - {1, 0, HSMP_SET}, + {1, 1, HSMP_SET_GET},/* -* HSMP_SET_DF_PSTATE, num_args = 1, response_sz = 0 -* input: args[0] = df pstate[7:0] +* HSMP_SET_DF_PSTATE (APBDisable), num_args = 1, response_sz = 0/1 +* input: args[0] = set APB_DISABLE / get APB state[31] +* (0 = set & lock DF P-state, 1 = get) + +* reserved[30:8] + +* DF P-state[7:0] (0..2; reserved on GET) +* output: args[0] = reserved[31:9] + +* APB state[8] (1 = disabled, 0 = enabled) + +* locked DF P-state[7:0] if [8] = 1, else reserved */ - {1, 0, HSMP_SET}, + {1, 1, HSMP_SET_GET},/* HSMP_SET_AUTO_DF_PSTATE, num_args = 0, response_sz = 0 */ {0, 0, HSMP_SET},
@@ -305,16 +328,32 @@ static const struct hsmp_msg_desc hsmp_msg_desc_table[] {1, 1, HSMP_SET},/* -* HSMP_SET_POWER_MODE, num_args = 1, response_sz = 0 -* input: args[0] = power efficiency mode[2:0] +* HSMP_SET_POWER_MODE (PwrEfficiencyModeSelection), +* num_args = 1, response_sz = 1 +* input: args[0] = set/get policy[31] (0 = set, 1 = get) + +* high util point[30:24] + +* low util point[23:17] + +* PPT limit[16:5] + +* reserved[4:3] + mode selection[2:0] +* [30:5] are valid only when [2:0] is a balanced core mode +* (4 or 5). [2:0] is reserved when getting (bit[31] = 1). +* output: args[0] same layout, [31] reserved, [2:0] = arbitrated +* current efficiency mode. */ {1, 1, HSMP_SET_GET},/* -* HSMP_SET_PSTATE_MAX_MIN, num_args = 1, response_sz = 0 -* input: args[0] = min df pstate[15:8] + max df pstate[7:0] +* HSMP_SET_PSTATE_MAX_MIN (DfPstateRange), num_args = 1, response_sz = 0/1 +* input: args[0] = set/get DF P-state range[31] (0 = set, 1 = get) + +* reserved[30:16] + +* min DF P-state[15:8] + max DF P-state[7:0] +* DF P-state encoding: 0 = DFP0 (high performance), +* 1 = DFP1, 2 = DFP2 (low performance). +* [15:0] are reserved when getting (args[0] bit[31] = 1). +* output: args[0] = reserved[31:16] + min DF P-state[15:8] + +* max DF P-state[7:0] */ - {1, 0, HSMP_SET}, + {1, 1, HSMP_SET_GET},/* * HSMP_GET_METRIC_TABLE_VER, num_args = 0, response_sz = 1
@@ -355,9 +394,31 @@ static const struct hsmp_msg_desc hsmp_msg_desc_table[]*/ {1, 1, HSMP_SET_GET}, -/* RESERVED(0x29-0x2f) */ - {0, 0, HSMP_RSVD}, - {0, 0, HSMP_RSVD}, +/* +* HSMP_PC6_ENABLE (Pc6Enable), num_args = 1, response_sz = 0/1 +* input: args[0] = set/get PC6 control[31] (0 = set, 1 = get) + +* reserved[30:1] + +* enable PC6[0] (0 = disable, 1 = enable; +* reserved on GET) +* output: args[0] = reserved[31:1] + current PC6 control[0] +* (last value configured via HSMP or APML) +*/ + {1, 1, HSMP_SET_GET}, + +/* +* HSMP_CC6_ENABLE (CC6Enable), num_args = 1, response_sz = 0/1 +* Configures CC6 enable for all cores; changing the setting does +* not by itself transition cores in or out of CC6. +* input: args[0] = set/get CC6 control[31] (0 = set, 1 = get) + +* reserved[30:1] + +* enable CC6[0] (0 = disable, 1 = enable; +* reserved on GET) +* output: args[0] = reserved[31:1] + current CC6 control[0] +* (last value configured via HSMP or APML) +*/ + {1, 1, HSMP_SET_GET}, + +/* RESERVED(0x2B-0x2F) */ {0, 0, HSMP_RSVD}, {0, 0, HSMP_RSVD}, {0, 0, HSMP_RSVD},
@@ -385,6 +446,86 @@ static const struct hsmp_msg_desc hsmp_msg_desc_table[]*/ {0, 2, HSMP_GET}, +/* +* HSMP_DIMM_SB_RD, num_args = 1, response_sz = 1 +* input: args[0] = reg space[23] + reg offset[22:12] + +* device LID[11:8] + DIMM address[7:0] +* output: args[0] = read data byte[3:0] +*/ + {1, 1, HSMP_GET}, + +/* +* HSMP_READ_CCD_POWER, num_args = 1, response_sz = 1 +* input: args[0] = apic id of core[15:0] +* output: args[0] = CCD power(mWatts)[31:0] +*/ + {1, 1, HSMP_GET}, + +/* +* HSMP_READ_TDELTA, num_args = 0, response_sz = 1 +* output: args[0] = thermal behaviour[31:0] +*/ + {0, 1, HSMP_GET}, + +/* +* HSMP_GET_SVI3_VR_CTRL_TEMP, num_args = 1, response_sz = 1 +* input: args[0] = SVI3 rail index[3:1] + read temperature[0] +* output: args[0] = SVI3 rail index[30:28] + +* rail temperature in degree C[27:0] +*/ + {1, 1, HSMP_GET}, + +/* +* HSMP_GET_ENABLED_HSMP_CMDS, num_args = 1, response_sz = 3 +* input: args[0] = HSMP command mask[0] +* output: status of HSMP command = args[0], args[1], args[2] +*/ + {1, 3, HSMP_GET}, + +/* +* HSMP_SET_GET_FLOOR_LIMIT, num_args = 1, response_sz = 1 +* input: args[0] = op[31:30] + reserved[29:28] + +* apic id[27:16] + floor frequency MHz[15:0] +* op encoding: 00 = set per-core floor, +* 01 = set all-cores floor (apic id reserved), +* 10 = get per-core floor, +* 11 = get per-core effective floor. +* Floor frequency field is reserved on GET (bit[31] = 1). +* output: args[0] = floor frequency MHz[15:0] +* (effective for op 11, configured for op 10; +* reserved on SET) +*/ + {1, 1, HSMP_SET_GET}, + +/* +* HSMP_DIMM_SB_WR, num_args = 1, response_sz = 0 +* input: args[0] = write data[31:24] + reg space[23] + +* reg offset[22:12] + device LID[11:8] + +* DIMM address[7:0] +*/ + {1, 0, HSMP_SET}, + +/* +* HSMP_SDPS_LIMIT, num_args = 1, response_sz = 1 +* input: args[0] = Set/Get[31] + SDPS Limit[30:0] +* output: args[0] = SDPS Limit[30:0] +*/ + {1, 1, HSMP_SET_GET}, + +/* +* HSMP_PQOS_TRAFFIC_PRIORITY, num_args = 1, response_sz = 1 +* input: args[0] = Op[31:30] + Priority sel[27:26] + Priority val[21:20] + Input[19:0] +* output: args[0] = Supported priorities or Priority val[1:0] +*/ + {1, 1, HSMP_SET_GET}, + +/* +* HSMP_PQOS_FLOATING_BW, num_args = 1, response_sz = 2 +* input: args[0] = Op[31] + Sub-op[30:29] + params[28:0] +* output: args[0] = Discovery bits or Floating/Global memory BW (Gbps) +* output: args[1] = Reserved or config (drop adj, sampling delay, hysteresis) +*/ + {1, 2, HSMP_SET_GET}, };/* Metrics table (supported only with proto version 6) */
drivers/platform/x86/amd/hsmp/hsmp.h +1 -1
@@ -27,7 +27,7 @@#define HSMP_DEVNODE_NAME "hsmp"#define ACPI_HSMP_DEVICE_HID "AMDI0097" -#define DRIVER_VERSION "2.5" +#define DRIVER_VERSION "2.6"struct hsmp_mbaddr_info {u32 base_addr;
Signature · 1 line
2.34.1
PATCH v6 2/8 platform/x86/amd/hsmp: Add UAPI structures for Family 1Ah Model 50h-5Fh metrics table
In reply to Muralidhara M KHSMP protocol version 7 on Family 1Ah Model 50h-5Fh delivers a new per-IOD/per-CCD metric-table layout that is not compatible with the existing hsmp_metric_table. Userspace tooling needs a stable UAPI description of this layout to interpret the firmware blob carried by the HSMP character device.
Add hsmp_metric_table_zen6_iod, hsmp_metric_table_zen6_ccd and the top-level hsmp_metric_table_zen6 that mirror the firmware-defined binary layout 1:1. The ccd[] array is sized to the hardware maximum (HSMP_F1A_M50_M5F_MAX_CCDS = 8) to keep that mirror exact; userspace iterates ccd[0 .. iod.num_active_ccds - 1] and ignores the rest. num_active_ccds lives in the IOD block because the IOD die is the aggregator that reports the CCD population, and keeping it there preserves the 1:1 mapping with the firmware layout.
Driver enablement for protocol version 7 is added in a follow-up patch.
--- arch/x86/include/uapi/asm/amd_hsmp.h | 93 ++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+)
arch/x86/include/uapi/asm/amd_hsmp.h +93 -0
@@ -608,6 +608,99 @@ struct hsmp_metric_table {__u32 gfxclk_frequency[8 ]; }; +#define HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD 32 +#define HSMP_F1A_M50_M5F_MAX_FREQ_TABLE_SIZE 4 +#define HSMP_F1A_M50_M5F_MAX_XGMI_LINKS 8 +#define HSMP_F1A_M50_M5F_MAX_PCIE_LINKS 8 +#define HSMP_F1A_M50_M5F_MAX_CCDS 8 + +/* Metrics table (supported only with proto version 7) */ +struct hsmp_metric_table_zen6_iod { +__u32 num_active_ccds;/* Number of valid entries in the sibling ccd[] array */ +__u32 accumulation_counter; + +/* TEMPERATURE */ +__u64 max_socket_temperature_acc; + +/* POWER */ +__u32 socket_power_limit; +__u32 max_socket_power_limit; +__u64 socket_power_acc; +__u64 core_power_acc; +__u64 uncore_power_acc; + +/* ENERGY */ +__u64 timestamp; +__u64 socket_energy_acc; +__u64 core_energy_acc; +__u64 uncore_energy_acc; + +/* FREQUENCY */ +__u64 fclk_frequency_acc; +__u64 uclk_frequency_acc; +__u64 ddr_rate_acc; +__u64 lclk_frequency_acc[HSMP_F1A_M50_M5F_MAX_FREQ_TABLE_SIZE ]; + +/* FREQUENCY RANGE */ +__u32 fclk_frequency_table[HSMP_F1A_M50_M5F_MAX_FREQ_TABLE_SIZE ]; +__u32 uclk_frequency_table[HSMP_F1A_M50_M5F_MAX_FREQ_TABLE_SIZE ]; +__u32 ddr_rate_table[HSMP_F1A_M50_M5F_MAX_FREQ_TABLE_SIZE ]; +__u32 max_df_pstate_range; +__u32 min_df_pstate_range; +__u32 lclk_frequency_table[HSMP_F1A_M50_M5F_MAX_FREQ_TABLE_SIZE ]; +__u32 max_lclk_dpm_range; +__u32 min_lclk_dpm_range; + +/* XGMI */ +__u64 xgmi_bit_rate[HSMP_F1A_M50_M5F_MAX_XGMI_LINKS ]; +__u64 xgmi_read_bandwidth[HSMP_F1A_M50_M5F_MAX_XGMI_LINKS ]; +__u64 xgmi_write_bandwidth[HSMP_F1A_M50_M5F_MAX_XGMI_LINKS ]; + +/* ACTIVITY */ +__u64 socket_c0_residency_acc; +__u64 socket_df_cstate_residency_acc; +__u64 dram_read_bandwidth_acc; +__u64 dram_write_bandwidth_acc; +__u32 max_dram_bandwidth; +__u64 pcie_bandwidth_acc[HSMP_F1A_M50_M5F_MAX_PCIE_LINKS ]; + +/* THROTTLERS */ +__u32 prochot_residency_acc; +__u32 ppt_residency_acc; +__u32 thm_residency_acc; +__u32 vrhot_residency_acc; +__u32 cpu_tdc_residency_acc; +__u32 soc_tdc_residency_acc; +__u32 io_mem_tdc_residency_acc; +__u32 fit_residency_acc; +}; + +struct hsmp_metric_table_zen6_ccd { +__u32 core_apicid_of_thread0[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +__u64 core_c0[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +__u64 core_cc1[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +__u64 core_cc6[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +__u64 core_frequency[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +__u64 core_frequency_effective[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +__u64 core_power[HSMP_F1A_M50_M5F_MAX_CORES_PER_CCD ]; +}; + +/* +* Metrics table for Family 0x1A, Models 0x50 to 0x5F, table version 0x00700000. +* +* The layout mirrors the SMU's binary metric table verbatim: one IOD block +* followed by HSMP_F1A_M50_M5F_MAX_CCDS fixed-size CCD blocks. Only the first +* iod.num_active_ccds entries in ccd[] carry meaningful data; the remaining +* entries are firmware padding and must be ignored by userspace. +* +* Future processors within the same family and model may support a +* variable number of CCDs and cores. +*/ +struct hsmp_metric_table_zen6 { +struct hsmp_metric_table_zen6_iod iod; +struct hsmp_metric_table_zen6_ccd ccd[HSMP_F1A_M50_M5F_MAX_CCDS ]; +}; +/* Reset to default packing */ #pragma pack()
Signature · 1 line
2.34.1
PATCH v6 3/8 platform/x86/amd/hsmp: Unify response_sz validation to an upper-bound check
In reply to Muralidhara M KAs HSMP protocol versions evolve, existing message IDs sometimes gain additional response words on newer firmware. validate_message() currently enforces a strict equality (response_sz == table value) for HSMP_SET and HSMP_GET, so userspace compiled against an earlier descriptor table is rejected with -EINVAL when it asks for fewer response words than the in-kernel table now declares - even though that caller has no interest in the additional words. Only HSMP_SET_GET already used a relaxed upper-bound check.
Replace the per-type branching with a single upper-bound check for all message types. Userspace can now request fewer response words than hardware provides, while requests that exceed the descriptor table (and therefore the hardware capability) are still rejected.
--- drivers/platform/x86/amd/hsmp/hsmp.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/platform/x86/amd/hsmp/hsmp.c +8 -13
@@ -182,20 +182,15 @@ static int validate_message(struct hsmp_message *msg)return -EINVAL ;/* -* Some older HSMP SET messages are updated to add GET in the same message. -* In these messages, GET returns the current value and SET also returns -* the successfully set value. To support this GET and SET in same message -* while maintaining backward compatibility for the HSMP users, -* hsmp_msg_desc_table[] indicates only maximum allowed response_sz. +* As the HSMP protocol evolves, newer platforms may define more +* response arguments for existing messages. Use an upper-bound +* check so that older userspace callers requesting fewer response +* words than what the current hsmp_msg_desc_table[] defines are +* still accepted, while rejecting requests that exceed the +* hardware capability. */ -if (hsmp_msg_desc_table[msg->msg_id].type ==HSMP_SET_GET ) { -if (msg->response_sz > hsmp_msg_desc_table[msg->msg_id].response_sz) -return -EINVAL ; - }else { -/* only HSMP_SET or HSMP_GET messages go through this strict check */ -if (msg->response_sz != hsmp_msg_desc_table[msg->msg_id].response_sz) -return -EINVAL ; - } +if (msg->response_sz > hsmp_msg_desc_table[msg->msg_id].response_sz) +return -EINVAL ;return 0 ; }
Signature · 1 line
2.34.1
PATCH v6 4/8 platform/x86/amd/hsmp: Source metric-table size from firmware
In reply to Muralidhara M KThe driver hard-codes the metric-table region size to sizeof(struct hsmp_metric_table). That is correct for HSMP protocol version 6 but mis-sizes the ioremap of the SMU DRAM region on newer
platforms: Family 1Ah Model 50h-5Fh exposes a ~13 KB hsmp_metric_table_zen6, and the table is expected to keep growing on future firmware. The same hard-coded value also forces hsmp_metric_tbl_read() to reject any read that follows the actual firmware layout.
Pick up the table size from firmware instead. SMU on Family 1Ah Model 50h and later populates HSMP_GET_METRIC_TABLE_DRAM_ADDR's args[2] with the DRAM region size in bytes; older firmware leaves it 0. Bump the descriptor's response_sz to 3 so the field is read, store the value in hsmp_pdev.hsmp_table_size at probe time, and fall back to sizeof(struct hsmp_metric_table) when firmware reports
0. Use hsmp_table_size both for the devm_ioremap() of the region and as the upper bound for hsmp_metric_tbl_read().
Behaviour on existing protocol-version-6 hardware is unchanged:
firmware returns 0, the fallback yields the same value as the previous hard-coded one, and both the ioremap and the size check produce the same result as before.
The ioctl interface added later in this series uses the same hsmp_table_size to validate the userspace request and to copy out the full firmware-reported region.
--- arch/x86/include/uapi/asm/amd_hsmp.h | 5 +++-- drivers/platform/x86/amd/hsmp/hsmp.c | 11 ++++++++--- drivers/platform/x86/amd/hsmp/hsmp.h | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-)
arch/x86/include/uapi/asm/amd_hsmp.h +3 -2
@@ -367,11 +367,12 @@ static const struct hsmp_msg_desc hsmp_msg_desc_table[] {0, 0, HSMP_GET},/* -* HSMP_GET_METRIC_TABLE_DRAM_ADDR, num_args = 0, response_sz = 2 +* HSMP_GET_METRIC_TABLE_DRAM_ADDR, num_args = 0, response_sz = 3 * output: args[0] = lower 32 bits of the address * output: args[1] = upper 32 bits of the address +* output: args[2] = DRAM region size in bytes */ - {0, 2, HSMP_GET}, + {0, 3, HSMP_GET},/* * HSMP_SET_XGMI_PSTATE_RANGE, num_args = 1, response_sz = 0
drivers/platform/x86/amd/hsmp/hsmp.c +8 -3
@@ -356,8 +356,7 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size)return -ENOMEM ; } -/* Do not support lseek(), also don't allow more than the size of metric table */ -if (size !=sizeof (struct hsmp_metric_table )) { +if (size != hsmp_pdev.hsmp_table_size) {dev_err (sock->dev,"Wrong buffer size\n" );return -EINVAL ; }
@@ -398,8 +397,14 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)dev_err (sock->dev,"Invalid DRAM address for metric table\n" );return -ENOMEM ; } +/* SMU returns table size from Family 1Ah Model 50h and forward */ +if (msg.args[2 ]) + hsmp_pdev.hsmp_table_size = msg.args[2 ]; +else + hsmp_pdev.hsmp_table_size =sizeof (struct hsmp_metric_table ); + sock->metric_tbl_addr =devm_ioremap (sock->dev, dram_addr, -sizeof (struct hsmp_metric_table )); + hsmp_pdev.hsmp_table_size);if (!sock->metric_tbl_addr) {dev_err (sock->dev,"Failed to ioremap metric table addr\n" );return -ENOMEM ;
drivers/platform/x86/amd/hsmp/hsmp.h +2 -0
@@ -18,6 +18,7 @@#include <linux/pci.h> #include <linux/semaphore.h> #include <linux/sysfs.h> +#include <linux/types.h> #define HSMP_METRICS_TABLE_NAME "metrics_bin"
@@ -55,6 +56,7 @@ struct hsmp_plat_device {u32 proto_ver;u16 num_sockets;bool is_probed; +size_t hsmp_table_size; };int hsmp_cache_proto_ver (u16 sock_ind);
Signature · 1 line
2.34.1
PATCH v6 5/8 platform/x86/amd/hsmp: Add IOCTL_GET_TELEMETRY_DATA for metric table reads
In reply to Muralidhara M KThe metric table needs to be delivered to userspace as a single atomic snapshot, but the current sysfs metrics_bin path is a file
read: userspace can read it in chunks and observe a torn snapshot if an SMU refresh happens between read() calls. The same path is also bounded by PAGE_SIZE, so the ~13 KB hsmp_metric_table_zen6 used on Family 1Ah Model 50h-5Fh cannot be returned at all, regardless of how userspace reads it. Rather than extend sysfs to lift both restrictions, expose the metric table through the existing HSMP character device using a new ioctl that always copies the full table in one shot.
Add struct hsmp_telemetry_data and HSMP_IOCTL_GET_TELEMETRY_DATA to the UAPI header. Under the surrounding #pragma pack(4), placing the __u64 user pointer first gives a tight 16-byte layout that is identical for 32- and 64-bit callers, and the trailing __u16 reserved field is rejected with -EINVAL if non-zero so future kernels can repurpose it without breaking already-deployed userspace. Userspace sizes its buffer using the matching UAPI metric-table struct for the running platform; sizes that disagree with the firmware-reported table size are rejected so a short copy can never silently truncate the snapshot.
Dispatch hsmp_ioctl() on the ioctl command: the existing message handler is factored out as hsmp_ioctl_msg() for HSMP_IOCTL_CMD, and HSMP_IOCTL_GET_TELEMETRY_DATA goes to a new hsmp_ioctl_get_telemetry() helper.
The user-controlled socket index in HSMP_IOCTL_GET_TELEMETRY_DATA is clamped with array_index_nospec() before indexing hsmp_pdev.sock[], mitigating Spectre v1 (CVE-2017-5753).
--- Documentation/arch/x86/amd_hsmp.rst | 30 +++++++- arch/x86/include/uapi/asm/amd_hsmp.h | 43 +++++++++++ drivers/platform/x86/amd/hsmp/hsmp.c | 104 ++++++++++++++++++++++++++- 3 files changed, 175 insertions(+), 2 deletions(-)
Documentation/arch/x86/amd_hsmp.rst +29 -1
@@ -68,6 +68,14 @@ under per socket sysfs directory created at Note: lseek() is not supported as entire metrics table is read. +The sysfs metrics_bin path supports only HSMP protocol version 6 and, +because it is a file read, can return a torn snapshot if userspace +reads in pieces. Protocol version 7 metric tables +(``struct hsmp_metric_table_zen6``, ~13 KB) also exceed PAGE_SIZE, so +a read returns ``-EOPNOTSUPP``. For atomic reads on any protocol +version, use the ``HSMP_IOCTL_GET_TELEMETRY_DATA`` ioctl on /dev/hsmp +(see below). + Metrics table definitions will be documented as part of Public PPR. The same is defined in the amd_hsmp.h header.
@@ -167,7 +175,7 @@ Next thing, open the device file, as follows:: exit(1); } -The following IOCTL is defined: +The following IOCTLs are defined: ``ioctl(file, HSMP_IOCTL_CMD, struct hsmp_message *msg)`` The argument is a pointer to a::
@@ -180,6 +188,26 @@ The following IOCTL is defined: __u16 sock_ind; /* socket number */ }; +``ioctl(file, HSMP_IOCTL_GET_TELEMETRY_DATA, struct hsmp_telemetry_data *req)`` + Atomically fetch the firmware metric (telemetry) table for a socket. + The ioctl copies the table in one shot, so unlike the metrics_bin + sysfs path it cannot return a torn snapshot and is not bounded by + PAGE_SIZE. Required for HSMP protocol version 7+ (e.g. Family 1Ah + Model 50h-5Fh, ~13 KB ``struct hsmp_metric_table_zen6``). Argument:: + + struct hsmp_telemetry_data { + __u64 buf; /* User pointer to destination buffer */ + __u32 size; /* Size of @buf, must equal firmware-reported table size */ + __u16 sock_ind; /* Socket index */ + __u16 reserved; /* Reserved, must be zero */ + }; + + Userspace picks the layout matching the running protocol version + (``struct hsmp_metric_table`` for v6, ``struct hsmp_metric_table_zen6`` + for v7, queried via the ``protocol_version`` sysfs attribute) and + uses ``sizeof()`` of that struct for ``size``. The kernel rejects + a mismatched ``size`` or non-zero ``reserved`` with ``-EINVAL``. + The ioctl would return a non-zero on failure; you can read errno to see what happened. The transaction returns 0 on success.
arch/x86/include/uapi/asm/amd_hsmp.h +43 -0
53 lines not on this page · raw message
drivers/platform/x86/amd/hsmp/hsmp.c +103 -1
124 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v6 6/8 platform/x86/amd/hsmp: Clamp ioctl/send_message indices (Spectre v1)
In reply to Muralidhara M KAlthough validate_message() checks msg_id, a mispredicted branch can still allow speculative indexing into hsmp_msg_desc_table[]. Clamp msg.msg_id with array_index_nospec() at entry to hsmp_ioctl_msg() so downstream dereferences (including via is_get_msg() and hsmp_send_message()) see a bounded index.
Similarly, hsmp_send_message() bounds-checks msg->sock_ind before indexing hsmp_pdev.sock[], but a mispredicted branch can still speculatively use the raw index (Spectre v1, CVE-2017-5753). Apply array_index_nospec() after the check so every caller that reaches hsmp_pdev.sock[] through this helper sees a clamped socket index—including hsmp_ioctl_msg() and any other path that hands a user-derived struct hsmp_message to hsmp_send_message().
--- drivers/platform/x86/amd/hsmp/hsmp.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/platform/x86/amd/hsmp/hsmp.c +23 -1
@@ -200,6 +200,7 @@ static int validate_message(struct hsmp_message *msg)int hsmp_send_message (struct hsmp_message *msg) {struct hsmp_socket *sock; +unsigned int sock_ind;int ret;if (!msg)
@@ -210,7 +211,15 @@ int hsmp_send_message(struct hsmp_message *msg)if (!hsmp_pdev.sock || msg->sock_ind >= hsmp_pdev.num_sockets)return -ENODEV ; - sock = &hsmp_pdev.sock[msg->sock_ind]; + +/* +* Sanitize sock_ind after the bounds check. A mispredicted branch can +* still let the CPU speculatively use msg->sock_ind as an index into +* hsmp_pdev.sock[] (Spectre v1, CVE-2017-5753), including for callers +* other than hsmp_ioctl_msg() that pass a user-derived socket index. +*/ + sock_ind =array_index_nospec (msg->sock_ind, hsmp_pdev.num_sockets); + sock = &hsmp_pdev.sock[sock_ind]; ret =down_interruptible (&sock->hsmp_sem);if (ret <0 )
@@ -306,6 +315,19 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)if (msg.msg_id <HSMP_TEST || msg.msg_id >=HSMP_MSG_ID_MAX )return -ENOMSG ; +/* +* Sanitize the user-controlled msg_id against speculative +* execution. The bounds check above retires the out-of-range +* case with -ENOMSG, but a mispredicted branch can still let the +* CPU speculatively use msg_id as an index into +* hsmp_msg_desc_table[] (here and in validate_message() / +* is_get_msg() called downstream via hsmp_send_message()), and +* pull arbitrary kernel memory into the cache (Spectre v1, +* CVE-2017-5753). Clamp once into msg.msg_id so every downstream +* dereference sees the sanitized value. +*/ + msg.msg_id =array_index_nospec (msg.msg_id,HSMP_MSG_ID_MAX ); +switch (fp->f_mode & (FMODE_WRITE |FMODE_READ )) {case FMODE_WRITE :/*
Signature · 1 line
2.34.1
PATCH v6 7/8 platform/x86/amd/hsmp: Enable HSMP_PROTO_VER7 metric tables on the ACPI driver via the IOCTL
In reply to Muralidhara M KThe ACPI driver currently prepares the per-socket metric table only on HSMP_PROTO_VER6. With protocol version 7 in use on Family 1Ah Model 50h-5Fh, userspace cannot reach the new ~13 KBhsmp_metric_table_zen6: hsmp_get_tbl_dram_base() is skipped, sock->metric_tbl_addr stays NULL, and the ioctl added earlier in this series has nothing to read.
Widen the proto_ver gate in init_acpi() from '== HSMP_PROTO_VER6' to '>= HSMP_PROTO_VER6' so the DRAM region is mapped and hsmp_pdev.hsmp_table_size is populated on protocol version 7 (and any future compatible version), making the ioctl path functional.
hsmp_metric_tbl_acpi_read() now returns -EOPNOTSUPP whenever the running protocol version is not VER6. v7 userspace gets a clear, actionable error and a documented pointer to HSMP_IOCTL_GET_TELEMETRY_DATA; v6 userspace sees no change.
The non-ACPI plat.c path is intentionally left untouched: it covers Family 1Ah Model 0h-Fh hardware fixed at protocol version 6, where the existing metrics_bin remains the supported interface.
--- drivers/platform/x86/amd/hsmp/acpi.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +21 -2
@@ -238,13 +238,32 @@ static ssize_t hsmp_metric_tbl_acpi_read(struct file *filp, struct kobject *kobj struct device *dev = container_of(kobj, struct device, kobj);struct hsmp_socket *sock =dev_get_drvdata (dev); +/* +* metrics_bin is a sysfs binary attribute and is capped at PAGE_SIZE. +* It can therefore only carry the protocol version 6 metric table +* (struct hsmp_metric_table). Larger tables (HSMP_PROTO_VER7+, e.g. +* struct hsmp_metric_table_zen6 at ~13 KB used on Family 1Ah Model +* 50h-5Fh) do not fit; userspace on those systems must read the +* snapshot through HSMP_IOCTL_GET_TELEMETRY_DATA on /dev/hsmp. +* Surface the unsupported case here as -EOPNOTSUPP rather than +* silently truncating the snapshot. +*/ +if (hsmp_pdev->proto_ver !=HSMP_PROTO_VER6 ) +return -EOPNOTSUPP ; +return hsmp_metric_tbl_read (sock, buf, count); }static umode_t hsmp_is_sock_attr_visible (struct kobject *kobj,const struct bin_attribute *battr,int id) { -if (hsmp_pdev->proto_ver ==HSMP_PROTO_VER6 ) +/* +* Keep metrics_bin visible for HSMP_PROTO_VER7+ as well, so that +* userspace which expects the file to exist gets a clear +* -EOPNOTSUPP from the read handler instead of -ENOENT, and is +* pointed at HSMP_IOCTL_GET_TELEMETRY_DATA as the supported path. +*/ +if (hsmp_pdev->proto_ver >=HSMP_PROTO_VER6 )return battr->attr.mode;return 0 ;
@@ -491,7 +510,7 @@ static int init_acpi(struct device *dev)return ret; } -if (hsmp_pdev->proto_ver ==HSMP_PROTO_VER6 ) { +if (hsmp_pdev->proto_ver >=HSMP_PROTO_VER6 ) { ret =hsmp_get_tbl_dram_base (sock_ind);if (ret)dev_info (dev,"Failed to init metric table\n" );
Signature · 1 line
2.34.1
PATCH v6 8/8 platform/x86/amd/hsmp: Serialize metric table access and ACPI HSMP teardown
In reply to Muralidhara M KEach ACPI HSMP socket bumps acpi_sock_refcnt after a successful probe; the last socket to remove runs refcount_dec_and_test() and tears down shared state once. If init_acpi() or misc_register() fails before refcount_inc(), call hsmp_acpi_free_sock_alloc_if_idle() to free hsmp_pdev->sock so a concurrent probe cannot follow a dangling pointer.
1) Metric table sysfs reads ask the SMU to refresh the table, then memcpy_fromio() from the mmio mapping. Concurrent readers could interleave those steps and return inconsistent data. Serialize with per-socket metric_tbl_lock and guard(mutex) around hsmp_send_message() and memcpy_fromio() so each read() observes one coherent snapshot.
2) Every ACPI AMDI0097 device shares hsmp_pdev->sock[]. The driver used devm_kcalloc() from whichever device probed first, an unlocked probe flag, and misc_register() from the first successful path. Parallel probes could race, and devm tied the array lifetime to one device while others (and devm hwmon paths) still called hsmp_send_message(), which could use-after-free the array on remove.
3) Metric DRAM used devm_ioremap() and devm_mutex_init() per ACPI device while the socket table is global, so devm teardown order did not match shared storage. Switch to ioremap(), one-time mutex_init() (metric_lock_inited), and hsmp_metric_tbl_unmap_all() on the last ACPI remove together with the rest of global teardown.
4) After kcalloc(sock) but before refcount_inc(), one failing probe could kfree() the array while another probe still ran init_acpi() on the same pointer. Hold hsmp_acpi_probe_mutex across init_acpi(), misc_register(), and refcount_inc() so setup does not overlap teardown or a concurrent probe.
5) On the legacy platform driver, call hsmp_misc_deregister() before hsmp_metric_tbl_unmap_all() so /dev/hsmp is gone before metric mmio is unmapped, matching the ACPI remove path.
--- drivers/platform/x86/amd/hsmp/acpi.c | 58 +++++++++++++++++++++------- drivers/platform/x86/amd/hsmp/hsmp.c | 56 +++++++++++++++++++++++++-- drivers/platform/x86/amd/hsmp/hsmp.h | 9 ++++- drivers/platform/x86/amd/hsmp/plat.c | 1 + 4 files changed, 106 insertions(+), 18 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +44 -14
@@ -20,7 +20,10 @@#include <linux/ioport.h> #include <linux/kstrtox.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/platform_device.h> +#include <linux/refcount.h> +#include <linux/slab.h> #include <linux/sysfs.h> #include <linux/topology.h> #include <linux/uuid.h>
@@ -595,6 +598,19 @@ static const struct acpi_device_id amd_hsmp_acpi_ids[] = { };MODULE_DEVICE_TABLE (acpi, amd_hsmp_acpi_ids); +static DEFINE_MUTEX (hsmp_acpi_probe_mutex); + +/* Caller holds hsmp_acpi_probe_mutex. */ +static void hsmp_acpi_free_sock_alloc_if_idle (void ) +{ +if (refcount_read (&hsmp_pdev->acpi_sock_refcnt) ==0 && + !hsmp_pdev->acpi_misc_registered && hsmp_pdev->sock) { +kfree (hsmp_pdev->sock); + hsmp_pdev->sock =NULL ; + hsmp_pdev->num_sockets =0 ; + } +} +static int hsmp_acpi_probe (struct platform_device *pdev) {int ret;
@@ -603,49 +619,63 @@ static int hsmp_acpi_probe(struct platform_device *pdev)if (!hsmp_pdev)return -ENOMEM ; -if (!hsmp_pdev->is_probed) { +mutex_lock (&hsmp_acpi_probe_mutex); +if (!hsmp_pdev->sock) { hsmp_pdev->num_sockets =topology_max_packages ();if (!hsmp_pdev->num_sockets) {dev_err (&pdev->dev,"No CPU sockets detected\n" ); +mutex_unlock (&hsmp_acpi_probe_mutex);return -ENODEV ; } - hsmp_pdev->sock =devm_kcalloc (&pdev->dev, hsmp_pdev->num_sockets, -sizeof (*hsmp_pdev->sock), -GFP_KERNEL ); -if (!hsmp_pdev->sock) + hsmp_pdev->sock =kcalloc (hsmp_pdev->num_sockets, +sizeof (*hsmp_pdev->sock), +GFP_KERNEL ); +if (!hsmp_pdev->sock) { +mutex_unlock (&hsmp_acpi_probe_mutex);return -ENOMEM ; + } } ret =init_acpi (&pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to initialize HSMP interface.\n" ); +hsmp_acpi_free_sock_alloc_if_idle (); +mutex_unlock (&hsmp_acpi_probe_mutex);return ret; } -if (!hsmp_pdev->is_probed) { +if (!hsmp_pdev->acpi_misc_registered) { ret =hsmp_misc_register (&pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to register misc device\n" ); +hsmp_acpi_free_sock_alloc_if_idle (); +mutex_unlock (&hsmp_acpi_probe_mutex);return ret; } - hsmp_pdev->is_probed = true; -dev_dbg (&pdev->dev,"AMD HSMP ACPI is probed successfully\n" ); + hsmp_pdev->acpi_misc_registered = true; +dev_dbg (&pdev->dev,"AMD HSMP ACPI misc device registered\n" ); } +refcount_inc (&hsmp_pdev->acpi_sock_refcnt); +mutex_unlock (&hsmp_acpi_probe_mutex);return 0 ; }static void hsmp_acpi_remove (struct platform_device *pdev) { -/* -* We register only one misc_device even on multi-socket system. -* So, deregister should happen only once. -*/ -if (hsmp_pdev->is_probed) { +mutex_lock (&hsmp_acpi_probe_mutex); +if (refcount_dec_and_test (&hsmp_pdev->acpi_sock_refcnt)) {hsmp_misc_deregister (); - hsmp_pdev->is_probed = false; + hsmp_pdev->acpi_misc_registered = false; +hsmp_metric_tbl_unmap_all (hsmp_pdev, hsmp_pdev->num_sockets); +kfree (hsmp_pdev->sock); + hsmp_pdev->sock =NULL ; + hsmp_pdev->num_sockets =0 ; + hsmp_pdev->proto_ver =0 ; + hsmp_pdev->hsmp_table_size =0 ; } +mutex_unlock (&hsmp_acpi_probe_mutex); }static struct platform_driver amd_hsmp_driver = {
drivers/platform/x86/amd/hsmp/hsmp.c +53 -3
92 lines not on this page · raw message
drivers/platform/x86/amd/hsmp/hsmp.h +8 -1
@@ -15,7 +15,9 @@#include <linux/hwmon.h> #include <linux/kconfig.h> #include <linux/miscdevice.h> +#include <linux/mutex.h> #include <linux/pci.h> +#include <linux/refcount.h> #include <linux/semaphore.h> #include <linux/sysfs.h> #include <linux/types.h>
@@ -42,11 +44,14 @@ struct hsmp_socket {struct bin_attribute hsmp_attr;struct hsmp_mbaddr_info mbinfo;void __iomem *metric_tbl_addr; +/* Serializes concurrent metric table refreshes from the sysfs path */ +struct mutex metric_tbl_lock;void __iomem *virt_base_addr;struct semaphore hsmp_sem;char name[HSMP_ATTR_GRP_NAME_SIZE ];struct device *dev;u16 sock_ind; +bool metric_lock_inited;int (*amd_hsmp_rdwr)(struct hsmp_socket *sock,u32 off,u32 *val,bool rw); };
@@ -55,7 +60,8 @@ struct hsmp_plat_device {struct hsmp_socket *sock;u32 proto_ver;u16 num_sockets; -bool is_probed; +refcount_t acpi_sock_refcnt; +bool acpi_misc_registered;size_t hsmp_table_size; };
@@ -66,6 +72,7 @@ void hsmp_misc_deregister(void);int hsmp_misc_register (struct device *dev);int hsmp_get_tbl_dram_base (u16 sock_ind);ssize_t hsmp_metric_tbl_read (struct hsmp_socket *sock,char *buf,size_t size); +void hsmp_metric_tbl_unmap_all (struct hsmp_plat_device *pdev,u16 num_sockets);struct hsmp_plat_device *get_hsmp_pdev (void );#if IS_ENABLED (CONFIG_HWMON )int hsmp_create_sensor (struct device *dev,u16 sock_ind);
drivers/platform/x86/amd/hsmp/plat.c +1 -0
@@ -230,6 +230,7 @@ static int hsmp_pltdrv_probe(struct platform_device *pdev)static void hsmp_pltdrv_remove (struct platform_device *pdev) {hsmp_misc_deregister (); +hsmp_metric_tbl_unmap_all (hsmp_pdev, hsmp_pdev->num_sockets); }static struct platform_driver amd_hsmp_driver = {
Signature · 1 line
2.34.1
Ilpo Järvinen
maintainerRe: [PATCH v6 8/8] platform/x86/amd/hsmp: Serialize metric table access and ACPI HSMP teardown
Quoted 23 lines · Muralidhara M K
Each ACPI HSMP socket bumps
acpi_sock_refcntafter a successful probe; the last socket to remove runsrefcount_dec_and_test()and tears down shared state once. Ifinit_acpi()ormisc_register()fails beforerefcount_inc(), callhsmp_acpi_free_sock_alloc_if_idle()to freehsmp_pdev->sockso a concurrent probe cannot follow a dangling pointer.1) Metric table sysfs reads ask the SMU to refresh the table, then
memcpy_fromio()from the mmio mapping. Concurrent readers could interleave those steps and return inconsistent data. Serialize with per-socketmetric_tbl_lockand guard(mutex) aroundhsmp_send_message()andmemcpy_fromio()so eachread()observes one coherent snapshot.
2) Every ACPI AMDI0097 device shares hsmp_pdev->sock[]. The driver useddevm_kcalloc()from whichever device probed first, an unlocked probe flag, andmisc_register()from the first successful path. Parallel probes could race, and devm tied the array lifetime to one device while others (and devm hwmon paths) still calledhsmp_send_message(), which could use-after-free the array on remove.
3) Metric DRAM useddevm_ioremap()anddevm_mutex_init()per ACPI device while the socket table is global, so devm teardown order did not match shared storage. Switch toioremap(), one-timemutex_init()(metric_lock_inited), andhsmp_metric_tbl_unmap_all()on the last ACPI remove together with the rest of global teardown.
Quoted 8 lines
4) After kcalloc(sock) but before
refcount_inc(), one failing probe couldkfree()the array while another probe still raninit_acpi()on the same pointer. Holdhsmp_acpi_probe_mutexacrossinit_acpi(),misc_register(), andrefcount_inc()so setup does not overlap teardown or a concurrent probe.
5) On the legacy platform driver, callhsmp_misc_deregister()beforehsmp_metric_tbl_unmap_all()so/dev/hsmpis gone before metric mmio is unmapped, matching the ACPI remove path.
Thanks. To me it would look better to have the concurrency fixes done first. You could actually send them in own series and once that work is done, I'll process the ver7 series on top of that.
I took patch 6 nospec change into the review-ilpo-next already as it seems independent of the ver7 changes.
Many comments below.
Quoted 25 lines
Tested-by Muthusamy Ramalingam <[email protected]>Signed-off-by Muralidhara M K <[email protected]>--- drivers/platform/x86/amd/hsmp/acpi.c | 58 +++++++++++++++++++++------- drivers/platform/x86/amd/hsmp/hsmp.c | 56 +++++++++++++++++++++++++-- drivers/platform/x86/amd/hsmp/hsmp.h | 9 ++++- drivers/platform/x86/amd/hsmp/plat.c | 1 + 4 files changed, 106 insertions(+), 18 deletions(-)drivers/platform/x86/amd/hsmp/acpi.c +3
-0@@ -20,7 +20,10 @@#include <linux/ioport.h> #include <linux/kstrtox.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/platform_device.h> +#include <linux/refcount.h> +#include <linux/slab.h> #include <linux/sysfs.h> #include <linux/topology.h> #include <linux/uuid.h>
drivers/platform/x86/amd/hsmp/acpi.c static const struct acpi_device_id amd_hsmp_acpi_ids[] = { · line 603
Quoted 6 lines
drivers/platform/x86/amd/hsmp/acpi.c +3
-0@@ -595,6 +598,19 @@ static const struct acpi_device_id amd_hsmp_acpi_ids[] = { };MODULE_DEVICE_TABLE (acpi, amd_hsmp_acpi_ids); +static DEFINE_MUTEX (hsmp_acpi_probe_mutex); + +/* Caller holds hsmp_acpi_probe_mutex. */
Please use lockdep_assert() to enforce any context requirement.
For comments, the kerneldoc compatible formatting should be used (even if not writing a full kerneldoc comment):
* Context: caller must hold hsmp_acpi_probe_mutex.
Quoted 14 lines
drivers/platform/x86/amd/hsmp/acpi.c +10
-0+static void hsmp_acpi_free_sock_alloc_if_idle (void ) +{ +if (refcount_read (&hsmp_pdev->acpi_sock_refcnt) ==0 && + !hsmp_pdev->acpi_misc_registered && hsmp_pdev->sock) { +kfree (hsmp_pdev->sock); + hsmp_pdev->sock =NULL ; + hsmp_pdev->num_sockets =0 ; + } +} +static int hsmp_acpi_probe (struct platform_device *pdev) {int ret;
drivers/platform/x86/amd/hsmp/acpi.c static int hsmp_acpi_probe(struct platform_device *pdev) · line 622
Quoted 5 lines
drivers/platform/x86/amd/hsmp/acpi.c +1
-1@@ -603,49 +619,63 @@ static int hsmp_acpi_probe(struct platform_device *pdev)if (!hsmp_pdev)return -ENOMEM ; -if (!hsmp_pdev->is_probed) { +mutex_lock (&hsmp_acpi_probe_mutex);
You should generally use cleanup.h when taking the mutex, especially, if you need to duplicate the unlock to many places.
Quoted 57 lines
drivers/platform/x86/amd/hsmp/acpi.c +18
-12+if (!hsmp_pdev->sock) { hsmp_pdev->num_sockets =topology_max_packages ();if (!hsmp_pdev->num_sockets) {dev_err (&pdev->dev,"No CPU sockets detected\n" ); +mutex_unlock (&hsmp_acpi_probe_mutex);return -ENODEV ; } - hsmp_pdev->sock =devm_kcalloc (&pdev->dev, hsmp_pdev->num_sockets, -sizeof (*hsmp_pdev->sock), -GFP_KERNEL ); -if (!hsmp_pdev->sock) + hsmp_pdev->sock =kcalloc (hsmp_pdev->num_sockets, +sizeof (*hsmp_pdev->sock), +GFP_KERNEL ); +if (!hsmp_pdev->sock) { +mutex_unlock (&hsmp_acpi_probe_mutex);return -ENOMEM ; + } } ret =init_acpi (&pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to initialize HSMP interface.\n" ); +hsmp_acpi_free_sock_alloc_if_idle (); +mutex_unlock (&hsmp_acpi_probe_mutex);return ret; } -if (!hsmp_pdev->is_probed) { +if (!hsmp_pdev->acpi_misc_registered) { ret =hsmp_misc_register (&pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to register misc device\n" ); +hsmp_acpi_free_sock_alloc_if_idle (); +mutex_unlock (&hsmp_acpi_probe_mutex);return ret; } - hsmp_pdev->is_probed = true; -dev_dbg (&pdev->dev,"AMD HSMP ACPI is probed successfully\n" ); + hsmp_pdev->acpi_misc_registered = true; +dev_dbg (&pdev->dev,"AMD HSMP ACPI misc device registered\n" ); } +refcount_inc (&hsmp_pdev->acpi_sock_refcnt); +mutex_unlock (&hsmp_acpi_probe_mutex);return 0 ; }static void hsmp_acpi_remove (struct platform_device *pdev) { -/* -* We register only one misc_device even on multi-socket system. -* So, deregister should happen only once. -*/ -if (hsmp_pdev->is_probed) { +mutex_lock (&hsmp_acpi_probe_mutex);
drivers/platform/x86/amd/hsmp/acpi.c static int hsmp_acpi_probe(struct platform_device *pdev) · line 676
Quoted 10 lines
drivers/platform/x86/amd/hsmp/acpi.c +8
-1+if (refcount_dec_and_test (&hsmp_pdev->acpi_sock_refcnt)) {hsmp_misc_deregister (); - hsmp_pdev->is_probed = false; + hsmp_pdev->acpi_misc_registered = false; +hsmp_metric_tbl_unmap_all (hsmp_pdev, hsmp_pdev->num_sockets); +kfree (hsmp_pdev->sock); + hsmp_pdev->sock =NULL ; + hsmp_pdev->num_sockets =0 ; + hsmp_pdev->proto_ver =0 ; + hsmp_pdev->hsmp_table_size =0 ;
Using kref.h, you can give a release function for the put call and get/put will be cleaner interface anyway than handling the refcount directly.
Quoted 82 lines
drivers/platform/x86/amd/hsmp/acpi.c +1
-0} +mutex_unlock (&hsmp_acpi_probe_mutex); }static struct platform_driver amd_hsmp_driver = {drivers/platform/x86/amd/hsmp/hsmp.c +40
-1@@ -12,6 +12,7 @@#include <linux/acpi.h> #include <linux/delay.h> #include <linux/device.h> +#include <linux/io.h> #include <linux/nospec.h> #include <linux/semaphore.h> #include <linux/slab.h> @@ -41,7 +42,9 @@*/ #define CHECK_GET_BIT BIT(31) -static struct hsmp_plat_device hsmp_pdev; +static struct hsmp_plat_device hsmp_pdev = { + .acpi_sock_refcnt =REFCOUNT_INIT (0 ), +};/* * Send a message to the HSMP port via PCI-e config space registers @@ -488,6 +491,7 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size) msg.msg_id =HSMP_GET_METRIC_TABLE ; msg.sock_ind = sock->sock_ind; +guard (mutex)(&sock->metric_tbl_lock); ret =hsmp_send_message (&msg);if (ret)return ret;@@ -497,6 +501,28 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size) }EXPORT_SYMBOL_NS_GPL (hsmp_metric_tbl_read,"AMD_HSMP" ); +void hsmp_metric_tbl_unmap_all (struct hsmp_plat_device *pdev,u16 num_sockets) +{ +u16 i; + +if (!pdev->sock || !num_sockets) +return ; + +for (i =0 ; i < num_sockets; i++) { +struct hsmp_socket *s = &pdev->sock[i]; + +if (s->metric_tbl_addr) { +iounmap (s->metric_tbl_addr); + s->metric_tbl_addr =NULL ; + } +if (s->metric_lock_inited) { +mutex_destroy (&s->metric_tbl_lock); + s->metric_lock_inited = false; + } + } +} +EXPORT_SYMBOL_NS_GPL (hsmp_metric_tbl_unmap_all,"AMD_HSMP" ); +int hsmp_get_tbl_dram_base (u16 sock_ind) {struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];@@ -504,6 +530,31 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)phys_addr_t dram_addr;int ret; +if (sock->metric_tbl_addr) +return 0 ; + +/* +* Initialize the per-socket lock before anything that can set +* sock->metric_tbl_addr to a non-NULL value. hsmp_metric_tbl_read() +* gates on sock->metric_tbl_addr being non-NULL and then takes +* metric_tbl_lock unconditionally; both callers of this function +* (init_acpi() and init_platform_device()) intentionally only log +* a failure here and continue probing, so an init order that left +* metric_tbl_addr populated while mutex_init failed would leave the +* read path locking an uninitialized mutex. Doing the mutex init +* first preserves the invariant "metric_tbl_addr != NULL implies the
drivers/platform/x86/amd/hsmp/hsmp.c int hsmp_get_tbl_dram_base(u16 sock_ind) · line 555
Quoted 10 lines
drivers/platform/x86/amd/hsmp/hsmp.c +10
-0+* lock is usable" on every error exit. +* +* Use non-devm mutex + ioremap so the shared sock[] array (ACPI +* allocates once for all sockets) can be torn down from a single +* refcounted release path without devm lifetime being tied to the +* wrong struct device. +*/ +if (!sock->metric_lock_inited) { +mutex_init (&sock->metric_tbl_lock); + sock->metric_lock_inited = true;
Perhaps it would be possible to arrange code such that booleans like this are not required.
This problem may in part stem from using global array to store sock instead of sock[] being just pointers to memory allocated dynamically on probe and freed on remove, and in part from the init order.
drivers/platform/x86/amd/hsmp/hsmp.c int hsmp_get_tbl_dram_base(u16 sock_ind) · line 556
Quoted 1 line
drivers/platform/x86/amd/hsmp/hsmp.c +1
-0+ }
This patch should probably be split into series so it's easier to differentiate what belongs to which fix (or if something strictly belongs to the ver7 ioctl additions and not the existing code, it should be sent with them once the concurrency changes are made to come first).
Quoted 44 lines
drivers/platform/x86/amd/hsmp/hsmp.c +2
-2+ msg.sock_ind = sock_ind; msg.response_sz = hsmp_msg_desc_table[HSMP_GET_METRIC_TABLE_DRAM_ADDR ].response_sz; msg.msg_id =HSMP_GET_METRIC_TABLE_DRAM_ADDR ;@@ -527,8 +578,7 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)else hsmp_pdev.hsmp_table_size =sizeof (struct hsmp_metric_table ); - sock->metric_tbl_addr =devm_ioremap (sock->dev, dram_addr, - hsmp_pdev.hsmp_table_size); + sock->metric_tbl_addr =ioremap (dram_addr, hsmp_pdev.hsmp_table_size);if (!sock->metric_tbl_addr) {dev_err (sock->dev,"Failed to ioremap metric table addr\n" );return -ENOMEM ;drivers/platform/x86/amd/hsmp/hsmp.h +5
-0@@ -15,7 +15,9 @@#include <linux/hwmon.h> #include <linux/kconfig.h> #include <linux/miscdevice.h> +#include <linux/mutex.h> #include <linux/pci.h> +#include <linux/refcount.h> #include <linux/semaphore.h> #include <linux/sysfs.h> #include <linux/types.h> @@ -42,11 +44,14 @@ struct hsmp_socket {struct bin_attribute hsmp_attr;struct hsmp_mbaddr_info mbinfo;void __iomem *metric_tbl_addr; +/* Serializes concurrent metric table refreshes from the sysfs path */ +struct mutex metric_tbl_lock;void __iomem *virt_base_addr;struct semaphore hsmp_sem;char name[HSMP_ATTR_GRP_NAME_SIZE ];struct device *dev;u16 sock_ind; +bool metric_lock_inited;int (*amd_hsmp_rdwr)(struct hsmp_socket *sock,u32 off,u32 *val,bool rw); };
drivers/platform/x86/amd/hsmp/hsmp.h struct hsmp_plat_device { · line 64
Quoted 6 lines
drivers/platform/x86/amd/hsmp/hsmp.h +2
-1@@ -55,7 +60,8 @@ struct hsmp_plat_device {struct hsmp_socket *sock;u32 proto_ver;u16 num_sockets; -bool is_probed; +refcount_t acpi_sock_refcnt; +bool acpi_misc_registered;
Don't you know this based on the reference count?
Quoted 24 lines
drivers/platform/x86/amd/hsmp/hsmp.h +1
-0size_t hsmp_table_size; };@@ -66,6 +72,7 @@ void hsmp_misc_deregister(void);int hsmp_misc_register (struct device *dev);int hsmp_get_tbl_dram_base (u16 sock_ind);ssize_t hsmp_metric_tbl_read (struct hsmp_socket *sock,char *buf,size_t size); +void hsmp_metric_tbl_unmap_all (struct hsmp_plat_device *pdev,u16 num_sockets);struct hsmp_plat_device *get_hsmp_pdev (void );#if IS_ENABLED (CONFIG_HWMON )int hsmp_create_sensor (struct device *dev,u16 sock_ind);drivers/platform/x86/amd/hsmp/plat.c +1
-0@@ -230,6 +230,7 @@ static int hsmp_pltdrv_probe(struct platform_device *pdev)static void hsmp_pltdrv_remove (struct platform_device *pdev) {hsmp_misc_deregister (); +hsmp_metric_tbl_unmap_all (hsmp_pdev, hsmp_pdev->num_sockets); }static struct platform_driver amd_hsmp_driver = {
Signature · 1 line
i.
Re: [PATCH v6 8/8] platform/x86/amd/hsmp: Serialize metric table access and ACPI HSMP teardown
Quoted 37 lines · Ilpo Järvinen
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 23 lines · Muralidhara M K
Each ACPI HSMP socket bumps
acpi_sock_refcntafter a successful probe; the last socket to remove runsrefcount_dec_and_test()and tears down shared state once. Ifinit_acpi()ormisc_register()fails beforerefcount_inc(), callhsmp_acpi_free_sock_alloc_if_idle()to freehsmp_pdev->sockso a concurrent probe cannot follow a dangling pointer.1) Metric table sysfs reads ask the SMU to refresh the table, then
memcpy_fromio()from the mmio mapping. Concurrent readers could interleave those steps and return inconsistent data. Serialize with per-socketmetric_tbl_lockand guard(mutex) aroundhsmp_send_message()andmemcpy_fromio()so eachread()observes one coherent snapshot.
2) Every ACPI AMDI0097 device shares hsmp_pdev->sock[]. The driver useddevm_kcalloc()from whichever device probed first, an unlocked probe flag, andmisc_register()from the first successful path. Parallel probes could race, and devm tied the array lifetime to one device while others (and devm hwmon paths) still calledhsmp_send_message(), which could use-after-free the array on remove.
3) Metric DRAM useddevm_ioremap()anddevm_mutex_init()per ACPI device while the socket table is global, so devm teardown order did not match shared storage. Switch toioremap(), one-timemutex_init()(metric_lock_inited), andhsmp_metric_tbl_unmap_all()on the last ACPI remove together with the rest of global teardown.Quoted 8 lines
4) After kcalloc(sock) but before
refcount_inc(), one failing probe couldkfree()the array while another probe still raninit_acpi()on the same pointer. Holdhsmp_acpi_probe_mutexacrossinit_acpi(),misc_register(), andrefcount_inc()so setup does not overlap teardown or a concurrent probe.
5) On the legacy platform driver, callhsmp_misc_deregister()beforehsmp_metric_tbl_unmap_all()so/dev/hsmpis gone before metric mmio is unmapped, matching the ACPI remove path.
Quoted 3 lines
Thanks. To me it would look better to have the concurrency fixes done first. You could actually send them in own series and once that work is done, I'll process the ver7 series on top of that.
Sure. I will send concurrency fixes in own series first.
Quoted 4 lines
I took patch 6 nospec change into the review-ilpo-next already as it seems independent of the ver7 changes.
Thanks.
Quoted 30 lines
Many comments below.
Quoted 28 lines
Tested-by Muthusamy Ramalingam <[email protected]>Signed-off-by Muralidhara M K <[email protected]>--- drivers/platform/x86/amd/hsmp/acpi.c | 58 +++++++++++++++++++++------- drivers/platform/x86/amd/hsmp/hsmp.c | 56 +++++++++++++++++++++++++-- drivers/platform/x86/amd/hsmp/hsmp.h | 9 ++++- drivers/platform/x86/amd/hsmp/plat.c | 1 + 4 files changed, 106 insertions(+), 18 deletions(-)drivers/platform/x86/amd/hsmp/acpi.c +3
-0@@ -20,7 +20,10 @@#include <linux/ioport.h> #include <linux/kstrtox.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/platform_device.h> +#include <linux/refcount.h> +#include <linux/slab.h> #include <linux/sysfs.h> #include <linux/topology.h> #include <linux/uuid.h> @@ -595,6 +598,19 @@ static const struct acpi_device_id amd_hsmp_acpi_ids[] = { };MODULE_DEVICE_TABLE (acpi, amd_hsmp_acpi_ids);
Quoted 6 lines
drivers/platform/x86/amd/hsmp/acpi.c static const struct acpi_device_id amd_hsmp_acpi_ids[] = { · line 603
Quoted 3 lines
drivers/platform/x86/amd/hsmp/acpi.c +3
-0+static DEFINE_MUTEX (hsmp_acpi_probe_mutex); + +/* Caller holds hsmp_acpi_probe_mutex. */ Please use
lockdep_assert()to enforce any context requirement.
Thanks for suggestion. I will use.
Quoted 22 lines
For comments, the kerneldoc compatible formatting should be used (even if not writing a full kerneldoc comment):
* Context: caller must hold
hsmp_acpi_probe_mutex.Quoted 17 lines
drivers/platform/x86/amd/hsmp/acpi.c +10
-0+static void hsmp_acpi_free_sock_alloc_if_idle (void ) +{ +if (refcount_read (&hsmp_pdev->acpi_sock_refcnt) ==0 && + !hsmp_pdev->acpi_misc_registered && hsmp_pdev->sock) { +kfree (hsmp_pdev->sock); + hsmp_pdev->sock =NULL ; + hsmp_pdev->num_sockets =0 ; + } +} +static int hsmp_acpi_probe (struct platform_device *pdev) {int ret;@@ -603,49 +619,63 @@ static int hsmp_acpi_probe(struct platform_device *pdev)if (!hsmp_pdev)return -ENOMEM ;
Quoted 6 lines
drivers/platform/x86/amd/hsmp/acpi.c static int hsmp_acpi_probe(struct platform_device *pdev) · line 622
Quoted 2 lines
drivers/platform/x86/amd/hsmp/acpi.c +1
-1-if (!hsmp_pdev->is_probed) { +mutex_lock (&hsmp_acpi_probe_mutex);You should generally use
cleanup.hwhen taking the mutex, especially, if you need to duplicate the unlock to many places.
understood.
Quoted 68 lines
Quoted 57 lines
drivers/platform/x86/amd/hsmp/acpi.c +18
-12+if (!hsmp_pdev->sock) { hsmp_pdev->num_sockets =topology_max_packages ();if (!hsmp_pdev->num_sockets) {dev_err (&pdev->dev,"No CPU sockets detected\n" ); +mutex_unlock (&hsmp_acpi_probe_mutex);return -ENODEV ; } - hsmp_pdev->sock =devm_kcalloc (&pdev->dev, hsmp_pdev->num_sockets, -sizeof (*hsmp_pdev->sock), -GFP_KERNEL ); -if (!hsmp_pdev->sock) + hsmp_pdev->sock =kcalloc (hsmp_pdev->num_sockets, +sizeof (*hsmp_pdev->sock), +GFP_KERNEL ); +if (!hsmp_pdev->sock) { +mutex_unlock (&hsmp_acpi_probe_mutex);return -ENOMEM ; + } } ret =init_acpi (&pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to initialize HSMP interface.\n" ); +hsmp_acpi_free_sock_alloc_if_idle (); +mutex_unlock (&hsmp_acpi_probe_mutex);return ret; } -if (!hsmp_pdev->is_probed) { +if (!hsmp_pdev->acpi_misc_registered) { ret =hsmp_misc_register (&pdev->dev);if (ret) {dev_err (&pdev->dev,"Failed to register misc device\n" ); +hsmp_acpi_free_sock_alloc_if_idle (); +mutex_unlock (&hsmp_acpi_probe_mutex);return ret; } - hsmp_pdev->is_probed = true; -dev_dbg (&pdev->dev,"AMD HSMP ACPI is probed successfully\n" ); + hsmp_pdev->acpi_misc_registered = true; +dev_dbg (&pdev->dev,"AMD HSMP ACPI misc device registered\n" ); } +refcount_inc (&hsmp_pdev->acpi_sock_refcnt); +mutex_unlock (&hsmp_acpi_probe_mutex);return 0 ; }static void hsmp_acpi_remove (struct platform_device *pdev) { -/* -* We register only one misc_device even on multi-socket system. -* So, deregister should happen only once. -*/ -if (hsmp_pdev->is_probed) { +mutex_lock (&hsmp_acpi_probe_mutex);drivers/platform/x86/amd/hsmp/acpi.c static int hsmp_acpi_probe(struct platform_device *pdev) · line 676
Quoted 10 lines
drivers/platform/x86/amd/hsmp/acpi.c +8
-1+if (refcount_dec_and_test (&hsmp_pdev->acpi_sock_refcnt)) {hsmp_misc_deregister (); - hsmp_pdev->is_probed = false; + hsmp_pdev->acpi_misc_registered = false; +hsmp_metric_tbl_unmap_all (hsmp_pdev, hsmp_pdev->num_sockets); +kfree (hsmp_pdev->sock); + hsmp_pdev->sock =NULL ; + hsmp_pdev->num_sockets =0 ; + hsmp_pdev->proto_ver =0 ; + hsmp_pdev->hsmp_table_size =0 ;
Quoted 4 lines
Using
kref.h, you can give a release function for the put call and get/put will be cleaner interface anyway than handling the refcount directly.
Ya I will define the interface.
Quoted 100 lines
Quoted 82 lines
drivers/platform/x86/amd/hsmp/acpi.c +1
-0} +mutex_unlock (&hsmp_acpi_probe_mutex); }static struct platform_driver amd_hsmp_driver = {drivers/platform/x86/amd/hsmp/hsmp.c +40
-1@@ -12,6 +12,7 @@#include <linux/acpi.h> #include <linux/delay.h> #include <linux/device.h> +#include <linux/io.h> #include <linux/nospec.h> #include <linux/semaphore.h> #include <linux/slab.h> @@ -41,7 +42,9 @@*/ #define CHECK_GET_BIT BIT(31) -static struct hsmp_plat_device hsmp_pdev; +static struct hsmp_plat_device hsmp_pdev = { + .acpi_sock_refcnt =REFCOUNT_INIT (0 ), +};/* * Send a message to the HSMP port via PCI-e config space registers @@ -488,6 +491,7 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size) msg.msg_id =HSMP_GET_METRIC_TABLE ; msg.sock_ind = sock->sock_ind; +guard (mutex)(&sock->metric_tbl_lock); ret =hsmp_send_message (&msg);if (ret)return ret;@@ -497,6 +501,28 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size) }EXPORT_SYMBOL_NS_GPL (hsmp_metric_tbl_read,"AMD_HSMP" ); +void hsmp_metric_tbl_unmap_all (struct hsmp_plat_device *pdev,u16 num_sockets) +{ +u16 i; + +if (!pdev->sock || !num_sockets) +return ; + +for (i =0 ; i < num_sockets; i++) { +struct hsmp_socket *s = &pdev->sock[i]; + +if (s->metric_tbl_addr) { +iounmap (s->metric_tbl_addr); + s->metric_tbl_addr =NULL ; + } +if (s->metric_lock_inited) { +mutex_destroy (&s->metric_tbl_lock); + s->metric_lock_inited = false; + } + } +} +EXPORT_SYMBOL_NS_GPL (hsmp_metric_tbl_unmap_all,"AMD_HSMP" ); +int hsmp_get_tbl_dram_base (u16 sock_ind) {struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];@@ -504,6 +530,31 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)phys_addr_t dram_addr;int ret; +if (sock->metric_tbl_addr) +return 0 ; + +/* +* Initialize the per-socket lock before anything that can set +* sock->metric_tbl_addr to a non-NULL value. hsmp_metric_tbl_read() +* gates on sock->metric_tbl_addr being non-NULL and then takes +* metric_tbl_lock unconditionally; both callers of this function +* (init_acpi() and init_platform_device()) intentionally only log +* a failure here and continue probing, so an init order that left +* metric_tbl_addr populated while mutex_init failed would leave the +* read path locking an uninitialized mutex. Doing the mutex init +* first preserves the invariant "metric_tbl_addr != NULL implies the drivers/platform/x86/amd/hsmp/hsmp.c int hsmp_get_tbl_dram_base(u16 sock_ind) · line 555
Quoted 10 lines
drivers/platform/x86/amd/hsmp/hsmp.c +10
-0+* lock is usable" on every error exit. +* +* Use non-devm mutex + ioremap so the shared sock[] array (ACPI +* allocates once for all sockets) can be torn down from a single +* refcounted release path without devm lifetime being tied to the +* wrong struct device. +*/ +if (!sock->metric_lock_inited) { +mutex_init (&sock->metric_tbl_lock); + sock->metric_lock_inited = true;Perhaps it would be possible to arrange code such that booleans like this are not required.
This problem may in part stem from using global array to store sock instead of sock[] being just pointers to memory allocated dynamically on probe and freed on remove, and in part from the init order.
Quoted 7 lines
drivers/platform/x86/amd/hsmp/hsmp.c int hsmp_get_tbl_dram_base(u16 sock_ind) · line 556
Quoted 1 line
drivers/platform/x86/amd/hsmp/hsmp.c +1
-0+ }This patch should probably be split into series so it's easier to differentiate what belongs to which fix (or if something strictly belongs to the ver7 ioctl additions and not the existing code, it should be sent with them once the concurrency changes are made to come first).
I will create seperate patch list to fix them first.
Quoted 83 lines
Quoted 44 lines
drivers/platform/x86/amd/hsmp/hsmp.c +2
-2+ msg.sock_ind = sock_ind; msg.response_sz = hsmp_msg_desc_table[HSMP_GET_METRIC_TABLE_DRAM_ADDR ].response_sz; msg.msg_id =HSMP_GET_METRIC_TABLE_DRAM_ADDR ;@@ -527,8 +578,7 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)else hsmp_pdev.hsmp_table_size =sizeof (struct hsmp_metric_table ); - sock->metric_tbl_addr =devm_ioremap (sock->dev, dram_addr, - hsmp_pdev.hsmp_table_size); + sock->metric_tbl_addr =ioremap (dram_addr, hsmp_pdev.hsmp_table_size);if (!sock->metric_tbl_addr) {dev_err (sock->dev,"Failed to ioremap metric table addr\n" );return -ENOMEM ;drivers/platform/x86/amd/hsmp/hsmp.h +5
-0@@ -15,7 +15,9 @@#include <linux/hwmon.h> #include <linux/kconfig.h> #include <linux/miscdevice.h> +#include <linux/mutex.h> #include <linux/pci.h> +#include <linux/refcount.h> #include <linux/semaphore.h> #include <linux/sysfs.h> #include <linux/types.h> @@ -42,11 +44,14 @@ struct hsmp_socket {struct bin_attribute hsmp_attr;struct hsmp_mbaddr_info mbinfo;void __iomem *metric_tbl_addr; +/* Serializes concurrent metric table refreshes from the sysfs path */ +struct mutex metric_tbl_lock;void __iomem *virt_base_addr;struct semaphore hsmp_sem;char name[HSMP_ATTR_GRP_NAME_SIZE ];struct device *dev;u16 sock_ind; +bool metric_lock_inited;int (*amd_hsmp_rdwr)(struct hsmp_socket *sock,u32 off,u32 *val,bool rw); };drivers/platform/x86/amd/hsmp/hsmp.h struct hsmp_plat_device { · line 64
Quoted 6 lines
drivers/platform/x86/amd/hsmp/hsmp.h +2
-1@@ -55,7 +60,8 @@ struct hsmp_plat_device {struct hsmp_socket *sock;u32 proto_ver;u16 num_sockets; -bool is_probed; +refcount_t acpi_sock_refcnt; +bool acpi_misc_registered;Don't you know this based on the reference count?
Quoted 17 lines
drivers/platform/x86/amd/hsmp/hsmp.h +1
-0size_t hsmp_table_size; };@@ -66,6 +72,7 @@ void hsmp_misc_deregister(void);int hsmp_misc_register (struct device *dev);int hsmp_get_tbl_dram_base (u16 sock_ind);ssize_t hsmp_metric_tbl_read (struct hsmp_socket *sock,char *buf,size_t size); +void hsmp_metric_tbl_unmap_all (struct hsmp_plat_device *pdev,u16 num_sockets);struct hsmp_plat_device *get_hsmp_pdev (void );#if IS_ENABLED (CONFIG_HWMON )int hsmp_create_sensor (struct device *dev,u16 sock_ind);drivers/platform/x86/amd/hsmp/plat.c +0
-0drivers/platform/x86/amd/hsmp/plat.c static int hsmp_pltdrv_probe(struct platform_device *pdev) · line 236
Quoted 7 lines
drivers/platform/x86/amd/hsmp/plat.c +1
-0@@ -230,6 +230,7 @@ static int hsmp_pltdrv_probe(struct platform_device *pdev)static void hsmp_pltdrv_remove (struct platform_device *pdev) {hsmp_misc_deregister (); +hsmp_metric_tbl_unmap_all (hsmp_pdev, hsmp_pdev->num_sockets); }static struct platform_driver amd_hsmp_driver = {--
i.
Ilpo Järvinen
maintainerQuoted 41 lines · Muralidhara M K
Hi,
This series extends the AMD HSMP platform driver and UAPI for Family 1Ah, Model 50h-5Fh: new HSMP message IDs (including PQoS-related IDs in the first patch), Zen 6 metric table layout in UAPI, relaxed
response_szhandling against the descriptor table, firmware-reported metric table sizing for ioremap and reads,HSMP_IOCTL_GET_TELEMETRY_DATAon the existing character device for a single-shot full-table copy (avoiding sysfsPAGE_SIZElimits and torn reads), ACPI enablement for protocol version 7 tables, Spectre-v1 hardening on user-controlled indices, and coordinated metric-table access plus ACPI HSMP probe/remove lifetime fixes on the shared global socket array. Documentation is updated inDocumentation/arch/x86/amd_hsmp.rst.v5 submission (lore.kernel.org)
https://lore.kernel.org/platform-driver-x86/[email protected]/T/#t
Changes since v5
* Patch 6/8: Tighten the Spectre-v1 changelog and subject around clamping ioctl and
hsmp_send_message()indices; wrap the commit description soscripts/checkpatch.plstays quiet on line length.* Patch 8/8: Broaden the final fix beyond sysfs metric locking alone. Serialize metric table refresh and
memcpy_fromio(), refcount ACPI HSMP sockets with a probe mutex acrossinit_acpi(),misc_register(), andrefcount_inc(), replace devm metric DRAM maps with explicit ioremap()/mutex_init plushsmp_metric_tbl_unmap_all()on last ACPI remove, addhsmp_acpi_free_sock_alloc_if_idle()for early probe failures, and order legacy platform remove so misc is deregistered before metric unmap (matching ACPI teardown).The series is bisect-safe: each commit was built with
make M=drivers/platform/x86/amd/hsmp
after the previous commits, andscripts/checkpatch.pl-g <rev> was run on each change (0 errors / 0 warnings).Thanks,
Muralidhara M K
Quoted 14 lines
Muralidhara M K (8):
platform/x86/amd/hsmp: Add HSMP messages for Family 1Ah, Model 50h-5Fh platform/x86/amd/hsmp: Add UAPI structures for Family 1Ah Model
50h-5Fh metrics table
platform/x86/amd/hsmp: Unifyresponse_szvalidation to an upper-bound
check
platform/x86/amd/hsmp: Source metric-table size from firmware platform/x86/amd/hsmp: AddIOCTL_GET_TELEMETRY_DATAfor metric table
reads
platform/x86/amd/hsmp: Clamp ioctl/send_message indices (Spectre v1) platform/x86/amd/hsmp: EnableHSMP_PROTO_VER7metric tables on the
ACPI driver via the IOCTL
platform/x86/amd/hsmp: Serialize metric table access and ACPI HSMP
teardown
Hi,
Could you please do a fresh iteration of this. It no longer seems to apply cleanly and maybe sashiko now figures we should have the locking in place as well so we get a fresh review which hopefully does take the latest improvements into account.
Signature · 1 line
i.