Enable lock context analysis
Subsystems (12)
v2 cover Enable lock context analysis
Hi Jens,
During the most recent merge window the following patch series has been merged:
[PATCH v5 00/36] Compiler-Based Context- and Locking-Analysis
(https://lore.kernel.org/lkml/[email protected]/). That patch series drops support for verifying lock context annotations with sparse and introduces support for verifying lock context annotations with Clang. The support in Clang for lock context annotation and verification is better than that in sparse. As an example, __cond_acquires() and __guarded_by() are supported by Clang but not by sparse. Hence this patch series that enables lock context analysis for the block layer core and all block drivers.
Please consider this patch series for the next merge window.
Thanks,
Bart.
Changes compared to v1:
- Rebased this patch series on top of Jens' for-next branch.
- Included two patches that split blkg_conf_prep() and blkg_conf_exit().
- Modified how patches are split. Split the block layer core patch into multiple patches and moved the CONTEXT_ANALYSIS := y assignments into the block driver patches.
- Made the new source code comments easier to comprehend.
- Introduced macros in the mq-deadline and Kyber I/O schedulers to make the __acquires() expressions easier to read.
- Removed the changes from this series that are not block layer changes.
Bart Van Assche (26):
block: Annotate the queue limits functions
block: Annotate the block device functions
block/cgroup: Split blkg_conf_prep()
block/cgroup: Split blkg_conf_exit()
block/cgroup: Modify the blkg_conf_open_bdev_frozen() calling
convention
block/crypto: Annotate the crypto functions block/blk-iocost: Add lock context annotations block/blk-mq-debugfs: Improve lock context annotations block/blk-zoned: Add lock context annotations block/ioctl: Add lock context annotations
block/Kyber: Make the lock context annotations compatible with Clang block/mq-deadline: Make the lock context annotations compatible with
Clang
block: Enable lock context analysis
aoe: Add a lock context annotation
drbd: Balance RCU calls in drbd_adm_dump_devices()
drbd: Make the lock context annotations compatible with Clang
loop: Add lock context annotations
mtip32: Enable lock context analysis
nbd: Enable lock context analysis
null_blk: Enable lock context analysis
rbd: Enable lock context analysis
ublk: Enable lock context analysis
xen-blkback: Enable lock context analysis
zram: Enable lock context analysis
rnbd: Enable lock context analysis
block: Enable lock context analysis for all block drivers
block/Makefile | 2 + block/bdev.c | 10 +++- block/bfq-cgroup.c | 11 +++- block/blk-cgroup.c | 82 +++++++++++++--------------- block/blk-cgroup.h | 19 +++++-- block/blk-crypto-profile.c | 2 + block/blk-iocost.c | 77 ++++++++++++++------------ block/blk-iolatency.c | 19 ++++--- block/blk-mq-debugfs.c | 12 ++-- block/blk-throttle.c | 34 +++++++----- block/blk-zoned.c | 1 + block/blk.h | 4 ++ block/ioctl.c | 1 + block/kyber-iosched.c | 7 ++- block/mq-deadline.c | 12 ++-- drivers/block/Makefile | 2 + drivers/block/aoe/Makefile | 2 + drivers/block/aoe/aoecmd.c | 1 + drivers/block/drbd/Makefile | 3 + drivers/block/drbd/drbd_bitmap.c | 26 ++++++--- drivers/block/drbd/drbd_int.h | 88 ++++++++++++++++++------------ drivers/block/drbd/drbd_main.c | 40 ++++++++++---- drivers/block/drbd/drbd_nl.c | 13 +++-- drivers/block/drbd/drbd_receiver.c | 31 ++++++++--- drivers/block/drbd/drbd_req.c | 3 + drivers/block/drbd/drbd_state.c | 2 + drivers/block/drbd/drbd_state.h | 4 -- drivers/block/drbd/drbd_worker.c | 6 +- drivers/block/loop.c | 4 ++ drivers/block/mtip32xx/Makefile | 2 + drivers/block/nbd.c | 3 + drivers/block/null_blk/Makefile | 2 + drivers/block/null_blk/main.c | 7 ++- drivers/block/null_blk/zoned.c | 2 + drivers/block/rbd.c | 8 +++ drivers/block/rnbd/Makefile | 2 + drivers/block/ublk_drv.c | 4 ++ drivers/block/xen-blkback/Makefile | 3 + drivers/block/zram/Makefile | 2 + drivers/block/zram/zcomp.c | 3 +- drivers/block/zram/zcomp.h | 6 +- drivers/block/zram/zram_drv.c | 1 + include/linux/blkdev.h | 9 ++- 43 files changed, 369 insertions(+), 203 deletions(-)
PATCH v2 1/26 block: Annotate the queue limits functions
Let the thread-safety checker verify whether every start of a queue limits update is followed by a call to a function that finishes a queue limits update.
--- include/linux/blkdev.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
include/linux/blkdev.h +6 -3
@@ -1092,15 +1092,17 @@ static inline unsigned int blk_boundary_sectors_left(sector_t offset,*/ static inline struct queue_limits queue_limits_start_update (struct request_queue *q) + __acquires(&q->limits_lock) {mutex_lock (&q->limits_lock);return q->limits; } int queue_limits_commit_update_frozen(struct request_queue *q, -struct queue_limits *lim); + struct queue_limits *lim) __releases(&q->limits_lock); int queue_limits_commit_update(struct request_queue *q, -struct queue_limits *lim); -int queue_limits_set (struct request_queue *q,struct queue_limits *lim); + struct queue_limits *lim) __releases(&q->limits_lock); +int queue_limits_set(struct request_queue *q, struct queue_limits *lim) + __must_not_hold(&q->limits_lock);int blk_validate_limits (struct queue_limits *lim);/**
@@ -1112,6 +1114,7 @@ int blk_validate_limits(struct queue_limits *lim);* starting update. */ static inline void queue_limits_cancel_update (struct request_queue *q) + __releases(&q->limits_lock) {mutex_unlock (&q->limits_lock); }
Re: [PATCH v2 01/26] block: Annotate the queue limits functions
Looks good:
PATCH v2 2/26 block: Annotate the block device functions
In reply to Bart Van AsschePrepare for enabling thread-safety analysis by adding __release() annotations where appropriate. Explicit __release() annotations have been added since Clang does not support adding a __releases() annotation to a function pointer.
--- block/bdev.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
block/bdev.c +8 -2
@@ -312,7 +312,9 @@ int bdev_freeze(struct block_device *bdev)mutex_lock (&bdev->bd_holder_lock);if (bdev->bd_holder_ops && bdev->bd_holder_ops->freeze) { error = bdev->bd_holder_ops->freeze (bdev); +/* bdev->bd_holder_ops->freeze() released bd_holder_lock */ lockdep_assert_not_held (&bdev->bd_holder_lock); +__release (&bdev->bd_holder_lock); }else {mutex_unlock (&bdev->bd_holder_lock); error =sync_blockdev (bdev);
@@ -355,7 +357,9 @@ int bdev_thaw(struct block_device *bdev)mutex_lock (&bdev->bd_holder_lock);if (bdev->bd_holder_ops && bdev->bd_holder_ops->thaw) { error = bdev->bd_holder_ops->thaw (bdev); +/* bdev->bd_holder_ops->freeze() released bd_holder_lock */ lockdep_assert_not_held (&bdev->bd_holder_lock); +__release (&bdev->bd_holder_lock); }else {mutex_unlock (&bdev->bd_holder_lock); }
@@ -1254,9 +1258,11 @@ EXPORT_SYMBOL(lookup_bdev);void bdev_mark_dead (struct block_device *bdev,bool surprise) {mutex_lock (&bdev->bd_holder_lock); -if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) +if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) { bdev->bd_holder_ops->mark_dead (bdev, surprise); -else { +/* bdev->bd_holder_ops->mark_dead() released bd_holder_lock */ +__release (&bdev->bd_holder_lock); + }else {mutex_unlock (&bdev->bd_holder_lock);sync_blockdev (bdev); }
Re: [PATCH v2 02/26] block: Annotate the block device functions
Quoted 35 lines · Bart Van Assche
Prepare for enabling thread-safety analysis by adding
__release()annotations where appropriate. Explicit__release()annotations have been added since Clang does not support adding a__releases()annotation to a function pointer.Signed-off-by Bart Van Assche <[email protected]>--- block/bdev.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)block/bdev.c +4
-0@@ -312,7 +312,9 @@ int bdev_freeze(struct block_device *bdev)mutex_lock (&bdev->bd_holder_lock);if (bdev->bd_holder_ops && bdev->bd_holder_ops->freeze) { error = bdev->bd_holder_ops->freeze (bdev); +/* bdev->bd_holder_ops->freeze() released bd_holder_lock */ lockdep_assert_not_held (&bdev->bd_holder_lock); +__release (&bdev->bd_holder_lock); }else {mutex_unlock (&bdev->bd_holder_lock); error =sync_blockdev (bdev);@@ -355,7 +357,9 @@ int bdev_thaw(struct block_device *bdev)mutex_lock (&bdev->bd_holder_lock);if (bdev->bd_holder_ops && bdev->bd_holder_ops->thaw) { error = bdev->bd_holder_ops->thaw (bdev); +/* bdev->bd_holder_ops->freeze() released bd_holder_lock */ lockdep_assert_not_held (&bdev->bd_holder_lock); +__release (&bdev->bd_holder_lock); }else {mutex_unlock (&bdev->bd_holder_lock); }
block/bdev.c EXPORT_SYMBOL(lookup_bdev); · line 1264
Quoted 9 lines
block/bdev.c +3
-2@@ -1254,9 +1258,11 @@ EXPORT_SYMBOL(lookup_bdev);void bdev_mark_dead (struct block_device *bdev,bool surprise) {mutex_lock (&bdev->bd_holder_lock); -if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) +if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) { bdev->bd_holder_ops->mark_dead (bdev, surprise); -else { +/* bdev->bd_holder_ops->mark_dead() released bd_holder_lock */ +__release (&bdev->bd_holder_lock);
This sounds like ->mark_dead also needs matching annotations. Or can we have a way to annotate the expexcted lock context changes on indirect function calls, as that would be very useful in general.
Re: [PATCH v2 02/26] block: Annotate the block device functions
Quoted 12 lines · Christoph Hellwig
block/bdev.c EXPORT_SYMBOL(lookup_bdev); · line 1264
Quoted 7 lines · Bart Van Assche
mutex_lock(&bdev->bd_holder_lock);block/bdev.c +3
-2-if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) +if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) { bdev->bd_holder_ops->mark_dead (bdev, surprise); -else { +/* bdev->bd_holder_ops->mark_dead() released bd_holder_lock */ +__release (&bdev->bd_holder_lock);This sounds like ->mark_dead also needs matching annotations. Or can we have a way to annotate the expexcted lock context changes on indirect function calls, as that would be very useful in general.
(+Marco)
Agreed that this would be useful.
Marco, is my understanding correct that Clang does not yet support adding lock context annotations to function pointers and hence that adding this support would require modifying the Clang compiler?
Thanks,
Bart.
Re: [PATCH v2 02/26] block: Annotate the block device functions
Quoted 15 lines · Bart Van Assche
Quoted 9 lines · Christoph Hellwig
block/bdev.c EXPORT_SYMBOL(lookup_bdev); · line 1264
Quoted 7 lines · Bart Van Assche
mutex_lock(&bdev->bd_holder_lock);block/bdev.c +3
-2-if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) +if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) { bdev->bd_holder_ops->mark_dead (bdev, surprise); -else { +/* bdev->bd_holder_ops->mark_dead() released bd_holder_lock */ +__release (&bdev->bd_holder_lock);Quoted 3 lines
This sounds like ->mark_dead also needs matching annotations. Or can we have a way to annotate the expexcted lock context changes on indirect function calls, as that would be very useful in general.
Quoted 7 lines
(+Marco)
Agreed that this would be useful.
Marco, is my understanding correct that Clang does not yet support adding lock context annotations to function pointers and hence that adding this support would require modifying the Clang compiler?
No that's not supported (yet). There are various design options, each with their own pros/cons: the one question is if attributes should behave more like type qualifiers in this case (enforces functions to match), or just be a best-effort thing (functions may not match function pointer).
Re: [PATCH v2 02/26] block: Annotate the block device functions
Quoted 5 lines · Marco Elver
No that's not supported (yet). There are various design options, each with their own pros/cons: the one question is if attributes should behave more like type qualifiers in this case (enforces functions to match), or just be a best-effort thing (functions may not match function pointer).
It would be great if lock context annotations would be integrated in the function pointer type because today these annotations are lost if one function pointer is assigned to a function pointer variable.
Maybe there is something that I'm overlooking?
Thanks,
Bart.
Re: [PATCH v2 02/26] block: Annotate the block device functions
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Marco Elver
No that's not supported (yet). There are various design options, each with their own pros/cons: the one question is if attributes should behave more like type qualifiers in this case (enforces functions to match), or just be a best-effort thing (functions may not match function pointer).
Quoted 5 lines
It would be great if lock context annotations would be integrated in the function pointer type because today these annotations are lost if one function pointer is assigned to a function pointer variable.
Maybe there is something that I'm overlooking?
The system was designed with attributes (for optionality). Changing it to treat attributes more like type qualifiers will beg the
question: "why not make it a real type qualifier?" from the Clang maintainers. And getting that right is very tricky, as it's a fundamental and rather intrusive change.
I can explore the attribute based design, but if that's rejected, I think we'll have to leave it at that for now.
PATCH v2 3/26 block/cgroup: Split blkg_conf_prep()
In reply to Bart Van AsscheMove the blkg_conf_open_bdev() call out of blkg_conf_prep() to make it possible to add lock context annotations to blkg_conf_prep(). Change an if-statement in blkg_conf_open_bdev() into a WARN_ON_ONCE() call. Export blkg_conf_open_bdev() because it is called by the BFQ I/O scheduler and the BFQ I/O scheduler may be built as a kernel module.
--- block/bfq-cgroup.c | 4 ++++ block/blk-cgroup.c | 16 ++++++---------- block/blk-iocost.c | 4 ++++ 3 files changed, 14 insertions(+), 10 deletions(-)
block/bfq-cgroup.c +4 -0
@@ -1051,6 +1051,10 @@ static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,blkg_conf_init (&ctx, buf); + ret =blkg_conf_open_bdev (&ctx); +if (ret) + goto out; + ret =blkg_conf_prep (blkcg, &blkcg_policy_bfq, &ctx);if (ret) goto out;
block/blk-cgroup.c +6 -10
@@ -771,10 +771,7 @@ EXPORT_SYMBOL_GPL(blkg_conf_init);* @ctx->input and get and store the matching bdev in @ctx->bdev. @ctx->body is * set to point past the device node prefix. * -* This function may be called multiple times on @ctx and the extra calls become -* NOOPs. blkg_conf_prep() implicitly calls this function. Use this function -* explicitly if bdev access is needed without resolving the blkcg / policy part -* of @ctx->input. Returns -errno on error. +* Returns: -errno on error. */ int blkg_conf_open_bdev (struct blkg_conf_ctx *ctx) {
@@ -783,8 +780,7 @@ int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)struct block_device *bdev;int key_len; -if (ctx->bdev) -return 0 ; +WARN_ON_ONCE (ctx->bdev);if (sscanf (input,"%u:%u%n" , &major, &minor, &key_len) !=2 )return -EINVAL ;
@@ -813,6 +809,8 @@ int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx) ctx->bdev = bdev;return 0 ; } +EXPORT_SYMBOL_GPL (blkg_conf_open_bdev); +/* * Similar to blkg_conf_open_bdev, but additionally freezes the queue, * ensures the correct locking order between freeze queue and q->rq_qos_mutex.
@@ -857,7 +855,7 @@ unsigned long __must_check blkg_conf_open_bdev_frozen(struct blkg_conf_ctx *ctx)* following MAJ:MIN, @ctx->bdev points to the target block device and * @ctx->blkg to the blkg being configured. * -* blkg_conf_open_bdev() may be called on @ctx beforehand. On success, this +* blkg_conf_open_bdev() must be called on @ctx beforehand. On success, this * function returns with queue lock held and must be followed by * blkg_conf_exit(). */
@@ -870,9 +868,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,struct blkcg_gq *blkg;int ret; - ret =blkg_conf_open_bdev (ctx); -if (ret) -return ret; +WARN_ON_ONCE (!ctx->bdev); disk = ctx->bdev->bd_disk; q = disk->queue;
block/blk-iocost.c +4 -0
@@ -3129,6 +3129,10 @@ static ssize_t ioc_weight_write(struct kernfs_open_file *of, char *buf,blkg_conf_init (&ctx, buf); + ret =blkg_conf_open_bdev (&ctx); +if (ret) + goto err; + ret =blkg_conf_prep (blkcg, &blkcg_policy_iocost, &ctx);if (ret) goto err;
Re: [PATCH v2 03/26] block/cgroup: Split blkg_conf_prep()
Quoted 46 lines · Bart Van Assche
Move the
blkg_conf_open_bdev()call out ofblkg_conf_prep()to make it possible to add lock context annotations toblkg_conf_prep(). Change an if-statement inblkg_conf_open_bdev()into aWARN_ON_ONCE()call. Exportblkg_conf_open_bdev()because it is called by the BFQ I/O scheduler and the BFQ I/O scheduler may be built as a kernel module.Cc Tejun Heo <[email protected]>Signed-off-by Bart Van Assche <[email protected]>--- block/bfq-cgroup.c | 4 ++++ block/blk-cgroup.c | 16 ++++++---------- block/blk-iocost.c | 4 ++++ 3 files changed, 14 insertions(+), 10 deletions(-)block/bfq-cgroup.c +4
-0@@ -1051,6 +1051,10 @@ static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,blkg_conf_init (&ctx, buf); + ret =blkg_conf_open_bdev (&ctx); +if (ret) + goto out; + ret =blkg_conf_prep (blkcg, &blkcg_policy_bfq, &ctx);if (ret) goto out;block/blk-cgroup.c +1
-4@@ -771,10 +771,7 @@ EXPORT_SYMBOL_GPL(blkg_conf_init);* @ctx->input and get and store the matching bdev in @ctx->bdev. @ctx->body is * set to point past the device node prefix. * -* This function may be called multiple times on @ctx and the extra calls become -* NOOPs. blkg_conf_prep() implicitly calls this function. Use this function -* explicitly if bdev access is needed without resolving the blkcg / policy part -* of @ctx->input. Returns -errno on error. +* Returns: -errno on error. */ int blkg_conf_open_bdev (struct blkg_conf_ctx *ctx) {
block/blk-cgroup.c int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx) · line 783
Quoted 6 lines
block/blk-cgroup.c +1
-2@@ -783,8 +780,7 @@ int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)struct block_device *bdev;int key_len; -if (ctx->bdev) -return 0 ; +WARN_ON_ONCE (ctx->bdev);
Return on this case?
Otherwise looks good:
PATCH v2 4/26 block/cgroup: Split blkg_conf_exit()
In reply to Bart Van AsscheSplit blkg_conf_exit() into blkg_conf_unprep() and blkg_conf_close_bdev() because blkg_conf_exit() is not compatible with the Clang thread-safety annotations. Remove blkg_conf_exit(). Rename blkg_conf_exit_frozen() into blkg_conf_close_bdev_frozen(). Add thread-safety annotations to the new functions.
--- block/bfq-cgroup.c | 9 ++++-- block/blk-cgroup.c | 58 ++++++++++++++++++------------------- block/blk-cgroup.h | 15 +++++++--- block/blk-iocost.c | 67 +++++++++++++++++++++---------------------- block/blk-iolatency.c | 19 ++++++------ block/blk-throttle.c | 34 +++++++++++++--------- 6 files changed, 108 insertions(+), 94 deletions(-)
block/bfq-cgroup.c +6 -3
@@ -1053,11 +1053,11 @@ static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of, ret =blkg_conf_open_bdev (&ctx);if (ret) - goto out; +return ret; ret =blkg_conf_prep (blkcg, &blkcg_policy_bfq, &ctx);if (ret) - goto out; + goto close_bdev;if (sscanf (ctx.body,"%llu" , &v) ==1 ) {/* require "default" on dfl */
@@ -1078,8 +1078,11 @@ static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,bfq_group_set_weight (bfqg, bfqg->entity.weight, v); ret =0 ; } + out: -blkg_conf_exit (&ctx); +blkg_conf_unprep (&ctx); +close_bdev: +blkg_conf_close_bdev (&ctx);return ret ?: nbytes; }
block/blk-cgroup.c +28 -30
96 lines not on this page · raw message
block/blk-cgroup.h +11 -4
@@ -218,12 +218,19 @@ struct blkg_conf_ctx { };void blkg_conf_init (struct blkg_conf_ctx *ctx,char *input); -int blkg_conf_open_bdev (struct blkg_conf_ctx *ctx); +int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx) + __cond_acquires(0, &ctx->bdev->bd_queue->rq_qos_mutex);unsigned long blkg_conf_open_bdev_frozen (struct blkg_conf_ctx *ctx); int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, -struct blkg_conf_ctx *ctx); -void blkg_conf_exit (struct blkg_conf_ctx *ctx); -void blkg_conf_exit_frozen (struct blkg_conf_ctx *ctx,unsigned long memflags); + struct blkg_conf_ctx *ctx) + __cond_acquires(0, &ctx->bdev->bd_disk->queue->queue_lock); +void blkg_conf_unprep(struct blkg_conf_ctx *ctx) + __releases(ctx->bdev->bd_disk->queue->queue_lock); +void blkg_conf_close_bdev(struct blkg_conf_ctx *ctx) + __releases(&ctx->bdev->bd_queue->rq_qos_mutex); +void blkg_conf_close_bdev_frozen(struct blkg_conf_ctx *ctx, + unsigned long memflags) + __releases(&ctx->bdev->bd_queue->rq_qos_mutex);/** * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
block/blk-iocost.c +32 -35
161 lines not on this page · raw message
block/blk-iolatency.c +11 -8
@@ -840,7 +840,7 @@ static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf, ret =blkg_conf_open_bdev (&ctx);if (ret) - goto out; +return ret;/* * blk_iolatency_init() may fail after rq_qos_add() succeeds which can
@@ -850,11 +850,11 @@ static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf,if (!iolat_rq_qos (ctx.bdev->bd_queue)) ret =blk_iolatency_init (ctx.bdev->bd_disk);if (ret) - goto out; + goto close_bdev; ret =blkg_conf_prep (blkcg, &blkcg_policy_iolatency, &ctx);if (ret) - goto out; + goto close_bdev; iolat =blkg_to_lat (ctx.blkg); p = ctx.body;
@@ -865,7 +865,7 @@ static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf,char val[21 ];/* 18446744073709551616 */ if (sscanf (tok,"%15[^=]=%20s" , key, val) !=2 ) - goto out; + goto unprep;if (!strcmp (key,"target" )) {u64 v;
@@ -875,9 +875,9 @@ static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf,else if (sscanf (val,"%llu" , &v) ==1 ) lat_val = v *NSEC_PER_USEC ;else - goto out; + goto unprep; }else { - goto out; + goto unprep; } }
@@ -889,8 +889,11 @@ static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf,if (oldval != iolat->min_lat_nsec)iolatency_clear_scaling (blkg); ret =0 ; -out: -blkg_conf_exit (&ctx); + +unprep: +blkg_conf_unprep (&ctx); +close_bdev: +blkg_conf_close_bdev (&ctx);return ret ?: nbytes; }
block/blk-throttle.c +20 -14
@@ -1353,21 +1353,21 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of, ret =blkg_conf_open_bdev (&ctx);if (ret) - goto out_finish; +return ret;if (!blk_throtl_activated (ctx.bdev->bd_queue)) { ret =blk_throtl_init (ctx.bdev->bd_disk);if (ret) - goto out_finish; + goto close_bdev; } ret =blkg_conf_prep (blkcg, &blkcg_policy_throtl, &ctx);if (ret) - goto out_finish; + goto close_bdev; ret = -EINVAL ;if (sscanf (ctx.body,"%llu" , &v) !=1 ) - goto out_finish; + goto unprep;if (!v) v =U64_MAX ;
@@ -1381,8 +1381,12 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,tg_conf_updated (tg, false); ret =0 ; -out_finish: -blkg_conf_exit (&ctx); + +unprep: +blkg_conf_unprep (&ctx); + +close_bdev: +blkg_conf_close_bdev (&ctx);return ret ?: nbytes; }
@@ -1537,17 +1541,17 @@ static ssize_t tg_set_limit(struct kernfs_open_file *of, ret =blkg_conf_open_bdev (&ctx);if (ret) - goto out_finish; +return ret;if (!blk_throtl_activated (ctx.bdev->bd_queue)) { ret =blk_throtl_init (ctx.bdev->bd_disk);if (ret) - goto out_finish; + goto close_bdev; } ret =blkg_conf_prep (blkcg, &blkcg_policy_throtl, &ctx);if (ret) - goto out_finish; + goto close_bdev; tg =blkg_to_tg (ctx.blkg);tg_update_carryover (tg);
@@ -1573,11 +1577,11 @@ static ssize_t tg_set_limit(struct kernfs_open_file *of, p = tok;strsep (&p,"=" );if (!p || (sscanf (p,"%llu" , &val) !=1 &&strcmp (p,"max" ))) - goto out_finish; + goto unprep; ret = -ERANGE ;if (!val) - goto out_finish; + goto unprep; ret = -EINVAL ;if (!strcmp (tok,"rbps" ))
@@ -1589,7 +1593,7 @@ static ssize_t tg_set_limit(struct kernfs_open_file *of,else if (!strcmp (tok,"wiops" )) v[3 ] =min_t (u64, val,UINT_MAX );else - goto out_finish; + goto unprep; } tg->bps[READ ] = v[0 ];
@@ -1599,8 +1603,10 @@ static ssize_t tg_set_limit(struct kernfs_open_file *of,tg_conf_updated (tg, false); ret =0 ; -out_finish: -blkg_conf_exit (&ctx); +unprep: +blkg_conf_unprep (&ctx); +close_bdev: +blkg_conf_close_bdev (&ctx);return ret ?: nbytes; }
Re: [PATCH v2 04/26] block/cgroup: Split blkg_conf_exit()
Looks good:
PATCH v2 5/26 block/cgroup: Modify the blkg_conf_open_bdev_frozen() calling convention
In reply to Bart Van AsscheInstead of returning either a negative error number or a positive memory flags value, pass a memory flags pointer as an argument and return zero upon success. This patch prepares for adding thread-safety annotations by changing the calling convention into something that is supported by __cond_acquires().
--- block/blk-cgroup.c | 12 +++++------- block/blk-cgroup.h | 4 +++- block/blk-iocost.c | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-)
block/blk-cgroup.c +5 -7
@@ -815,14 +815,12 @@ EXPORT_SYMBOL_GPL(blkg_conf_open_bdev);* Similar to blkg_conf_open_bdev, but additionally freezes the queue, * ensures the correct locking order between freeze queue and q->rq_qos_mutex. * -* This function returns negative error on failure. On success it returns -* memflags which must be saved and later passed to -* blkg_conf_close_bdev_frozen() for restoring the memalloc scope. +* Returns: a negative error on failure or zero if successful. */ -unsigned long __must_check blkg_conf_open_bdev_frozen (struct blkg_conf_ctx *ctx) +int blkg_conf_open_bdev_frozen (struct blkg_conf_ctx *ctx, +unsigned long *memflags) {int ret; -unsigned long memflags;if (ctx->bdev)return -EINVAL ;
@@ -838,10 +836,10 @@ unsigned long __must_check blkg_conf_open_bdev_frozen(struct blkg_conf_ctx *ctx)*/ mutex_unlock (&ctx->bdev->bd_queue->rq_qos_mutex); - memflags =blk_mq_freeze_queue (ctx->bdev->bd_queue); + *memflags =blk_mq_freeze_queue (ctx->bdev->bd_queue);mutex_lock (&ctx->bdev->bd_queue->rq_qos_mutex); -return memflags; +return 0 ; }/**
block/blk-cgroup.h +3 -1
@@ -220,7 +220,9 @@ struct blkg_conf_ctx {void blkg_conf_init (struct blkg_conf_ctx *ctx,char *input); int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx) __cond_acquires(0, &ctx->bdev->bd_queue->rq_qos_mutex); -unsigned long blkg_conf_open_bdev_frozen (struct blkg_conf_ctx *ctx); +int blkg_conf_open_bdev_frozen(struct blkg_conf_ctx *ctx, + unsigned long *memflags) + __cond_acquires(0, &ctx->bdev->bd_queue->rq_qos_mutex); int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, struct blkg_conf_ctx *ctx) __cond_acquires(0, &ctx->bdev->bd_disk->queue->queue_lock);
block/blk-iocost.c +3 -3
@@ -3232,9 +3232,9 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,blkg_conf_init (&ctx, input); - memflags =blkg_conf_open_bdev_frozen (&ctx); -if (IS_ERR_VALUE (memflags)) -return memflags; + ret =blkg_conf_open_bdev_frozen (&ctx, &memflags); +if (ret) +return ret; body = ctx.body; disk = ctx.bdev->bd_disk;
Re: [PATCH v2 05/26] block/cgroup: Modify the blkg_conf_open_bdev_frozen() calling convention
Quoted 5 lines · Bart Van Assche
Instead of returning either a negative error number or a positive memory flags value, pass a memory flags pointer as an argument and return zero upon success. This patch prepares for adding thread-safety annotations by changing the calling convention into something that is supported by
__cond_acquires().
Looking at this code it seems like a really messy API. It seems like blkg_conf_open_bdev_frozen and blkg_conf_exit_frozen should just be open coded in the only caller using the lower-level helpers you just added earlier.
PATCH v2 6/26 block/crypto: Annotate the crypto functions
In reply to Bart Van AsscheAdd the lock context annotations required for Clang's thread-safety analysis.
--- block/blk-crypto-profile.c | 2 ++ 1 file changed, 2 insertions(+)
block/blk-crypto-profile.c +2 -0
@@ -43,6 +43,7 @@ struct blk_crypto_keyslot { };static inline void blk_crypto_hw_enter (struct blk_crypto_profile *profile) + __acquires(&profile->lock) {/* * Calling into the driver requires profile->lock held and the device
@@ -55,6 +56,7 @@ static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile) }static inline void blk_crypto_hw_exit (struct blk_crypto_profile *profile) + __releases(&profile->lock) {up_write (&profile->lock);if (profile->dev)
Re: [PATCH v2 06/26] block/crypto: Annotate the crypto functions
Looks good:
PATCH v2 7/26 block/blk-iocost: Add lock context annotations
In reply to Bart Van AsscheSince iocg_lock() and iocg_unlock() both use conditional locking, annotate both with __no_context_analysis and use token_context_lock() to introduce a new lock context.
--- block/blk-iocost.c | 6 ++++++ 1 file changed, 6 insertions(+)
block/blk-iocost.c +6 -0
@@ -727,7 +727,11 @@ static void iocg_commit_bio(struct ioc_gq *iocg, struct bio *bio,put_cpu_ptr (gcs); } +token_context_lock (ioc_lock); +static void iocg_lock (struct ioc_gq *iocg,bool lock_ioc,unsigned long *flags) +__acquires (ioc_lock) + __no_context_analysis/* conditional locking */ {if (lock_ioc) {spin_lock_irqsave (&iocg->ioc->lock, *flags);
@@ -738,6 +742,8 @@ static void iocg_lock(struct ioc_gq *iocg, bool lock_ioc, unsigned long *flags) }static void iocg_unlock (struct ioc_gq *iocg,bool unlock_ioc,unsigned long *flags) +__releases (ioc_lock) + __no_context_analysis/* conditional locking */ {if (unlock_ioc) {spin_unlock (&iocg->waitq.lock);
Re: [PATCH v2 07/26] block/blk-iocost: Add lock context annotations
Quoted 3 lines · Bart Van Assche
Since
iocg_lock()andiocg_unlock()both use conditional locking, annotate both with__no_context_analysisand usetoken_context_lock()to introduce a new lock context.
Looking at the two callers it seems like these helpers should simply go away in favour of refactoring the callers. iocg_waitq_timer_fn only calls a single function inside the lock, which can be turned into a conditional trivially, and ioc_rqos_throttle does basically totally different things based on the same paramter passed as the lock_ioc argument to iocg_lock/iocg_unlock.
Re: [PATCH v2 07/26] block/blk-iocost: Add lock context annotations
Quoted 3 lines · Christoph Hellwig
and
ioc_rqos_throttledoes basically totally
different things based on the same paramter passed as thelock_iocargument to iocg_lock/iocg_unlock.
Hi Christoph,
I looked into splitting ioc_rqos_throttle() into two functions: the existing code without the code protected by iocg_lock() / iocg_unlock() and a new function with all the code protected by iocg_lock() and iocg_unlock(). That new function can tell ioc_rqos_throttle() how to proceed via its return value. However, the number of parameters of that new function is large. Almost all local variables declared in ioc_rqos_throttle() would have to be passed to that new function. Hence, I prefer not to modify ioc_rqos_throttle().
Thanks,
Bart.
PATCH v2 8/26 block/blk-mq-debugfs: Improve lock context annotations
In reply to Bart Van AsscheMake the existing lock context annotations compatible with Clang. Add the lock context annotations that are missing.
--- block/blk-mq-debugfs.c | 12 ++++++------ block/blk.h | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-)
block/blk-mq-debugfs.c +6 -6
@@ -20,7 +20,7 @@ static int queue_poll_stat_show(void *data, struct seq_file *m) }static void *queue_requeue_list_start (struct seq_file *m,loff_t *pos) - __acquires(&q->requeue_lock) + __acquires(&((struct request_queue *)m->private)->requeue_lock) {struct request_queue *q = m->private;
@@ -36,7 +36,7 @@ static void *queue_requeue_list_next(struct seq_file *m, void *v, loff_t *pos) }static void queue_requeue_list_stop (struct seq_file *m,void *v) - __releases(&q->requeue_lock) + __releases(&((struct request_queue *)m->private)->requeue_lock) {struct request_queue *q = m->private;
@@ -298,7 +298,7 @@ int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)EXPORT_SYMBOL_GPL (blk_mq_debugfs_rq_show);static void *hctx_dispatch_start (struct seq_file *m,loff_t *pos) - __acquires(&hctx->lock) + __acquires(&((struct blk_mq_hw_ctx *)m->private)->lock) {struct blk_mq_hw_ctx *hctx = m->private;
@@ -314,7 +314,7 @@ static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos) }static void hctx_dispatch_stop (struct seq_file *m,void *v) - __releases(&hctx->lock) + __releases(&((struct blk_mq_hw_ctx *)m->private)->lock) {struct blk_mq_hw_ctx *hctx = m->private;
@@ -486,7 +486,7 @@ static int hctx_dispatch_busy_show(void *data, struct seq_file *m)#define CTX_RQ_SEQ_OPS (name, type) \ static void *ctx_##name##_rq_list_start(struct seq_file *m, loff_t *pos) \ - __acquires(&ctx->lock) \ + __acquires(&((struct blk_mq_ctx *)m->private)->lock) \ { \ struct blk_mq_ctx *ctx = m->private; \ \
@@ -503,7 +503,7 @@ static void *ctx_##name##_rq_list_next(struct seq_file *m, void *v, \ } \ \ static void ctx_##name##_rq_list_stop(struct seq_file *m, void *v) \ - __releases(&ctx->lock) \ + __releases(&((struct blk_mq_ctx *)m->private)->lock) \ { \ struct blk_mq_ctx *ctx = m->private; \ \
block/blk.h +4 -0
@@ -733,16 +733,19 @@ static inline void blk_unfreeze_release_lock(struct request_queue *q)* reclaim from triggering block I/O. */ static inline void blk_debugfs_lock_nomemsave (struct request_queue *q) + __acquires(&q->debugfs_mutex) {mutex_lock (&q->debugfs_mutex); }static inline void blk_debugfs_unlock_nomemrestore (struct request_queue *q) + __releases(&q->debugfs_mutex) {mutex_unlock (&q->debugfs_mutex); }static inline unsigned int __must_check blk_debugfs_lock (struct request_queue *q) + __acquires(&q->debugfs_mutex) {unsigned int memflags =memalloc_noio_save ();
@@ -752,6 +755,7 @@ static inline unsigned int __must_check blk_debugfs_lock(struct request_queue *qstatic inline void blk_debugfs_unlock (struct request_queue *q,unsigned int memflags) + __releases(&q->debugfs_mutex) {blk_debugfs_unlock_nomemrestore (q);memalloc_noio_restore (memflags);
Re: [PATCH v2 08/26] block/blk-mq-debugfs: Improve lock context annotations
Looks fine, althought I'd still love to find a way to avoid all these open coded dereferences of the private data pointers using casts.
Re: [PATCH v2 08/26] block/blk-mq-debugfs: Improve lock context annotations
Quoted 2 lines · Christoph Hellwig
Looks fine, althought I'd still love to find a way to avoid all these open coded dereferences of the private data pointers using casts.
A possible solution is to introduce new single-line functions that perform these conversions. Please let me know if you agree with making this change.
Thanks,
Bart.
PATCH v2 9/26 block/blk-zoned: Add lock context annotations
In reply to Bart Van AsscheAdd the lock context annotations required for Clang's thread-safety analysis.
--- block/blk-zoned.c | 1 + 1 file changed, 1 insertion(+)
block/blk-zoned.c +1 -0
@@ -439,6 +439,7 @@ static int blkdev_truncate_zone_range(struct block_device *bdev,*/ int blkdev_zone_mgmt_ioctl (struct block_device *bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg) + __cond_acquires(0, bdev->bd_mapping->host->i_rwsem) {void __user *argp = (void __user *)arg;struct blk_zone_range zrange;
Re: [PATCH v2 09/26] block/blk-zoned: Add lock context annotations
Review
Quoted 6 lines · Bart Van Assche
int blkdev_zone_mgmt_ioctl(struct
block_device*bdev,blk_mode_tmode,(quoted diff) +1
-0unsigned int cmd, unsigned long arg) + __cond_acquires(0, bdev->bd_mapping->host->i_rwsem) { void __user *argp = (void __user *)arg; struct blk_zone_range zrange;
This looks wrong, the lock is just conditionally taken inside the function and dropped again. This cleanup should fix the lock annotation issues:
---
From 99e701882a9026f56cf6940d858e92ce51d86638 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Thu, 26 Mar 2026 07:37:43 +0100
Subject: block: refactor blkdev_zone_mgmt_ioctl
Split the zone reset case into a separate helper so that the conditional locking goes away.
--- block/blk-zoned.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-)
block/blk-zoned.c +18 -23
@@ -412,20 +412,32 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,return 0 ; } -static int blkdev_truncate_zone_range (struct block_device *bdev, -blk_mode_t mode,const struct blk_zone_range *zrange) +static int blkdev_reset_zone (struct block_device *bdev,blk_mode_t mode, +struct blk_zone_range *zrange) {loff_t start, end; +int ret = -EINVAL ; +inode_lock (bdev->bd_mapping->host); +filemap_invalidate_lock (bdev->bd_mapping);if (zrange->sector + zrange->nr_sectors <= zrange->sector || zrange->sector + zrange->nr_sectors >get_capacity (bdev->bd_disk))/* Out of range */ -return -EINVAL ; + goto out_unlock; start = zrange->sector <<SECTOR_SHIFT ; end = ((zrange->sector + zrange->nr_sectors) <<SECTOR_SHIFT ) -1 ; -return truncate_bdev_range (bdev, mode, start, end); + ret =truncate_bdev_range (bdev, mode, start, end); +if (ret) + goto out_unlock; + + ret =blkdev_zone_mgmt (bdev,REQ_OP_ZONE_RESET , zrange->sector, + zrange->nr_sectors); +out_unlock: +filemap_invalidate_unlock (bdev->bd_mapping); +inode_unlock (bdev->bd_mapping->host); +return ret; }/*
@@ -438,7 +450,6 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,void __user *argp = (void __user *)arg;struct blk_zone_range zrange;enum req_op op; -int ret;if (!argp)return -EINVAL ;
@@ -454,15 +465,7 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,switch (cmd) {case BLKRESETZONE : - op =REQ_OP_ZONE_RESET ; - -/* Invalidate the page cache, including dirty pages. */ -inode_lock (bdev->bd_mapping->host); -filemap_invalidate_lock (bdev->bd_mapping); - ret =blkdev_truncate_zone_range (bdev, mode, &zrange); -if (ret) - goto fail; -break ; +return blkdev_reset_zone (bdev, mode, &zrange);case BLKOPENZONE : op =REQ_OP_ZONE_OPEN ;break ;
@@ -476,15 +479,7 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,return -ENOTTY ; } - ret =blkdev_zone_mgmt (bdev, op, zrange.sector, zrange.nr_sectors); - -fail: -if (cmd ==BLKRESETZONE ) { -filemap_invalidate_unlock (bdev->bd_mapping); -inode_unlock (bdev->bd_mapping->host); - } - -return ret; +return blkdev_zone_mgmt (bdev, op, zrange.sector, zrange.nr_sectors); }static bool disk_zone_is_last (struct gendisk *disk,struct blk_zone *zone)
Signature · 1 line
2.47.3
PATCH v2 10/26 block/ioctl: Add lock context annotations
In reply to Bart Van AsscheAdd a lock context annotation that is required for Clang's thread-safety analysis.
--- block/ioctl.c | 1 + 1 file changed, 1 insertion(+)
block/ioctl.c +1 -0
@@ -518,6 +518,7 @@ static int blkdev_pr_read_reservation(struct block_device *bdev,static int blkdev_flushbuf (struct block_device *bdev,unsigned cmd,unsigned long arg) + __cond_acquires(0, bdev->bd_holder_lock) {if (!capable (CAP_SYS_ADMIN ))return -EACCES ;
Re: [PATCH v2 10/26] block/ioctl: Add lock context annotations
Quoted 2 lines · Bart Van Assche
Add a lock context annotation that is required for Clang's thread-safety analysis.
This doesn't explain much. The problem seems to be that ->synmc can drop the holder lock. I think we need a proper way to identify how function pointers acquire or drop locks as said before instead of using a big, misleading and incorrectly documented hammer like this.
PATCH v2 11/26 block/Kyber: Make the lock context annotations compatible with Clang
In reply to Bart Van AsschePrepare for enabling lock context analysis.
--- block/kyber-iosched.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
block/kyber-iosched.c +5 -2
@@ -882,6 +882,9 @@ static const struct elv_fs_entry kyber_sched_attrs[] = { };#undef KYBER_LAT_ATTR +#define HCTX_FROM_SEQ_FILE (m) ((struct blk_mq_hw_ctx *)(m)->private) +#define KYBER_HCTX_DATA (hctx) ((struct kyber_hctx_data *)(hctx)->sched_data) +#ifdef CONFIG_BLK_DEBUG_FS #define KYBER_DEBUGFS_DOMAIN_ATTRS (domain, name) \ static int kyber_##name##_tokens_show(void *data, struct seq_file *m) \
@@ -894,7 +897,7 @@ static int kyber_##name##_tokens_show(void *data, struct seq_file *m) \ } \ \ static void *kyber_##name##_rqs_start(struct seq_file *m, loff_t *pos) \ - __acquires(&khd->lock) \ + __acquires(&KYBER_HCTX_DATA(HCTX_FROM_SEQ_FILE(m))->lock) \ { \ struct blk_mq_hw_ctx *hctx = m->private; \ struct kyber_hctx_data *khd = hctx->sched_data; \
@@ -913,7 +916,7 @@ static void *kyber_##name##_rqs_next(struct seq_file *m, void *v, \ } \ \ static void kyber_##name##_rqs_stop(struct seq_file *m, void *v) \ - __releases(&khd->lock) \ + __releases(&KYBER_HCTX_DATA(HCTX_FROM_SEQ_FILE(m))->lock) \ { \ struct blk_mq_hw_ctx *hctx = m->private; \ struct kyber_hctx_data *khd = hctx->sched_data; \
Re: [PATCH v2 11/26] block/Kyber: Make the lock context annotations compatible with Clang
Kyber in the subject seem incorrectly capitalized.
Quoted 1 line · Bart Van Assche
Prepare for enabling lock context analysis.
This doesn't say anything.
PATCH v2 12/26 block/mq-deadline: Make the lock context annotations compatible with Clang
In reply to Bart Van AsschePrepare for enabling lock context analysis.
--- block/mq-deadline.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
block/mq-deadline.c +8 -4
@@ -794,11 +794,15 @@ static const struct elv_fs_entry deadline_attrs[] = {__ATTR_NULL }; +#define RQ_FROM_SEQ_FILE (m) ((struct request_queue *)(m)->private) +#define DD_DATA_FROM_RQ (rq) \ + ((struct deadline_data *)(rq)->elevator->elevator_data) +#ifdef CONFIG_BLK_DEBUG_FS #define DEADLINE_DEBUGFS_DDIR_ATTRS (prio, data_dir, name) \ static void *deadline_##name##_fifo_start(struct seq_file *m, \ loff_t *pos) \ - __acquires(&dd->lock) \ + __acquires(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) \ { \ struct request_queue *q = m->private; \ struct deadline_data *dd = q->elevator->elevator_data; \
@@ -819,7 +823,7 @@ static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \ } \ \ static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \ - __releases(&dd->lock) \ + __releases(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) \ { \ struct request_queue *q = m->private; \ struct deadline_data *dd = q->elevator->elevator_data; \
@@ -921,7 +925,7 @@ static int dd_owned_by_driver_show(void *data, struct seq_file *m) }static void *deadline_dispatch_start (struct seq_file *m,loff_t *pos) - __acquires(&dd->lock) + __acquires(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) {struct request_queue *q = m->private;struct deadline_data *dd = q->elevator->elevator_data;
@@ -939,7 +943,7 @@ static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos) }static void deadline_dispatch_stop (struct seq_file *m,void *v) - __releases(&dd->lock) + __releases(&DD_DATA_FROM_RQ(RQ_FROM_SEQ_FILE(m))->lock) {struct request_queue *q = m->private;struct deadline_data *dd = q->elevator->elevator_data;
PATCH v2 13/26 block: Enable lock context analysis
In reply to Bart Van AsscheNow that all block/*.c files have been annotated, enable lock context analysis for all these source files.
--- block/Makefile | 2 ++ 1 file changed, 2 insertions(+)
block/Makefile +2 -0
@@ -3,6 +3,8 @@ # Makefile for the kernel block layer # +CONTEXT_ANALYSIS := y + obj-y := bdev.o fops.o bio.o elevator.o blk-core.o blk-sysfs.o \ blk-flush.o blk-settings.o blk-ioc.o blk-map.o \ blk-merge.o blk-timeout.o blk-lib.o blk-mq.o \
PATCH v2 14/26 aoe: Add a lock context annotation
In reply to Bart Van AsscheAdd a missing __must_hold() annotation. Enable lock context analysis in the Makefile.
--- drivers/block/aoe/Makefile | 2 ++ drivers/block/aoe/aoecmd.c | 1 + 2 files changed, 3 insertions(+)
drivers/block/aoe/Makefile +2 -0
@@ -3,5 +3,7 @@ # Makefile for ATA over Ethernet # +CONTEXT_ANALYSIS := y + obj-$(CONFIG_ATA_OVER_ETH) += aoe.o aoe-y := aoeblk.o aoechr.o aoecmd.o aoedev.o aoemain.o aoenet.o
drivers/block/aoe/aoecmd.c +1 -0
@@ -1193,6 +1193,7 @@ noskb: if (buf)*/ static int ktio (int id) + __must_hold(&iocq[id].lock) {struct frame *f;struct list_head *pos;
Re: [PATCH v2 14/26] aoe: Add a lock context annotation
Looks good:
PATCH v2 15/26 drbd: Balance RCU calls in drbd_adm_dump_devices()
In reply to Bart Van AsscheMake drbd_adm_dump_devices() call rcu_read_lock() before rcu_read_unlock() is called. This has been detected by the Clang thread-safety analyzer.
--- drivers/block/drbd/drbd_nl.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/block/drbd/drbd_nl.c +6 -2
@@ -3443,8 +3443,10 @@ int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)if (resource_filter) { retcode =ERR_RES_NOT_KNOWN ; resource =drbd_find_resource (nla_data (resource_filter)); -if (!resource) +if (!resource) { +rcu_read_lock (); goto put_result; + } cb->args[0 ] = (long )resource; } }
@@ -3693,8 +3695,10 @@ int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)if (resource_filter) { retcode =ERR_RES_NOT_KNOWN ; resource =drbd_find_resource (nla_data (resource_filter)); -if (!resource) +if (!resource) { +rcu_read_lock (); goto put_result; + } } cb->args[0 ] = (long )resource; }
Re: [PATCH v2 15/26] drbd: Balance RCU calls in drbd_adm_dump_devices()
Quoted 3 lines · Bart Van Assche
Make
drbd_adm_dump_devices()callrcu_read_lock()beforercu_read_unlock()is called. This has been detected by the Clang thread-safety analyzer.
Please split out fixes so they:
a) stand out
b) can be queued up in the current merge window
The patch itself looks good:
PATCH v2 16/26 drbd: Make the lock context annotations compatible with Clang
In reply to Bart Van AsscheClang performs more strict checking of lock context annotations than sparse. This patch makes the DRBD lock context annotations compatible with Clang and enables lock context analysis in the Makefile.
--- drivers/block/drbd/Makefile | 3 + drivers/block/drbd/drbd_bitmap.c | 26 ++++++--- drivers/block/drbd/drbd_int.h | 88 ++++++++++++++++++------------ drivers/block/drbd/drbd_main.c | 40 ++++++++++---- drivers/block/drbd/drbd_nl.c | 5 +- drivers/block/drbd/drbd_receiver.c | 31 ++++++++--- drivers/block/drbd/drbd_req.c | 3 + drivers/block/drbd/drbd_state.c | 2 + drivers/block/drbd/drbd_state.h | 4 -- drivers/block/drbd/drbd_worker.c | 6 +- 10 files changed, 138 insertions(+), 70 deletions(-)
drivers/block/drbd/Makefile +3 -0
@@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only + +CONTEXT_ANALYSIS := y + drbd-y := drbd_buildtag.o drbd_bitmap.o drbd_proc.o drbd-y += drbd_worker.o drbd_receiver.o drbd_req.o drbd_actlog.o drbd-y += drbd_main.o drbd_strings.o drbd_nl.o
drivers/block/drbd/drbd_bitmap.c +18 -8
@@ -122,12 +122,16 @@ static void __bm_print_lock_info(struct drbd_device *device, const char *func) }void drbd_bm_lock (struct drbd_device *device,char *why,enum bm_flag flags) + __acquires(&device->bitmap->bm_change) {struct drbd_bitmap *b = device->bitmap;int trylock_failed;if (!b) {drbd_err (device,"FIXME no bitmap in drbd_bm_lock!?\n" ); +/* Fake __acquire() to keep the compiler happy. */ +__acquire (&b->bm_change); +__acquire (drbd_bitmap_lock);return ; }
@@ -146,13 +150,18 @@ void drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags) b->bm_why = why; b->bm_task = current; +__acquire (drbd_bitmap_lock); }void drbd_bm_unlock (struct drbd_device *device) + __releases(&device->bitmap->bm_change) {struct drbd_bitmap *b = device->bitmap;if (!b) {drbd_err (device,"FIXME no bitmap in drbd_bm_unlock!?\n" ); +/* Fake __release() to keep the compiler happy. */ +__release (&b->bm_change); +__release (drbd_bitmap_lock);return ; }
@@ -163,6 +172,7 @@ void drbd_bm_unlock(struct drbd_device *device) b->bm_why =NULL ; b->bm_task =NULL ;mutex_unlock (&b->bm_change); +__release (drbd_bitmap_lock); }/* we store some "meta" info about our pages in page->private */
@@ -987,7 +997,7 @@ static inline sector_t drbd_md_last_bitmap_sector(struct drbd_backing_dev *bdev) } } -static void bm_page_io_async (struct drbd_bm_aio_ctx *ctx,int page_nr)__must_hold (local) +static void bm_page_io_async (struct drbd_bm_aio_ctx *ctx,int page_nr) {struct drbd_device *device = ctx->device;enum req_op op = ctx->flags &BM_AIO_READ ?REQ_OP_READ :REQ_OP_WRITE ;
@@ -1060,7 +1070,7 @@ static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_ho/* * bm_rw: read/write the whole bitmap from/to its on disk location. */ -static int bm_rw (struct drbd_device *device,const unsigned int flags,unsigned lazy_writeout_upper_idx)__must_hold (local) +static int bm_rw (struct drbd_device *device,const unsigned int flags,unsigned lazy_writeout_upper_idx) {struct drbd_bm_aio_ctx *ctx;struct drbd_bitmap *b = device->bitmap;
@@ -1215,7 +1225,7 @@ static int bm_rw(struct drbd_device *device, const unsigned int flags, unsigned* @device: DRBD device. */ int drbd_bm_read (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {return bm_rw (device,BM_AIO_READ ,0 );
@@ -1228,7 +1238,7 @@ int drbd_bm_read(struct drbd_device *device,* Will only write pages that have changed since last IO. */ int drbd_bm_write (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {return bm_rw (device,0 ,0 ); }
@@ -1240,7 +1250,7 @@ int drbd_bm_write(struct drbd_device *device,* Will write all pages. */ int drbd_bm_write_all (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {return bm_rw (device,BM_AIO_WRITE_ALL_PAGES ,0 ); }
@@ -1250,7 +1260,7 @@ int drbd_bm_write_all(struct drbd_device *device,* @device: DRBD device. * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages */ -int drbd_bm_write_lazy (struct drbd_device *device,unsigned upper_idx)__must_hold (local) +int drbd_bm_write_lazy (struct drbd_device *device,unsigned upper_idx) {return bm_rw (device,BM_AIO_COPY_PAGES , upper_idx); }
@@ -1267,7 +1277,7 @@ int drbd_bm_write_lazy(struct drbd_device *device, unsigned upper_idx) __must_ho* pending resync acks are still being processed. */ int drbd_bm_write_copy_pages (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {return bm_rw (device,BM_AIO_COPY_PAGES ,0 ); }
@@ -1276,7 +1286,7 @@ int drbd_bm_write_copy_pages(struct drbd_device *device,* drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed. * @device: DRBD device. */ -int drbd_bm_write_hinted (struct drbd_device *device)__must_hold (local) +int drbd_bm_write_hinted (struct drbd_device *device) {return bm_rw (device,BM_AIO_WRITE_HINTED |BM_AIO_COPY_PAGES ,0 ); }
drivers/block/drbd/drbd_int.h +54 -34
162 lines not on this page · raw message
drivers/block/drbd/drbd_main.c +30 -10
@@ -3282,7 +3282,7 @@ void drbd_md_mark_dirty(struct drbd_device *device)mod_timer (&device->md_sync_timer, jiffies +5 *HZ ); } -void drbd_uuid_move_history (struct drbd_device *device)__must_hold (local) +void drbd_uuid_move_history (struct drbd_device *device) {int i;
@@ -3290,7 +3290,7 @@ void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local) device->ldev->md.uuid[i+1 ] = device->ldev->md.uuid[i]; } -void __drbd_uuid_set (struct drbd_device *device,int idx,u64 val)__must_hold (local) +void __drbd_uuid_set (struct drbd_device *device,int idx,u64 val) {if (idx ==UI_CURRENT ) {if (device->state.role ==R_PRIMARY )
@@ -3305,7 +3305,7 @@ void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(ldrbd_md_mark_dirty (device); } -void _drbd_uuid_set (struct drbd_device *device,int idx,u64 val)__must_hold (local) +void _drbd_uuid_set (struct drbd_device *device,int idx,u64 val) {unsigned long flags;spin_lock_irqsave (&device->ldev->md.uuid_lock, flags);
@@ -3313,7 +3313,7 @@ void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(lospin_unlock_irqrestore (&device->ldev->md.uuid_lock, flags); } -void drbd_uuid_set (struct drbd_device *device,int idx,u64 val)__must_hold (local) +void drbd_uuid_set (struct drbd_device *device,int idx,u64 val) {unsigned long flags;spin_lock_irqsave (&device->ldev->md.uuid_lock, flags);
@@ -3332,7 +3332,7 @@ void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(loc* Creates a new current UUID, and rotates the old current UUID into * the bitmap slot. Causes an incremental resync upon next connect. */ -void drbd_uuid_new_current (struct drbd_device *device)__must_hold (local) +void drbd_uuid_new_current (struct drbd_device *device) {u64 val;unsigned long long bm_uuid;
@@ -3354,7 +3354,7 @@ void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)drbd_md_sync (device); } -void drbd_uuid_set_bm (struct drbd_device *device,u64 val)__must_hold (local) +void drbd_uuid_set_bm (struct drbd_device *device,u64 val) {unsigned long flags;spin_lock_irqsave (&device->ldev->md.uuid_lock, flags);
@@ -3387,7 +3387,7 @@ void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)* Sets all bits in the bitmap and writes the whole bitmap to stable storage. */ int drbd_bmio_set_n_write (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {int rv = -EIO ;
@@ -3414,7 +3414,7 @@ int drbd_bmio_set_n_write(struct drbd_device *device,* Clears all bits in the bitmap and writes the whole bitmap to stable storage. */ int drbd_bmio_clear_n_write (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {drbd_resume_al (device);
@@ -3541,7 +3541,7 @@ int drbd_bitmap_io(struct drbd_device *device,return rv; } -void drbd_md_set_flag (struct drbd_device *device,int flag)__must_hold (local) +void drbd_md_set_flag (struct drbd_device *device,int flag) {if ((device->ldev->md.flags & flag) != flag) {drbd_md_mark_dirty (device);
@@ -3549,7 +3549,7 @@ void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local) } } -void drbd_md_clear_flag (struct drbd_device *device,int flag)__must_hold (local) +void drbd_md_clear_flag (struct drbd_device *device,int flag) {if ((device->ldev->md.flags & flag) !=0 ) {drbd_md_mark_dirty (device);
@@ -3678,24 +3678,44 @@ int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i) }void lock_all_resources (void ) +__acquires (all_drbd_resources) + __acquires(&resources_mutex) {struct drbd_resource *resource;int __maybe_unused i =0 ;mutex_lock (&resources_mutex);local_irq_disable (); +/* +* context_unsafe() because the thread-safety analyzer does not support +* locking inside loops. +*/ +context_unsafe ( for_each_resource(resource, &drbd_resources) spin_lock_nested(&resource->req_lock, i++); + ); + +__acquire (all_drbd_resources); }void unlock_all_resources (void ) +__releases (all_drbd_resources) + __releases(&resources_mutex) {struct drbd_resource *resource; +/* +* context_unsafe() because the thread-safety analyzer does not support +* locking inside loops. +*/ +context_unsafe ( for_each_resource(resource, &drbd_resources) spin_unlock(&resource->req_lock); + );local_irq_enable ();mutex_unlock (&resources_mutex); + +__release (all_drbd_resources); }#ifdef CONFIG_DRBD_FAULT_INJECTION
drivers/block/drbd/drbd_nl.c +3 -2
@@ -993,7 +993,7 @@ void drbd_resume_io(struct drbd_device *device)* You should call drbd_md_sync() after calling this function. */ enum determine_dev_size -drbd_determine_dev_size (struct drbd_device *device,enum dds_flags flags,struct resize_parms *rs)__must_hold (local) +drbd_determine_dev_size (struct drbd_device *device,enum dds_flags flags,struct resize_parms *rs) {struct md_offsets_and_sizes {u64 last_agreed_sect;
@@ -3090,7 +3090,7 @@ static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info * }static int drbd_bmio_set_susp_al (struct drbd_device *device, -struct drbd_peer_device *peer_device)__must_hold (local) +struct drbd_peer_device *peer_device) {int rv;
@@ -3518,6 +3518,7 @@ int drbd_adm_dump_connections_done(struct netlink_callback *cb)enum {SINGLE_RESOURCE ,ITERATE_RESOURCES };int drbd_adm_dump_connections (struct sk_buff *skb,struct netlink_callback *cb) + __no_context_analysis/* too complex for Clang */ {struct nlattr *resource_filter;struct drbd_resource *resource =NULL , *next_resource;
drivers/block/drbd/drbd_receiver.c +23 -8
@@ -175,7 +175,7 @@ You must not have the req_lock:* trim: payload_size == 0 */ struct drbd_peer_request *drbd_alloc_peer_req (struct drbd_peer_device *peer_device,u64 id,sector_t sector, -unsigned int request_size,unsigned int payload_size,gfp_t gfp_mask)__must_hold (local) +unsigned int request_size,unsigned int payload_size,gfp_t gfp_mask) {struct drbd_device *device = peer_device->device;struct drbd_peer_request *peer_req;
@@ -287,6 +287,7 @@ static int drbd_finish_peer_reqs(struct drbd_device *device)static void _drbd_wait_ee_list_empty (struct drbd_device *device,struct list_head *head) + __must_hold(&device->resource->req_lock) {DEFINE_WAIT (wait);
@@ -896,6 +897,11 @@ static int conn_connect(struct drbd_connection *connection)if (drbd_send_protocol (connection) == -EOPNOTSUPP )return -1 ; +/* +* context_unsafe() because the thread-safety analyzer does not support +* locking inside loops. +*/ + context_unsafe(/* Prevent a race between resync-handshake and * being promoted to Primary. *
@@ -905,14 +911,21 @@ static int conn_connect(struct drbd_connection *connection)*/ idr_for_each_entry (&connection->peer_devices, peer_device, vnr)mutex_lock (peer_device->device->state_mutex); + );/* avoid a race with conn_request_state( C_DISCONNECTING ) */ spin_lock_irq (&connection->resource->req_lock);set_bit (STATE_SENT , &connection->flags);spin_unlock_irq (&connection->resource->req_lock); +/* +* context_unsafe() because the thread-safety analyzer does not support +* locking inside loops. +*/ +context_unsafe ( idr_for_each_entry(&connection->peer_devices, peer_device, vnr) mutex_unlock(peer_device->device->state_mutex); + );rcu_read_lock ();idr_for_each_entry (&connection->peer_devices, peer_device, vnr) {
@@ -1657,7 +1670,7 @@ static void drbd_csum_ee_size(struct crypto_shash *h,*/ static struct drbd_peer_request *read_in_block (struct drbd_peer_device *peer_device,u64 id,sector_t sector, -struct packet_info *pi)__must_hold (local) +struct packet_info *pi) {struct drbd_device *device = peer_device->device;const sector_t capacity =get_capacity (device->vdisk);
@@ -1869,7 +1882,7 @@ static int e_end_resync_block(struct drbd_work *w, int unused) }static int recv_resync_read (struct drbd_peer_device *peer_device,sector_t sector, -struct packet_info *pi)__releases (local) +struct packet_info *pi) {struct drbd_device *device = peer_device->device;struct drbd_peer_request *peer_req;
@@ -2230,6 +2243,7 @@ static blk_opf_t wire_flags_to_bio(struct drbd_connection *connection, u32 dpf)static void fail_postponed_requests (struct drbd_device *device,sector_t sector,unsigned int size) + __must_hold(&device->resource->req_lock) {struct drbd_peer_device *peer_device =first_peer_device (device);struct drbd_interval *i;
@@ -2256,6 +2270,7 @@ static void fail_postponed_requests(struct drbd_device *device, sector_t sector,static int handle_write_conflicts (struct drbd_device *device,struct drbd_peer_request *peer_req) + __must_hold(&device->resource->req_lock) {struct drbd_connection *connection = peer_req->peer_device->connection;bool resolve_conflicts =test_bit (RESOLVE_CONFLICTS , &connection->flags);
@@ -2826,7 +2841,7 @@ static int receive_DataRequest(struct drbd_connection *connection, struct packet/* * drbd_asb_recover_0p - Recover after split-brain with no remaining primaries */ -static int drbd_asb_recover_0p (struct drbd_peer_device *peer_device)__must_hold (local) +static int drbd_asb_recover_0p (struct drbd_peer_device *peer_device) {struct drbd_device *device = peer_device->device;int self, peer, rv =-100 ;
@@ -2909,7 +2924,7 @@ static int drbd_asb_recover_0p(struct drbd_peer_device *peer_device) __must_hold/* * drbd_asb_recover_1p - Recover after split-brain with one remaining primary */ -static int drbd_asb_recover_1p (struct drbd_peer_device *peer_device)__must_hold (local) +static int drbd_asb_recover_1p (struct drbd_peer_device *peer_device) {struct drbd_device *device = peer_device->device;int hg, rv =-100 ;
@@ -2966,7 +2981,7 @@ static int drbd_asb_recover_1p(struct drbd_peer_device *peer_device) __must_hold/* * drbd_asb_recover_2p - Recover after split-brain with two remaining primaries */ -static int drbd_asb_recover_2p (struct drbd_peer_device *peer_device)__must_hold (local) +static int drbd_asb_recover_2p (struct drbd_peer_device *peer_device) {struct drbd_device *device = peer_device->device;int hg, rv =-100 ;
@@ -3044,7 +3059,7 @@ static void drbd_uuid_dump(struct drbd_device *device, char *text, u64 *uuid,*/ static int drbd_uuid_compare (struct drbd_peer_device *const peer_device, -enum drbd_role const peer_role,int *rule_nr)__must_hold (local) +enum drbd_role const peer_role,int *rule_nr) {struct drbd_connection *const connection = peer_device->connection;struct drbd_device *device = peer_device->device;
@@ -3264,7 +3279,7 @@ static int drbd_uuid_compare(struct drbd_peer_device *const peer_device,*/ static enum drbd_conns drbd_sync_handshake (struct drbd_peer_device *peer_device,enum drbd_role peer_role, -enum drbd_disk_state peer_disk)__must_hold (local) +enum drbd_disk_state peer_disk) {struct drbd_device *device = peer_device->device;enum drbd_conns rv =C_MASK ;
drivers/block/drbd/drbd_req.c +3 -0
@@ -952,6 +952,7 @@ static bool remote_due_to_read_balancing(struct drbd_device *device, sector_t se* Only way out: remove the conflicting intervals from the tree. */ static void complete_conflicting_writes (struct drbd_request *req) + __must_hold(&req->device->resource->req_lock) {DEFINE_WAIT (wait);struct drbd_device *device = req->device;
@@ -1325,6 +1326,8 @@ static void drbd_send_and_submit(struct drbd_device *device, struct drbd_requestbool submit_private_bio = false;spin_lock_irq (&resource->req_lock); +/* Tell the compiler that &resource->req_lock == &req->device->resource->req_lock. */ +__assume_ctx_lock (&req->device->resource->req_lock);if (rw ==WRITE ) {/* This may temporarily give up the req_lock, * but will re-aquire it before it returns here.
drivers/block/drbd/drbd_state.c +2 -0
@@ -562,6 +562,7 @@ _req_st_cond(struct drbd_device *device, union drbd_state mask,static enum drbd_state_rv drbd_req_state (struct drbd_device *device,union drbd_state mask,union drbd_state val,enum chg_state_flags f) + __no_context_analysis/* conditional locking */ {struct completion done;unsigned long flags;
@@ -2292,6 +2293,7 @@ _conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union denum drbd_state_rv _conn_request_state (struct drbd_connection *connection,union drbd_state mask,union drbd_state val,enum chg_state_flags flags) + __no_context_analysis/* conditional locking */ {enum drbd_state_rv rv =SS_SUCCESS ;struct after_conn_state_chg_work *acscw;
drivers/block/drbd/drbd_state.h +0 -4
@@ -123,10 +123,6 @@ extern enum drbd_state_rv _drbd_request_state(struct drbd_device *, union drbd_state, enum chg_state_flags); -extern enum drbd_state_rv -_drbd_request_state_holding_state_mutex (struct drbd_device *,union drbd_state , -union drbd_state ,enum chg_state_flags ); -extern enum drbd_state_rv _drbd_set_state (struct drbd_device *,union drbd_state ,enum chg_state_flags ,struct completion *done);
drivers/block/drbd/drbd_worker.c +2 -4
@@ -78,7 +78,7 @@ void drbd_md_endio(struct bio *bio)/* reads on behalf of the partner, * "submitted" by the receiver */ -static void drbd_endio_read_sec_final (struct drbd_peer_request *peer_req)__releases (local) +static void drbd_endio_read_sec_final (struct drbd_peer_request *peer_req) {unsigned long flags =0 ;struct drbd_peer_device *peer_device = peer_req->peer_device;
@@ -99,7 +99,7 @@ static void drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __rele/* writes on behalf of the partner, or resync writes, * "submitted" by the receiver, final stage. */ -void drbd_endio_write_sec_final (struct drbd_peer_request *peer_req)__releases (local) +void drbd_endio_write_sec_final (struct drbd_peer_request *peer_req) {unsigned long flags =0 ;struct drbd_peer_device *peer_device = peer_req->peer_device;
@@ -1923,10 +1923,8 @@ static void drbd_ldev_destroy(struct drbd_device *device)lc_destroy (device->act_log); device->act_log =NULL ; -__acquire (local);drbd_backing_dev_free (device, device->ldev); device->ldev =NULL ; -__release (local);clear_bit (GOING_DISKLESS , &device->flags);wake_up (&device->misc_wait);
Re: [PATCH v2 16/26] drbd: Make the lock context annotations compatible with Clang
Quoted 40 lines · Bart Van Assche
Clang performs more strict checking of lock context annotations than sparse. This patch makes the DRBD lock context annotations compatible with Clang and enables lock context analysis in the
Makefile.Reviewed-by Christoph Böhmwalder <[email protected]>Signed-off-by Bart Van Assche <[email protected]>--- drivers/block/drbd/Makefile | 3 + drivers/block/drbd/drbd_bitmap.c | 26 ++++++--- drivers/block/drbd/drbd_int.h | 88 ++++++++++++++++++------------ drivers/block/drbd/drbd_main.c | 40 ++++++++++---- drivers/block/drbd/drbd_nl.c | 5 +- drivers/block/drbd/drbd_receiver.c | 31 ++++++++--- drivers/block/drbd/drbd_req.c | 3 + drivers/block/drbd/drbd_state.c | 2 + drivers/block/drbd/drbd_state.h | 4 -- drivers/block/drbd/drbd_worker.c | 6 +- 10 files changed, 138 insertions(+), 70 deletions(-)drivers/block/drbd/Makefile +3
-0@@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only + +CONTEXT_ANALYSIS := y + drbd-y := drbd_buildtag.o drbd_bitmap.o drbd_proc.o drbd-y += drbd_worker.o drbd_receiver.o drbd_req.o drbd_actlog.o drbd-y += drbd_main.o drbd_strings.o drbd_nl.odrivers/block/drbd/drbd_bitmap.c +1
-0@@ -122,12 +122,16 @@ static void __bm_print_lock_info(struct drbd_device *device, const char *func) } void drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags) + __acquires(&device->bitmap->bm_change)
drivers/block/drbd/drbd_bitmap.c static void __bm_print_lock_info(struct drbd_device *device, const char *func) · line 135
Quoted 10 lines
drivers/block/drbd/drbd_bitmap.c +3
-0{struct drbd_bitmap *b = device->bitmap;int trylock_failed;if (!b) {drbd_err (device,"FIXME no bitmap in drbd_bm_lock!?\n" ); +/* Fake __acquire() to keep the compiler happy. */ +__acquire (&b->bm_change); +__acquire (drbd_bitmap_lock);return ;
This looks like an impossible to hit case with cargo cult code. I'd suggest to remove it instead.
Similarly please kill the trylock_faiuled code, which has weird debugging that is strictly inferior to what lockdep already provides.
drivers/block/drbd/drbd_bitmap.c void drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags) · line 166
Quoted 11 lines
drivers/block/drbd/drbd_bitmap.c +4
-0void drbd_bm_unlock (struct drbd_device *device) + __releases(&device->bitmap->bm_change) {struct drbd_bitmap *b = device->bitmap;if (!b) {drbd_err (device,"FIXME no bitmap in drbd_bm_unlock!?\n" ); +/* Fake __release() to keep the compiler happy. */ +__release (&b->bm_change); +__release (drbd_bitmap_lock);return ; }
Same comment as above.
drivers/block/drbd/drbd_bitmap.c
Quoted 8 lines
drivers/block/drbd/drbd_bitmap.c +8
-0+extern void drbd_uuid_set (struct drbd_device *device,int idx,u64 val); +extern void _drbd_uuid_set (struct drbd_device *device,int idx,u64 val); +extern void drbd_uuid_new_current (struct drbd_device *device); +extern void drbd_uuid_set_bm (struct drbd_device *device,u64 val); +extern void drbd_uuid_move_history (struct drbd_device *device); +extern void __drbd_uuid_set (struct drbd_device *device,int idx,u64 val); +extern void drbd_md_set_flag (struct drbd_device *device,int flags); +extern void drbd_md_clear_flag (struct drbd_device *device,int flags);
All these externs should go away.
In general this still mixes up way too many things. Address one lock/issue at a time, and write proper commit logs explain why a given solution is taken.
PATCH v2 17/26 loop: Add lock context annotations
In reply to Bart Van AsscheAdd lock context annotations that are compatible with Clang and enable lock context analysis.
--- drivers/block/loop.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/block/loop.c +4 -0
@@ -107,6 +107,8 @@ static DEFINE_MUTEX(loop_validate_mutex);* loop_configure()/loop_change_fd()/__loop_clr_fd() calls. */ static int loop_global_lock_killable (struct loop_device *lo,bool global) + __cond_acquires(0, &lo->lo_mutex) + __no_context_analysis/* conditional locking */ {int err;
@@ -128,6 +130,8 @@ static int loop_global_lock_killable(struct loop_device *lo, bool global)* @global: true if @lo was about to bind another "struct loop_device", false otherwise */ static void loop_global_unlock (struct loop_device *lo,bool global) + __releases(&lo->lo_mutex) + __no_context_analysis/* conditional locking */ {mutex_unlock (&lo->lo_mutex);if (global)
Re: [PATCH v2 17/26] loop: Add lock context annotations
Quoted 13 lines · Bart Van Assche
Add lock context annotations that are compatible with Clang and enable lock context analysis.
Signed-off-by Bart Van Assche <[email protected]>--- drivers/block/loop.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/block/loop.c +0
-0
drivers/block/loop.c static DEFINE_MUTEX(loop_validate_mutex); · line 111
Quoted 5 lines
drivers/block/loop.c +2
-0@@ -107,6 +107,8 @@ static DEFINE_MUTEX(loop_validate_mutex);* loop_configure()/loop_change_fd()/__loop_clr_fd() calls. */ static int loop_global_lock_killable(struct loop_device *lo, bool global) + __cond_acquires(0, &lo->lo_mutex) + __no_context_analysis /* conditional locking */
There's not explanation here why this is needed.
PATCH v2 18/26 mtip32: Enable lock context analysis
In reply to Bart Van AsscheLet the compiler verify lock and unlock calls. Enable lock context analysis in the Makefile.
--- drivers/block/mtip32xx/Makefile | 2 ++ 1 file changed, 2 insertions(+)
drivers/block/mtip32xx/Makefile +2 -0
@@ -3,4 +3,6 @@ # Makefile for Block device driver for Micron PCIe SSD # +CONTEXT_ANALYSIS := y + obj-$(CONFIG_BLK_DEV_PCIESSD_MTIP32XX) += mtip32xx.o
Re: [PATCH v2 18/26] mtip32: Enable lock context analysis
Looks good:
PATCH v2 19/26 nbd: Enable lock context analysis
In reply to Bart Van AsscheAdd the missing __must_hold() annotations. Enable lock context analysis in the Makefile.
--- drivers/block/nbd.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/block/nbd.c +3 -0
@@ -1469,6 +1469,7 @@ static void nbd_config_put(struct nbd_device *nbd) }static int nbd_start_device (struct nbd_device *nbd) + __must_hold(&nbd->config_lock) {struct nbd_config *config = nbd->config;int num_connections = config->num_connections;
@@ -1541,6 +1542,7 @@ static int nbd_start_device(struct nbd_device *nbd) }static int nbd_start_device_ioctl (struct nbd_device *nbd) + __must_hold(nbd->config_lock) {struct nbd_config *config = nbd->config;int ret;
@@ -1592,6 +1594,7 @@ static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)/* Must be called with config_lock held */ static int __nbd_ioctl (struct block_device *bdev,struct nbd_device *nbd,unsigned int cmd,unsigned long arg) + __must_hold(nbd->config_lock) {struct nbd_config *config = nbd->config;loff_t bytesize;
Re: [PATCH v2 19/26] nbd: Enable lock context analysis
Looks good:
PATCH v2 20/26 null_blk: Enable lock context analysis
In reply to Bart Van AsscheConvert __releases() __acquires() into __must_hold(). Add __must_hold() annotations where these are missing. Annotate two functions that use conditional locking with __no_context_analysis. Enable lock context analysis in the Makefile.
--- drivers/block/null_blk/Makefile | 2 ++ drivers/block/null_blk/main.c | 7 +++++-- drivers/block/null_blk/zoned.c | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-)
drivers/block/null_blk/Makefile +2 -0
@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 +CONTEXT_ANALYSIS := y + # needed for trace events ccflags-y += -I$(src)
drivers/block/null_blk/main.c +5 -2
@@ -1004,8 +1004,7 @@ static struct nullb_page *null_lookup_page(struct nullb *nullb,static struct nullb_page *null_insert_page (struct nullb *nullb,sector_t sector,bool ignore_cache) - __releases(&nullb->lock) - __acquires(&nullb->lock) + __must_hold(&nullb->lock) {u64 idx;struct nullb_page *t_page;
@@ -1038,6 +1037,7 @@ static struct nullb_page *null_insert_page(struct nullb *nullb, }static int null_flush_cache_page (struct nullb *nullb,struct nullb_page *c_page) + __must_hold(&nullb->lock) {int i;unsigned int offset;
@@ -1087,6 +1087,7 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page) }static int null_make_cache_space (struct nullb *nullb,unsigned long n) + __must_hold(&nullb->lock) {int i, err, nr_pages;struct nullb_page *c_pages[FREE_BATCH ];
@@ -1141,6 +1142,7 @@ static int null_make_cache_space(struct nullb *nullb, unsigned long n)static blk_status_t copy_to_nullb (struct nullb *nullb,void *source,loff_t pos,size_t n,bool is_fua) + __must_hold(&nullb->lock) {size_t temp, count =0 ;struct nullb_page *t_page;
@@ -1242,6 +1244,7 @@ static blk_status_t null_handle_flush(struct nullb *nullb)static blk_status_t null_transfer (struct nullb *nullb,struct page *page,unsigned int len,unsigned int off,bool is_write,loff_t pos,bool is_fua) + __must_hold(&nullb->lock) {struct nullb_device *dev = nullb->dev;blk_status_t err =BLK_STS_OK ;
drivers/block/null_blk/zoned.c +2 -0
@@ -32,6 +32,7 @@ static inline void null_init_zone_lock(struct nullb_device *dev,static inline void null_lock_zone (struct nullb_device *dev,struct nullb_zone *zone) + __no_context_analysis/* conditional locking */ {if (!dev->memory_backed)spin_lock_irq (&zone->spinlock);
@@ -41,6 +42,7 @@ static inline void null_lock_zone(struct nullb_device *dev,static inline void null_unlock_zone (struct nullb_device *dev,struct nullb_zone *zone) + __no_context_analysis/* conditional locking */ {if (!dev->memory_backed)spin_unlock_irq (&zone->spinlock);
Re: [PATCH v2 20/26] null_blk: Enable lock context analysis
Quoted 4 lines · Bart Van Assche
Convert
__releases()__acquires()into__must_hold(). Add__must_hold()annotations where these are missing. Annotate two functions that use conditional locking with__no_context_analysis. Enable lock context analysis in theMakefile.
Not a very good explanation why this is needed and there's not better way to avoid the big hammer.
Re: [PATCH v2 20/26] null_blk: Enable lock context analysis
In reply to Bart Van AsscheQuoted 29 lines · Bart Van Assche
Convert
__releases()__acquires()into__must_hold(). Add__must_hold()annotations where these are missing. Annotate two functions that use conditional locking with__no_context_analysis. Enable lock context analysis in theMakefile.Signed-off-by Bart Van Assche <[email protected]>--- drivers/block/null_blk/Makefile | 2 ++ drivers/block/null_blk/main.c | 7 +++++-- drivers/block/null_blk/zoned.c | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-)drivers/block/null_blk/Makefile +2
-0@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 +CONTEXT_ANALYSIS := y + # needed for trace events ccflags-y += -I$(src)drivers/block/null_blk/main.c +0
-0
drivers/block/null_blk/main.c static struct nullb_page *null_lookup_page(struct nullb *nullb, · line 1007
Quoted 6 lines
drivers/block/null_blk/main.c +1
-2@@ -1004,8 +1004,7 @@ static struct nullb_page *null_lookup_page(struct nullb *nullb,static struct nullb_page *null_insert_page (struct nullb *nullb,sector_t sector,bool ignore_cache) - __releases(&nullb->lock) - __acquires(&nullb->lock) + __must_hold(&nullb->lock)
This function temporarily drops the &nullb->lock and requires it. So why do we need to replace __releases/__acquires with __must_hold? This is not clear.
[...]
drivers/block/null_blk/zoned.c static inline void null_init_zone_lock(struct nullb_device *dev, · line 35
Quoted 9 lines
drivers/block/null_blk/zoned.c +1
-0@@ -32,6 +32,7 @@ static inline void null_init_zone_lock(struct nullb_device *dev, static inline void null_lock_zone(struct nullb_device *dev, struct nullb_zone *zone) + __no_context_analysis /* conditional locking */
I see we already have __context_unsafe(comment) macro defined which may be used instead for inline comment.
Thanks,
--Nilay
Re: [PATCH v2 20/26] null_blk: Enable lock context analysis
(+Marco)
Quoted 16 lines · Nilay Shroff
drivers/block/null_blk/main.c static struct nullb_page · line 1007
Quoted 11 lines · Bart Van Assche
drivers/block/null_blk/main.c +1
-2@@ -1004,8 +1004,7 @@ static struct nullb_page *null_lookup_page(struct nullb *nullb, static struct nullb_page *null_insert_page(struct nullb *nullb, sector_t sector, bool ignore_cache) - __releases(&nullb->lock) - __acquires(&nullb->lock) + __must_hold(&nullb->lock)This function temporarily drops the
&nullb->lockand requires it. So why do we need to replace __releases/__acquires with__must_hold? This is not clear.
__must_hold() has the same meaning as __releases() + __acquires(). The only difference between these two annotations is that __must_hold() is more compact. I can leave out this change if this change is considered confusing.
Quoted 4 lines
Quoted 4 lines
drivers/block/null_blk/zoned.c +0
-0
Quoted 8 lines
drivers/block/null_blk/zoned.c static inline void null_init_zone_lock(struct · line 35
Quoted 5 lines
drivers/block/null_blk/zoned.c +1
-0@@ -32,6 +32,7 @@ static inline void null_init_zone_lock(struct nullb_device *dev, static inline void null_lock_zone(struct nullb_device *dev, struct nullb_zone *zone) + __no_context_analysis /* conditional locking */I see we already have
__context_unsafe(comment)macro defined which may be used instead for inline comment.
context_unsafe() suppresses warnings triggered by __guarded_by() and __guarded_by_ptr() but does not suppress analysis of functions in the function body annotated with __releases() and __acquired(). If I remove the __no_context_analysis annotation and surround the function body with context_unsafe(), the following appears:
drivers/block/null_blk/zoned.c:37:2: error: spinlock 'zone->.spinlock' is not held on every path through here [-Werror,-Wthread-safety-analysis]
37 | context_unsafe(
| ^
./include/linux/compiler-context-analysis.h:201:30: note: expanded from macro 'context_unsafe'
201 | #define context_unsafe(...) \
| ^
drivers/block/null_blk/zoned.c:39:3: note: spinlock acquired here
39 | spin_lock_irq(&zone->spinlock);
| ^drivers/block/null_blk/zoned.c:37:2: error: mutex 'zone->.mutex' is not held on every path through here [-Werror,-Wthread-safety-analysis]
37 | context_unsafe(
| ^
./include/linux/compiler-context-analysis.h:201:30: note: expanded from macro 'context_unsafe'
201 | #define context_unsafe(...) \
| ^
drivers/block/null_blk/zoned.c:41:3: note: mutex acquired here
41 | mutex_lock(&zone->mutex);
| ^2 errors generated.
Thanks,
Bart.
Re: [PATCH v2 20/26] null_blk: Enable lock context analysis
Quoted 19 lines · Bart Van Assche
(+Marco)
Quoted 15 lines · Nilay Shroff
drivers/block/null_blk/main.c static struct nullb_page *null_lookup_page(struct nullb *nullb, · line 1006
Quoted 10 lines · Bart Van Assche
drivers/block/null_blk/main.c +1
-2@@ -1004,8 +1004,7 @@ static struct nullb_page *null_lookup_page(struct nullb *nullb, static struct nullb_page *null_insert_page(struct nullb *nullb, sector_t sector, bool ignore_cache) - __releases(&nullb->lock) - __acquires(&nullb->lock) + __must_hold(&nullb->lock)This function temporarily drops the
&nullb->lockand requires it. So why do we need to replace __releases/__acquires with__must_hold? This is not clear.
Quoted 5 lines
__must_hold()has the same meaning as__releases()+__acquires(). The only difference between these two annotations is that__must_hold()is more compact. I can leave out this change if this change is considered confusing.
Hmm, but per the documentation of __must_hold() it seems that this attribute signifies the caller of the function must hold the exclusive context lock. On contrary, __releases() attribute is meant to be used when function releases a context lock exclusively and __acquires() should be used when function acquires context lock exclusively.
From the above rational, it seems that null_insert_page() should have all the above three attributes applicable, isn't it?
Quoted 28 lines
Quoted 11 lines
drivers/block/null_blk/zoned.c static inline void null_init_zone_lock(struct nullb_device *dev, · line 34
Quoted 8 lines
drivers/block/null_blk/zoned.c +1
-0@@ -32,6 +32,7 @@ static inline void null_init_zone_lock(struct nullb_device *dev, static inline void null_lock_zone(struct nullb_device *dev, struct nullb_zone *zone) + __no_context_analysis /* conditional locking */I see we already have
__context_unsafe(comment)macro defined which may be used instead for inline comment.
context_unsafe()suppresses warnings triggered by__guarded_by()and__guarded_by_ptr()but does not suppress analysis of functions in the function body annotated with__releases()and__acquired(). If I remove the__no_context_analysisannotation and surround the function body withcontext_unsafe(), the following appears:
drivers/block/null_blk/zoned.c:37:2: error: spinlock 'zone->.spinlock' is not held on every path through here [-Werror,-Wthread-safety-analysis]
37 | context_unsafe(
| ^./include/linux/compiler-context-analysis.h:201:30: note: expanded from macro 'context_unsafe'
201 | #define context_unsafe(...) \
| ^drivers/block/null_blk/zoned.c:39:3: note: spinlock acquired here
39 |spin_lock_irq(&zone->spinlock);
| ^
Quoted 11 lines
drivers/block/null_blk/zoned.c:37:2: error: mutex 'zone->.mutex' is not held on every path through here [-Werror,-Wthread-safety-analysis]
37 | context_unsafe(
| ^./include/linux/compiler-context-analysis.h:201:30: note: expanded from macro 'context_unsafe'
201 | #define context_unsafe(...) \
| ^drivers/block/null_blk/zoned.c:41:3: note: mutex acquired here
41 |mutex_lock(&zone->mutex);
| ^
2 errors generated.
I am not sure why did compiler generate the above errors? If I look at the macro definition then it seems __context_unsafe(/* comment */) should simply expand to __no_context_analysis.
// From include/linux/compiler-context-analysis.h:
#define __context_unsafe(comment) __no_context_analysis
Thanks,
--Nilay
Re: [PATCH v2 20/26] null_blk: Enable lock context analysis
Quoted 21 lines · Nilay Shroff
Quoted 20 lines · Bart Van Assche
(+Marco)
Quoted 16 lines · Nilay Shroff
drivers/block/null_blk/main.c static struct nullb_page · line 1007
Quoted 11 lines · Bart Van Assche
drivers/block/null_blk/main.c +1
-2@@ -1004,8 +1004,7 @@ static struct nullb_page *null_lookup_page(struct nullb *nullb, static struct nullb_page *null_insert_page(struct nullb *nullb, sector_t sector, bool ignore_cache) - __releases(&nullb->lock) - __acquires(&nullb->lock) + __must_hold(&nullb->lock)This function temporarily drops the
&nullb->lockand requires it. So why do we need to replace __releases/__acquires with__must_hold? This is not clear.
Quoted 8 lines
Quoted 5 lines
__must_hold()has the same meaning as__releases()+__acquires(). The only difference between these two annotations is that__must_hold()is more compact. I can leave out this change if this change is considered confusing.Hmm, but per the documentation of
__must_hold()it seems that this attribute signifies the caller of the function must hold the exclusive context lock. On contrary,__releases()attribute is meant to be used when function releases a context lock exclusively and__acquires()should be used when function acquires context lock exclusively. From the above rational, it seems thatnull_insert_page()should have all the above three attributes
applicable, isn't it?
__must_hold() is defined as __attribute__((requires_capability(...)))
In the Clang documentation this capability is called REQUIRES(). From
https://clang.llvm.org/docs/ThreadSafetyAnalysis.html: "REQUIRES is an attribute on functions, methods or function parameters of reference to SCOPED_CAPABILITY-annotated type, which declares that the calling thread
must have exclusive access to the given capabilities. More than one
capability may be specified. The capabilities must be held on entry to
the function, and must still be held on exit."
To me the above text means that the __must_hold() annotation is sufficient.
Quoted 7 lines
I am not sure why did compiler generate the above errors? If I look at the macro definition then it seems
__context_unsafe(/* comment */)should simply expand to__no_context_analysis.// From
include/linux/compiler-context-analysis.h:#define
__context_unsafe(comment)__no_context_analysis
I misread your previous comment. The output I shared is produced when the function body is surrounded with context_unsafe() (no leading underscores). Your request was to annotate the function with__context_unsafe() (with two leading underscores).
I will consider to use __context_unsafe() instead of __no_context_analysis and a comment.
Thanks,
Bart.
Re: [PATCH v2 20/26] null_blk: Enable lock context analysis
Quoted 39 lines · Bart Van Assche
Quoted 20 lines · Nilay Shroff
Quoted 19 lines · Bart Van Assche
(+Marco)
Quoted 15 lines · Nilay Shroff
drivers/block/null_blk/main.c static struct nullb_page *null_lookup_page(struct nullb *nullb, · line 1006
Quoted 10 lines · Bart Van Assche
drivers/block/null_blk/main.c +1
-2@@ -1004,8 +1004,7 @@ static struct nullb_page *null_lookup_page(struct nullb *nullb, static struct nullb_page *null_insert_page(struct nullb *nullb, sector_t sector, bool ignore_cache) - __releases(&nullb->lock) - __acquires(&nullb->lock) + __must_hold(&nullb->lock)This function temporarily drops the
&nullb->lockand requires it. So why do we need to replace __releases/__acquires with__must_hold? This is not clear.Quoted 8 lines
Quoted 5 lines
__must_hold()has the same meaning as__releases()+__acquires(). The only difference between these two annotations is that__must_hold()is more compact. I can leave out this change if this change is considered confusing.Hmm, but per the documentation of
__must_hold()it seems that this attribute signifies the caller of the function must hold the exclusive context lock. On contrary,__releases()attribute is meant to be used when function releases a context lock exclusively and__acquires()should be used when function acquires context lock exclusively. From the above rational, it seems thatnull_insert_page()should have all the above three attributes
applicable, isn't it?
__must_hold()is defined as __attribute__((requires_capability(...)))
In the Clang documentation this capability is calledREQUIRES(). From
https://clang.llvm.org/docs/ThreadSafetyAnalysis.html: "REQUIRES is an attribute on functions, methods or function parameters of reference to SCOPED_CAPABILITY-annotated type, which declares that the calling thread
must have exclusive access to the given capabilities. More than one
capability may be specified. The capabilities must be held on entry to
the function, and must still be held on exit."
Quoted 2 lines
To me the above text means that the
__must_hold()annotation is sufficient.
Okay, that makes sense — using only __must_hold() is reasonable. That said, since this function temporarily releases and then re-acquires the lock, it may not be entirely redundant to also annotate it with __releases() and __acquires(). Those annotations more explicitly describe the lock transitions happening within the function body.
Still, I’m fine with keeping just __must_hold() if we prefer a more concise annotation.
Quoted 6 lines
Quoted 6 lines
I am not sure why did compiler generate the above errors? If I look at the macro definition then it seems
__context_unsafe(/* comment */)should simply expand to__no_context_analysis.// From
include/linux/compiler-context-analysis.h:
Quoted 8 lines
Quoted 1 line
#define
__context_unsafe(comment)__no_context_analysisI misread your previous comment. The output I shared is produced when the function body is surrounded with
context_unsafe()(no leading underscores). Your request was to annotate the function with__context_unsafe()(with two leading underscores).I will consider to use
__context_unsafe()instead of__no_context_analysisand a comment.
Thanks,
--Nilay
PATCH v2 21/26 rbd: Enable lock context analysis
In reply to Bart Van AsscheAdd lock context annotations and enable lock context analysis.
--- drivers/block/rbd.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/block/rbd.c +8 -0
@@ -4185,6 +4185,7 @@ static void rbd_acquire_lock(struct work_struct *work) }static bool rbd_quiesce_lock (struct rbd_device *rbd_dev) + __must_hold(&rbd_dev->lock_rwsem) {dout ("%s rbd_dev %p\n" , __func__, rbd_dev);lockdep_assert_held_write (&rbd_dev->lock_rwsem);
@@ -4229,6 +4230,7 @@ static void __rbd_release_lock(struct rbd_device *rbd_dev)* lock_rwsem must be held for write */ static void rbd_release_lock (struct rbd_device *rbd_dev) + __must_hold(&rbd_dev->lock_rwsem) {if (!rbd_quiesce_lock (rbd_dev))return ;
@@ -4597,6 +4599,7 @@ static void rbd_unregister_watch(struct rbd_device *rbd_dev)* lock_rwsem must be held for write */ static void rbd_reacquire_lock (struct rbd_device *rbd_dev) + __must_hold(&rbd_dev->lock_rwsem) {struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;char cookie[32 ];
@@ -6789,6 +6792,7 @@ static void rbd_dev_device_release(struct rbd_device *rbd_dev)* upon return. */ static int rbd_dev_device_setup (struct rbd_device *rbd_dev) + __releases(&rbd_dev->header_rwsem) {int ret;
@@ -6890,6 +6894,7 @@ static void rbd_dev_image_release(struct rbd_device *rbd_dev)* with @depth == 0. */ static int rbd_dev_image_probe (struct rbd_device *rbd_dev,int depth) + __no_context_analysis/* conditional locking */ {bool need_watch = !rbd_is_ro (rbd_dev);int ret;
@@ -7143,6 +7148,9 @@ static ssize_t do_rbd_add(const char *buf, size_t count)if (rc <0 ) goto err_out_rbd_dev; +/* Acquired by rbd_dev_image_probe(rbd_dev, 0) */ +__acquire (&rbd_dev->header_rwsem); +if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) {rbd_warn (rbd_dev,"alloc_size adjusted to %u" , rbd_dev->layout.object_size);
PATCH v2 22/26 ublk: Enable lock context analysis
In reply to Bart Van AsscheAdd the lock context annotations that are required by Clang and enable lock context analysis.
--- drivers/block/ublk_drv.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/block/ublk_drv.c +4 -0
@@ -353,11 +353,13 @@ static inline bool ublk_support_batch_io(const struct ublk_queue *ubq) }static inline void ublk_io_lock (struct ublk_io *io) + __acquires(&io->lock) {spin_lock (&io->lock); }static inline void ublk_io_unlock (struct ublk_io *io) + __releases(&io->lock) {spin_unlock (&io->lock); }
@@ -3166,6 +3168,7 @@ static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr)static int __ublk_fetch (struct io_uring_cmd *cmd,struct ublk_device *ub,struct ublk_io *io,u16 q_id) + __must_hold(&ub->mutex) {/* UBLK_IO_FETCH_REQ is only allowed before dev is setup */ if (ublk_dev_ready (ub))
@@ -3587,6 +3590,7 @@ static void ublk_batch_revert_prep_cmd(struct ublk_batch_io_iter *iter,static int ublk_batch_prep_io (struct ublk_queue *ubq,const struct ublk_batch_io_data *data,const struct ublk_elem_header *elem) + __must_hold(&data->ub->mutex) {struct ublk_io *io = &ubq->ios[elem->tag];const struct ublk_batch_io *uc = &data->header;
PATCH v2 23/26 xen-blkback: Enable lock context analysis
In reply to Bart Van AsscheLet Clang verify lock and unlock calls. Enable lock context analysis in the Makefile.
--- drivers/block/xen-blkback/Makefile | 3 +++ 1 file changed, 3 insertions(+)
drivers/block/xen-blkback/Makefile +3 -0
@@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only + +CONTEXT_ANALYSIS := y + obj-$(CONFIG_XEN_BLKDEV_BACKEND) := xen-blkback.o xen-blkback-y := blkback.o xenbus.o
Re: [PATCH v2 23/26] xen-blkback: Enable lock context analysis
Quoted 4 lines · Bart Van Assche
Let Clang verify lock and unlock calls. Enable lock context analysis in the
Makefile.Signed-off-by Bart Van Assche <[email protected]>
Thanks, Roger.
PATCH v2 24/26 zram: Enable lock context analysis
In reply to Bart Van AsscheAdd the lock context annotations that are required by Clang. Enable lock context analysis in the Makefile.
--- drivers/block/zram/Makefile | 2 ++ drivers/block/zram/zcomp.c | 3 ++- drivers/block/zram/zcomp.h | 6 ++++-- drivers/block/zram/zram_drv.c | 1 + 4 files changed, 9 insertions(+), 3 deletions(-)
drivers/block/zram/Makefile +2 -0
@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only +CONTEXT_ANALYSIS := y + zram-y := zcomp.o zram_drv.o zram-$(CONFIG_ZRAM_BACKEND_LZO) += backend_lzorle.o backend_lzo.o
drivers/block/zram/zcomp.c +2 -1
@@ -107,7 +107,8 @@ ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at)return at; } -struct zcomp_strm *zcomp_stream_get (struct zcomp *comp) +struct zcomp_strm *__zcomp_stream_get (struct zcomp *comp) + __no_context_analysis/* __acquires(&raw_cpu_ptr(comp->stream)->lock) */ {for (;;) {struct zcomp_strm *zstrm =raw_cpu_ptr (comp->stream);
drivers/block/zram/zcomp.h +4 -2
@@ -85,8 +85,10 @@ bool zcomp_available_algorithm(const char *comp);struct zcomp *zcomp_create (const char *alg,struct zcomp_params *params);void zcomp_destroy (struct zcomp *comp); -struct zcomp_strm *zcomp_stream_get (struct zcomp *comp); -void zcomp_stream_put (struct zcomp_strm *zstrm); +#define zcomp_stream_get (...) __acquire_ret(__zcomp_stream_get(__VA_ARGS__), &__ret->lock) +struct zcomp_strm *__zcomp_stream_get (struct zcomp *comp); +void zcomp_stream_put(struct zcomp_strm *zstrm) + __releases(&zstrm->lock);int zcomp_compress (struct zcomp *comp,struct zcomp_strm *zstrm,const void *src,unsigned int *dst_len);
drivers/block/zram/zram_drv.c +1 -0
@@ -2395,6 +2395,7 @@ static int scan_slots_for_recompress(struct zram *zram, u32 mode, u32 prio_max,static int recompress_slot (struct zram *zram,u32 index,struct page *page,u64 *num_recomp_pages,u32 threshold,u32 prio,u32 prio_max) + __no_context_analysis/* too complex for Clang */ {struct zcomp_strm *zstrm =NULL ;unsigned long handle_old;
PATCH v2 25/26 rnbd: Enable lock context analysis
In reply to Bart Van AsscheLet Clang verify lock and unlock calls. Enable lock context analysis in the Makefile.
--- drivers/block/rnbd/Makefile | 2 ++ 1 file changed, 2 insertions(+)
drivers/block/rnbd/Makefile +2 -0
@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later +CONTEXT_ANALYSIS := y + ccflags-y := -I$(srctree)/drivers/infiniband/ulp/rtrs rnbd-client-y := rnbd-clt.o \
Re: [PATCH v2 25/26] rnbd: Enable lock context analysis
Quoted 5 lines · Bart Van Assche
Let Clang verify lock and unlock calls. Enable lock context analysis in the
Makefile.Signed-off-by Bart Van Assche <[email protected]>
Quoted 17 lines
--- drivers/block/rnbd/Makefile | 2 ++ 1 file changed, 2 insertions(+)drivers/block/rnbd/Makefile +2
-0@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later +CONTEXT_ANALYSIS := y + ccflags-y := -I$(srctree)/drivers/infiniband/ulp/rtrs rnbd-client-y := rnbd-clt.o \
PATCH v2 26/26 block: Enable lock context analysis for all block drivers
In reply to Bart Van AsscheNow that all locking functions in block drivers have been annotated, enable lock context analysis for all block drivers at the top level of drivers/block/.
--- drivers/block/Makefile | 2 ++ 1 file changed, 2 insertions(+)
drivers/block/Makefile +2 -0
@@ -6,6 +6,8 @@ # Rewritten to use lists instead of if-statements. # +CONTEXT_ANALYSIS := y + # needed for trace events ccflags-y += -I$(src)
Hi Bart,
this is a huge series for very different things. I'd suggest you submit the core block annotations first, and after that you can handle drivers indepdently, whіch reduces review load and the amount of spamming of the list for repeated resends. It should also help you to iterate faster.