Commit 72511b1d authored by Daniel Wagner's avatar Daniel Wagner Committed by Christoph Hellwig
Browse files

nvmet-fcloop: add ref counting to lport



The fcloop_lport objects live time is controlled by the user interface
add_local_port and del_local_port. nport, rport and tport objects are
pointing to the lport objects but here is no clear tracking. Let's
introduce an explicit ref counter for the lport objects and prepare the
stage for restructuring how lports are used.

Signed-off-by: default avatarDaniel Wagner <wagi@kernel.org>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
parent f22c458f
Loading
Loading
Loading
Loading
+31 −13
Original line number Diff line number Diff line
@@ -208,6 +208,7 @@ struct fcloop_lport {
	struct nvme_fc_local_port *localport;
	struct list_head lport_list;
	struct completion unreg_done;
	refcount_t ref;
};

struct fcloop_lport_priv {
@@ -994,6 +995,27 @@ fcloop_fcp_abort(struct nvme_fc_local_port *localport,
	}
}

static void
fcloop_lport_put(struct fcloop_lport *lport)
{
	unsigned long flags;

	if (!refcount_dec_and_test(&lport->ref))
		return;

	spin_lock_irqsave(&fcloop_lock, flags);
	list_del(&lport->lport_list);
	spin_unlock_irqrestore(&fcloop_lock, flags);

	kfree(lport);
}

static int
fcloop_lport_get(struct fcloop_lport *lport)
{
	return refcount_inc_not_zero(&lport->ref);
}

static void
fcloop_nport_put(struct fcloop_nport *nport)
{
@@ -1017,6 +1039,8 @@ fcloop_localport_delete(struct nvme_fc_local_port *localport)

	/* release any threads waiting for the unreg to complete */
	complete(&lport->unreg_done);

	fcloop_lport_put(lport);
}

static void
@@ -1128,6 +1152,7 @@ fcloop_create_local_port(struct device *dev, struct device_attribute *attr,

		lport->localport = localport;
		INIT_LIST_HEAD(&lport->lport_list);
		refcount_set(&lport->ref, 1);

		spin_lock_irqsave(&fcloop_lock, flags);
		list_add_tail(&lport->lport_list, &fcloop_lports);
@@ -1144,13 +1169,6 @@ fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
	return ret ? ret : count;
}


static void
__unlink_local_port(struct fcloop_lport *lport)
{
	list_del(&lport->lport_list);
}

static int
__wait_localport_unreg(struct fcloop_lport *lport)
{
@@ -1163,8 +1181,6 @@ __wait_localport_unreg(struct fcloop_lport *lport)
	if (!ret)
		wait_for_completion(&lport->unreg_done);

	kfree(lport);

	return ret;
}

@@ -1187,8 +1203,9 @@ fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
	list_for_each_entry(tlport, &fcloop_lports, lport_list) {
		if (tlport->localport->node_name == nodename &&
		    tlport->localport->port_name == portname) {
			if (!fcloop_lport_get(tlport))
				break;
			lport = tlport;
			__unlink_local_port(lport);
			break;
		}
	}
@@ -1198,6 +1215,7 @@ fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
		return -ENOENT;

	ret = __wait_localport_unreg(lport);
	fcloop_lport_put(lport);

	return ret ? ret : count;
}
@@ -1625,17 +1643,17 @@ static void __exit fcloop_exit(void)
	for (;;) {
		lport = list_first_entry_or_null(&fcloop_lports,
						typeof(*lport), lport_list);
		if (!lport)
		if (!lport || !fcloop_lport_get(lport))
			break;

		__unlink_local_port(lport);

		spin_unlock_irqrestore(&fcloop_lock, flags);

		ret = __wait_localport_unreg(lport);
		if (ret)
			pr_warn("%s: Failed deleting local port\n", __func__);

		fcloop_lport_put(lport);

		spin_lock_irqsave(&fcloop_lock, flags);
	}