Skip to contentSkip to conversation

blk-crypto: pass a gendisk to blk_crypto_sysfs_register

PATCH In review Christoph Hellwig 2 participants 7 messages Nov 5, 2022 – Nov 9, 2022

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

PATCH 1/3 blk-crypto: pass a gendisk to blk_crypto_sysfs_register

Prepare for changes to the block layer sysfs handling by passing the readily available gendisk to blk_crypto_sysfs_register.

---
 block/blk-crypto-internal.h | 4 ++--
 block/blk-crypto-sysfs.c    | 3 ++-
 block/blk-sysfs.c           | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
block/blk-crypto-internal.h +2 -2
index e6818ffaddbf8..32990299dace1 100644
@@ -21,7 +21,7 @@ extern const struct blk_crypto_mode blk_crypto_modes[];
 
 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
 
-int blk_crypto_sysfs_register(struct request_queue *q);
+int blk_crypto_sysfs_register(struct gendisk *disk);
 
 void blk_crypto_sysfs_unregister(struct request_queue *q);
 
@@ -67,7 +67,7 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
 
 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
 
-static inline int blk_crypto_sysfs_register(struct request_queue *q)
+static inline int blk_crypto_sysfs_register(struct gendisk *disk)
 {
 	return 0;
 }
block/blk-crypto-sysfs.c +2 -1
index fd93bd2f33b75..a638a2eecfc89 100644
@@ -126,8 +126,9 @@ static struct kobj_type blk_crypto_ktype = {
  * If the request_queue has a blk_crypto_profile, create the "crypto"
  * subdirectory in sysfs (/sys/block/$disk/queue/crypto/).
  */
-int blk_crypto_sysfs_register(struct request_queue *q)
+int blk_crypto_sysfs_register(struct gendisk *disk)
 {
+	struct request_queue *q = disk->queue;
 	struct blk_crypto_kobj *obj;
 	int err;
 
block/blk-sysfs.c +1 -1
index 02e94c4beff17..440149d0e21b9 100644
@@ -836,7 +836,7 @@ int blk_register_queue(struct gendisk *disk)
 			goto put_dev;
 	}
 
-	ret = blk_crypto_sysfs_register(q);
+	ret = blk_crypto_sysfs_register(disk);
 	if (ret)
 		goto put_dev;
 
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/3 block: untangle request_queue refcounting from sysfs

The kobject embedded into the request_queue is used for the queue directory in sysfs, but that is a child of the gendisks directory and is intimately tied to it. Move this kobject to the gendisk and use a refcount_t in the request_queue for the actual request_queue refcounting that is completely unrelated to the device model.

---
 block/blk-core.c         | 42 +++++++++++++++++----
 block/blk-crypto-sysfs.c |  4 +-
 block/blk-ia-ranges.c    |  3 +-
 block/blk-sysfs.c        | 79 ++++++++++------------------------------
 block/blk.h              |  4 --
 block/bsg.c              | 11 ++++--
 block/elevator.c         |  2 +-
 include/linux/blkdev.h   |  6 +--
 8 files changed, 68 insertions(+), 83 deletions(-)
block/blk-core.c +34 -8
index e9e2bf15cd909..d14317bfdf654 100644
@@ -59,12 +59,12 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
 EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
 EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
 
-DEFINE_IDA(blk_queue_ida);
+static DEFINE_IDA(blk_queue_ida);
 
 /*
  * For queue allocation
  */
-struct kmem_cache *blk_requestq_cachep;
+static struct kmem_cache *blk_requestq_cachep;
 
 /*
  * Controlling structure to kblockd
@@ -252,19 +252,46 @@ void blk_clear_pm_only(struct request_queue *q)
 }
 EXPORT_SYMBOL_GPL(blk_clear_pm_only);
 
+static void blk_free_queue_rcu(struct rcu_head *rcu_head)
+{
+	kmem_cache_free(blk_requestq_cachep,
+			container_of(rcu_head, struct request_queue, rcu_head));
+}
+
+static void blk_free_queue(struct request_queue *q)
+{
+	might_sleep();
+
+	percpu_ref_exit(&q->q_usage_counter);
+
+	if (q->poll_stat)
+		blk_stat_remove_callback(q, q->poll_cb);
+	blk_stat_free_callback(q->poll_cb);
+
+	blk_free_queue_stats(q->stats);
+	kfree(q->poll_stat);
+
+	if (queue_is_mq(q))
+		blk_mq_release(q);
+
+	ida_free(&blk_queue_ida, q->id);
+	call_rcu(&q->rcu_head, blk_free_queue_rcu);
+}
+
 /**
  * blk_put_queue - decrement the request_queue refcount
  * @q: the request_queue structure to decrement the refcount for
  *
- * Decrements the refcount of the request_queue kobject. When this reaches 0
- * we'll have blk_release_queue() called.
+ * Decrements the refcount of the request_queue and free it when the refcount
+ * reaches 0.
  *
  * Context: Any context, but the last reference must not be dropped from
  *          atomic context.
  */
 void blk_put_queue(struct request_queue *q)
 {
-	kobject_put(&q->kobj);
+	if (refcount_dec_and_test(&q->refs))
+		blk_free_queue(q);
 }
 EXPORT_SYMBOL(blk_put_queue);
 
@@ -399,8 +426,7 @@ struct request_queue *blk_alloc_queue(int node_id)
 	INIT_WORK(&q->timeout_work, blk_timeout_work);
 	INIT_LIST_HEAD(&q->icq_list);
 
-	kobject_init(&q->kobj, &blk_queue_ktype);
-
+	refcount_set(&q->refs, 1);
 	mutex_init(&q->debugfs_mutex);
 	mutex_init(&q->sysfs_lock);
 	mutex_init(&q->sysfs_dir_lock);
@@ -445,7 +471,7 @@ bool blk_get_queue(struct request_queue *q)
 {
 	if (unlikely(blk_queue_dying(q)))
 		return false;
-	kobject_get(&q->kobj);
+	refcount_inc(&q->refs);
 	return true;
 }
 EXPORT_SYMBOL(blk_get_queue);
block/blk-crypto-sysfs.c +2 -2
index a638a2eecfc89..e74a918ef9f18 100644
@@ -140,8 +140,8 @@ int blk_crypto_sysfs_register(struct gendisk *disk)
 		return -ENOMEM;
 	obj->profile = q->crypto_profile;
 
-	err = kobject_init_and_add(&obj->kobj, &blk_crypto_ktype, &q->kobj,
-				   "crypto");
+	err = kobject_init_and_add(&obj->kobj, &blk_crypto_ktype,
+				   &disk->queue_kobj, "crypto");
 	if (err) {
 		kobject_put(&obj->kobj);
 		return err;
block/blk-ia-ranges.c +2 -1
index 2bd1d311033b5..2141931ddd37e 100644
@@ -123,7 +123,8 @@ int disk_register_independent_access_ranges(struct gendisk *disk)
 	 */
 	WARN_ON(iars->sysfs_registered);
 	ret = kobject_init_and_add(&iars->kobj, &blk_ia_ranges_ktype,
-				   &q->kobj, "%s", "independent_access_ranges");
+				   &disk->queue_kobj, "%s",
+				   "independent_access_ranges");
 	if (ret) {
 		disk->ia_ranges = NULL;
 		kobject_put(&iars->kobj);
block/blk-sysfs.c +20 -59

146 lines not on this page · raw message

block/blk.h +0 -4
index e85703ae81dd1..a8ac9803fcb36 100644
@@ -26,10 +26,6 @@ struct blk_flush_queue {
 	spinlock_t		mq_flush_lock;
 };
 
-extern struct kmem_cache *blk_requestq_cachep;
-extern struct kobj_type blk_queue_ktype;
-extern struct ida blk_queue_ida;
-
 bool is_flush_rq(struct request *req);
 
 struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,
block/bsg.c +7 -4
index 2ab1351eb0823..8eba57b9bb461 100644
@@ -175,8 +175,10 @@ static void bsg_device_release(struct device *dev)
 
 void bsg_unregister_queue(struct bsg_device *bd)
 {
-	if (bd->queue->kobj.sd)
-		sysfs_remove_link(&bd->queue->kobj, "bsg");
+	struct gendisk *disk = bd->queue->disk;
+
+	if (disk && disk->queue_kobj.sd)
+		sysfs_remove_link(&disk->queue_kobj, "bsg");
 	cdev_device_del(&bd->cdev, &bd->device);
 	put_device(&bd->device);
 }
@@ -216,8 +218,9 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
 	if (ret)
 		goto out_put_device;
 
-	if (q->kobj.sd) {
-		ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg");
+	if (q->disk && q->disk->queue_kobj.sd) {
+		ret = sysfs_create_link(&q->disk->queue_kobj, &bd->device.kobj,
+					"bsg");
 		if (ret)
 			goto out_device_del;
 	}
block/elevator.c +1 -1
index 800e0038be0d7..1d47eecad7c65 100644
@@ -469,7 +469,7 @@ int elv_register_queue(struct request_queue *q, bool uevent)
 
 	lockdep_assert_held(&q->sysfs_lock);
 
-	error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
+	error = kobject_add(&e->kobj, &q->disk->queue_kobj, "iosched");
 	if (!error) {
 		struct elv_fs_entry *attr = e->type->elevator_attrs;
 		if (attr) {
include/linux/blkdev.h +2 -4
index 9188aa3f62595..6e6d172309880 100644
@@ -155,6 +155,7 @@ struct gendisk {
 	unsigned open_partitions;	/* number of open partitions */
 
 	struct backing_dev_info	*bdi;
+	struct kobject queue_kobj;	/* the queue/ directory */
 	struct kobject *slave_dir;
 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
 	struct list_head slave_bdevs;
@@ -430,10 +431,7 @@ struct request_queue {
 
 	struct gendisk		*disk;
 
-	/*
-	 * queue kobject
-	 */
-	struct kobject kobj;
+	refcount_t		refs;
 
 	/*
 	 * mq queue kobject
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 2/3] block: untangle request_queue refcounting from sysfs

block/blk-sysfs.c int blk_register_queue(struct gendisk *disk) · line 776

Quoted 7 lines · Christoph Hellwig
block/blk-sysfs.c +2 -1
@@ -811,7 +772,8 @@ int blk_register_queue(struct gendisk *disk)
 
 	mutex_lock(&q->sysfs_dir_lock);
 
-	ret = kobject_add(&q->kobj, &disk_to_dev(disk)->kobj, "queue");
+	kobject_init(&disk->queue_kobj, &blk_queue_ktype);
+	ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue");

Use kobject_init_and_add()? Also, kobject_put() needs to be called on error.

- Eric

#3 lore raw

Re: [PATCH 2/3] block: untangle request_queue refcounting from sysfs

Quoted 10 lines · Eric Biggers

block/blk-sysfs.c int blk_register_queue(struct gendisk *disk) · line 776

Quoted 7 lines · Christoph Hellwig
block/blk-sysfs.c +2 -1
@@ -811,7 +772,8 @@ int blk_register_queue(struct gendisk *disk)
 
 	mutex_lock(&q->sysfs_dir_lock);
 
-	ret = kobject_add(&q->kobj, &disk_to_dev(disk)->kobj, "queue");
+	kobject_init(&disk->queue_kobj, &blk_queue_ktype);
+	ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue");

Use kobject_init_and_add()?

I never found a good reason to use that over the two calls. It doesn't clear up what is done, and doesn't save code size either.

Quoted 1 line

Also, kobject_put() needs to be called on error.

Indeed. Also for the later error cases.

#4 lore raw

PATCH 3/3 block: mark blk_put_queue as potentially blocking

In reply to Christoph Hellwig

We can't just say that the last reference release may block, as any reference dropped could be the last one. So move the might_sleep() from blk_free_queue to blk_put_queue and update the documentation.

---
 block/blk-core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/blk-core.c +2 -4
index d14317bfdf654..8ab21dd01cd1c 100644
@@ -260,8 +260,6 @@ static void blk_free_queue_rcu(struct rcu_head *rcu_head)
 
 static void blk_free_queue(struct request_queue *q)
 {
-	might_sleep();
-
 	percpu_ref_exit(&q->q_usage_counter);
 
 	if (q->poll_stat)
@@ -285,11 +283,11 @@ static void blk_free_queue(struct request_queue *q)
  * Decrements the refcount of the request_queue and free it when the refcount
  * reaches 0.
  *
- * Context: Any context, but the last reference must not be dropped from
- *          atomic context.
+ * Context: Can sleep.
  */
 void blk_put_queue(struct request_queue *q)
 {
+	might_sleep();
 	if (refcount_dec_and_test(&q->refs))
 		blk_free_queue(q);
 }
Signature · 1 line
2.30.2
#5 lore raw
In reply to Christoph Hellwig
Quoted 15 lines · Christoph Hellwig

Prepare for changes to the block layer sysfs handling by passing the readily available gendisk to blk_crypto_sysfs_register.

---
 block/blk-crypto-internal.h | 4 ++--
 block/blk-crypto-sysfs.c    | 3 ++-
 block/blk-sysfs.c           | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
block/blk-crypto-internal.h +0 -0
index e6818ffaddbf8..32990299dace1 100644

block/blk-crypto-internal.h extern const struct blk_crypto_mode blk_crypto_modes[]; · line 26

Quoted 7 lines
block/blk-crypto-internal.h +1 -1
@@ -21,7 +21,7 @@ extern const struct blk_crypto_mode blk_crypto_modes[];
 
 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
 
-int blk_crypto_sysfs_register(struct request_queue *q);
+int blk_crypto_sysfs_register(struct gendisk *disk);
 
 void blk_crypto_sysfs_unregister(struct request_queue *q);

Shouldn't blk_crypto_sysfs_unregister() be updated to match?

- Eric

#6 lore raw
Quoted 7 lines · Eric Biggers

block/blk-crypto-internal.h extern const struct blk_crypto_mode blk_crypto_modes[]; · line 26

Quoted 5 lines
block/blk-crypto-internal.h +1 -1
-int blk_crypto_sysfs_register(struct request_queue *q);
+int blk_crypto_sysfs_register(struct gendisk *disk);
 
 void blk_crypto_sysfs_unregister(struct request_queue *q);

Shouldn't blk_crypto_sysfs_unregister() be updated to match?

While not strictly required, that would keep the interface symmetric, so yes.