platform/x86/amd/hsmp: Validate num_args and Report raw SMU mailbox status
Subsystems (4)
v4 cover platform/x86/amd/hsmp: Validate num_args and Report raw SMU mailbox status
This series adds a __u16 smu_response field to struct hsmp_message and struct hsmp_telemetry_data, so userspace can inspect the raw SMU mailbox status instead of only the translated errno.
Patch 1 is a preparatory fix: hsmp_ioctl_msg()'s fd-mode gate calls is_get_msg(), which reads msg.args[0], before checking that num_args matches the message descriptor, so a request with no args can still reach that read. Validate num_args first.
Patch 2 adds smu_response to both structs, set from the polled SMU mailbox status register, and has hsmp_ioctl_msg() always copy the message struct back to userspace, even on failure, so smu_response is visible on every call. It also fixes a related copy_to_user() error-check bug in hsmp_ioctl_get_telemetry() uncovered along the way.
smu_response is __u16, matching the width of existing spare padding in both structs, so sizeof(struct hsmp_message) and sizeof(struct hsmp_telemetry_data) are unchanged. Both ioctl command numbers, HSMP_IOCTL_CMD and HSMP_IOCTL_GET_TELEMETRY_DATA, are also unchanged from what is already shipped.
v4:
- Kept HSMP_IOCTL_GET_TELEMETRY_DATA as _IOW instead of switching it to _IOWR. This ioctl already shipped as _IOW in v7.3; _IOW/_IOWR only affect the ioctl command number used for dispatch, not whether the kernel is allowed to write data back via copy_to_user(), so there is no functional need for _IOWR here, and switching to it would have changed the command number and broken already-compiled userspace built against the released header.
v3:
- Reverted smu_response from __u32 back to __u16: the mailbox status register is accessed as __u32 purely because that is the SMN/MMIO bus access granularity shared by all mailbox registers, not because the status value itself needs 32 bits. Every documented HSMP status/error code fits in a single byte, so __u16 is lossless and avoids the ABI break (struct size / ioctl command number change) that __u32 would have introduced.
- Fixed a redundant whitespace-only diff in Documentation/arch/x86/amd_hsmp.rst where the struct hsmp_message code block had been reformatted from tabs to spaces; restored the original tab alignment so only the smu_response line shows as new.
Each patch builds individually and is clean under checkpatch.pl --strict.
Muralidhara M K (2):
platform/x86/amd/hsmp: Validate num_args before the fd-mode gate in
hsmp_ioctl_msg()
platform/x86/amd/hsmp: Report raw SMU mailbox status via smu_response
Documentation/arch/x86/amd_hsmp.rst | 23 +++++- arch/x86/include/uapi/asm/amd_hsmp.h | 19 ++++- drivers/platform/x86/amd/hsmp/hsmp.c | 117 +++++++++++++++++---------- 3 files changed, 111 insertions(+), 48 deletions(-)
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
Signature · 1 line
2.34.1
PATCH v4 1/2 platform/x86/amd/hsmp: Validate num_args before the fd-mode gate in hsmp_ioctl_msg()
is_get_msg() reads msg.args[0]'s CHECK_GET_BIT to tell GET and SET apart for HSMP_SET_GET messages, but hsmp_ioctl_msg() calls it before validate_message() (reached later through hsmp_send_message()) checks that msg.num_args matches desc->num_args. A request with num_args = 0 for such a message reaches is_get_msg() anyway, so args[0] is read even though the request does not carry it.
Check msg.num_args against desc->num_args before the fd-mode switch, so a num_args mismatch is rejected with -EINVAL before is_get_msg() reads args[0], rather than surfacing as -EPERM from the fd-mode gate.
--- drivers/platform/x86/amd/hsmp/hsmp.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/platform/x86/amd/hsmp/hsmp.c +10 -0
@@ -485,6 +485,16 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)if (!desc)return -ENOMSG ; +/* +* is_get_msg() below reads msg.args[0] for HSMP_SET_GET +* messages. Reject a num_args mismatch here first, so a +* malformed request fails with -EINVAL instead of letting +* the fd-mode gate read an argument slot the request does +* not actually carry. +*/ +if (msg.num_args != desc->num_args) +return -EINVAL ; +switch (fp->f_mode & (FMODE_WRITE |FMODE_READ )) {case FMODE_WRITE :/*
Signature · 1 line
2.34.1
PATCH v4 2/2 platform/x86/amd/hsmp: Report raw SMU mailbox status via smu_response
In reply to Muralidhara M KAdd smu_response to struct hsmp_message and struct hsmp_telemetry_data, set from the polled SMU mailbox status register and surfaced to userspace on every HSMP_IOCTL_CMD and HSMP_IOCTL_GET_TELEMETRY_DATA call. Both fields fit in existing spare bytes, keeping sizeof() and both ioctl command numbers unchanged.
Update Documentation/arch/x86/amd_hsmp.rst to match.
--- Documentation/arch/x86/amd_hsmp.rst | 23 +++++- arch/x86/include/uapi/asm/amd_hsmp.h | 19 ++++- drivers/platform/x86/amd/hsmp/hsmp.c | 111 ++++++++++++++++----------- 3 files changed, 103 insertions(+), 50 deletions(-)
Documentation/arch/x86/amd_hsmp.rst +20 -3
@@ -193,8 +193,20 @@ The following IOCTLs are defined: __u16 response_sz; /* Number of expected output/response words */ __u32 args[HSMP_MAX_MSG_LEN]; /* argument/response buffer */ __u16 sock_ind; /* socket number */ + __u16 smu_response; /* Output: raw SMU mailbox status */ }; + ``msg`` is copied back to userspace after every call, regardless of + whether the ioctl succeeds or fails, so ``smu_response`` is always + visible: 1 means the SMU accepted the message, and any other + nonzero value is an SMU-specific error code. 0 (HSMP_STATUS_NOT_READY) + is overloaded: it means either the message was rejected before + dispatch (e.g. an invalid msg_id or argument count, or an unprobed + socket) and never reached the SMU at all, or it was written to the + mailbox but the SMU never responded before the driver's internal + timeout (reported as ``-ETIMEDOUT``). Callers cannot tell these two + 0 cases apart from ``smu_response`` alone. + ``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
@@ -206,14 +218,19 @@ The following IOCTLs are defined: __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 */ + __u16 smu_response; /* Output: raw SMU mailbox status */ }; ``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``. + ``size`` bytes of the snapshot. ``req`` is copied back to userspace + with ``smu_response`` filled in, using the same values as ``msg``'s + ``smu_response`` above, on every call that reaches the point of + reading ``req`` from userspace, including failures from there on. + The one exception is a request on an fd not opened for reading, + which is rejected with ``-EPERM`` before ``req`` is read at all, so + there is nothing to copy back. The table layout depends on the protocol version, which userspace reads from the ``protocol_version`` sysfs attribute. On version 6
arch/x86/include/uapi/asm/amd_hsmp.h +15 -4
@@ -88,6 +88,13 @@ struct hsmp_message {__u16 response_sz;/* Number of expected output/response words */ __u32 args[HSMP_MAX_MSG_LEN ];/* argument/response buffer */ __u16 sock_ind;/* socket number */ +/* +* Output: raw SMU mailbox status. 1 = accepted; any other nonzero +* value is an SMU-specific error code. 0 means either the message +* never reached the SMU, or it did and the SMU never responded +* (-ETIMEDOUT); the two are not distinguishable from this field. +*/ +__u16 smu_response; };enum hsmp_msg_type {
@@ -644,9 +651,13 @@ struct hsmp_metric_table {* short-written. A smaller value returns the leading @size bytes * of the snapshot. The kernel does not write this field back. * @sock_ind: Input. Socket index from which the metric table is read. -* @reserved: Reserved for future use. Callers should set this to zero; -* future kernels may begin interpreting the field, so passing -* a non-zero value today is not forwards compatible. +* @smu_response: Output. Raw SMU mailbox status from the HSMP_GET_METRIC_TABLE +* message that refilled the table. 1 = success; any other +* nonzero value is an SMU-specific error code. 0 means +* either the message never reached the SMU, or it did and +* the SMU never responded; the two are not distinguishable +* from this field. Callers should set this to zero on +* input; the kernel overwrites it before returning. * * Placing @buf first lets all fields fall on their natural alignment under * the surrounding #pragma pack(4), so the struct is a tight 16 bytes with
@@ -662,7 +673,7 @@ struct hsmp_telemetry_data {__u64 buf;__u32 size;__u16 sock_ind; -__u16 reserved; +__u16 smu_response; };/* Reset to default packing */
drivers/platform/x86/amd/hsmp/hsmp.c +68 -43
233 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v4 2/2] platform/x86/amd/hsmp: Report raw SMU mailbox status via smu_response
Quoted 69 lines · Muralidhara M K
Add
smu_responseto structhsmp_messageand structhsmp_telemetry_data, set from the polled SMU mailbox status register and surfaced to userspace on everyHSMP_IOCTL_CMDandHSMP_IOCTL_GET_TELEMETRY_DATAcall. Both fields fit in existing spare bytes, keepingsizeof()and both ioctl command numbers unchanged.Update
Documentation/arch/x86/amd_hsmp.rstto match.Signed-off-by Muralidhara M K <[email protected]>--- Documentation/arch/x86/amd_hsmp.rst | 23 +++++- arch/x86/include/uapi/asm/amd_hsmp.h | 19 ++++- drivers/platform/x86/amd/hsmp/hsmp.c | 111 ++++++++++++++++----------- 3 files changed, 103 insertions(+), 50 deletions(-)Documentation/arch/x86/amd_hsmp.rst +20
-3@@ -193,8 +193,20 @@ The following IOCTLs are defined: __u16 response_sz; /* Number of expected output/response words */ __u32 args[HSMP_MAX_MSG_LEN]; /* argument/response buffer */ __u16 sock_ind; /* socket number */ + __u16 smu_response; /* Output: raw SMU mailbox status */ }; + ``msg`` is copied back to userspace after every call, regardless of + whether the ioctl succeeds or fails, so ``smu_response`` is always + visible: 1 means the SMU accepted the message, and any other + nonzero value is an SMU-specific error code. 0 (HSMP_STATUS_NOT_READY) + is overloaded: it means either the message was rejected before + dispatch (e.g. an invalid msg_id or argument count, or an unprobed + socket) and never reached the SMU at all, or it was written to the + mailbox but the SMU never responded before the driver's internal + timeout (reported as ``-ETIMEDOUT``). Callers cannot tell these two + 0 cases apart from ``smu_response`` alone. + ``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@@ -206,14 +218,19 @@ The following IOCTLs are defined: __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 */ + __u16 smu_response; /* Output: raw SMU mailbox status */ }; ``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``. + ``size`` bytes of the snapshot. ``req`` is copied back to userspace + with ``smu_response`` filled in, using the same values as ``msg``'s + ``smu_response`` above, on every call that reaches the point of + reading ``req`` from userspace, including failures from there on. + The one exception is a request on an fd not opened for reading, + which is rejected with ``-EPERM`` before ``req`` is read at all, so + there is nothing to copy back. The table layout depends on the protocol version, which userspace reads from the ``protocol_version`` sysfs attribute. On version 6arch/x86/include/uapi/asm/amd_hsmp.h +0
-0@@ -88,6 +88,13 @@ struct hsmp_message {__u16 response_sz;/* Number of expected output/response words */
arch/x86/include/uapi/asm/amd_hsmp.h struct hsmp_message { · line 98
Quoted 10 lines
arch/x86/include/uapi/asm/amd_hsmp.h +7
-0__u32 args[HSMP_MAX_MSG_LEN ];/* argument/response buffer */ __u16 sock_ind;/* socket number */ +/* +* Output: raw SMU mailbox status. 1 = accepted; any other nonzero +* value is an SMU-specific error code. 0 means either the message +* never reached the SMU, or it did and the SMU never responded +* (-ETIMEDOUT); the two are not distinguishable from this field. +*/ +__u16 smu_response; };
Isn't this still breaking for userspace? It will send the wrong sized struct with the older struct hsmp_message.
Why not instead map the SMU error codes and return the code to the caller? Then you don't need to change your interface at all.
This is what we do in other code that talks to the ASP.
Take a look here: https://github.com/torvalds/linux/blob/master/drivers/crypto/ccp/dbc.c#L30
I think you can do something pretty similar.
Quoted 20 lines
arch/x86/include/uapi/asm/amd_hsmp.h +7
-3enum hsmp_msg_type {@@ -644,9 +651,13 @@ struct hsmp_metric_table {* short-written. A smaller value returns the leading @size bytes * of the snapshot. The kernel does not write this field back. * @sock_ind: Input. Socket index from which the metric table is read. -* @reserved: Reserved for future use. Callers should set this to zero; -* future kernels may begin interpreting the field, so passing -* a non-zero value today is not forwards compatible. +* @smu_response: Output. Raw SMU mailbox status from the HSMP_GET_METRIC_TABLE +* message that refilled the table. 1 = success; any other +* nonzero value is an SMU-specific error code. 0 means +* either the message never reached the SMU, or it did and +* the SMU never responded; the two are not distinguishable +* from this field. Callers should set this to zero on +* input; the kernel overwrites it before returning. * * Placing @buf first lets all fields fall on their natural alignment under * the surrounding #pragma pack(4), so the struct is a tight 16 bytes with
arch/x86/include/uapi/asm/amd_hsmp.h struct hsmp_telemetry_data { · line 676
Quoted 5 lines
arch/x86/include/uapi/asm/amd_hsmp.h +1
-1@@ -662,7 +673,7 @@ struct hsmp_telemetry_data {__u64 buf;__u32 size;__u16 sock_ind; -__u16 reserved; +__u16 smu_response;
Given you only have 16 bits left do you really want to use the whole thing for the response? Aren't most responses only 8 bits?
Quoted 257 lines
arch/x86/include/uapi/asm/amd_hsmp.h +0
-0};/* Reset to default packing */ drivers/platform/x86/amd/hsmp/hsmp.c +68
-43@@ -173,6 +173,9 @@ static int __hsmp_send_message(struct hsmp_socket *sock, struct hsmp_message *ms mbinfo = &sock->mbinfo; +/* Default to "not sent" until the mailbox status is actually polled */ + msg->smu_response =HSMP_STATUS_NOT_READY ; +/* Clear the status register */ mbox_status =HSMP_STATUS_NOT_READY ; ret = sock->amd_hsmp_rdwr (sock, mbinfo->msg_resp_off, &mbox_status,HSMP_WR );@@ -229,6 +232,9 @@ static int __hsmp_send_message(struct hsmp_socket *sock, struct hsmp_message *msusleep_range (1000 ,2000 ); } +/* Report the raw mailbox status regardless of outcome */ + msg->smu_response = mbox_status; +if (unlikely (mbox_status ==HSMP_STATUS_NOT_READY )) {dev_err (sock->dev,"Message ID 0x%X failure : SMU timeout (status = 0x%X)\n" , msg->msg_id, mbox_status);@@ -340,6 +346,10 @@ static int hsmp_send_message_locked(struct hsmp_message *msg)if (!msg)return -EINVAL ; + +/* Default to "not sent" until __hsmp_send_message() runs */ + msg->smu_response =HSMP_STATUS_NOT_READY ; + ret =validate_message (msg);if (ret)return ret;@@ -471,6 +481,9 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)if (copy_struct_from_user (&msg,sizeof (msg), arguser,sizeof (struct hsmp_message )))return -EFAULT ; +/* Default to "not sent" until hsmp_send_message() runs */ + msg.smu_response =HSMP_STATUS_NOT_READY ; +/* * The client message set has no per-message descriptor table to * bounds-check msg_id against or index into, and no per-message @@ -482,8 +495,10 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)if (plat_desc->msg_desc) {/* Rejects out-of-range and reserved msg_ids */ desc =get_msg_desc (msg.msg_id); -if (!desc) -return -ENOMSG ; +if (!desc) { + ret = -ENOMSG ; + goto out; + }/* * is_get_msg() below reads msg.args[0] for HSMP_SET_GET @@ -492,8 +507,10 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)* the fd-mode gate read an argument slot the request does * not actually carry. */ -if (msg.num_args != desc->num_args) -return -EINVAL ; +if (msg.num_args != desc->num_args) { + ret = -EINVAL ; + goto out; + }switch (fp->f_mode & (FMODE_WRITE |FMODE_READ )) {case FMODE_WRITE :@@ -501,16 +518,20 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)* Device is opened in O_WRONLY mode * Execute only set/configure commands. */ -if (is_get_msg (desc, &msg)) -return -EPERM ; +if (is_get_msg (desc, &msg)) { + ret = -EPERM ; + goto out; + }break ;case FMODE_READ :/* * Device is opened in O_RDONLY mode * Execute only get/monitor commands. */ -if (!is_get_msg (desc, &msg)) -return -EPERM ; +if (!is_get_msg (desc, &msg)) { + ret = -EPERM ; + goto out; + }break ;case FMODE_READ |FMODE_WRITE :/* @@ -519,30 +540,23 @@ static long hsmp_ioctl_msg(struct file *fp, unsigned long arg)*/ break ;default : -return -EPERM ; + ret = -EPERM ; + goto out; } } ret =hsmp_send_message (&msg); -if (ret) -return ret; -/* -* The client message set has no per-message response_sz to check -* here (desc is NULL); validate_message() already bounded -* msg.response_sz. -*/ -if ((desc ? desc->response_sz : msg.response_sz) >0 ) { -/* Copy results back to user for get/monitor commands */ -if (copy_to_user (arguser, &msg,sizeof (struct hsmp_message ))) -return -EFAULT ; - } +out: +/* Copy back even on failure, so smu_response is always visible */ +if (copy_to_user (arguser, &msg,sizeof (struct hsmp_message ))) +return -EFAULT ; -return 0 ; +return ret; }static ssize_t hsmp_metric_tbl_read_locked (struct hsmp_socket *sock,char *buf, -size_t size); +size_t size,u16 *smu_response);/* * Fetch the firmware metric (telemetry) table for the requested socket and @@ -558,6 +572,7 @@ static long hsmp_ioctl_get_telemetry(struct file *fp, unsigned long arg) void *kbuf __free(kvfree) = NULL;void __user *arguser = (void __user *)arg;struct hsmp_telemetry_data req; +u16 smu_response =HSMP_STATUS_NOT_READY ;struct hsmp_socket *sock;void __user *user_buf;size_t tbl_size;@@ -571,13 +586,6 @@ static long hsmp_ioctl_get_telemetry(struct file *fp, unsigned long arg)if (copy_from_user (&req, arguser,sizeof (req)))return -EFAULT ; -/* -* Reserved fields must be zero so future kernels can safely -* repurpose them without breaking already-deployed userspace. -*/ -if (req.reserved) -return -EINVAL ; - user_buf =u64_to_user_ptr (req.buf);/* @@ -594,8 +602,10 @@ static long hsmp_ioctl_get_telemetry(struct file *fp, unsigned long arg)* which would leave a socket unbind waiting for the write lock. */ scoped_guard(rwsem_read, &hsmp_sock_rwsem) { -if (!hsmp_pdev.sock || req.sock_ind >= hsmp_pdev.num_sockets) -return -ENODEV ; +if (!hsmp_pdev.sock || req.sock_ind >= hsmp_pdev.num_sockets) { + ret = -ENODEV ; +break ; + }/* * Sanitize the user-controlled socket index against speculative @@ -609,12 +619,16 @@ static long hsmp_ioctl_get_telemetry(struct file *fp, unsigned long arg)*/ sock_ind =array_index_nospec (req.sock_ind, hsmp_pdev.num_sockets); sock = &hsmp_pdev.sock[sock_ind]; -if (!sock->metric_tbl_addr) -return -ENODEV ; +if (!sock->metric_tbl_addr) { + ret = -ENODEV ; +break ; + } tbl_size = sock->metric_tbl_size; -if (!tbl_size) -return -ENODEV ; +if (!tbl_size) { + ret = -ENODEV ; +break ; + }/* * A request shorter than the firmware table is served with the @@ -624,8 +638,10 @@ static long hsmp_ioctl_get_telemetry(struct file *fp, unsigned long arg)* rejected rather than short-written, so a caller can never * mistake a partial copy for a full one. */ -if (!req.size || req.size > tbl_size) -return -EINVAL ; +if (!req.size || req.size > tbl_size) { + ret = -EINVAL ; +break ; + }/* * The bounce buffer is overwritten in full by memcpy_fromio() @@ -634,12 +650,19 @@ static long hsmp_ioctl_get_telemetry(struct file *fp, unsigned long arg)* on every ioctl call. */ kbuf =kvmalloc (tbl_size,GFP_KERNEL ); -if (!kbuf) -return -ENOMEM ; +if (!kbuf) { + ret = -ENOMEM ; +break ; + } - ret =hsmp_metric_tbl_read_locked (sock, kbuf, tbl_size); + ret =hsmp_metric_tbl_read_locked (sock, kbuf, tbl_size, &smu_response); } +/* Report the raw SMU mailbox status back to userspace */ + req.smu_response = smu_response; +if (copy_to_user (arguser, &req,sizeof (req)) && ret >=0 ) + ret = -EFAULT ; +if (ret <0 )return ret;@@ -667,7 +690,7 @@ long hsmp_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)* write while they bring sockets up and tear them down. */ static ssize_t hsmp_metric_tbl_read_locked (struct hsmp_socket *sock,char *buf, -size_t size) +size_t size,u16 *smu_response) {struct hsmp_message msg = {0 };int ret;@@ -699,6 +722,8 @@ static ssize_t hsmp_metric_tbl_read_locked(struct hsmp_socket *sock, char *buf,guard (mutex)(&sock->metric_read_lock); ret =hsmp_send_message_locked (&msg); +if (smu_response) + *smu_response = msg.smu_response;if (ret)return ret;memcpy_fromio (buf, sock->metric_tbl_addr, size);@@ -710,7 +735,7 @@ ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size) {guard (rwsem_read)(&hsmp_sock_rwsem); -return hsmp_metric_tbl_read_locked (sock, buf, size); +return hsmp_metric_tbl_read_locked (sock, buf, size,NULL ); }EXPORT_SYMBOL_NS_GPL (hsmp_metric_tbl_read,"AMD_HSMP" );