Message ID | 0f5fe62576e70de12515e323ca5c0a45ef92832d.1521482296.git.jonas.rabenstein@studium.uni-erlangen.de (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On Mon, Mar 19, 2018 at 08:59:45PM +0100, Christoph Hellwig wrote: > > +static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len) > > { > > size_t header_len = 1; > > bool is_short_atom = true; > > - > > - if (*err) > > - return; > > + char *start; > > Shouldn't this also return early if we have a pending error? It will short circuit and bail out via can_add failing. So even though you have to go dig to see if the following functions handle the erorr I think it's slightly cleaner to have a single if (*err) in the deeper functions. This lest the error back out the call chain instead of having multiple if (*err) checks earlier in the call chains.
> +static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len) > { > size_t header_len = 1; > bool is_short_atom = true; > - > - if (*err) > - return; > + char *start; Shouldn't this also return early if we have a pending error? Except for that this looks fine: Reviewed-by: Christoph Hellwig <hch@lst.de>
diff --git a/block/sed-opal.c b/block/sed-opal.c index 9b6f14e7aeb1..fc10f81d4892 100644 --- a/block/sed-opal.c +++ b/block/sed-opal.c @@ -581,14 +581,11 @@ static void add_token_u64(int *err, struct opal_dev *cmd, u64 number) add_token_u8(err, cmd, number >> (len * 8)); } -static void add_token_bytestring(int *err, struct opal_dev *cmd, - const u8 *bytestring, size_t len) +static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len) { size_t header_len = 1; bool is_short_atom = true; - - if (*err) - return; + char *start; if (len & ~SHORT_ATOM_LEN_MASK) { header_len = 2; @@ -597,17 +594,27 @@ static void add_token_bytestring(int *err, struct opal_dev *cmd, if (!can_add(err, cmd, header_len + len)) { pr_debug("Error adding bytestring: end of buffer.\n"); - return; + return NULL; } if (is_short_atom) add_short_atom_header(cmd, true, false, len); else add_medium_atom_header(cmd, true, false, len); - - memcpy(&cmd->cmd[cmd->pos], bytestring, len); + start = &cmd->cmd[cmd->pos]; cmd->pos += len; + return start; +} +static void add_token_bytestring(int *err, struct opal_dev *cmd, + const u8 *bytestring, size_t len) +{ + u8 *start; + + start = add_bytestring_header(err, cmd, len); + if (!start) + return; + memcpy(start, bytestring, len); } static int build_locking_range(u8 *buffer, size_t length, u8 lr)
Split the header generation from the (normal) memcpy part if a bytestring is copied into the command buffer. This allows in-place generation of the bytestring content. For example, copy_from_user may be used without an intermediate buffer. Signed-off-by: Jonas Rabenstein <jonas.rabenstein@studium.uni-erlangen.de>