Skip to content

Commit a90ac7b

Browse files
igawkeithbusch
authored andcommitted
nvmet-fc: use RCU list iterator for assoc_list
The assoc_list is a RCU protected list, thus use the RCU flavor of list functions. Let's use this opportunity and refactor this code and move the lookup into a helper and give it a descriptive name. Signed-off-by: Daniel Wagner <dwagner@suse.de> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent fe506a7 commit a90ac7b

File tree

1 file changed

+22
-12
lines changed
  • drivers/nvme/target

1 file changed

+22
-12
lines changed

drivers/nvme/target/fc.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,14 +1114,27 @@ nvmet_fc_schedule_delete_assoc(struct nvmet_fc_tgt_assoc *assoc)
11141114
queue_work(nvmet_wq, &assoc->del_work);
11151115
}
11161116

1117+
static bool
1118+
nvmet_fc_assoc_exits(struct nvmet_fc_tgtport *tgtport, u64 association_id)
1119+
{
1120+
struct nvmet_fc_tgt_assoc *a;
1121+
1122+
list_for_each_entry_rcu(a, &tgtport->assoc_list, a_list) {
1123+
if (association_id == a->association_id)
1124+
return true;
1125+
}
1126+
1127+
return false;
1128+
}
1129+
11171130
static struct nvmet_fc_tgt_assoc *
11181131
nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
11191132
{
1120-
struct nvmet_fc_tgt_assoc *assoc, *tmpassoc;
1133+
struct nvmet_fc_tgt_assoc *assoc;
11211134
unsigned long flags;
1135+
bool done;
11221136
u64 ran;
11231137
int idx;
1124-
bool needrandom = true;
11251138

11261139
if (!tgtport->pe)
11271140
return NULL;
@@ -1145,24 +1158,21 @@ nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
11451158
INIT_WORK(&assoc->del_work, nvmet_fc_delete_assoc_work);
11461159
atomic_set(&assoc->terminating, 0);
11471160

1148-
while (needrandom) {
1161+
done = false;
1162+
do {
11491163
get_random_bytes(&ran, sizeof(ran) - BYTES_FOR_QID);
11501164
ran = ran << BYTES_FOR_QID_SHIFT;
11511165

11521166
spin_lock_irqsave(&tgtport->lock, flags);
1153-
needrandom = false;
1154-
list_for_each_entry(tmpassoc, &tgtport->assoc_list, a_list) {
1155-
if (ran == tmpassoc->association_id) {
1156-
needrandom = true;
1157-
break;
1158-
}
1159-
}
1160-
if (!needrandom) {
1167+
rcu_read_lock();
1168+
if (!nvmet_fc_assoc_exits(tgtport, ran)) {
11611169
assoc->association_id = ran;
11621170
list_add_tail_rcu(&assoc->a_list, &tgtport->assoc_list);
1171+
done = true;
11631172
}
1173+
rcu_read_unlock();
11641174
spin_unlock_irqrestore(&tgtport->lock, flags);
1165-
}
1175+
} while (!done);
11661176

11671177
return assoc;
11681178

0 commit comments

Comments
 (0)