* [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev
@ 2018-12-04 22:09 David Kozub
0 siblings, 0 replies; 5+ messages in thread
From: David Kozub @ 2018-12-04 22:09 UTC (permalink / raw)
To: Jens Axboe, Scott Bauer, Jonathan Derrick, linux-block, linux-kernel
Cc: Jonas Rabenstein
The steps argument is only read by the next function, so it can
be passed directly as an argument rather than via opal_dev.
Normally, the steps is an array on the stack, so the pointer stops
being valid then the function that set opal_dev.steps returns.
If opal_dev.steps was not set to NULL before return it would become
a dangling pointer. When the steps are passed as argument this
becomes easier to see and more difficult to misuse.
Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz>
---
block/sed-opal.c | 142 ++++++++++++++++++++---------------------------
1 file changed, 61 insertions(+), 81 deletions(-)
diff --git a/block/sed-opal.c b/block/sed-opal.c
index ad52091531f2..82ef81b66ed5 100644
--- a/block/sed-opal.c
+++ b/block/sed-opal.c
@@ -88,7 +88,6 @@ struct opal_dev {
void *data;
sec_send_recv *send_recv;
- const struct opal_step *steps;
struct mutex dev_lock;
u16 comid;
u32 hsn;
@@ -387,19 +386,19 @@ static void check_geometry(struct opal_dev *dev, const void *data)
dev->lowest_lba = geo->lowest_aligned_lba;
}
-static int next(struct opal_dev *dev)
+static int next(struct opal_dev *dev, const struct opal_step *steps,
+ size_t n_steps)
{
const struct opal_step *step;
- int state = 0, error = 0;
+ size_t state;
+ int error = 0;
- do {
- step = &dev->steps[state];
- if (!step->fn)
- break;
+ for (state = 0; !error && state < n_steps; state++) {
+ step = &steps[state];
error = step->fn(dev, step->data);
if (error) {
- pr_debug("Step %d (%pS) failed wit error %d: %s\n",
+ pr_debug("Step %zu (%pS) failed with error %d: %s\n",
state, step->fn, error,
opal_error_to_human(error));
@@ -416,8 +415,7 @@ static int next(struct opal_dev *dev)
}
}
- state++;
- } while (!error);
+ }
return error;
}
@@ -1940,17 +1938,13 @@ static int end_opal_session(struct opal_dev *dev, void *data)
static int end_opal_session_error(struct opal_dev *dev)
{
const struct opal_step error_end_session[] = {
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
- dev->steps = error_end_session;
- return next(dev);
+ return next(dev, error_end_session, ARRAY_SIZE(error_end_session));
}
-static inline void setup_opal_dev(struct opal_dev *dev,
- const struct opal_step *steps)
+static inline void setup_opal_dev(struct opal_dev *dev)
{
- dev->steps = steps;
dev->tsn = 0;
dev->hsn = 0;
dev->prev_data = NULL;
@@ -1959,14 +1953,13 @@ static inline void setup_opal_dev(struct opal_dev *dev,
static int check_opal_support(struct opal_dev *dev)
{
const struct opal_step steps[] = {
- { opal_discovery0, },
- { NULL, }
+ { opal_discovery0, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, steps, ARRAY_SIZE(steps));
dev->supported = !ret;
mutex_unlock(&dev->dev_lock);
return ret;
@@ -2023,14 +2016,13 @@ static int opal_secure_erase_locking_range(struct opal_dev *dev,
{ start_auth_opal_session, opal_session },
{ get_active_key, &opal_session->opal_key.lr },
{ gen_key, },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, erase_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, erase_steps, ARRAY_SIZE(erase_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2042,14 +2034,13 @@ static int opal_erase_locking_range(struct opal_dev *dev,
{ opal_discovery0, },
{ start_auth_opal_session, opal_session },
{ erase_locking_range, opal_session },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, erase_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, erase_steps, ARRAY_SIZE(erase_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2066,8 +2057,7 @@ static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
{ end_opal_session, },
{ start_admin1LSP_opal_session, &opal_mbr->key },
{ set_mbr_enable_disable, &token },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2076,8 +2066,8 @@ static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, mbr_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2090,8 +2080,7 @@ static int opal_mbr_status(struct opal_dev *dev, struct opal_mbr_data *opal_mbr)
{ opal_discovery0, },
{ start_admin1LSP_opal_session, &opal_mbr->key },
{ set_mbr_done, &token },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2100,8 +2089,8 @@ static int opal_mbr_status(struct opal_dev *dev, struct opal_mbr_data *opal_mbr)
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, mbr_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2113,8 +2102,7 @@ static int opal_write_shadow_mbr(struct opal_dev *dev,
{ opal_discovery0, },
{ start_admin1LSP_opal_session, &info->key },
{ write_shadow_mbr, info },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2125,8 +2113,8 @@ static int opal_write_shadow_mbr(struct opal_dev *dev,
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, mbr_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2143,7 +2131,7 @@ static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
suspend->lr = lk_unlk->session.opal_key.lr;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, NULL);
+ setup_opal_dev(dev);
add_suspend_info(dev, suspend);
mutex_unlock(&dev->dev_lock);
return 0;
@@ -2156,8 +2144,7 @@ static int opal_add_user_to_lr(struct opal_dev *dev,
{ opal_discovery0, },
{ start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
{ add_user_to_lr, lk_unlk },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2179,8 +2166,8 @@ static int opal_add_user_to_lr(struct opal_dev *dev,
}
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, steps, ARRAY_SIZE(steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2190,14 +2177,13 @@ static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal)
const struct opal_step revert_steps[] = {
{ opal_discovery0, },
{ start_SIDASP_opal_session, opal },
- { revert_tper, }, /* controller will terminate session */
- { NULL, }
+ { revert_tper, } /* controller will terminate session */
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, revert_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, revert_steps, ARRAY_SIZE(revert_steps));
mutex_unlock(&dev->dev_lock);
/*
@@ -2217,19 +2203,20 @@ static int __opal_lock_unlock(struct opal_dev *dev,
{ opal_discovery0, },
{ start_auth_opal_session, &lk_unlk->session },
{ lock_unlock_locking_range, lk_unlk },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
const struct opal_step unlock_sum_steps[] = {
{ opal_discovery0, },
{ start_auth_opal_session, &lk_unlk->session },
{ lock_unlock_locking_range_sum, lk_unlk },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
- dev->steps = lk_unlk->session.sum ? unlock_sum_steps : unlock_steps;
- return next(dev);
+ if (lk_unlk->session.sum)
+ return next(dev, unlock_sum_steps,
+ ARRAY_SIZE(unlock_sum_steps));
+ else
+ return next(dev, unlock_steps, ARRAY_SIZE(unlock_steps));
}
static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
@@ -2239,12 +2226,10 @@ static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
{ opal_discovery0, },
{ start_admin1LSP_opal_session, key },
{ set_mbr_done, &mbr_done_tf },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
- dev->steps = mbrdone_step;
- return next(dev);
+ return next(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
}
static int opal_lock_unlock(struct opal_dev *dev,
@@ -2271,8 +2256,7 @@ static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
{ end_opal_session, },
{ start_SIDASP_opal_session, opal },
{ set_sid_cpin_pin, opal },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2280,8 +2264,8 @@ static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
return -ENODEV;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, owner_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, owner_steps, ARRAY_SIZE(owner_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2294,8 +2278,7 @@ static int opal_activate_lsp(struct opal_dev *dev,
{ start_SIDASP_opal_session, &opal_lr_act->key },
{ get_lsp_lifecycle, },
{ activate_lsp, opal_lr_act },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2303,8 +2286,8 @@ static int opal_activate_lsp(struct opal_dev *dev,
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, active_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, active_steps, ARRAY_SIZE(active_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2316,14 +2299,13 @@ static int opal_setup_locking_range(struct opal_dev *dev,
{ opal_discovery0, },
{ start_auth_opal_session, &opal_lrs->session },
{ setup_locking_range, opal_lrs },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, lr_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, lr_steps, ARRAY_SIZE(lr_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2334,8 +2316,7 @@ static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
{ opal_discovery0, },
{ start_auth_opal_session, &opal_pw->session },
{ set_new_pw, &opal_pw->new_user_pw },
- { end_opal_session, },
- { NULL }
+ { end_opal_session, }
};
int ret;
@@ -2346,8 +2327,8 @@ static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, pw_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, pw_steps, ARRAY_SIZE(pw_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2359,8 +2340,7 @@ static int opal_activate_user(struct opal_dev *dev,
{ opal_discovery0, },
{ start_admin1LSP_opal_session, &opal_session->opal_key },
{ internal_activate_user, opal_session },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2372,8 +2352,8 @@ static int opal_activate_user(struct opal_dev *dev,
}
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, act_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, act_steps, ARRAY_SIZE(act_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2390,7 +2370,7 @@ bool opal_unlock_from_suspend(struct opal_dev *dev)
return false;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, NULL);
+ setup_opal_dev(dev);
list_for_each_entry(suspend, &dev->unlk_lst, node) {
dev->tsn = 0;
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev
2019-02-14 0:16 ` [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
2019-03-28 15:44 ` Derrick, Jonathan
2019-03-28 17:07 ` Christoph Hellwig
@ 2019-04-06 15:20 ` Scott Bauer
2 siblings, 0 replies; 5+ messages in thread
From: Scott Bauer @ 2019-04-06 15:20 UTC (permalink / raw)
To: David Kozub
Cc: Jens Axboe, Jonathan Derrick, linux-block, linux-kernel,
Jonas Rabenstein
On Thu, Feb 14, 2019 at 01:16:06AM +0100, David Kozub wrote:
> The steps argument is only read by the next function, so it can
> be passed directly as an argument rather than via opal_dev.
>
> Normally, the steps is an array on the stack, so the pointer stops
> being valid then the function that set opal_dev.steps returns.
> If opal_dev.steps was not set to NULL before return it would become
> a dangling pointer. When the steps are passed as argument this
> becomes easier to see and more difficult to misuse.
>
> Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz>
Reviewed-by: Scott Bauer <sbauer@plzdonthack.me>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev
2019-02-14 0:16 ` [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
2019-03-28 15:44 ` Derrick, Jonathan
@ 2019-03-28 17:07 ` Christoph Hellwig
2019-04-06 15:20 ` Scott Bauer
2 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2019-03-28 17:07 UTC (permalink / raw)
To: David Kozub
Cc: Jens Axboe, Jonathan Derrick, Scott Bauer, linux-block,
linux-kernel, Jonas Rabenstein
On Thu, Feb 14, 2019 at 01:16:06AM +0100, David Kozub wrote:
> The steps argument is only read by the next function, so it can
> be passed directly as an argument rather than via opal_dev.
>
> Normally, the steps is an array on the stack, so the pointer stops
> being valid then the function that set opal_dev.steps returns.
> If opal_dev.steps was not set to NULL before return it would become
> a dangling pointer. When the steps are passed as argument this
> becomes easier to see and more difficult to misuse.
>
> Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz>
Looks good,
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev
2019-02-14 0:16 ` [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
@ 2019-03-28 15:44 ` Derrick, Jonathan
2019-03-28 17:07 ` Christoph Hellwig
2019-04-06 15:20 ` Scott Bauer
2 siblings, 0 replies; 5+ messages in thread
From: Derrick, Jonathan @ 2019-03-28 15:44 UTC (permalink / raw)
To: linux-kernel, zub, linux-block, sbauer, axboe; +Cc: jonas.rabenstein
[-- Attachment #1: Type: text/plain, Size: 12503 bytes --]
Acked-by: Jon Derrick <jonathan.derrick@intel.com>
On Thu, 2019-02-14 at 01:16 +0100, David Kozub wrote:
> The steps argument is only read by the next function, so it can
> be passed directly as an argument rather than via opal_dev.
>
> Normally, the steps is an array on the stack, so the pointer stops
> being valid then the function that set opal_dev.steps returns.
> If opal_dev.steps was not set to NULL before return it would become
> a dangling pointer. When the steps are passed as argument this
> becomes easier to see and more difficult to misuse.
>
> Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz>
> ---
> block/sed-opal.c | 158 +++++++++++++++++++++----------------------
> ----
> 1 file changed, 69 insertions(+), 89 deletions(-)
>
> diff --git a/block/sed-opal.c b/block/sed-opal.c
> index be0d633783c8..f027c0cb682e 100644
> --- a/block/sed-opal.c
> +++ b/block/sed-opal.c
> @@ -85,7 +85,6 @@ struct opal_dev {
> void *data;
> sec_send_recv *send_recv;
>
> - const struct opal_step *steps;
> struct mutex dev_lock;
> u16 comid;
> u32 hsn;
> @@ -382,37 +381,34 @@ static void check_geometry(struct opal_dev
> *dev, const void *data)
> dev->lowest_lba = geo->lowest_aligned_lba;
> }
>
> -static int next(struct opal_dev *dev)
> +static int next(struct opal_dev *dev, const struct opal_step *steps,
> + size_t n_steps)
> {
> const struct opal_step *step;
> - int state = 0, error = 0;
> + size_t state;
> + int error = 0;
>
> - do {
> - step = &dev->steps[state];
> - if (!step->fn)
> - break;
> + for (state = 0; state < n_steps; state++) {
> + step = &steps[state];
>
> error = step->fn(dev, step->data);
> - if (error) {
> - pr_debug("Step %d (%pS) failed with error %d:
> %s\n",
> - state, step->fn, error,
> - opal_error_to_human(error));
> -
> - /* For each OPAL command we do a discovery0
> then we
> - * start some sort of session.
> - * If we haven't passed state 1 then there was
> an error
> - * on discovery0 or during the attempt to start
> a
> - * session. Therefore we shouldn't attempt to
> terminate
> - * a session, as one has not yet been created.
> - */
> - if (state > 1) {
> - end_opal_session_error(dev);
> - return error;
> - }
> + if (error)
> + goto out_error;
> + }
>
> - }
> - state++;
> - } while (!error);
> + return 0;
> +
> +out_error:
> + /*
> + * For each OPAL command the first step in steps does a
> discovery0
> + * and the second step starts some sort of session. If an error
> occurred
> + * in the first two steps (and thus stopping the loop with
> state <= 1)
> + * then there was an error before or during the attempt to
> start a
> + * session. Therefore we shouldn't attempt to terminate a
> session, as
> + * one has not yet been created.
> + */
> + if (state > 1)
> + end_opal_session_error(dev);
>
> return error;
> }
> @@ -1836,17 +1832,13 @@ static int end_opal_session(struct opal_dev
> *dev, void *data)
> static int end_opal_session_error(struct opal_dev *dev)
> {
> const struct opal_step error_end_session[] = {
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> - dev->steps = error_end_session;
> - return next(dev);
> + return next(dev, error_end_session,
> ARRAY_SIZE(error_end_session));
> }
>
> -static inline void setup_opal_dev(struct opal_dev *dev,
> - const struct opal_step *steps)
> +static inline void setup_opal_dev(struct opal_dev *dev)
> {
> - dev->steps = steps;
> dev->tsn = 0;
> dev->hsn = 0;
> dev->prev_data = NULL;
> @@ -1855,14 +1847,13 @@ static inline void setup_opal_dev(struct
> opal_dev *dev,
> static int check_opal_support(struct opal_dev *dev)
> {
> const struct opal_step steps[] = {
> - { opal_discovery0, },
> - { NULL, }
> + { opal_discovery0, }
> };
> int ret;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, steps, ARRAY_SIZE(steps));
> dev->supported = !ret;
> mutex_unlock(&dev->dev_lock);
> return ret;
> @@ -1919,14 +1910,13 @@ static int
> opal_secure_erase_locking_range(struct opal_dev *dev,
> { start_auth_opal_session, opal_session },
> { get_active_key, &opal_session->opal_key.lr },
> { gen_key, },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, erase_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, erase_steps, ARRAY_SIZE(erase_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -1938,14 +1928,13 @@ static int opal_erase_locking_range(struct
> opal_dev *dev,
> { opal_discovery0, },
> { start_auth_opal_session, opal_session },
> { erase_locking_range, opal_session },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, erase_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, erase_steps, ARRAY_SIZE(erase_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -1963,8 +1952,7 @@ static int
> opal_enable_disable_shadow_mbr(struct opal_dev *dev,
> { end_opal_session, },
> { start_admin1LSP_opal_session, &opal_mbr->key },
> { set_mbr_enable_disable, &enable_disable },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> @@ -1973,8 +1961,8 @@ static int
> opal_enable_disable_shadow_mbr(struct opal_dev *dev,
> return -EINVAL;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, mbr_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -1991,7 +1979,7 @@ static int opal_save(struct opal_dev *dev,
> struct opal_lock_unlock *lk_unlk)
> suspend->lr = lk_unlk->session.opal_key.lr;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, NULL);
> + setup_opal_dev(dev);
> add_suspend_info(dev, suspend);
> mutex_unlock(&dev->dev_lock);
> return 0;
> @@ -2004,8 +1992,7 @@ static int opal_add_user_to_lr(struct opal_dev
> *dev,
> { opal_discovery0, },
> { start_admin1LSP_opal_session, &lk_unlk-
> >session.opal_key },
> { add_user_to_lr, lk_unlk },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> @@ -2027,8 +2014,8 @@ static int opal_add_user_to_lr(struct opal_dev
> *dev,
> }
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, steps, ARRAY_SIZE(steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -2038,14 +2025,13 @@ static int opal_reverttper(struct opal_dev
> *dev, struct opal_key *opal)
> const struct opal_step revert_steps[] = {
> { opal_discovery0, },
> { start_SIDASP_opal_session, opal },
> - { revert_tper, }, /* controller will terminate session
> */
> - { NULL, }
> + { revert_tper, } /* controller will terminate session
> */
> };
> int ret;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, revert_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, revert_steps, ARRAY_SIZE(revert_steps));
> mutex_unlock(&dev->dev_lock);
>
> /*
> @@ -2065,19 +2051,20 @@ static int __opal_lock_unlock(struct opal_dev
> *dev,
> { opal_discovery0, },
> { start_auth_opal_session, &lk_unlk->session },
> { lock_unlock_locking_range, lk_unlk },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> const struct opal_step unlock_sum_steps[] = {
> { opal_discovery0, },
> { start_auth_opal_session, &lk_unlk->session },
> { lock_unlock_locking_range_sum, lk_unlk },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
>
> - dev->steps = lk_unlk->session.sum ? unlock_sum_steps :
> unlock_steps;
> - return next(dev);
> + if (lk_unlk->session.sum)
> + return next(dev, unlock_sum_steps,
> + ARRAY_SIZE(unlock_sum_steps));
> + else
> + return next(dev, unlock_steps,
> ARRAY_SIZE(unlock_steps));
> }
>
> static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key
> *key)
> @@ -2087,12 +2074,10 @@ static int __opal_set_mbr_done(struct
> opal_dev *dev, struct opal_key *key)
> { opal_discovery0, },
> { start_admin1LSP_opal_session, key },
> { set_mbr_done, &mbr_done_tf },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
>
> - dev->steps = mbrdone_step;
> - return next(dev);
> + return next(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
> }
>
> static int opal_lock_unlock(struct opal_dev *dev,
> @@ -2119,8 +2104,7 @@ static int opal_take_ownership(struct opal_dev
> *dev, struct opal_key *opal)
> { end_opal_session, },
> { start_SIDASP_opal_session, opal },
> { set_sid_cpin_pin, opal },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> @@ -2128,8 +2112,8 @@ static int opal_take_ownership(struct opal_dev
> *dev, struct opal_key *opal)
> return -ENODEV;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, owner_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, owner_steps, ARRAY_SIZE(owner_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -2142,8 +2126,7 @@ static int opal_activate_lsp(struct opal_dev
> *dev,
> { start_SIDASP_opal_session, &opal_lr_act->key },
> { get_lsp_lifecycle, },
> { activate_lsp, opal_lr_act },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> @@ -2151,8 +2134,8 @@ static int opal_activate_lsp(struct opal_dev
> *dev,
> return -EINVAL;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, active_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, active_steps, ARRAY_SIZE(active_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -2164,14 +2147,13 @@ static int opal_setup_locking_range(struct
> opal_dev *dev,
> { opal_discovery0, },
> { start_auth_opal_session, &opal_lrs->session },
> { setup_locking_range, opal_lrs },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, lr_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, lr_steps, ARRAY_SIZE(lr_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -2182,8 +2164,7 @@ static int opal_set_new_pw(struct opal_dev
> *dev, struct opal_new_pw *opal_pw)
> { opal_discovery0, },
> { start_auth_opal_session, &opal_pw->session },
> { set_new_pw, &opal_pw->new_user_pw },
> - { end_opal_session, },
> - { NULL }
> + { end_opal_session, }
> };
> int ret;
>
> @@ -2194,8 +2175,8 @@ static int opal_set_new_pw(struct opal_dev
> *dev, struct opal_new_pw *opal_pw)
> return -EINVAL;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, pw_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, pw_steps, ARRAY_SIZE(pw_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -2207,8 +2188,7 @@ static int opal_activate_user(struct opal_dev
> *dev,
> { opal_discovery0, },
> { start_admin1LSP_opal_session, &opal_session->opal_key
> },
> { internal_activate_user, opal_session },
> - { end_opal_session, },
> - { NULL, }
> + { end_opal_session, }
> };
> int ret;
>
> @@ -2220,8 +2200,8 @@ static int opal_activate_user(struct opal_dev
> *dev,
> }
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, act_steps);
> - ret = next(dev);
> + setup_opal_dev(dev);
> + ret = next(dev, act_steps, ARRAY_SIZE(act_steps));
> mutex_unlock(&dev->dev_lock);
> return ret;
> }
> @@ -2238,7 +2218,7 @@ bool opal_unlock_from_suspend(struct opal_dev
> *dev)
> return false;
>
> mutex_lock(&dev->dev_lock);
> - setup_opal_dev(dev, NULL);
> + setup_opal_dev(dev);
>
> list_for_each_entry(suspend, &dev->unlk_lst, node) {
> dev->tsn = 0;
[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3278 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev
2019-02-14 0:15 [PATCH 00/16] sed-opal: fix shadow MBR enable/disable and clean up code David Kozub
@ 2019-02-14 0:16 ` David Kozub
2019-03-28 15:44 ` Derrick, Jonathan
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: David Kozub @ 2019-02-14 0:16 UTC (permalink / raw)
To: Jens Axboe, Jonathan Derrick, Scott Bauer, linux-block, linux-kernel
Cc: Jonas Rabenstein, David Kozub
The steps argument is only read by the next function, so it can
be passed directly as an argument rather than via opal_dev.
Normally, the steps is an array on the stack, so the pointer stops
being valid then the function that set opal_dev.steps returns.
If opal_dev.steps was not set to NULL before return it would become
a dangling pointer. When the steps are passed as argument this
becomes easier to see and more difficult to misuse.
Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz>
---
block/sed-opal.c | 158 +++++++++++++++++++++--------------------------
1 file changed, 69 insertions(+), 89 deletions(-)
diff --git a/block/sed-opal.c b/block/sed-opal.c
index be0d633783c8..f027c0cb682e 100644
--- a/block/sed-opal.c
+++ b/block/sed-opal.c
@@ -85,7 +85,6 @@ struct opal_dev {
void *data;
sec_send_recv *send_recv;
- const struct opal_step *steps;
struct mutex dev_lock;
u16 comid;
u32 hsn;
@@ -382,37 +381,34 @@ static void check_geometry(struct opal_dev *dev, const void *data)
dev->lowest_lba = geo->lowest_aligned_lba;
}
-static int next(struct opal_dev *dev)
+static int next(struct opal_dev *dev, const struct opal_step *steps,
+ size_t n_steps)
{
const struct opal_step *step;
- int state = 0, error = 0;
+ size_t state;
+ int error = 0;
- do {
- step = &dev->steps[state];
- if (!step->fn)
- break;
+ for (state = 0; state < n_steps; state++) {
+ step = &steps[state];
error = step->fn(dev, step->data);
- if (error) {
- pr_debug("Step %d (%pS) failed with error %d: %s\n",
- state, step->fn, error,
- opal_error_to_human(error));
-
- /* For each OPAL command we do a discovery0 then we
- * start some sort of session.
- * If we haven't passed state 1 then there was an error
- * on discovery0 or during the attempt to start a
- * session. Therefore we shouldn't attempt to terminate
- * a session, as one has not yet been created.
- */
- if (state > 1) {
- end_opal_session_error(dev);
- return error;
- }
+ if (error)
+ goto out_error;
+ }
- }
- state++;
- } while (!error);
+ return 0;
+
+out_error:
+ /*
+ * For each OPAL command the first step in steps does a discovery0
+ * and the second step starts some sort of session. If an error occurred
+ * in the first two steps (and thus stopping the loop with state <= 1)
+ * then there was an error before or during the attempt to start a
+ * session. Therefore we shouldn't attempt to terminate a session, as
+ * one has not yet been created.
+ */
+ if (state > 1)
+ end_opal_session_error(dev);
return error;
}
@@ -1836,17 +1832,13 @@ static int end_opal_session(struct opal_dev *dev, void *data)
static int end_opal_session_error(struct opal_dev *dev)
{
const struct opal_step error_end_session[] = {
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
- dev->steps = error_end_session;
- return next(dev);
+ return next(dev, error_end_session, ARRAY_SIZE(error_end_session));
}
-static inline void setup_opal_dev(struct opal_dev *dev,
- const struct opal_step *steps)
+static inline void setup_opal_dev(struct opal_dev *dev)
{
- dev->steps = steps;
dev->tsn = 0;
dev->hsn = 0;
dev->prev_data = NULL;
@@ -1855,14 +1847,13 @@ static inline void setup_opal_dev(struct opal_dev *dev,
static int check_opal_support(struct opal_dev *dev)
{
const struct opal_step steps[] = {
- { opal_discovery0, },
- { NULL, }
+ { opal_discovery0, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, steps, ARRAY_SIZE(steps));
dev->supported = !ret;
mutex_unlock(&dev->dev_lock);
return ret;
@@ -1919,14 +1910,13 @@ static int opal_secure_erase_locking_range(struct opal_dev *dev,
{ start_auth_opal_session, opal_session },
{ get_active_key, &opal_session->opal_key.lr },
{ gen_key, },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, erase_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, erase_steps, ARRAY_SIZE(erase_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -1938,14 +1928,13 @@ static int opal_erase_locking_range(struct opal_dev *dev,
{ opal_discovery0, },
{ start_auth_opal_session, opal_session },
{ erase_locking_range, opal_session },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, erase_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, erase_steps, ARRAY_SIZE(erase_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -1963,8 +1952,7 @@ static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
{ end_opal_session, },
{ start_admin1LSP_opal_session, &opal_mbr->key },
{ set_mbr_enable_disable, &enable_disable },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -1973,8 +1961,8 @@ static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, mbr_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -1991,7 +1979,7 @@ static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
suspend->lr = lk_unlk->session.opal_key.lr;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, NULL);
+ setup_opal_dev(dev);
add_suspend_info(dev, suspend);
mutex_unlock(&dev->dev_lock);
return 0;
@@ -2004,8 +1992,7 @@ static int opal_add_user_to_lr(struct opal_dev *dev,
{ opal_discovery0, },
{ start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
{ add_user_to_lr, lk_unlk },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2027,8 +2014,8 @@ static int opal_add_user_to_lr(struct opal_dev *dev,
}
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, steps, ARRAY_SIZE(steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2038,14 +2025,13 @@ static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal)
const struct opal_step revert_steps[] = {
{ opal_discovery0, },
{ start_SIDASP_opal_session, opal },
- { revert_tper, }, /* controller will terminate session */
- { NULL, }
+ { revert_tper, } /* controller will terminate session */
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, revert_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, revert_steps, ARRAY_SIZE(revert_steps));
mutex_unlock(&dev->dev_lock);
/*
@@ -2065,19 +2051,20 @@ static int __opal_lock_unlock(struct opal_dev *dev,
{ opal_discovery0, },
{ start_auth_opal_session, &lk_unlk->session },
{ lock_unlock_locking_range, lk_unlk },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
const struct opal_step unlock_sum_steps[] = {
{ opal_discovery0, },
{ start_auth_opal_session, &lk_unlk->session },
{ lock_unlock_locking_range_sum, lk_unlk },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
- dev->steps = lk_unlk->session.sum ? unlock_sum_steps : unlock_steps;
- return next(dev);
+ if (lk_unlk->session.sum)
+ return next(dev, unlock_sum_steps,
+ ARRAY_SIZE(unlock_sum_steps));
+ else
+ return next(dev, unlock_steps, ARRAY_SIZE(unlock_steps));
}
static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
@@ -2087,12 +2074,10 @@ static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
{ opal_discovery0, },
{ start_admin1LSP_opal_session, key },
{ set_mbr_done, &mbr_done_tf },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
- dev->steps = mbrdone_step;
- return next(dev);
+ return next(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
}
static int opal_lock_unlock(struct opal_dev *dev,
@@ -2119,8 +2104,7 @@ static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
{ end_opal_session, },
{ start_SIDASP_opal_session, opal },
{ set_sid_cpin_pin, opal },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2128,8 +2112,8 @@ static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
return -ENODEV;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, owner_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, owner_steps, ARRAY_SIZE(owner_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2142,8 +2126,7 @@ static int opal_activate_lsp(struct opal_dev *dev,
{ start_SIDASP_opal_session, &opal_lr_act->key },
{ get_lsp_lifecycle, },
{ activate_lsp, opal_lr_act },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2151,8 +2134,8 @@ static int opal_activate_lsp(struct opal_dev *dev,
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, active_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, active_steps, ARRAY_SIZE(active_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2164,14 +2147,13 @@ static int opal_setup_locking_range(struct opal_dev *dev,
{ opal_discovery0, },
{ start_auth_opal_session, &opal_lrs->session },
{ setup_locking_range, opal_lrs },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, lr_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, lr_steps, ARRAY_SIZE(lr_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2182,8 +2164,7 @@ static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
{ opal_discovery0, },
{ start_auth_opal_session, &opal_pw->session },
{ set_new_pw, &opal_pw->new_user_pw },
- { end_opal_session, },
- { NULL }
+ { end_opal_session, }
};
int ret;
@@ -2194,8 +2175,8 @@ static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
return -EINVAL;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, pw_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, pw_steps, ARRAY_SIZE(pw_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2207,8 +2188,7 @@ static int opal_activate_user(struct opal_dev *dev,
{ opal_discovery0, },
{ start_admin1LSP_opal_session, &opal_session->opal_key },
{ internal_activate_user, opal_session },
- { end_opal_session, },
- { NULL, }
+ { end_opal_session, }
};
int ret;
@@ -2220,8 +2200,8 @@ static int opal_activate_user(struct opal_dev *dev,
}
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, act_steps);
- ret = next(dev);
+ setup_opal_dev(dev);
+ ret = next(dev, act_steps, ARRAY_SIZE(act_steps));
mutex_unlock(&dev->dev_lock);
return ret;
}
@@ -2238,7 +2218,7 @@ bool opal_unlock_from_suspend(struct opal_dev *dev)
return false;
mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, NULL);
+ setup_opal_dev(dev);
list_for_each_entry(suspend, &dev->unlk_lst, node) {
dev->tsn = 0;
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-04-06 15:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-04 22:09 [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
2019-02-14 0:15 [PATCH 00/16] sed-opal: fix shadow MBR enable/disable and clean up code David Kozub
2019-02-14 0:16 ` [PATCH 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
2019-03-28 15:44 ` Derrick, Jonathan
2019-03-28 17:07 ` Christoph Hellwig
2019-04-06 15:20 ` Scott Bauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).