cxl/mbox: Handle variable size output while still signed
[email protected]
maintainerPATCH 1/1 cxl/mbox: Handle variable size output while still signed
From: Alison Schofield <[email protected]>
A mailbox command output size value of (-1) means that a device may return a variable sized output for that command. The device defines a maximum payload size, so the driver will allocate enough memory to receive that maximum payload for these commands.
A recent code refactoring moved the check for variable sized output to occur after the signedness is discarded from the output size. Move the check to occur while still operating on the signed output size variable.
Smatch warn: unsigned 'out_size' is never less than 0
ions")
--- drivers/cxl/core/mbox.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/cxl/core/mbox.c +7 -8
@@ -258,6 +258,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, *mbox = (struct cxl_mbox_cmd ) { .opcode = opcode, .size_in = in_size, + .size_out = out_size, };if (in_size) {
@@ -274,12 +275,6 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, } } -/* 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) {
@@ -320,7 +315,6 @@ static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd, .info = { .id = CXL_MEM_COMMAND_ID_RAW, .size_in = send_cmd->in.size, - .size_out = send_cmd->out.size, }, .opcode = send_cmd->raw.opcode };
@@ -365,7 +359,6 @@ static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd, .id = info->id, .flags = info->flags, .size_in = send_cmd->in.size, - .size_out = send_cmd->out.size, }, .opcode = c->opcode };
@@ -417,6 +410,12 @@ static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd,if (rc)return rc; +/* Prepare to handle a full payload for variable sized output */ +if (send_cmd->out.size <0 ) + mem_cmd.info.size_out = cxlds->payload_size; +else + mem_cmd.info.size_out = send_cmd->out.size; +/* Sanitize and construct a cxl_mbox_cmd */ return cxl_mbox_cmd_ctor(mbox_cmd, cxlds, mem_cmd.opcode, mem_cmd.info.size_in, mem_cmd.info.size_out,
base-commit: 7dc1d11d7abae52aada5340fb98885f0ddbb7c37
Signature · 1 line
2.31.1
Quoted 37 lines · On Fri, Apr 8, 2022 at 8:11 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
A mailbox command output size value of (-1) means that a device may return a variable sized output for that command. The device defines a maximum payload size, so the driver will allocate enough memory to receive that maximum payload for these commands.
A recent code refactoring moved the check for variable sized output to occur after the signedness is discarded from the output size. Move the check to occur while still operating on the signed output size variable.
Smatch warn: unsigned '
out_size' is never less than 0Fixes be0d0ce77aa3 ("cxl/mbox: Move build of user mailbox cmd to a helper functions")
Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Alison Schofield <[email protected]>--- drivers/cxl/core/mbox.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)drivers/cxl/core/mbox.c +1
-0@@ -258,6 +258,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, *mbox = (struct cxl_mbox_cmd ) { .opcode = opcode, .size_in = in_size, + .size_out = out_size, };if (in_size) {
drivers/cxl/core/mbox.c static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, · line 282
Quoted 9 lines
drivers/cxl/core/mbox.c +0
-6@@ -274,12 +275,6 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, } } -/* 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; -
Why not?
drivers/cxl/core/mbox.c +0 -0
@@ -253,7 +253,7 @@ static bool cxl_payload_from_user_allowed(u16
opcode, void *payload_in)
static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox,
struct cxl_dev_state *cxlds, u16 opcode,
- size_t in_size, size_t out_size, u64 in_payload)
+ ssize_t in_size, ssize_t out_size, u64 in_payload)
{
*mbox = (struct cxl_mbox_cmd) {
.opcode = opcode,
...and then the rest of this patch becomes unnecessary.