platform/x86/amd/hsmp: Family 1Ah Model 50h-5Fh HSMP and metrics
Subsystems (4)
v7 cover platform/x86/amd/hsmp: Family 1Ah Model 50h-5Fh HSMP and metrics
This series extends the AMD HSMP platform driver and UAPI for Family 1Ah, Model 50h-5Fh: new HSMP message IDs (including the PQoS-related IDs), 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, and ACPI enablement for protocol version 7 tables. Documentation is updated in Documentation/arch/x86/amd_hsmp.rst.
The motivation for the new ioctl is that the sysfs metrics_bin path cannot serve the newer tables. It is a file read, so userspace can read it in chunks and observe a torn snapshot if an SMU refresh lands between read() calls, and it is bounded by PAGE_SIZE, so the ~13 KB table used by protocol version 7 on Family 1Ah Model 50h-5Fh cannot be returned at all. Rather than extend sysfs to lift both restrictions, the metric table is exposed through /dev/hsmp with an ioctl that always copies the table in one shot.
v6 submission (lore.kernel.org)
This series applies on top of the HSMP concurrency series. The series is bisect-safe: each commit was built with
make W=1 drivers/platform/x86/amd/hsmp/
on top of the previous commits with no warnings, and scripts/checkpatch.pl --strict -g <rev> was run on each change (0 errors / 0 warnings / 0 checks).
Muralidhara M K (5):
platform/x86/amd/hsmp: Add HSMP messages for Family 1Ah, Model 50h-5Fh 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: Enable protocol version 7 metric tables on the
ACPI driver
Documentation/arch/x86/amd_hsmp.rst | 35 ++++- arch/x86/include/uapi/asm/amd_hsmp.h | 222 +++++++++++++++++++++++++-- drivers/platform/x86/amd/hsmp/acpi.c | 22 ++- drivers/platform/x86/amd/hsmp/hsmp.c | 183 +++++++++++++++++++--- drivers/platform/x86/amd/hsmp/hsmp.h | 5 +- 5 files changed, 428 insertions(+), 39 deletions(-)
base-commit: da97ec927e4441fed07044e3fed59763d7e07c3b
Signature · 1 line
2.34.1
PATCH v7 1/5 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 and convert the three messages to HSMP_SET_GET.
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. Converting the three SET messages to HSMP_SET_GET also keeps them accepted by validate_message(), which already applies a relaxed upper-bound check on response_sz for that type.
--- arch/x86/include/uapi/asm/amd_hsmp.h | 172 ++++++++++++++++++++++++--- 1 file changed, 158 insertions(+), 14 deletions(-)
arch/x86/include/uapi/asm/amd_hsmp.h +158 -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,89 @@ 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 SDPS limit[31] (0 = set, 1 = get) + +* 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) */
Signature · 1 line
2.34.1
PATCH v7 2/5 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 | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/platform/x86/amd/hsmp/hsmp.c +9 -13
@@ -197,20 +197,16 @@ 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 v7 3/5 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 table under protocol version 7, 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, and store the result in the new per-socket hsmp_socket.metric_tbl_size, which is then used both for the ioremap() of the region and as the expected size in hsmp_metric_tbl_read().
The size is stored per socket rather than per platform because hsmp_get_tbl_dram_base() runs once per socket and each socket maps its own region. A single platform-wide field would let the last socket's size be used to copy out of an earlier socket's smaller mapping.
Bump DRIVER_VERSION to 2.6.
Behaviour on existing protocol-version-6 hardware is unchanged. Reading a third response word is safe there: for this command SMU leaves args[2] as 0 rather than a stale value from an earlier mailbox transaction, so the fallback always applies, yielding the same value as the previous hard-coded one, and both the ioremap and the size check produce the same result as before.
--- arch/x86/include/uapi/asm/amd_hsmp.h | 5 +++-- drivers/platform/x86/amd/hsmp/hsmp.c | 17 ++++++++++++++--- drivers/platform/x86/amd/hsmp/hsmp.h | 5 ++++- 3 files changed, 21 insertions(+), 6 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 +14 -3
@@ -429,8 +429,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 != sock->metric_tbl_size) {dev_err (sock->dev,"Wrong buffer size\n" );return -EINVAL ; }
@@ -484,6 +483,7 @@ void hsmp_unmap_metric_tbls(struct hsmp_plat_device *pdev)iounmap (sock->metric_tbl_addr); sock->metric_tbl_addr =NULL ; } + sock->metric_tbl_size =0 ; } }EXPORT_SYMBOL_NS_GPL (hsmp_unmap_metric_tbls,"AMD_HSMP" );
@@ -493,6 +493,7 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];struct hsmp_message msg = {0 };phys_addr_t dram_addr; +size_t tbl_size;int ret; msg.sock_ind = sock_ind;
@@ -524,11 +525,21 @@ int hsmp_get_tbl_dram_base(u16 sock_ind)iounmap (sock->metric_tbl_addr); sock->metric_tbl_addr =NULL ; } - sock->metric_tbl_addr =ioremap (dram_addr,sizeof (struct hsmp_metric_table )); + sock->metric_tbl_size =0 ; + +/* SMU returns table size from Family 1Ah Model 50h and forward */ +if (msg.args[2 ]) + tbl_size = msg.args[2 ]; +else + tbl_size =sizeof (struct hsmp_metric_table ); + + sock->metric_tbl_addr =ioremap (dram_addr, tbl_size);if (!sock->metric_tbl_addr) {dev_err (sock->dev,"Failed to ioremap metric table addr\n" );return -ENOMEM ; } + sock->metric_tbl_size = tbl_size; +return 0 ; }EXPORT_SYMBOL_NS_GPL (hsmp_get_tbl_dram_base,"AMD_HSMP" );
drivers/platform/x86/amd/hsmp/hsmp.h +4 -1
@@ -20,6 +20,7 @@#include <linux/rwsem.h> #include <linux/semaphore.h> #include <linux/sysfs.h> +#include <linux/types.h> #define HSMP_METRICS_TABLE_NAME "metrics_bin"
@@ -29,7 +30,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;
@@ -43,6 +44,8 @@ struct hsmp_socket {struct bin_attribute hsmp_attr;struct hsmp_mbaddr_info mbinfo;void __iomem *metric_tbl_addr; +/* Size of the region mapped at @metric_tbl_addr, as reported by SMU */ +size_t metric_tbl_size;void __iomem *virt_base_addr;struct semaphore hsmp_sem;/* Serializes HSMP_GET_METRIC_TABLE fill-and-copy for this socket */
Signature · 1 line
2.34.1
PATCH v7 4/5 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 table used by HSMP protocol version 7 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 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. The command is encoded with _IOW because the kernel only reads the request struct; the snapshot travels through the user pointer it carries.
The requested size may be anything from one byte up to the size firmware reported for that socket's table. A short request returns the leading bytes of the snapshot, so userspace built against an older table layout keeps working on firmware that grew the table, mirroring the relaxed response_sz rule applied to HSMP messages earlier in this series. A request larger than the firmware table is rejected with -EINVAL rather than short-written, so a caller can never mistake a partial copy for a full one.
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.
/dev/hsmp is a singleton character device that outlives an individual socket unbind, so an ioctl issued on an already-open fd can run concurrently with socket teardown. hsmp_sock_rwsem is the driver's contract for that: the data plane takes it for read, and probe and remove take it for write to drain the data plane before freeing the socket array, unmapping the metric tables and destroying the per-socket mutexes. hsmp_ioctl_get_telemetry() takes it for read across the socket lookup, the checks on that socket's metric-table state and the table read itself, so none of that state can be torn down underneath it. Without this the handler would sleep in its kvmalloc() holding no lock at all, and could resume with a freed socket, locking a destroyed mutex and reading from an unmapped iomem region.
The lock is dropped before the copy_to_user(), because faulting in the destination can block indefinitely on a userfaultfd-backed buffer and would otherwise leave a socket unbind waiting for the write lock.
Since hsmp_metric_tbl_read() reached the mailbox through hsmp_send_message(), which takes hsmp_sock_rwsem itself, calling it with the lock already held would recursively take the read side and can deadlock against a queued writer. Split out hsmp_metric_tbl_read_locked(), which asserts the lock and uses hsmp_send_message_locked(), and leave hsmp_metric_tbl_read() as a wrapper that takes the read lock for the sysfs callers. This also brings the whole fill-and-copy under the rwsem for those callers, where the memcpy_fromio() previously ran outside it, and makes the lock order uniformly hsmp_sock_rwsem -> metric_read_lock -> hsmp_sem.
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). Include linux/nospec.h, which the file relied on getting transitively.
--- Documentation/arch/x86/amd_hsmp.rst | 35 ++++++- arch/x86/include/uapi/asm/amd_hsmp.h | 45 +++++++++ drivers/platform/x86/amd/hsmp/hsmp.c | 144 ++++++++++++++++++++++++++- 3 files changed, 220 insertions(+), 4 deletions(-)
Documentation/arch/x86/amd_hsmp.rst +34 -1
@@ -68,6 +68,13 @@ 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. The protocol version 7 metric table (~13 KB) also +exceeds PAGE_SIZE, so a read returns ``-EOPNOTSUPP`` there. 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 +174,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 +187,32 @@ 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, whose table is ~13 KB). Argument:: + + struct hsmp_telemetry_data { + __u64 buf; /* User pointer to destination buffer */ + __u32 size; /* Size of @buf in bytes */ + __u16 sock_ind; /* Socket index */ + __u16 reserved; /* Reserved, must be zero */ + }; + + ``size`` must be non-zero and no larger than the table size firmware + reports for that socket; a larger value is rejected with ``-EINVAL`` + rather than short-written, and a smaller one returns the leading + ``size`` bytes of the snapshot. A non-zero ``reserved`` is also + rejected with ``-EINVAL``. + + The table layout depends on the protocol version, which userspace + reads from the ``protocol_version`` sysfs attribute. On version 6 + the table is ``struct hsmp_metric_table``, so callers pass + ``sizeof(struct hsmp_metric_table)``. Later version metrics table + layout is documented in the Public PPR. + 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 +45 -0
55 lines not on this page · raw message
drivers/platform/x86/amd/hsmp/hsmp.c +141 -3
181 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v7 5/5 platform/x86/amd/hsmp: Enable protocol version 7 metric tables on the ACPI driver
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 larger ~13 KB table: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 sock->metric_tbl_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, because the sysfs binary attribute cannot carry a table larger than PAGE_SIZE. Version 7 userspace gets a clear, actionable error and a documented pointer to HSMP_IOCTL_GET_TELEMETRY_DATA; version 6 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 | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/platform/x86/amd/hsmp/acpi.c +20 -2
@@ -277,13 +277,31 @@ 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). The larger tables defined from protocol +* version 7 onwards 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 on protocol version 7 and later 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 ;
@@ -539,7 +557,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
Ilpo Järvinen
maintainerQuoted 9 lines · Muralidhara M K
This series extends the AMD HSMP platform driver and UAPI for Family 1Ah, Model 50h-5Fh: new HSMP message IDs (including the PQoS-related IDs), 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, and ACPI enablement for protocol version 7 tables. Documentation is updated inDocumentation/arch/x86/amd_hsmp.rst.[...]
Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.
FYI [if applicable to your patch], as per Linus' policy change, also fixes are mostly routed through for-next unless the fix is for a commit introduced in the most recent cycle or is clearly a regression fix.
The list of commits applied:
[1/5] platform/x86/amd/hsmp: Add HSMP messages for Family 1Ah, Model 50h-5Fh
commit: d30569cb5aadcc623accf0422132d0bc0286a9ff
[2/5] platform/x86/amd/hsmp: Unify response_sz validation to an upper-bound check
commit: 9185ad51dfd2bc8bde0c7e7d9f4493d8758d4fab
[3/5] platform/x86/amd/hsmp: Source metric-table size from firmware
commit: 96f1ba765ab55d57ee2a2bf88e05c2ee699c7c5b
[4/5] platform/x86/amd/hsmp: Add IOCTL_GET_TELEMETRY_DATA for metric table reads
commit: 5273183b6362cad9584bdfb5dddb2df30e4f5477
[5/5] platform/x86/amd/hsmp: Enable protocol version 7 metric tables on the ACPI driver
commit: aca39607c1734ed976fdd65deb75b3555a5a0326--
i.