linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Uma Krishnan <ukrishn@linux.vnet.ibm.com>
To: linux-scsi@vger.kernel.org,
	James Bottomley <jejb@linux.vnet.ibm.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>,
	"Matthew R. Ochs" <mrochs@linux.vnet.ibm.com>,
	"Manoj N. Kumar" <manoj@linux.vnet.ibm.com>
Cc: linuxppc-dev@lists.ozlabs.org,
	Andrew Donnellan <andrew.donnellan@au1.ibm.com>,
	Frederic Barrat <fbarrat@linux.vnet.ibm.com>,
	Christophe Lombard <clombard@linux.vnet.ibm.com>
Subject: [PATCH v3 35/41] cxlflash: Introduce OCXL context state machine
Date: Mon, 26 Mar 2018 11:35:00 -0500	[thread overview]
Message-ID: <1522082100-58750-1-git-send-email-ukrishn@linux.vnet.ibm.com> (raw)
In-Reply-To: <1522081759-57431-1-git-send-email-ukrishn@linux.vnet.ibm.com>

In order to protect the OCXL hardware contexts from getting clobbered,
a simple state machine is added to indicate when a context is in open,
close or start state. The expected states are validated throughout the
code to prevent illegal operations on a context. A mutex is added to
protect writes to the context state field.

Signed-off-by: Uma Krishnan <ukrishn@linux.vnet.ibm.com>
Acked-by: Matthew R. Ochs <mrochs@linux.vnet.ibm.com>
---
 drivers/scsi/cxlflash/ocxl_hw.c | 59 ++++++++++++++++++++++++++++++++++++++---
 drivers/scsi/cxlflash/ocxl_hw.h |  8 ++++++
 2 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/cxlflash/ocxl_hw.c b/drivers/scsi/cxlflash/ocxl_hw.c
index f77f4d7..89d3d89 100644
--- a/drivers/scsi/cxlflash/ocxl_hw.c
+++ b/drivers/scsi/cxlflash/ocxl_hw.c
@@ -163,6 +163,16 @@ static struct file *ocxlflash_getfile(struct device *dev, const char *name,
 static void __iomem *ocxlflash_psa_map(void *ctx_cookie)
 {
 	struct ocxlflash_context *ctx = ctx_cookie;
+	struct device *dev = ctx->hw_afu->dev;
+
+	mutex_lock(&ctx->state_mutex);
+	if (ctx->state != STARTED) {
+		dev_err(dev, "%s: Context not started, state=%d\n", __func__,
+			ctx->state);
+		mutex_unlock(&ctx->state_mutex);
+		return NULL;
+	}
+	mutex_unlock(&ctx->state_mutex);
 
 	return ioremap(ctx->psn_phys, ctx->psn_size);
 }
@@ -343,6 +353,14 @@ static int start_context(struct ocxlflash_context *ctx)
 	int rc = 0;
 	u32 pid;
 
+	mutex_lock(&ctx->state_mutex);
+	if (ctx->state != OPENED) {
+		dev_err(dev, "%s: Context state invalid, state=%d\n",
+			__func__, ctx->state);
+		rc = -EINVAL;
+		goto out;
+	}
+
 	if (master) {
 		ctx->psn_size = acfg->global_mmio_size;
 		ctx->psn_phys = afu->gmmio_phys;
@@ -366,7 +384,10 @@ static int start_context(struct ocxlflash_context *ctx)
 			__func__, rc);
 		goto out;
 	}
+
+	ctx->state = STARTED;
 out:
+	mutex_unlock(&ctx->state_mutex);
 	return rc;
 }
 
@@ -396,7 +417,15 @@ static int ocxlflash_stop_context(void *ctx_cookie)
 	struct ocxl_afu_config *acfg = &afu->acfg;
 	struct pci_dev *pdev = afu->pdev;
 	struct device *dev = afu->dev;
-	int rc;
+	enum ocxlflash_ctx_state state;
+	int rc = 0;
+
+	mutex_lock(&ctx->state_mutex);
+	state = ctx->state;
+	ctx->state = CLOSED;
+	mutex_unlock(&ctx->state_mutex);
+	if (state != STARTED)
+		goto out;
 
 	rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
 					 ctx->pe);
@@ -474,7 +503,9 @@ static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
 
 	spin_lock_init(&ctx->slock);
 	init_waitqueue_head(&ctx->wq);
+	mutex_init(&ctx->state_mutex);
 
+	ctx->state = OPENED;
 	ctx->pe = rc;
 	ctx->master = false;
 	ctx->mapping = NULL;
@@ -499,11 +530,23 @@ static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
 static int ocxlflash_release_context(void *ctx_cookie)
 {
 	struct ocxlflash_context *ctx = ctx_cookie;
+	struct device *dev;
 	int rc = 0;
 
 	if (!ctx)
 		goto out;
 
+	dev = ctx->hw_afu->dev;
+	mutex_lock(&ctx->state_mutex);
+	if (ctx->state >= STARTED) {
+		dev_err(dev, "%s: Context in use, state=%d\n", __func__,
+			ctx->state);
+		mutex_unlock(&ctx->state_mutex);
+		rc = -EBUSY;
+		goto out;
+	}
+	mutex_unlock(&ctx->state_mutex);
+
 	idr_remove(&ctx->hw_afu->idr, ctx->pe);
 	ocxlflash_release_mapping(ctx);
 	kfree(ctx);
@@ -947,7 +990,7 @@ static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
 	spin_lock_irqsave(&ctx->slock, lock_flags);
 	if (ctx_event_pending(ctx))
 		mask |= POLLIN | POLLRDNORM;
-	else
+	else if (ctx->state == CLOSED)
 		mask |= POLLERR;
 	spin_unlock_irqrestore(&ctx->slock, lock_flags);
 
@@ -990,7 +1033,7 @@ static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 	for (;;) {
 		prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
 
-		if (ctx_event_pending(ctx))
+		if (ctx_event_pending(ctx) || (ctx->state == CLOSED))
 			break;
 
 		if (file->f_flags & O_NONBLOCK) {
@@ -1076,12 +1119,22 @@ static int ocxlflash_mmap_fault(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	struct ocxlflash_context *ctx = vma->vm_file->private_data;
+	struct device *dev = ctx->hw_afu->dev;
 	u64 mmio_area, offset;
 
 	offset = vmf->pgoff << PAGE_SHIFT;
 	if (offset >= ctx->psn_size)
 		return VM_FAULT_SIGBUS;
 
+	mutex_lock(&ctx->state_mutex);
+	if (ctx->state != STARTED) {
+		dev_err(dev, "%s: Context not started, state=%d\n",
+			__func__, ctx->state);
+		mutex_unlock(&ctx->state_mutex);
+		return VM_FAULT_SIGBUS;
+	}
+	mutex_unlock(&ctx->state_mutex);
+
 	mmio_area = ctx->psn_phys;
 	mmio_area += offset;
 
diff --git a/drivers/scsi/cxlflash/ocxl_hw.h b/drivers/scsi/cxlflash/ocxl_hw.h
index acd2801..1829e55 100644
--- a/drivers/scsi/cxlflash/ocxl_hw.h
+++ b/drivers/scsi/cxlflash/ocxl_hw.h
@@ -46,6 +46,12 @@ struct ocxl_hw_afu {
 	bool is_present;		/* Function has AFUs defined */
 };
 
+enum ocxlflash_ctx_state {
+	CLOSED,
+	OPENED,
+	STARTED
+};
+
 struct ocxlflash_context {
 	struct ocxl_hw_afu *hw_afu;	/* HW AFU back pointer */
 	struct address_space *mapping;	/* Mapping for pseudo filesystem */
@@ -57,6 +63,8 @@ struct ocxlflash_context {
 
 	spinlock_t slock;		/* Protects irq/fault/event updates */
 	wait_queue_head_t wq;		/* Wait queue for poll and interrupts */
+	struct mutex state_mutex;	/* Mutex to update context state */
+	enum ocxlflash_ctx_state state;	/* Context state */
 
 	struct ocxlflash_irqs *irqs;	/* Pointer to array of structures */
 	int num_irqs;			/* Number of interrupts */
-- 
2.1.0

  parent reply	other threads:[~2018-03-26 16:35 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-26 16:29 [PATCH v3 00/41] cxlflash: OCXL transport support and miscellaneous fixes Uma Krishnan
2018-03-26 16:29 ` [PATCH v3 01/41] cxlflash: Preserve number of interrupts for master contexts Uma Krishnan
2018-03-26 16:30 ` [PATCH v3 02/41] cxlflash: Avoid clobbering context control register value Uma Krishnan
2018-03-26 16:30 ` [PATCH v3 03/41] cxlflash: Add argument identifier names Uma Krishnan
2018-03-26 16:30 ` [PATCH v3 04/41] cxlflash: Introduce OCXL backend Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 05/41] cxlflash: Hardware AFU for OCXL Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 06/41] cxlflash: Read host function configuration Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 07/41] cxlflash: Setup function acTag range Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 08/41] cxlflash: Read host AFU configuration Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 09/41] cxlflash: Setup AFU acTag range Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 10/41] cxlflash: Setup AFU PASID Uma Krishnan
2018-03-26 16:31 ` [PATCH v3 11/41] cxlflash: Adapter context support for OCXL Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 12/41] cxlflash: Use IDR to manage adapter contexts Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 13/41] cxlflash: Support adapter file descriptors for OCXL Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 14/41] cxlflash: Support adapter context discovery Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 15/41] cxlflash: Support image reload policy modification Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 16/41] cxlflash: MMIO map the AFU Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 17/41] cxlflash: Support starting an adapter context Uma Krishnan
2018-03-26 16:32 ` [PATCH v3 18/41] cxlflash: Support process specific mappings Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 19/41] cxlflash: Support AFU state toggling Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 20/41] cxlflash: Support reading adapter VPD data Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 21/41] cxlflash: Setup function OCXL link Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 22/41] cxlflash: Setup OCXL transaction layer Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 23/41] cxlflash: Support process element lifecycle Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 24/41] cxlflash: Support AFU interrupt management Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 25/41] cxlflash: Support AFU interrupt mapping and registration Uma Krishnan
2018-03-26 16:33 ` [PATCH v3 26/41] cxlflash: Support starting user contexts Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 27/41] cxlflash: Support adapter context polling Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 28/41] cxlflash: Support adapter context reading Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 29/41] cxlflash: Support adapter context mmap and release Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 30/41] cxlflash: Support file descriptor mapping Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 31/41] cxlflash: Introduce object handle fop Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 32/41] cxlflash: Setup LISNs for user contexts Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 33/41] cxlflash: Setup LISNs for master contexts Uma Krishnan
2018-03-26 16:34 ` [PATCH v3 34/41] cxlflash: Update synchronous interrupt status bits Uma Krishnan
2018-03-26 16:35 ` Uma Krishnan [this message]
2018-03-26 16:35 ` [PATCH v3 36/41] cxlflash: Register for translation errors Uma Krishnan
2018-03-26 16:35 ` [PATCH v3 37/41] cxlflash: Support AFU reset Uma Krishnan
2018-03-26 16:35 ` [PATCH v3 38/41] cxlflash: Enable OCXL operations Uma Krishnan
2018-03-26 16:35 ` [PATCH v3 39/41] cxlflash: Synchronize reset and remove ops Uma Krishnan
2018-03-28 14:43   ` Matthew R. Ochs
2018-03-26 16:35 ` [PATCH v3 40/41] cxlflash: Remove commmands from pending list on timeout Uma Krishnan
2018-03-28 14:50   ` Matthew R. Ochs
2018-03-26 16:35 ` [PATCH v3 41/41] cxlflash: Handle spurious interrupts Uma Krishnan
2018-03-28 15:03   ` Matthew R. Ochs
2018-03-28 21:34 ` [PATCH v3 00/41] cxlflash: OCXL transport support and miscellaneous fixes Martin K. Petersen
2018-03-29 18:35   ` Uma Krishnan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1522082100-58750-1-git-send-email-ukrishn@linux.vnet.ibm.com \
    --to=ukrishn@linux.vnet.ibm.com \
    --cc=andrew.donnellan@au1.ibm.com \
    --cc=clombard@linux.vnet.ibm.com \
    --cc=fbarrat@linux.vnet.ibm.com \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=manoj@linux.vnet.ibm.com \
    --cc=martin.petersen@oracle.com \
    --cc=mrochs@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).