Skip to contentSkip to conversation

nvmet: protect sqhd update by a lock

PATCH New James Smart 4 participants 7 messages Oct 13, 2017 – Oct 16, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

In testing target io in read write mix, we did indeed get into cases where sqhd didn't update properly and slowly missed enough updates to shutdown the queue.

Protect the read/modify/set update of sqhd under a lock for coherency.

Signed-off-by James Smart <james.smart at broadcom.com>
---
 drivers/nvme/target/core.c  | 8 +++++++-
 drivers/nvme/target/nvmet.h | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +7 -1
index cfc35cbb6fe2..c753826390d6 100644
@@ -387,11 +387,16 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
 
 static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
 {
+	unsigned long flags;
+
 	if (status)
 		nvmet_set_status(req, status);
 
-	if (req->sq->size)
+	if (req->sq->size) {
+		spin_lock_irqsave(&req->sq->sqhd_lock, flags);
 		req->sq->sqhd = (req->sq->sqhd + 1) % req->sq->size;
+		spin_unlock_irqrestore(&req->sq->sqhd_lock, flags);
+	}
 	req->rsp->sq_head = cpu_to_le16(req->sq->sqhd);
 	req->rsp->sq_id = cpu_to_le16(req->sq->qid);
 	req->rsp->command_id = req->cmd->common.command_id;
@@ -425,6 +430,7 @@ void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
 	sq->size = size;
 
 	ctrl->sqs[qid] = sq;
+	spin_lock_init(&sq->sqhd_lock);
 }
 
 static void nvmet_confirm_sq(struct percpu_ref *ref)
drivers/nvme/target/nvmet.h +1 -0
index ed38b44a7007..bdb904bdf676 100644
@@ -77,6 +77,7 @@ struct nvmet_sq {
 	u16			sqhd;
 	struct completion	free_done;
 	struct completion	confirm_done;
+	spinlock_t		sqhd_lock;
 };
 
 /**
Signature · 1 line
2.13.1
#1 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

To me it looks a bit awkward why you're only protecting the RMW part but not the re-read below.

Could you please give some explanation why this is not needed?

Thanks,
Johannes

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#2 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

Quoted 8 lines · Johannes Thumshirn

To me it looks a bit awkward why you're only protecting the RMW part but not the re-read below.

Could you please give some explanation why this is not needed?

Thanks,
Johannes

Agree, is a little awkward. But works fine. The re-read will contain at least the value that it was updated to under the lock. If it's actually the value of a simultaneous update that is one further, that's actually fine too - as sqhd isn't 1:1 with a cqe. Yes a cqe must contain the sqhd, but sqhd can increment independently from a particuluar cqe. Key is that sqhd itself must increment sanely so that whenever stamped in a cqe it's valid.

I have no problem recutting with the lock around the assignment if desired.

-- james

#3 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

Quoted 8 lines · James Smart

Agree, is a little awkward. But works fine. The re-read will contain at least the value that it was updated to under the lock. If it's actually the value of a simultaneous update that is one further, that's actually fine too
- as sqhd isn't 1:1 with a cqe. Yes a cqe must contain the sqhd, but sqhd can increment independently from a particuluar cqe. Key is that sqhd itself must increment sanely so that whenever stamped in a cqe it's valid.

I have no problem recutting with the lock around the assignment if desired.

This to me sounds like a lock wasn't the right choice but a memory barrier would be needed. But OTOH I'm by no means a locking expert. What I _think_ is going on is, spin_lock_irqsave() is called, all the preempt_disable() and local_irq_save() (which are expensive) do their work and thus stretch timing enough that the desired effect is in place.

But this is all guesswork.

Byte,
Johannes

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#4 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

The real issue, where memory barrier isn't enough, is between the value of the sqhd increment followed by the modulo. Many of a time I've seen interruption in between these points.

As far as assignment of the cqe - as long a sqhd is getting updated properly, such that the cqe will return either the value just set? or a newer one, even if another cqe returns the same newer value, it's fine per the sqhd definition in nvme.

As it's no big deal - I'll repost with the lock moved

-- james

Quoted 20 lines · Johannes Thumshirn
Quoted 8 lines · James Smart

Agree, is a little awkward. But works fine. The re-read will contain at least the value that it was updated to under the lock. If it's actually the value of a simultaneous update that is one further, that's actually fine too
- as sqhd isn't 1:1 with a cqe. Yes a cqe must contain the sqhd, but sqhd can increment independently from a particuluar cqe. Key is that sqhd itself must increment sanely so that whenever stamped in a cqe it's valid.

I have no problem recutting with the lock around the assignment if desired.

This to me sounds like a lock wasn't the right choice but a memory barrier would be needed. But OTOH I'm by no means a locking expert. What I _think_ is going on is, spin_lock_irqsave() is called, all the preempt_disable() and local_irq_save() (which are expensive) do their work and thus stretch timing enough that the desired effect is in place.

But this is all guesswork.

Byte,
Johannes

#5 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

Quoted 10 lines · James Smart

The real issue, where memory barrier isn't enough, is between the value of the sqhd increment followed by the modulo. Many of a time I've seen interruption in between these points.

As far as assignment of the cqe - as long a sqhd is getting updated properly, such that the cqe will return either the value just set? or a newer one, even if another cqe returns the same newer value, it's fine per the sqhd definition in nvme.

As it's no big deal - I'll repost with the lock moved

OK, thanks.

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
maintainer
#6 lore raw

PATCH 1/1 nvmet: protect sqhd update by a lock

In reply to James Smart
Quoted 6 lines

In testing target io in read write mix, we did indeed get into cases where sqhd didn't update properly and slowly missed enough updates to shutdown the queue.

Protect the read/modify/set update of sqhd under a lock for coherency.

How does this fix the issue given that updates are not protected?