Commit 5e6632d1 authored by Matthew R. Ochs's avatar Matthew R. Ochs Committed by Martin K. Petersen

cxlflash: Split out context initialization

Presently, context information structures are allocated and
initialized in the same routine, create_context(). This imposes
an ordering restriction such that all pieces of information needed
to initialize a context must be known before the context is even
allocated.

This design point is not flexible when the order of context
creation needs to be modified. Specifically, this can lead to
problems when members of the context information structure are
a part of an ordering dependency (i.e. - the 'work' structure
embedded within the context).

To remedy, the allocation is left as-is, inside of the existing
create_context() routine and the initialization is transitioned
to a new void routine, init_context(). At the same time, in
anticipation of these routines not being called in sequence, a
state boolean is added to the context information structure to
track when the context has been initilized. The context teardown
routine, destroy_context(), is modified to support being called
with a non-initialized context.
Signed-off-by: default avatarMatthew R. Ochs <mrochs@linux.vnet.ibm.com>
Reviewed-by: default avatarUma Krishnan <ukrishn@linux.vnet.ibm.com>
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
parent 6ded8b3c
...@@ -709,29 +709,34 @@ int cxlflash_disk_release(struct scsi_device *sdev, ...@@ -709,29 +709,34 @@ int cxlflash_disk_release(struct scsi_device *sdev,
* @cfg: Internal structure associated with the host. * @cfg: Internal structure associated with the host.
* @ctxi: Context to release. * @ctxi: Context to release.
* *
* Note that the rht_lun member of the context was cut from a single * This routine is safe to be called with a a non-initialized context
* allocation when the context was created and therefore does not need * and is tolerant of being called with the context's mutex held (it
* to be explicitly freed. Also note that we conditionally check for the * will be unlocked if necessary before freeing). Also note that the
* existence of the context control map before clearing the RHT registers * routine conditionally checks for the existence of the context control
* and context capabilities because it is possible to destroy a context * map before clearing the RHT registers and context capabilities because
* while the context is in the error state (previous mapping was removed * it is possible to destroy a context while the context is in the error
* [so we don't have to worry about clearing] and context is waiting for * state (previous mapping was removed [so there is no need to worry about
* a new mapping). * clearing] and context is waiting for a new mapping).
*/ */
static void destroy_context(struct cxlflash_cfg *cfg, static void destroy_context(struct cxlflash_cfg *cfg,
struct ctx_info *ctxi) struct ctx_info *ctxi)
{ {
struct afu *afu = cfg->afu; struct afu *afu = cfg->afu;
if (ctxi->initialized) {
WARN_ON(!list_empty(&ctxi->luns)); WARN_ON(!list_empty(&ctxi->luns));
/* Clear RHT registers and drop all capabilities for this context */ /* Clear RHT registers and drop all capabilities for context */
if (afu->afu_map && ctxi->ctrl_map) { if (afu->afu_map && ctxi->ctrl_map) {
writeq_be(0, &ctxi->ctrl_map->rht_start); writeq_be(0, &ctxi->ctrl_map->rht_start);
writeq_be(0, &ctxi->ctrl_map->rht_cnt_id); writeq_be(0, &ctxi->ctrl_map->rht_cnt_id);
writeq_be(0, &ctxi->ctrl_map->ctx_cap); writeq_be(0, &ctxi->ctrl_map->ctx_cap);
} }
if (mutex_is_locked(&ctxi->mutex))
mutex_unlock(&ctxi->mutex);
}
/* Free memory associated with context */ /* Free memory associated with context */
free_page((ulong)ctxi->rht_start); free_page((ulong)ctxi->rht_start);
kfree(ctxi->rht_needs_ws); kfree(ctxi->rht_needs_ws);
...@@ -742,23 +747,12 @@ static void destroy_context(struct cxlflash_cfg *cfg, ...@@ -742,23 +747,12 @@ static void destroy_context(struct cxlflash_cfg *cfg,
/** /**
* create_context() - allocates and initializes a context * create_context() - allocates and initializes a context
* @cfg: Internal structure associated with the host. * @cfg: Internal structure associated with the host.
* @ctx: Previously obtained CXL context reference.
* @ctxid: Previously obtained process element associated with CXL context.
* @adap_fd: Previously obtained adapter fd associated with CXL context.
* @file: Previously obtained file associated with CXL context.
* @perms: User-specified permissions.
*
* The context's mutex is locked when an allocated context is returned.
* *
* Return: Allocated context on success, NULL on failure * Return: Allocated context on success, NULL on failure
*/ */
static struct ctx_info *create_context(struct cxlflash_cfg *cfg, static struct ctx_info *create_context(struct cxlflash_cfg *cfg)
struct cxl_context *ctx, int ctxid,
int adap_fd, struct file *file,
u32 perms)
{ {
struct device *dev = &cfg->dev->dev; struct device *dev = &cfg->dev->dev;
struct afu *afu = cfg->afu;
struct ctx_info *ctxi = NULL; struct ctx_info *ctxi = NULL;
struct llun_info **lli = NULL; struct llun_info **lli = NULL;
u8 *ws = NULL; u8 *ws = NULL;
...@@ -781,28 +775,49 @@ static struct ctx_info *create_context(struct cxlflash_cfg *cfg, ...@@ -781,28 +775,49 @@ static struct ctx_info *create_context(struct cxlflash_cfg *cfg,
ctxi->rht_lun = lli; ctxi->rht_lun = lli;
ctxi->rht_needs_ws = ws; ctxi->rht_needs_ws = ws;
ctxi->rht_start = rhte; ctxi->rht_start = rhte;
ctxi->rht_perms = perms; out:
return ctxi;
err:
kfree(ws);
kfree(lli);
kfree(ctxi);
ctxi = NULL;
goto out;
}
/**
* init_context() - initializes a previously allocated context
* @ctxi: Previously allocated context
* @cfg: Internal structure associated with the host.
* @ctx: Previously obtained CXL context reference.
* @ctxid: Previously obtained process element associated with CXL context.
* @adap_fd: Previously obtained adapter fd associated with CXL context.
* @file: Previously obtained file associated with CXL context.
* @perms: User-specified permissions.
*
* Upon return, the context is marked as initialized and the context's mutex
* is locked.
*/
static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
struct cxl_context *ctx, int ctxid, int adap_fd,
struct file *file, u32 perms)
{
struct afu *afu = cfg->afu;
ctxi->rht_perms = perms;
ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl; ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid); ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
ctxi->lfd = adap_fd; ctxi->lfd = adap_fd;
ctxi->pid = current->tgid; /* tgid = pid */ ctxi->pid = current->tgid; /* tgid = pid */
ctxi->ctx = ctx; ctxi->ctx = ctx;
ctxi->file = file; ctxi->file = file;
ctxi->initialized = true;
mutex_init(&ctxi->mutex); mutex_init(&ctxi->mutex);
INIT_LIST_HEAD(&ctxi->luns); INIT_LIST_HEAD(&ctxi->luns);
INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */ INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */
mutex_lock(&ctxi->mutex); mutex_lock(&ctxi->mutex);
out:
return ctxi;
err:
kfree(ws);
kfree(lli);
kfree(ctxi);
ctxi = NULL;
goto out;
} }
/** /**
...@@ -1396,13 +1411,16 @@ static int cxlflash_disk_attach(struct scsi_device *sdev, ...@@ -1396,13 +1411,16 @@ static int cxlflash_disk_attach(struct scsi_device *sdev,
/* Translate read/write O_* flags from fcntl.h to AFU permission bits */ /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
perms = SISL_RHT_PERM(attach->hdr.flags + 1); perms = SISL_RHT_PERM(attach->hdr.flags + 1);
ctxi = create_context(cfg, ctx, ctxid, fd, file, perms); ctxi = create_context(cfg);
if (unlikely(!ctxi)) { if (unlikely(!ctxi)) {
dev_err(dev, "%s: Failed to create context! (%d)\n", dev_err(dev, "%s: Failed to create context! (%d)\n",
__func__, ctxid); __func__, ctxid);
goto err3; goto err3;
} }
/* Context mutex is locked upon return */
init_context(ctxi, cfg, ctx, ctxid, fd, file, perms);
work = &ctxi->work; work = &ctxi->work;
work->num_interrupts = attach->num_interrupts; work->num_interrupts = attach->num_interrupts;
work->flags = CXL_START_WORK_NUM_IRQS; work->flags = CXL_START_WORK_NUM_IRQS;
......
...@@ -102,6 +102,7 @@ struct ctx_info { ...@@ -102,6 +102,7 @@ struct ctx_info {
u64 ctxid; u64 ctxid;
int lfd; int lfd;
pid_t pid; pid_t pid;
bool initialized;
bool unavail; bool unavail;
bool err_recovery_active; bool err_recovery_active;
struct mutex mutex; /* Context protection */ struct mutex mutex; /* Context protection */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment