Do not allow set-partition immediate mode
[email protected]
maintainerv4 cover Do not allow set-partition immediate mode
From: Alison Schofield <[email protected]>
Blocking immediate mode in set-partition info triggered a refactoring of the send path of userspace mailbox commands.
The v1 to address the issue was a single patch [1] that inserted a new immediate mode check in the send path where the payload was available for examining. That was not in a validation function.
Revisions after v1 included the refactoring of the send path so that validation work all spawns from cxl_validate_cmd_from_user().
Patches 1-7: Refactor existing code so that all validation work
can spawn from cxl_validate_cmd_from_user().
The movement intends to cleanly rip the work of building a mailbox command away from handle_mailbox_command_from_user() and give it to cxl_validate_cmd_from_user().
Patch 8: Blocks the immediate mode of the set-partition command. Patch 9: Removes CXL_PMEM exclusive commands restriction.
[1]https://lore.kernel.org/linux-cxl/[email protected]/
Changes in v4: (All from Jonathan's review - Thank-you Jonathan)
- Add a blank line after return
- Combine cxl_mem_command and info declaration and init.
- Replace "rc =" with direct returns in cxl_validate_cmd_from_user()
- Don't init and then overwrite out_size
- Rm the 'refactor foreshadowing' goto & don't restore it later
- Wordsmithing in Patch 4 commit log
- Mv error path to be out of line, normal path inline.
- Move kvfree()'s from handle_mbox...() to cxl_send_cmd() Patches 1,2,4,5,6,7,8: Added Jonathan Reviewed-by Tags. Patches 2,7,8,9: No code changes
Changes in v3:
- Split up the 'Centralize the validation...' patch into 6 pieces.
Patch: cxl/mbox: Move cxl_mem_command construction to helper funcs
- Safely initialize the cxl_mem_command structs. (Dan)
- Remove unneeded memcpy (Dan)
Patch: cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
- No Changes
Patch: cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
- No Changes
Changes in v2:
- Refactor the send path of userspace mbox cmds. (Dan, Ben)
- Patch 3 commit log - update the need to block. (Dan)
- Return -EBUSY (not -EINVAL), when blocking immediate mode. (Ben)
- Remove unneeded cast of void (payload_in). (dan)
- s/u64/__le64 in struct cxl_mbox_set_partition_info. (Dan)
Alison Schofield (9):
cxl/mbox: Move cxl_mem_command construction to helper funcs cxl/mbox: Move raw command warning to raw command validation cxl/mbox: Move build of user mailbox cmd to a helper function cxl/mbox: Construct a users cxl_mbox_cmd in the validation path cxl/mbox: Remove dependency on cxl_mem_command for a debug msg cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param cxl/mbox: Move cxl_mem_command param to a local variable cxl/mbox: Block immediate mode in SET_PARTITION_INFO command cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
drivers/cxl/core/mbox.c | 339 ++++++++++++++++++++++++---------------- drivers/cxl/cxlmem.h | 7 + drivers/cxl/pmem.c | 1 - 3 files changed, 211 insertions(+), 136 deletions(-)
base-commit: 9b688fc651b9d2b633e8d959454670aba1c39162
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 1/9 cxl/mbox: Move cxl_mem_command construction to helper funcs
From: Alison Schofield <[email protected]>
Sanitizing and constructing a cxl_mem_command from a userspace command is part of the validation process prior to submitting the command to a CXL device. Move this work to helper functions:cxl_to_mem_cmd(), cxl_to_mem_cmd_raw().
This declutters cxl_validate_cmd_from_user() in preparation for adding new validation steps.
--- drivers/cxl/core/mbox.c | 155 +++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 75 deletions(-)
drivers/cxl/core/mbox.c +80 -75
@@ -207,6 +207,81 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_to_mem_cmd_raw (struct cxl_dev_state *cxlds, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *mem_cmd) +{ +if (send_cmd->raw.rsvd) +return -EINVAL ; + +/* +* Unlike supported commands, the output size of RAW commands +* gets passed along without further checking, so it must be +* validated here. +*/ +if (send_cmd->out.size > cxlds->payload_size) +return -EINVAL ; + +if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode)) +return -EPERM ; + + *mem_cmd = (struct cxl_mem_command ) { + .info = { + .id =CXL_MEM_COMMAND_ID_RAW , + .size_in = send_cmd->in.size, + .size_out = send_cmd->out.size, + }, + .opcode = send_cmd->raw.opcode + }; + +return 0 ; +} + +static int cxl_to_mem_cmd (struct cxl_dev_state *cxlds, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *mem_cmd) +{ +struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; +const struct cxl_command_info *info = &c->info; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; + +if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Check that the command is enabled for hardware */ +if (!test_bit (info->id, cxlds->enabled_cmds)) +return -ENOTTY ; + +/* Check that the command is not claimed for exclusive kernel use */ +if (test_bit (info->id, cxlds->exclusive_cmds)) +return -EBUSY ; + +/* Check the input buffer is the expected size */ +if (info->size_in >=0 && info->size_in != send_cmd->in.size) +return -ENOMEM ; + +/* Check the output buffer is at least large enough */ +if (info->size_out >=0 && send_cmd->out.size < info->size_out) +return -ENOMEM ; + + *mem_cmd = (struct cxl_mem_command ) { + .info = { + .id = info->id, + .flags = info->flags, + .size_in = send_cmd->in.size, + .size_out = send_cmd->out.size, + }, + .opcode = c->opcode + }; + +return 0 ; +} +/** * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. * @cxlds: The device data for the operation
@@ -230,9 +305,6 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds, const struct cxl_send_command *send_cmd, struct cxl_mem_command *out_cmd) { - const struct cxl_command_info *info; -struct cxl_mem_command *c; - if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) return -ENOTTY;
@@ -244,78 +316,11 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,if (send_cmd->in.size > cxlds->payload_size)return -EINVAL ; -/* -* Checks are bypassed for raw commands but a WARN/taint will occur -* later in the callchain -*/ -if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) { -const struct cxl_mem_command temp = { - .info = { - .id =CXL_MEM_COMMAND_ID_RAW , - .flags =0 , - .size_in = send_cmd->in.size, - .size_out = send_cmd->out.size, - }, - .opcode = send_cmd->raw.opcode - }; - -if (send_cmd->raw.rsvd) -return -EINVAL ; - -/* -* Unlike supported commands, the output size of RAW commands -* gets passed along without further checking, so it must be -* validated here. -*/ -if (send_cmd->out.size > cxlds->payload_size) -return -EINVAL ; - -if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode)) -return -EPERM ; - -memcpy (out_cmd, &temp,sizeof (temp)); - -return 0 ; - } - -if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) -return -EINVAL ; - -if (send_cmd->rsvd) -return -EINVAL ; - -if (send_cmd->in.rsvd || send_cmd->out.rsvd) -return -EINVAL ; - -/* Convert user's command into the internal representation */ - c = &cxl_mem_commands[send_cmd->id]; - info = &c->info; - -/* Check that the command is enabled for hardware */ -if (!test_bit (info->id, cxlds->enabled_cmds)) -return -ENOTTY ; - -/* Check that the command is not claimed for exclusive kernel use */ -if (test_bit (info->id, cxlds->exclusive_cmds)) -return -EBUSY ; - -/* Check the input buffer is the expected size */ -if (info->size_in >=0 && info->size_in != send_cmd->in.size) -return -ENOMEM ; - -/* Check the output buffer is at least large enough */ -if (info->size_out >=0 && send_cmd->out.size < info->size_out) -return -ENOMEM ; - -memcpy (out_cmd, c,sizeof (*c)); - out_cmd->info.size_in = send_cmd->in.size; -/* -* XXX: out_cmd->info.size_out will be controlled by the driver, and the -* specified number of bytes @send_cmd->out.size will be copied back out -* to userspace. -*/ - -return 0 ; +/* Sanitize and construct a cxl_mem_command */ +if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) +return cxl_to_mem_cmd_raw (cxlds, send_cmd, out_cmd); +else +return cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); } int cxl_query_cmd(struct cxl_memdev *cxlmd,
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 3/9 cxl/mbox: Move build of user mailbox cmd to a helper function
In reply to [email protected]From: Alison Schofield <[email protected]>
In preparation for moving the construction of a mailbox command to the validation path, extract the work into a helper function.
--- drivers/cxl/core/mbox.c | 60 +++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 23 deletions(-)
drivers/cxl/core/mbox.c +37 -23
@@ -207,6 +207,38 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_to_mbox_cmd (struct cxl_dev_state *cxlds, +struct cxl_mbox_cmd *mbox,u16 opcode, +size_t in_size,size_t out_size,u64 in_payload) +{ + *mbox = (struct cxl_mbox_cmd ) { + .opcode = opcode, + .size_in = in_size, + }; + +if (in_size) { + mbox->payload_in =vmemdup_user (u64_to_user_ptr (in_payload), + in_size); +if (!mbox->payload_in) +return PTR_ERR (mbox->payload_in); + } + +/* Prepare to handle a full payload for variable sized output */ +if (out_size <0 ) + mbox->size_out = cxlds->payload_size; +else + mbox->size_out = out_size; + +if (mbox->size_out) { + mbox->payload_out =kvzalloc (mbox->size_out,GFP_KERNEL ); +if (!mbox->payload_out) { +kvfree (mbox->payload_in); +return -ENOMEM ; + } + } +return 0 ; +} +static int cxl_to_mem_cmd_raw (struct cxl_dev_state *cxlds,const struct cxl_send_command *send_cmd,struct cxl_mem_command *mem_cmd)
@@ -390,27 +422,13 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, s32 *size_out, u32 *retval) {struct device *dev = cxlds->dev; -struct cxl_mbox_cmd mbox_cmd = { - .opcode = cmd->opcode, - .size_in = cmd->info.size_in, - .size_out = cmd->info.size_out, - }; +struct cxl_mbox_cmd mbox_cmd;int rc; -if (cmd->info.size_out) { - mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); -if (!mbox_cmd.payload_out) -return -ENOMEM ; - } - -if (cmd->info.size_in) { - mbox_cmd.payload_in =vmemdup_user (u64_to_user_ptr (in_payload), - cmd->info.size_in); -if (IS_ERR (mbox_cmd.payload_in)) { -kvfree (mbox_cmd.payload_out); -return PTR_ERR (mbox_cmd.payload_in); - } - } + rc =cxl_to_mbox_cmd (cxlds, &mbox_cmd, cmd->opcode, cmd->info.size_in, + cmd->info.size_out, in_payload); +if (rc) +return rc;dev_dbg (dev, "Submitting %s command for user\n"
@@ -464,10 +482,6 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (rc)return rc; -/* Prepare to handle a full payload for variable sized output */ -if (c.info.size_out <0 ) - c.info.size_out = cxlds->payload_size; - rc =handle_mailbox_cmd_from_user (cxlds, &c, send.in.payload, send.out.payload, &send.out.size, &send.retval);
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 4/9 cxl/mbox: Construct a users cxl_mbox_cmd in the validation path
In reply to [email protected]From: Alison Schofield <[email protected]>
This is a step in refactoring the handling of user space mailbox commands. The intent is to have all the validation work originate in cxl_validate_cmd_from_user().
Move the construction and validation of a mailbox command to the validation path. Continue to pass both the out_cmd and the mbox_cmd until handle_mbox_cmd_from_user() learns how to use a mbox_cmd param.
--- drivers/cxl/core/mbox.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/cxl/core/mbox.c +17 -4
@@ -321,6 +321,7 @@ static int cxl_to_mem_cmd(struct cxl_dev_state *cxlds,* @cxlds: The device data for the operation * @send_cmd: &struct cxl_send_command copied in from userspace. * @out_cmd: Sanitized and populated &struct cxl_mem_command. +* @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. * * Return: * * %0 - @out_cmd is ready to send.
@@ -337,8 +338,11 @@ static int cxl_to_mem_cmd(struct cxl_dev_state *cxlds,*/ static int cxl_validate_cmd_from_user (struct cxl_dev_state *cxlds,const struct cxl_send_command *send_cmd, -struct cxl_mem_command *out_cmd) +struct cxl_mem_command *out_cmd, +struct cxl_mbox_cmd *mbox_cmd) { +int rc; +if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX )return -ENOTTY ;
@@ -352,9 +356,17 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,/* Sanitize and construct a cxl_mem_command */ if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) -return cxl_to_mem_cmd_raw (cxlds, send_cmd, out_cmd); + rc =cxl_to_mem_cmd_raw (cxlds, send_cmd, out_cmd);else -return cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); + rc =cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); + +if (rc) +return rc; + +/* Sanitize and construct a cxl_mbox_cmd */ +return cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, + out_cmd->info.size_in, out_cmd->info.size_out, + send_cmd->in.payload); } int cxl_query_cmd(struct cxl_memdev *cxlmd,
@@ -471,6 +483,7 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)struct device *dev = &cxlmd->dev;struct cxl_send_command send;struct cxl_mem_command c; +struct cxl_mbox_cmd mbox_cmd;int rc;dev_dbg (dev,"Send IOCTL\n" );
@@ -478,7 +491,7 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (copy_from_user (&send, s,sizeof (send)))return -EFAULT ; - rc =cxl_validate_cmd_from_user (cxlmd->cxlds, &send, &c); + rc =cxl_validate_cmd_from_user (cxlmd->cxlds, &send, &c, &mbox_cmd);if (rc)return rc;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 6/9 cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param
In reply to [email protected]From: Alison Schofield <[email protected]>
Previously, handle_mailbox_cmd_from_user(), constructed the mailbox command and dispatched it to the hardware. The construction work has moved to the validation path.
handle_mailbox_cmd_from_user() now expects a fully validated mbox param. Make it's caller, cxl_send_cmd(), deliver it. Update the comments and dereferencing of the new mbox parameter.
--- drivers/cxl/core/mbox.c | 54 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 32 deletions(-)
drivers/cxl/core/mbox.c +22 -32
@@ -417,8 +417,7 @@ int cxl_query_cmd(struct cxl_memdev *cxlmd,/** * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. * @cxlds: The device data for the operation -* @cmd: The validated command. -* @in_payload: Pointer to userspace's input payload. +* @mbox_cmd: The validated mailbox command. * @out_payload: Pointer to userspace's output payload. * @size_out: (Input) Max payload size to copy out. * (Output) Payload size hardware generated.
@@ -433,59 +432,48 @@ int cxl_query_cmd(struct cxl_memdev *cxlmd,* * %-EINTR - Mailbox acquisition interrupted. * * %-EXXX - Transaction level failures. * -* Creates the appropriate mailbox command and dispatches it on behalf of a -* userspace request. The input and output payloads are copied between -* userspace. +* Dispatches a mailbox command on behalf of a userspace request. +* The output payload is copied to userspace. * * See cxl_send_cmd(). */ static int handle_mailbox_cmd_from_user (struct cxl_dev_state *cxlds, -const struct cxl_mem_command *cmd, -u64 in_payload,u64 out_payload, -s32 *size_out,u32 *retval) +struct cxl_mbox_cmd *mbox_cmd, +u64 out_payload,s32 *size_out, +u32 *retval) {struct device *dev = cxlds->dev; -struct cxl_mbox_cmd mbox_cmd;int rc; - rc =cxl_to_mbox_cmd (cxlds, &mbox_cmd, cmd->opcode, cmd->info.size_in, - cmd->info.size_out, in_payload); -if (rc) -return rc; -dev_dbg (dev,"Submitting %s command for user\n" "\topcode: %x\n" "\tsize: %zx\n" , -cxl_mem_opcode_to_name (mbox_cmd.opcode), - mbox_cmd.opcode, mbox_cmd.size_in); +cxl_mem_opcode_to_name (mbox_cmd->opcode), + mbox_cmd->opcode, mbox_cmd->size_in); - rc = cxlds->mbox_send (cxlds, &mbox_cmd); + rc = cxlds->mbox_send (cxlds, mbox_cmd);if (rc) - goto out; +return rc;/* * @size_out contains the max size that's allowed to be written back out * to userspace. While the payload may have written more output than * this it will have to be ignored. */ -if (mbox_cmd.size_out) { -dev_WARN_ONCE (dev, mbox_cmd.size_out > *size_out, +if (mbox_cmd->size_out) { +dev_WARN_ONCE (dev, mbox_cmd->size_out > *size_out,"Invalid return size\n" );if (copy_to_user (u64_to_user_ptr (out_payload), - mbox_cmd.payload_out, mbox_cmd.size_out)) { - rc = -EFAULT ; - goto out; + mbox_cmd->payload_out, mbox_cmd->size_out)) { +return -EFAULT ; } } - *size_out = mbox_cmd.size_out; - *retval = mbox_cmd.return_code; + *size_out = mbox_cmd->size_out; + *retval = mbox_cmd->return_code; -out: -kvfree (mbox_cmd.payload_in); -kvfree (mbox_cmd.payload_out); -return rc; +return 0 ; }int cxl_send_cmd (struct cxl_memdev *cxlmd,struct cxl_send_command __user *s)
@@ -506,9 +494,11 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (rc)return rc; - rc =handle_mailbox_cmd_from_user (cxlds, &c, send.in.payload, - send.out.payload, &send.out.size, - &send.retval); + rc =handle_mailbox_cmd_from_user (cxlds, &mbox_cmd, send.out.payload, + &send.out.size, &send.retval); + +kvfree (mbox_cmd.payload_in); +kvfree (mbox_cmd.payload_out);if (rc)return rc;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 2/9 cxl/mbox: Move raw command warning to raw command validation
In reply to [email protected]From: Alison Schofield <[email protected]>
This move serves two purposes: 1) Emit the warning in the raw command validation path, and 2) Remove the dependency on the struct cxl_mem_command in handle_mailbox_cmd_from_user() in preparation for a refactor of that function.
--- drivers/cxl/core/mbox.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cxl/core/mbox.c +2 -3
@@ -225,6 +225,8 @@ static int cxl_to_mem_cmd_raw(struct cxl_dev_state *cxlds,if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode))return -EPERM ; +dev_WARN_ONCE (cxlds->dev, true,"raw command path used\n" ); + *mem_cmd = (struct cxl_mem_command ) { .info = { .id =CXL_MEM_COMMAND_ID_RAW ,
@@ -417,9 +419,6 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, cmd->info.size_in); -dev_WARN_ONCE (dev, cmd->info.id ==CXL_MEM_COMMAND_ID_RAW , -"raw command path used\n" ); - rc = cxlds->mbox_send (cxlds, &mbox_cmd);if (rc) goto out;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 5/9 cxl/mbox: Remove dependency on cxl_mem_command for a debug msg
In reply to [email protected]From: Alison Schofield <[email protected]>
In preparation for removing access to struct cxl_mem_command, change this debug message to use cxl_mbox_cmd fields instead. Retrieve the pretty command name from cxl_mbox_cmd using a new opcode to command name helper.
--- drivers/cxl/core/mbox.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/cxl/core/mbox.c +14 -3
@@ -127,6 +127,17 @@ static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)return NULL ; } +static const char *cxl_mem_opcode_to_name (u16 opcode) +{ +struct cxl_mem_command *c; + + c =cxl_mem_find_command (opcode); +if (!c) +return NULL ; + +return cxl_command_names[c->info.id].name; +} +/** * cxl_mbox_send_cmd() - Send a mailbox command to a device. * @cxlds: The device data for the operation
@@ -445,9 +456,9 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,dev_dbg (dev,"Submitting %s command for user\n" "\topcode: %x\n" -"\tsize: %ub\n" , - cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, - cmd->info.size_in); +"\tsize: %zx\n" , +cxl_mem_opcode_to_name (mbox_cmd.opcode), + mbox_cmd.opcode, mbox_cmd.size_in); rc = cxlds->mbox_send (cxlds, &mbox_cmd);if (rc)
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 7/9 cxl/mbox: Move cxl_mem_command param to a local variable
In reply to [email protected]From: Alison Schofield <[email protected]>
cxl_validate_command_from_user() is now the single point of validation for mailbox commands coming from user space. Previously, it returned a a cxl_mem_command, but that was not sufficient when validation of the actual mailbox command became a requirement. Now, it returns a fully validated cxl_mbox_cmd.
Remove the extraneous cxl_mem_command parameter. Define and use a local version only.
--- drivers/cxl/core/mbox.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
drivers/cxl/core/mbox.c +8 -12
@@ -328,10 +328,9 @@ static int cxl_to_mem_cmd(struct cxl_dev_state *cxlds, }/** -* cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. +* cxl_validate_cmd_from_user() - Construct a valid cxl_mbox_cmd * @cxlds: The device data for the operation * @send_cmd: &struct cxl_send_command copied in from userspace. -* @out_cmd: Sanitized and populated &struct cxl_mem_command. * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. * * Return:
@@ -342,16 +341,14 @@ static int cxl_to_mem_cmd(struct cxl_dev_state *cxlds,* * %-EPERM - Attempted to use a protected command. * * %-EBUSY - Kernel has claimed exclusive access to this opcode * -* The result of this command is a fully validated command in @out_cmd that is +* The result of this command is a fully validated command in @mbox_cmd that is * safe to send to the hardware. -* -* See handle_mailbox_cmd_from_user() */ static int cxl_validate_cmd_from_user (struct cxl_dev_state *cxlds,const struct cxl_send_command *send_cmd, -struct cxl_mem_command *out_cmd,struct cxl_mbox_cmd *mbox_cmd) { +struct cxl_mem_command mem_cmd;int rc;if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX )
@@ -367,16 +364,16 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,/* Sanitize and construct a cxl_mem_command */ if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) - rc =cxl_to_mem_cmd_raw (cxlds, send_cmd, out_cmd); + rc =cxl_to_mem_cmd_raw (cxlds, send_cmd, &mem_cmd);else - rc =cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); + rc =cxl_to_mem_cmd (cxlds, send_cmd, &mem_cmd);if (rc)return rc;/* Sanitize and construct a cxl_mbox_cmd */ -return cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, - out_cmd->info.size_in, out_cmd->info.size_out, +return cxl_to_mbox_cmd (cxlds, mbox_cmd, mem_cmd.opcode, + mem_cmd.info.size_in, mem_cmd.info.size_out, send_cmd->in.payload); }
@@ -481,7 +478,6 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)struct cxl_dev_state *cxlds = cxlmd->cxlds;struct device *dev = &cxlmd->dev;struct cxl_send_command send; -struct cxl_mem_command c;struct cxl_mbox_cmd mbox_cmd;int rc;
@@ -490,7 +486,7 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (copy_from_user (&send, s,sizeof (send)))return -EFAULT ; - rc =cxl_validate_cmd_from_user (cxlmd->cxlds, &send, &c, &mbox_cmd); + rc =cxl_validate_cmd_from_user (cxlmd->cxlds, &send, &mbox_cmd);if (rc)return rc;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 9/9 cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
In reply to [email protected]From: Alison Schofield <[email protected]>
With SET_PARTITION_INFO on the exclusive_cmds list for the CXL_PMEM driver, userspace cannot execute a set-partition command without first unbinding the pmem driver from the device.
When userspace requests a partition change to take effect on the next reboot this unbind requirement is unnecessarily restrictive. The driver does not need to enforce an unbind because partitions will not change until the next reboot. Of course, userspace still needs to be aware that changing the size of persistent capacity on the next reboot will result in the loss of data stored. That can happen regardless of whether it is presently bound at the time of issuing the set-partition command.
When userspace requests a partition change to take effect immediately, restrictions are needed. The CXL_MEM driver currently blocks the usage of immediate mode, making the presence of SET_PARTITION_INFO, in this exclusive commands list, redundant.
In the future, when the CXL_MEM driver adds support for immediate changes to device partitions it will ensure that the partition change will not affect any active decode. That means the work will not fall right back here, onto the CXL_PMEM driver.
--- drivers/cxl/pmem.c | 1 - 1 file changed, 1 deletion(-)
drivers/cxl/pmem.c +0 -1
@@ -344,7 +344,6 @@ static __init int cxl_pmem_init(void) {int rc; -set_bit (CXL_MEM_COMMAND_ID_SET_PARTITION_INFO , exclusive_cmds);set_bit (CXL_MEM_COMMAND_ID_SET_SHUTDOWN_STATE , exclusive_cmds);set_bit (CXL_MEM_COMMAND_ID_SET_LSA , exclusive_cmds);
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v4 8/9 cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
In reply to [email protected]From: Alison Schofield <[email protected]>
User space may send the SET_PARTITION_INFO mailbox command using the IOCTL interface. Inspect the input payload and fail if the immediate flag is set.
This is the first instance of the driver inspecting an input payload from user space. Assume there will be more such cases and implement with an extensible helper.
In order for the kernel to react to an immediate partition change it needs to assert that the change will not affect any active decode. At a minimum this requires validating that the device is using HDM decoders instead of the CXL DVSEC for decode, and that none of the active HDM decoders are affected by the partition change. For now, just fail until that support arrives.
--- drivers/cxl/core/mbox.c | 41 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxlmem.h | 7 +++++++ 2 files changed, 48 insertions(+)
drivers/cxl/core/mbox.c +41 -0
@@ -218,6 +218,40 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +/** +* cxl_payload_from_user_allowed() - Check contents of in_payload. +* @opcode: The mailbox command opcode. +* @payload_in: Pointer to the input payload passed in from user space. +* +* Return: +* * true - payload_in passes check for @opcode. +* * false - payload_in contains invalid or unsupported values. +* +* The driver may inspect payload contents before sending a mailbox +* command from user space to the device. The intent is to reject +* commands with input payloads that are known to be unsafe. This +* check is not intended to replace the users careful selection of +* mailbox command parameters and makes no guarantee that the user +* command will succeed, nor that it is appropriate. +* +* The specific checks are determined by the opcode. +*/ +static bool cxl_payload_from_user_allowed (u16 opcode,void *payload_in) +{ +switch (opcode) { +case CXL_MBOX_OP_SET_PARTITION_INFO : { +struct cxl_mbox_set_partition_info *pi = payload_in; + +if (pi->flags &&CXL_SET_PARTITION_IMMEDIATE_FLAG ) +return false; +break ; + } +default : +break ; + } +return true; +} +static int cxl_to_mbox_cmd (struct cxl_dev_state *cxlds,struct cxl_mbox_cmd *mbox,u16 opcode,size_t in_size,size_t out_size,u64 in_payload)
@@ -232,6 +266,13 @@ static int cxl_to_mbox_cmd(struct cxl_dev_state *cxlds, in_size);if (!mbox->payload_in)return PTR_ERR (mbox->payload_in); + +if (!cxl_payload_from_user_allowed (opcode, mbox->payload_in)) { +dev_dbg (cxlds->dev,"%s: input payload not allowed\n" , +cxl_mem_opcode_to_name (opcode)); +kvfree (mbox->payload_in); +return -EBUSY ; + } }/* Prepare to handle a full payload for variable sized output */
drivers/cxl/cxlmem.h +7 -0
@@ -264,6 +264,13 @@ struct cxl_mbox_set_lsa {u8 data[]; } __packed; +struct cxl_mbox_set_partition_info { +__le64 volatile_capacity; +u8 flags; +} __packed; + +#define CXL_SET_PARTITION_IMMEDIATE_FLAG BIT(0) +/** * struct cxl_mem_command - Driver representation of a memory device command * @info: Command information as it exists for the UAPI
Signature · 1 line
2.31.1
Re: [PATCH v4 1/9] cxl/mbox: Move cxl_mem_command construction to helper funcs
In reply to [email protected]Quoted 22 lines · On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
Sanitizing and constructing a
cxl_mem_commandfrom a userspace command is part of the validation process prior to submitting the command to a CXL device. Move this work to helper functions:cxl_to_mem_cmd(),cxl_to_mem_cmd_raw().This declutters
cxl_validate_cmd_from_user()in preparation for adding new validation steps.Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 155 +++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 75 deletions(-)drivers/cxl/core/mbox.c +0
-0
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 212
Quoted 6 lines
drivers/cxl/core/mbox.c +3
-0@@ -207,6 +207,81 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_to_mem_cmd_raw (struct cxl_dev_state *cxlds, +const struct cxl_send_command *send_cmd, +struct cxl_mem_command *mem_cmd)
For multi-parameter "to()" style helpers like this with a destination, I tend to prefer memcpy() style argument ordering where the destination to be clobbered is the first parameter and the other arguments tag on afterwards.
Other than that this looks good to me and you can add:
Re: [PATCH v4 2/9] cxl/mbox: Move raw command warning to raw command validation
In reply to [email protected]Quoted 11 lines · On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
This move serves two purposes: 1) Emit the warning in the raw command validation path, and 2) Remove the dependency on the struct
cxl_mem_commandinhandle_mailbox_cmd_from_user()in preparation for a refactor of that function.Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>---
Looks good to me:
Re: [PATCH v4 3/9] cxl/mbox: Move build of user mailbox cmd to a helper function
In reply to [email protected]Quoted 16 lines · On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
In preparation for moving the construction of a mailbox command to the validation path, extract the work into a helper function.
Signed-off-by Alison Schofield <[email protected]>--- drivers/cxl/core/mbox.c | 60 +++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 23 deletions(-)drivers/cxl/core/mbox.c +0
-0
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 212
Quoted 6 lines
drivers/cxl/core/mbox.c +3
-0@@ -207,6 +207,38 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_to_mbox_cmd (struct cxl_dev_state *cxlds, +struct cxl_mbox_cmd *mbox,u16 opcode, +size_t in_size,size_t out_size,u64 in_payload)
Here the output object ends up in the middle of the arg list, so it should at least be consistent with the other to() functions and as before I'd prefer it to be the first argument.
Other than that looks good to me:
Re: [PATCH v4 4/9] cxl/mbox: Construct a users cxl_mbox_cmd in the validation path
In reply to [email protected]Quoted 11 lines · On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
This is a step in refactoring the handling of user space mailbox commands. The intent is to have all the validation work originate in
cxl_validate_cmd_from_user().Move the construction and validation of a mailbox command to the validation path. Continue to pass both the
out_cmdand thembox_cmduntilhandle_mbox_cmd_from_user()learns how to use ambox_cmdparam.
Quoted 5 lines
Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
Looks good modulo destination parameter ordering.
Re: [PATCH v4 5/9] cxl/mbox: Remove dependency on cxl_mem_command for a debug msg
In reply to [email protected]Quoted 37 lines · On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
In preparation for removing access to struct
cxl_mem_command, change this debug message to usecxl_mbox_cmdfields instead. Retrieve the pretty command name fromcxl_mbox_cmdusing a new opcode to command name helper.Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)drivers/cxl/core/mbox.c +11
-0@@ -127,6 +127,17 @@ static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)return NULL ; } +static const char *cxl_mem_opcode_to_name (u16 opcode) +{ +struct cxl_mem_command *c; + + c =cxl_mem_find_command (opcode); +if (!c) +return NULL ; + +return cxl_command_names[c->info.id].name; +} +/** * cxl_mbox_send_cmd() - Send a mailbox command to a device. * @cxlds: The device data for the operation
drivers/cxl/core/mbox.c static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, · line 461
Quoted 9 lines
drivers/cxl/core/mbox.c +3
-3@@ -445,9 +456,9 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,dev_dbg (dev,"Submitting %s command for user\n" "\topcode: %x\n" -"\tsize: %ub\n" , - cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, - cmd->info.size_in); +"\tsize: %zx\n" , +cxl_mem_opcode_to_name (mbox_cmd.opcode), + mbox_cmd.opcode, mbox_cmd.size_in);
Looks ok, although it's just a debug statement so the raw opcode would also be acceptable to me to meet the goal of dropping the cxl_mem_command usage.
Either way you decide to go:
Re: [PATCH v4 6/9] cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param
In reply to [email protected]Quoted 10 lines · On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
Previously,
handle_mailbox_cmd_from_user(), constructed the mailbox command and dispatched it to the hardware. The construction work has moved to the validation path.
handle_mailbox_cmd_from_user()now expects a fully validated mbox param. Make it's caller,cxl_send_cmd(), deliver it. Update the comments and dereferencing of the new mbox parameter.
It's not clear to me that handle_mailbox_cmd_from_user() has reason to exist as a helper vs being open coded in cxl_send_cmd() after this, but that can be a follow-on cleanup.
Quoted 99 lines
Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/cxl/core/mbox.c | 54 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 32 deletions(-)drivers/cxl/core/mbox.c +17
-29@@ -417,8 +417,7 @@ int cxl_query_cmd(struct cxl_memdev *cxlmd,/** * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. * @cxlds: The device data for the operation -* @cmd: The validated command. -* @in_payload: Pointer to userspace's input payload. +* @mbox_cmd: The validated mailbox command. * @out_payload: Pointer to userspace's output payload. * @size_out: (Input) Max payload size to copy out. * (Output) Payload size hardware generated. @@ -433,59 +432,48 @@ int cxl_query_cmd(struct cxl_memdev *cxlmd,* * %-EINTR - Mailbox acquisition interrupted. * * %-EXXX - Transaction level failures. * -* Creates the appropriate mailbox command and dispatches it on behalf of a -* userspace request. The input and output payloads are copied between -* userspace. +* Dispatches a mailbox command on behalf of a userspace request. +* The output payload is copied to userspace. * * See cxl_send_cmd(). */ static int handle_mailbox_cmd_from_user (struct cxl_dev_state *cxlds, -const struct cxl_mem_command *cmd, -u64 in_payload,u64 out_payload, -s32 *size_out,u32 *retval) +struct cxl_mbox_cmd *mbox_cmd, +u64 out_payload,s32 *size_out, +u32 *retval) {struct device *dev = cxlds->dev; -struct cxl_mbox_cmd mbox_cmd;int rc; - rc =cxl_to_mbox_cmd (cxlds, &mbox_cmd, cmd->opcode, cmd->info.size_in, - cmd->info.size_out, in_payload); -if (rc) -return rc; -dev_dbg (dev,"Submitting %s command for user\n" "\topcode: %x\n" "\tsize: %zx\n" , -cxl_mem_opcode_to_name (mbox_cmd.opcode), - mbox_cmd.opcode, mbox_cmd.size_in); +cxl_mem_opcode_to_name (mbox_cmd->opcode), + mbox_cmd->opcode, mbox_cmd->size_in); - rc = cxlds->mbox_send (cxlds, &mbox_cmd); + rc = cxlds->mbox_send (cxlds, mbox_cmd);if (rc) - goto out; +return rc;/* * @size_out contains the max size that's allowed to be written back out * to userspace. While the payload may have written more output than * this it will have to be ignored. */ -if (mbox_cmd.size_out) { -dev_WARN_ONCE (dev, mbox_cmd.size_out > *size_out, +if (mbox_cmd->size_out) { +dev_WARN_ONCE (dev, mbox_cmd->size_out > *size_out,"Invalid return size\n" );if (copy_to_user (u64_to_user_ptr (out_payload), - mbox_cmd.payload_out, mbox_cmd.size_out)) { - rc = -EFAULT ; - goto out; + mbox_cmd->payload_out, mbox_cmd->size_out)) { +return -EFAULT ; } } - *size_out = mbox_cmd.size_out; - *retval = mbox_cmd.return_code; + *size_out = mbox_cmd->size_out; + *retval = mbox_cmd->return_code; -out: -kvfree (mbox_cmd.payload_in); -kvfree (mbox_cmd.payload_out); -return rc; +return 0 ; }int cxl_send_cmd (struct cxl_memdev *cxlmd,struct cxl_send_command __user *s)@@ -506,9 +494,11 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (rc)
drivers/cxl/core/mbox.c int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s) · line 501
Quoted 10 lines
drivers/cxl/core/mbox.c +5
-3return rc; - rc =handle_mailbox_cmd_from_user (cxlds, &c, send.in.payload, - send.out.payload, &send.out.size, - &send.retval); + rc =handle_mailbox_cmd_from_user (cxlds, &mbox_cmd, send.out.payload, + &send.out.size, &send.retval); + +kvfree (mbox_cmd.payload_in); +kvfree (mbox_cmd.payload_out);
It's not obvious that these are paired with the allocations in cxl_to_mbox_cmd().
How about rename cxl_to_mbox_cmd() to cxl_mbox_cmd_ctor(), and wrap these kvfree() calls in a helper called cxl_mbox_cmd_dtor()?
Otherwise, these are only minor comments, so you can add:
Alison Schofield
maintainerRe: [PATCH v4 5/9] cxl/mbox: Remove dependency on cxl_mem_command for a debug msg
In reply to Dan WilliamsQuoted 1 line · Dan Williams
On Tue, Mar 29, 2022 at 6:28 PM <[email protected]> wrote:
snip
Quoted 11 lines
drivers/cxl/core/mbox.c static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, · line 461
Quoted 10 lines
dev_dbg(dev,
"Submitting %s command for user\n" "\topcode: %x\n"drivers/cxl/core/mbox.c +3
-3-"\tsize: %ub\n" , - cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, - cmd->info.size_in); +"\tsize: %zx\n" , +cxl_mem_opcode_to_name (mbox_cmd.opcode), + mbox_cmd.opcode, mbox_cmd.size_in);
Quoted 3 lines
Looks ok, although it's just a debug statement so the raw opcode would also be acceptable to me to meet the goal of dropping the
cxl_mem_commandusage.
I thought the opcode_to_name helper might be overkill, until it came in handy in a later patch. It gets used again to find the command name when a payload is not allowed.
Quoted 4 lines
Either way you decide to go:
Reviewed-by Dan Williams <[email protected]>