Do not allow set-partition immediate mode
[email protected]
maintainerv3 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.
The v2 patchset [2] added refactoring of the send path so that validation work can all spawn from cxl_validate_cmd_from_user().
Here, v3 offers a finer level of refactoring:
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]/
[2] https://lore.kernel.org/linux-cxl/a4daafc4b537a0b1a673c55300da7747784c4573.1645817416.git.alison.schofield@intel.com/
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 | 311 +++++++++++++++++++++++++--------------- drivers/cxl/cxlmem.h | 7 + drivers/cxl/pmem.c | 1 - 3 files changed, 200 insertions(+), 119 deletions(-)
base-commit: 9b688fc651b9d2b633e8d959454670aba1c39162
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v3 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 | 158 +++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 73 deletions(-)
drivers/cxl/core/mbox.c +85 -73
@@ -207,6 +207,84 @@ 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) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; + +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 ; + + *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,8 +308,7 @@ 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; + int rc;if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX )return -ENOTTY ;
@@ -244,78 +321,13 @@ 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 - }; +/* 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); +else + rc =cxl_to_mem_cmd (cxlds, send_cmd, out_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 ; - -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 ; +return rc; } int cxl_query_cmd(struct cxl_memdev *cxlmd,
Signature · 1 line
2.31.1
Re: [PATCH v3 1/9] cxl/mbox: Move cxl_mem_command construction to helper funcs
Quoted 11 lines · On Wed, 23 Mar 2022 18:11:18 -0700 [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]>
A few trivial comments inline.
Quoted 9 lines
--- drivers/cxl/core/mbox.c | 158 +++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 73 deletions(-)drivers/cxl/core/mbox.c +0
-0
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 215
Quoted 9 lines
drivers/cxl/core/mbox.c +6
-0@@ -207,6 +207,84 @@ 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 ;
trivial: Blank line here would be good.
Quoted 30 lines
drivers/cxl/core/mbox.c +30
-0+/* +* 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) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; +
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 255
Quoted 10 lines
drivers/cxl/core/mbox.c +10
-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 */
Not clear which chunk of code this applies to. Seems like we are just getting some addresses here (obviously that applies to original code as well) Perhaps move down to where you fill in mem_cmd?
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 257
Quoted 2 lines
drivers/cxl/core/mbox.c +2
-0+ c = &cxl_mem_commands[send_cmd->id]; + info = &c->info;
I don't mind that much either way, but you could do these at declaration of the local variables above, before doing the sanity checks.
Quoted 115 lines
drivers/cxl/core/mbox.c +36
-64+ +/* 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,8 +308,7 @@ 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; + int rc;if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX )return -ENOTTY ;@@ -244,78 +321,13 @@ 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 - }; +/* 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); +else + rc =cxl_to_mem_cmd (cxlds, send_cmd, out_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 ; - -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 ; -
drivers/cxl/core/mbox.c static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds, · line 330
Quoted 10 lines
drivers/cxl/core/mbox.c +1
-9-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 ; +return rc;
I haven't read on yet so I'll assume there is more coming in this function as otherwise you could just return directly in the two if / else paths.
Thanks,
Jonathan
Quoted 3 lines
drivers/cxl/core/mbox.c +0
-0} int cxl_query_cmd(struct cxl_memdev *cxlmd,
Alison Schofield
maintainerRe: [PATCH v3 1/9] cxl/mbox: Move cxl_mem_command construction to helper funcs
Quoted 10 lines · Jonathan Cameron
Quoted 7 lines · On Wed, 23 Mar 2022 18:11:18 -0700 [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().
Quoted 9 lines
Quoted 4 lines
This declutters
cxl_validate_cmd_from_user()in preparation for adding new validation steps.Signed-off-by Alison Schofield <[email protected]>A few trivial comments inline.
Reviewed-by Jonathan Cameron <[email protected]>
Thanks for the review Jonathan -
Quoted 1 line
Quoted 1 line
---
snip
Quoted 14 lines
Quoted 14 lines
drivers/cxl/core/mbox.c +13
-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) +{ +const struct cxl_command_info *info; +struct cxl_mem_command *c; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; +
Quoted 8 lines
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 255
Quoted 4 lines
drivers/cxl/core/mbox.c +4
-0+if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Convert user's command into the internal representation */ Not clear which chunk of code this applies to. Seems like we are just getting some addresses here (obviously that applies to original code as well) Perhaps move down to where you fill in
mem_cmd?
The comment above applies to the line that immediately follow. ie. senc_cmd->id indexes into the command array.
Quoted 7 lines
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 257
Quoted 3 lines
drivers/cxl/core/mbox.c +2
-0+ c = &cxl_mem_commands[send_cmd->id]; + info = &c->info;I don't mind that much either way, but you could do these at declaration of the local variables above, before doing the sanity checks.
I err'd on moving things as much 'as is' as possible to keep the patches easier to review. I like your suggestion and will do it. (Please like it when you see it in the next version ;))
Quoted 1 line
Quoted 1 line
+
snip
Quoted 5 lines
drivers/cxl/core/mbox.c static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds, · line 330
Quoted 3 lines
drivers/cxl/core/mbox.c +1
-2- -return 0 ; +return rc;I haven't read on yet so I'll assume there is more coming in this function as otherwise you could just return directly in the two if / else paths.
Yeah, you are seeing some foreshadowing, and you've noted similar in other patches. I'll clean that up.
Thanks,
Alison
Quoted 9 lines
Thanks,
Jonathan
Quoted 3 lines
}
int cxl_query_cmd(struct
cxl_memdev*cxlmd,
[email protected]
maintainerPATCH v3 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
@@ -224,6 +224,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 ,
@@ -424,9 +426,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
Re: [PATCH v3 2/9] cxl/mbox: Move raw command warning to raw command validation
Quoted 8 lines · On Wed, 23 Mar 2022 18:11:19 -0700 [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]>
FWIW as this is obviously fine.
Quoted 27 lines
--- drivers/cxl/core/mbox.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/cxl/core/mbox.c +2
-3@@ -224,6 +224,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 ,@@ -424,9 +426,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;
[email protected]
maintainerPATCH v3 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, + .size_out = out_size, + }; + +if (!in_size) + goto size_out; + + mbox->payload_in =vmemdup_user (u64_to_user_ptr (in_payload), in_size); +if (!mbox->payload_in) +return PTR_ERR (mbox->payload_in); + +size_out: +/* Prepare to handle a full payload for variable sized output */ +if (out_size <0 ) + mbox->size_out = cxlds->payload_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)
@@ -397,27 +429,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"
@@ -471,10 +489,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
Re: [PATCH v3 3/9] cxl/mbox: Move build of user mailbox cmd to a helper function
Quoted 6 lines · On Wed, 23 Mar 2022 18:11:20 -0700 [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]>
Some suggestions for possible improvements to code organization now possible because everything is in one place.
Otherwise looks good to me.
Jonathan
Quoted 10 lines
--- drivers/cxl/core/mbox.c | 60 +++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 23 deletions(-)drivers/cxl/core/mbox.c +0
-0@@ -207,6 +207,38 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true;
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 217
Quoted 10 lines
drivers/cxl/core/mbox.c +8
-0} +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, + .size_out = out_size,
Given we are going to possibly override this later, my inclination would be to not set it here and instead do
if (out_size < 0) mbox->size_out = cxlds->payload_size else mbox->size_out = out_size;
or
mbox->size_out = out_size < 0 ? cxlds->payload_size : out_size;
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 227
Quoted 10 lines
drivers/cxl/core/mbox.c +10
-0+ }; + +if (!in_size) + goto size_out; + + mbox->payload_in =vmemdup_user (u64_to_user_ptr (in_payload), in_size); +if (!mbox->payload_in) +return PTR_ERR (mbox->payload_in); + +size_out:
Why format this like this? Using a goto to skip a couple of lines of code is a bit ugly.
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);
}
etcQuoted 61 lines
drivers/cxl/core/mbox.c +19
-23+/* Prepare to handle a full payload for variable sized output */ +if (out_size <0 ) + mbox->size_out = cxlds->payload_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)@@ -397,27 +429,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"@@ -471,10 +489,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);
[email protected]
maintainerPATCH v3 4/9 cxl/mbox: Construct a users cxl_mbox_cmd in the validation path
In reply to [email protected]From: Alison Schofield <[email protected]>
Since the validation of a mailbox command is done as it is built, move that work out of the dispatch path and into the validation path.
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().
--- drivers/cxl/core/mbox.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/cxl/core/mbox.c +13 -2
@@ -324,6 +324,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.
@@ -340,7 +341,8 @@ 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;
@@ -361,6 +363,14 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,else rc =cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); +if (rc) +return rc; + +/* Sanitize and construct a cxl_mbox_cmd */ + rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, + out_cmd->info.size_in, out_cmd->info.size_out, + send_cmd->in.payload); +return rc; }
@@ -478,6 +488,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" );
@@ -485,7 +496,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
Re: [PATCH v3 4/9] cxl/mbox: Construct a users cxl_mbox_cmd in the validation path
Quoted 3 lines · On Wed, 23 Mar 2022 18:11:21 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
Since the validation of a mailbox command is done as it is built,
Perhaps reword this. I agree it's desirable to have validation and build together but this says 'is done' and it wasn't done until this patch.
Quoted 8 lines
move that work out of the dispatch path and into the validation path.
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().Signed-off-by Alison Schofield <[email protected]>
Sometimes things can get broken into too many baby steps!
This change looks odd until patch 7 given info is passed twice in mbox_cmd and out_cmd. Maybe note in the patch description that out_cmd will be brought local in a few patches time? Probably not worth working out how to reorder and squish the patches to make this easier to review.
Otherwise, one trivial inline.
Quoted 28 lines
--- drivers/cxl/core/mbox.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)drivers/cxl/core/mbox.c +3
-1@@ -324,6 +324,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. @@ -340,7 +341,8 @@ 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;@@ -361,6 +363,14 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,else
drivers/cxl/core/mbox.c static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds, · line 373
Quoted 10 lines
drivers/cxl/core/mbox.c +8
-0rc =cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); +if (rc) +return rc; + +/* Sanitize and construct a cxl_mbox_cmd */ + rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, + out_cmd->info.size_in, out_cmd->info.size_out, + send_cmd->in.payload); +
return cxl_to_mbox_cmd()....
Quoted 20 lines
drivers/cxl/core/mbox.c +2
-1return rc; }@@ -478,6 +488,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" );@@ -485,7 +496,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;
Alison Schofield
maintainerRe: [PATCH v3 4/9] cxl/mbox: Construct a users cxl_mbox_cmd in the validation path
Quoted 20 lines · Jonathan Cameron
Quoted 3 lines · On Wed, 23 Mar 2022 18:11:21 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
Since the validation of a mailbox command is done as it is built,
Perhaps reword this. I agree it's desirable to have validation and build together but this says 'is done' and it wasn't done until this patch.
Quoted 8 lines
move that work out of the dispatch path and into the validation path.
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().Signed-off-by Alison Schofield <[email protected]>
Quoted 7 lines
Sometimes things can get broken into too many baby steps!
This change looks odd until patch 7 given info is passed twice in
mbox_cmdandout_cmd. Maybe note in the patch description thatout_cmdwill be brought local in a few patches time? Probably not worth working out how to reorder and squish the patches to make this easier to review.
I'm reworking w your feedback and will take another pass at this piece, and either change it or document it better.
As you've seen, I did a build up of the new way, then a tear down of the old way. Let me see if there is a graceful way of avoiding the overlap here.
Thanks for the review.
Alison
Quoted 67 lines
Otherwise, one trivial inline.
Reviewed-by Jonathan Cameron <[email protected]>Quoted 28 lines
--- drivers/cxl/core/mbox.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)drivers/cxl/core/mbox.c +3
-1@@ -324,6 +324,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. @@ -340,7 +341,8 @@ 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;@@ -361,6 +363,14 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,else drivers/cxl/core/mbox.c static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds, · line 373
Quoted 10 lines
drivers/cxl/core/mbox.c +8
-0rc =cxl_to_mem_cmd (cxlds, send_cmd, out_cmd); +if (rc) +return rc; + +/* Sanitize and construct a cxl_mbox_cmd */ + rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, + out_cmd->info.size_in, out_cmd->info.size_out, + send_cmd->in.payload); +return
cxl_to_mbox_cmd()....Quoted 20 lines
drivers/cxl/core/mbox.c +2
-1return rc; }@@ -478,6 +488,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" );@@ -485,7 +496,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;
[email protected]
maintainerPATCH v3 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 cxl_command_names[c->info.id].name; + +return NULL ; +} +/** * cxl_mbox_send_cmd() - Send a mailbox command to a device. * @cxlds: The device data for the operation
@@ -450,9 +461,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
Re: [PATCH v3 5/9] cxl/mbox: Remove dependency on cxl_mem_command for a debug msg
Quoted 17 lines · On Wed, 23 Mar 2022 18:11:22 -0700 [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]>--- drivers/cxl/core/mbox.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)drivers/cxl/core/mbox.c +0
-0
drivers/cxl/core/mbox.c static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) · line 136
Quoted 10 lines
drivers/cxl/core/mbox.c +7
-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 cxl_command_names[c->info.id].name;
nitpick, but i'd prefer the error path out of line.
if (!c) return NULL; return cxl_command_names[c->info.id].name;
Otherwise
Quoted 20 lines
drivers/cxl/core/mbox.c +7
-3+ +return NULL ; +} +/** * cxl_mbox_send_cmd() - Send a mailbox command to a device. * @cxlds: The device data for the operation @@ -450,9 +461,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)
Alison Schofield
maintainerRe: [PATCH v3 5/9] cxl/mbox: Remove dependency on cxl_mem_command for a debug msg
Quoted 32 lines · Jonathan Cameron
Quoted 17 lines · On Wed, 23 Mar 2022 18:11:22 -0700 [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]>--- drivers/cxl/core/mbox.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)drivers/cxl/core/mbox.c +0
-0drivers/cxl/core/mbox.c static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) · line 136
Quoted 10 lines
drivers/cxl/core/mbox.c +7
-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 cxl_command_names[c->info.id].name;nitpick, but i'd prefer the error path out of line.
Quoted 4 lines
if (!c) return NULL; return cxl_command_names[c->info.id].name;
Got it, thanks!
Quoted 4 lines
Otherwise
Reviewed-by Jonathan Cameron <[email protected]>
[email protected]
maintainerPATCH v3 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 | 45 +++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 27 deletions(-)
drivers/cxl/core/mbox.c +18 -27
@@ -422,8 +422,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.
@@ -438,34 +437,27 @@ 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;
@@ -474,22 +466,22 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,* 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)) { + mbox_cmd->payload_out, mbox_cmd->size_out)) { rc = -EFAULT ; goto out; } } - *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); +kvfree (mbox_cmd->payload_in); +kvfree (mbox_cmd->payload_out);return rc; }
@@ -511,9 +503,8 @@ 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);if (rc)return rc;
Signature · 1 line
2.31.1
Re: [PATCH v3 6/9] cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param
Quoted 11 lines · On Wed, 23 Mar 2022 18:11:23 -0700 [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.Signed-off-by Alison Schofield <[email protected]>
One suggestion below.
Quoted 78 lines
--- drivers/cxl/core/mbox.c | 45 +++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 27 deletions(-)drivers/cxl/core/mbox.c +12
-20@@ -422,8 +422,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. @@ -438,34 +437,27 @@ 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;@@ -474,22 +466,22 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,* 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)) { + mbox_cmd->payload_out, mbox_cmd->size_out)) { rc = -EFAULT ; goto out; } }
drivers/cxl/core/mbox.c static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, · line 484
Quoted 10 lines
drivers/cxl/core/mbox.c +4
-4- *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); +kvfree (mbox_cmd->payload_in); +kvfree (mbox_cmd->payload_out);
As this function is no longer responsible for allocating these, I'd be inclined to pull the frees out to the caller.
That will make things less fragile to any additional code that might in future occur between
cxl_validate_cmd_from_user() and handle_mailbox_cmd_from_user()
Quoted 15 lines
drivers/cxl/core/mbox.c +2
-3return rc; }@@ -511,9 +503,8 @@ 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);if (rc)return rc;
Alison Schofield
maintainerRe: [PATCH v3 6/9] cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param
Quoted 17 lines · Jonathan Cameron
Quoted 11 lines · On Wed, 23 Mar 2022 18:11:23 -0700 [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.Signed-off-by Alison Schofield <[email protected]>One suggestion below.
snip
Quoted 31 lines
Quoted 17 lines
drivers/cxl/core/mbox.c +3
-3@@ -474,22 +466,22 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,* 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)) { + mbox_cmd->payload_out, mbox_cmd->size_out)) { rc = -EFAULT ; goto out; } }drivers/cxl/core/mbox.c static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, · line 484
Quoted 10 lines
drivers/cxl/core/mbox.c +4
-4- *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); +kvfree (mbox_cmd->payload_in); +kvfree (mbox_cmd->payload_out);As this function is no longer responsible for allocating these, I'd be inclined to pull the frees out to the caller.
Quoted 7 lines
That will make things less fragile to any additional code that might in future occur between
cxl_validate_cmd_from_user()andhandle_mailbox_cmd_from_user()Quoted 2 lines
drivers/cxl/core/mbox.c +0
-0return rc; }
Yeah, not so graceful there. I'll pull them out to the caller, but the caller isn't the place were they were alloc'd. It goes like this:
cxl_send_cmd() {
copy_from_user() cxl_validate_cmd_from_user() - does the allocs now handle_mailbox_from_user() - does the frees now ? Move the frees here ?
copy_to_user()
}
I'll move. See what you think in next version.
Quoted 14 lines
Quoted 13 lines
drivers/cxl/core/mbox.c +2
-3@@ -511,9 +503,8 @@ 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);if (rc)return rc;
Re: [PATCH v3 6/9] cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param
Quoted 59 lines · On Fri, 25 Mar 2022 17:25:35 -0700 Alison Schofield…
Quoted 17 lines · Jonathan Cameron
Quoted 11 lines · On Wed, 23 Mar 2022 18:11:23 -0700 [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.Signed-off-by Alison Schofield <[email protected]>One suggestion below.
snip
Quoted 31 lines
Quoted 17 lines
drivers/cxl/core/mbox.c +3
-3@@ -474,22 +466,22 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,* 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)) { + mbox_cmd->payload_out, mbox_cmd->size_out)) { rc = -EFAULT ; goto out; } }drivers/cxl/core/mbox.c static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, · line 484
Quoted 10 lines
drivers/cxl/core/mbox.c +4
-4- *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); +kvfree (mbox_cmd->payload_in); +kvfree (mbox_cmd->payload_out);As this function is no longer responsible for allocating these, I'd be inclined to pull the frees out to the caller.
Quoted 7 lines
That will make things less fragile to any additional code that might in future occur between
cxl_validate_cmd_from_user()andhandle_mailbox_cmd_from_user()Quoted 2 lines
drivers/cxl/core/mbox.c +0
-0return rc; }
Quoted 8 lines
Yeah, not so graceful there. I'll pull them out to the caller, but the caller isn't the place were they were alloc'd. It goes like this:
cxl_send_cmd(){
copy_from_user()cxl_validate_cmd_from_user()- does the allocs nowhandle_mailbox_from_user()- does the frees now ? Move the frees here ?
Could wrap them in a function to balance with the
validate, though that would need renaming to make the connection obvious.
Quoted 19 lines
copy_to_user() }I'll move. See what you think in next version.
Quoted 14 lines
Quoted 13 lines
drivers/cxl/core/mbox.c +2
-3@@ -511,9 +503,8 @@ 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);if (rc)return rc;
[email protected]
maintainerPATCH v3 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
@@ -331,10 +331,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:
@@ -345,16 +344,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 )
@@ -370,16 +367,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 */ - rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, - out_cmd->info.size_in, out_cmd->info.size_out, + rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, mem_cmd.opcode, + mem_cmd.info.size_in, mem_cmd.info.size_out, send_cmd->in.payload);return rc;
@@ -490,7 +487,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;
@@ -499,7 +495,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
Re: [PATCH v3 7/9] cxl/mbox: Move cxl_mem_command param to a local variable
Quoted 8 lines · On Wed, 23 Mar 2022 18:11:24 -0700 [email protected] wrote:
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 acxl_mem_command, but that was not sufficient when validation of the actual mailbox command became a requirement. Now, it returns a fully validatedcxl_mbox_cmd.
Quoted 4 lines
Remove the extraneous
cxl_mem_commandparameter. Define and use a local version only.Signed-off-by Alison Schofield <[email protected]>
Quoted 77 lines
--- drivers/cxl/core/mbox.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)drivers/cxl/core/mbox.c +8
-12@@ -331,10 +331,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: @@ -345,16 +344,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 )@@ -370,16 +367,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 */ - rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, out_cmd->opcode, - out_cmd->info.size_in, out_cmd->info.size_out, + rc =cxl_to_mbox_cmd (cxlds, mbox_cmd, mem_cmd.opcode, + mem_cmd.info.size_in, mem_cmd.info.size_out, send_cmd->in.payload);return rc;@@ -490,7 +487,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;@@ -499,7 +495,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;
[email protected]
maintainerPATCH v3 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)
@@ -235,6 +269,13 @@ static int cxl_to_mbox_cmd(struct cxl_dev_state *cxlds,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 ; + } + size_out:/* Prepare to handle a full payload for variable sized output */ if (out_size <0 )
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 v3 8/9] cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
Quoted 10 lines · On Wed, 23 Mar 2022 18:11:25 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
User space may send the
SET_PARTITION_INFOmailbox 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.
Quoted 8 lines
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.
Signed-off-by Alison Schofield <[email protected]>
Follow on comment from one in earlier patch on the use of a goto to skip the input payload case... Otherwise,
Quoted 18 lines
--- drivers/cxl/core/mbox.c | 41 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxlmem.h | 7 +++++++ 2 files changed, 48 insertions(+)drivers/cxl/core/mbox.c +5
-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. +*
drivers/cxl/core/mbox.c static bool cxl_mem_raw_command_allowed(u16 opcode) · line 235
Quoted 10 lines
drivers/cxl/core/mbox.c +10
-0+* 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.
Channeling your inner Lawyer :)
Quoted 23 lines
drivers/cxl/core/mbox.c +19
-0+* +* 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)
drivers/cxl/core/mbox.c static int cxl_to_mbox_cmd(struct cxl_dev_state *cxlds, · line 277
Quoted 9 lines
drivers/cxl/core/mbox.c +6
-0@@ -235,6 +269,13 @@ static int cxl_to_mbox_cmd(struct cxl_dev_state *cxlds,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 ; + }
Ah. This is making the block before the label larger. I'm still doubtful that the code is more readable with the goto though rather than just having it indented one more tab.
Quoted 22 lines
drivers/cxl/core/mbox.c +1
-0+ size_out:/* Prepare to handle a full payload for variable sized output */ if (out_size <0 )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
Alison Schofield
maintainerRe: [PATCH v3 8/9] cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
Quoted 27 lines · Jonathan Cameron
Quoted 10 lines · On Wed, 23 Mar 2022 18:11:25 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
User space may send the
SET_PARTITION_INFOmailbox 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.
Quoted 8 lines
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.
Signed-off-by Alison Schofield <[email protected]>Follow on comment from one in earlier patch on the use of a goto to skip the input payload case... Otherwise,
Reviewed-by Jonathan Cameron <[email protected]>
snip
Quoted 7 lines
Quoted 7 lines
static int cxl_to_mbox_cmd(struct
cxl_dev_state*cxlds,drivers/cxl/core/mbox.c +0
-0struct cxl_mbox_cmd *mbox, u16 opcode, size_t in_size, size_t out_size, u64 in_payload)@@ -235,6 +269,13 @@ static int cxl_to_mbox_cmd(struct cxl_dev_state *cxlds,if (!mbox->payload_in)return PTR_ERR (mbox->payload_in);
Quoted 11 lines
drivers/cxl/core/mbox.c static int cxl_to_mbox_cmd(struct cxl_dev_state *cxlds, · line 277
Quoted 6 lines
drivers/cxl/core/mbox.c +6
-0+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 ; + }Ah. This is making the block before the label larger. I'm still doubtful that the code is more readable with the goto though rather than just having it indented one more tab.
You remembered my goto from a few patches back and still don't appreciate it ;)
Will redo.
Thanks for the reviews Jonathan!
[email protected]
maintainerPATCH v3 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
Re: [PATCH v3 9/9] cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
Quoted 20 lines · On Wed, 23 Mar 2022 18:11:26 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
With
SET_PARTITION_INFOon theexclusive_cmdslist for theCXL_PMEMdriver, 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_MEMdriver currently blocks the usage of immediate mode, making the presence ofSET_PARTITION_INFO, in this exclusive commands list, redundant.
Quoted 6 lines
In the future, when the
CXL_MEMdriver 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 theCXL_PMEMdriver.Signed-off-by Alison Schofield <[email protected]>
Quoted 16 lines
--- 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);
Quoted 12 lines · On Wed, 23 Mar 2022 18:11:17 -0700 [email protected] wrote:
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.
The v2 patchset [2] added refactoring of the send path so that validation work can all spawn from
cxl_validate_cmd_from_user().
Quoted 8 lines
Here, v3 offers a finer level of refactoring:
Patches 1-7: Refactor existing code so that all validation work
can spawn fromcxl_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 tocxl_validate_cmd_from_user().
This makes me wonder a bit if cxl_validate_cmd_from_user() is an appropriate name given it now does more than validation?
Quoted 42 lines
Patch 8: Blocks the immediate mode of the set-partition command. Patch 9: Removes
CXL_PMEMexclusive commands restriction.[1] https://lore.kernel.org/linux-cxl/[email protected]/
[2] https://lore.kernel.org/linux-cxl/a4daafc4b537a0b1a673c55300da7747784c4573.1645817416.git.alison.schofield@intel.com/Changes in v3:
- Split up the 'Centralize the validation...' patch into 6 pieces.
Patch: cxl/mbox: Movecxl_mem_commandconstruction to helper funcs
- Safely initialize thecxl_mem_commandstructs. (Dan)
- Remove unneeded memcpy (Dan)
Patch: cxl/mbox: Block immediate mode inSET_PARTITION_INFOcommand
- No Changes
Patch: cxl/pmem: Remove CXLSET_PARTITION_INFOfromexclusive_cmdslist
- No ChangesChanges 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 structcxl_mbox_set_partition_info. (Dan)Alison Schofield (9):
cxl/mbox: Movecxl_mem_commandconstruction 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 userscxl_mbox_cmdin the validation path cxl/mbox: Remove dependency oncxl_mem_commandfor a debug msg cxl/mbox: Makehandle_mailbox_cmd_from_user()use a mbox param cxl/mbox: Movecxl_mem_commandparam to a local variable cxl/mbox: Block immediate mode inSET_PARTITION_INFOcommand cxl/pmem: Remove CXLSET_PARTITION_INFOfromexclusive_cmdslistdrivers/cxl/core/mbox.c | 311 +++++++++++++++++++++++++--------------- drivers/cxl/cxlmem.h | 7 + drivers/cxl/pmem.c | 1 - 3 files changed, 200 insertions(+), 119 deletions(-)base-commit:
9b688fc651b9d2b633e8d959454670aba1c39162
Quoted 21 lines · Jonathan Cameron
Quoted 17 lines · On Wed, 23 Mar 2022 18:11:17 -0700 [email protected] wrote:
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.
The v2 patchset [2] added refactoring of the send path so that validation work can all spawn from
cxl_validate_cmd_from_user().Here, v3 offers a finer level of refactoring:
Patches 1-7: Refactor existing code so that all validation work
can spawn fromcxl_validate_cmd_from_user().
Quoted 7 lines
Quoted 3 lines
The movement intends to cleanly rip the work of building a mailbox command away from
handle_mailbox_command_from_user()and give it tocxl_validate_cmd_from_user().This makes me wonder a bit if
cxl_validate_cmd_from_user()is an appropriate name given it now does more than validation?
Yeah, perhaps as a follow on, I think there's renaming possible to make it clear what the difference is between:
cxl_send_cmdcxl_mbox_send_cmdcxl_pci_mbox_sendcxl_validate_cmd_from_userhandle_mailbox_cmd_from_user
---
cxl_send_cmd is really just a helper for the ioctl path so perhaps:
s/cxl_query_cmd/cxl_ioctl_query/ s/cxl_send_cmd/cxl_ioctl_send/
cxl_mbox_send_cmd() is the path that kernel-internal users take that don't need to do any copy from user work to pass buffers to cxl_pci_mbox_send() (which needs kernel buffers from memcpy_{to,from}io()), so perhaps:
s/cxl_mbox_send_cmd/cxl_internal_cmd_send/
cxl_pci_mbox_send seems ok if the other function names change.
cxl_validate_cmd_from_user and handle_mailbox_cmd_from_user are really doing three distinct operations. Check the payload for correctness, check the command against the submit policy, and construct a kernel command buffer from the user buffer. So how about:
cxl_ioctl_send_checks() (inspired by submit_bio_checks()) cxl_ioctl_send_to_kbuf()
...whatever we pick I think it's ok if this is done after this reorganization completes with the current names.
Quoted 59 lines · On Tue, 29 Mar 2022 18:24:50 -0700 Dan Williams <[email protected]>…
Quoted 21 lines · Jonathan Cameron
Quoted 17 lines · On Wed, 23 Mar 2022 18:11:17 -0700 [email protected] wrote:
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.
The v2 patchset [2] added refactoring of the send path so that validation work can all spawn from
cxl_validate_cmd_from_user().Here, v3 offers a finer level of refactoring:
Patches 1-7: Refactor existing code so that all validation work
can spawn fromcxl_validate_cmd_from_user().Quoted 7 lines
Quoted 3 lines
The movement intends to cleanly rip the work of building a mailbox command away from
handle_mailbox_command_from_user()and give it tocxl_validate_cmd_from_user().This makes me wonder a bit if
cxl_validate_cmd_from_user()is an appropriate name given it now does more than validation?Yeah, perhaps as a follow on, I think there's renaming possible to make it clear what the difference is between:
cxl_send_cmdcxl_mbox_send_cmdcxl_pci_mbox_sendcxl_validate_cmd_from_userhandle_mailbox_cmd_from_user---
cxl_send_cmdis really just a helper for the ioctl path so perhaps:
s/cxl_query_cmd/cxl_ioctl_query/ s/cxl_send_cmd/cxl_ioctl_send/
cxl_mbox_send_cmd()is the path that kernel-internal users take that don't need to do any copy from user work to pass buffers tocxl_pci_mbox_send()(which needs kernel buffers from memcpy_{to,from}io()), so perhaps:
s/cxl_mbox_send_cmd/cxl_internal_cmd_send/
cxl_pci_mbox_sendseems ok if the other function names change.
cxl_validate_cmd_from_userandhandle_mailbox_cmd_from_userare really doing three distinct operations. Check the payload for correctness, check the command against the submit policy, and construct a kernel command buffer from the user buffer. So how about:
Quoted 5 lines
cxl_ioctl_send_checks()(inspired bysubmit_bio_checks())cxl_ioctl_send_to_kbuf()...whatever we pick I think it's ok if this is done after this reorganization completes with the current names.
Agreed. Keep this series clean, but then circle back to cleanup the naming is a good approach.
Jonathan