rbd: prevent open of image being unmapped
Subsystems (2)
cover rbd: prevent open of image being unmapped
This series protects an open of a mapped rbd image from succeeding once an unmap of that image is underway.
Note: Once committed these should be back-ported.
-Alex
[PATCH 1/2] rbd: define flags field, use it for exists flag [PATCH 2/2] rbd: prevent open for image being removed
PATCH 1/2 rbd: define flags field, use it for exists flag
Define a new rbd device flags field, manipulated using atomic bit operations. Replace the use of the current "exists" flag with a bit in this new "flags" field.
--- drivers/block/rbd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/block/rbd.c +7 -1
@@ -232,7 +232,7 @@ struct rbd_device {spinlock_t lock;/* queue lock */ struct rbd_image_header header; -atomic_t exists; +unsigned long flags;struct rbd_spec *spec;char *header_name;
@@ -260,6 +260,12 @@ struct rbd_device {unsigned long open_count; }; +/* Flag bits for rbd_dev->flags */ + +enum rbd_dev_flags { + rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ +}; +static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */ static LIST_HEAD (rbd_dev_list);/* devices */
@@ -756,7 +762,8 @@ static int rbd_dev_set_mapping(struct rbd_device
*rbd_dev)
goto done;
rbd_dev->mapping.read_only = true;
}
- atomic_set(&rbd_dev->exists, 1);
+ set_bit(rbd_dev_flag_exists, &rbd_dev->flags);
+
done:
return ret;
}
@@ -1654,7 +1661,7 @@ static void rbd_rq_fn(struct request_queue *q)
snapc = ceph_get_snap_context(rbd_dev->header.snapc); up_read(&rbd_dev->header_rwsem); rbd_assert(snapc != NULL);
- } else if (!atomic_read(&rbd_dev->exists)) {
+ } else if (!test_bit(rbd_dev_flag_exists, &rbd_dev->flags)) {
rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); dout("request for non-existent snapshot"); result = -ENXIO;
@@ -2270,7 +2277,7 @@ struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
return NULL;
spin_lock_init(&rbd_dev->lock);
- atomic_set(&rbd_dev->exists, 0);
+ rbd_dev->flags = 0;
INIT_LIST_HEAD(&rbd_dev->node); INIT_LIST_HEAD(&rbd_dev->snaps); init_rwsem(&rbd_dev->header_rwsem);
@@ -2902,7 +2909,7 @@ static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
/* Existing snapshot not in the new snap context */
if (rbd_dev->spec->snap_id == snap->id)
- atomic_set(&rbd_dev->exists, 0);
+ set_bit(rbd_dev_flag_exists, &rbd_dev->flags);
rbd_remove_snap_dev(snap); dout("%ssnap id %llu has been removed\n",
rbd_dev->spec->snap_id == snap->id ?
Signature · 1 line
1.7.9.5
PATCH 2/2 rbd: prevent open for image being removed
In reply to Alex ElderAn open request for a mapped rbd image can arrive while removal of that mapping is underway. The control mutex and an open count is protect a mapped device that's in use from being removed. But it is possible for the removal of the mapping to reach the point of no return *after* a racing open has concluded it is OK to proceed. The result of this is not good.
Define and use a flag to indicate a mapping is getting removed to avoid this problem.
This addresses http://tracker.newdream.net/issues/3427
--- drivers/block/rbd.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/block/rbd.c +1 -0
@@ -264,6 +264,7 @@ struct rbd_device {enum rbd_dev_flags { rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ + rbd_dev_flag_removing,/* this mapping is being removed */ };static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */
@@ -351,17 +352,22 @@ static int rbd_dev_v2_refresh(struct rbd_device
*rbd_dev, u64 *hver);
static int rbd_open(struct block_device *bdev, fmode_t mode) {
struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
+ int ret = 0;
if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
return -EROFS;
mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
- (void) get_device(&rbd_dev->dev);
- set_device_ro(bdev, rbd_dev->mapping.read_only);
- rbd_dev->open_count++;
+ if (!test_bit(rbd_dev_flag_removing, &rbd_dev->flags)) {
+ (void) get_device(&rbd_dev->dev);
+ set_device_ro(bdev, rbd_dev->mapping.read_only);
+ rbd_dev->open_count++;
+ } else {
+ ret = -ENOENT;
+ }
mutex_unlock(&ctl_mutex);
- return 0;
+ return ret;
}
static int rbd_release(struct gendisk *disk, fmode_t mode)
@@ -3796,6 +3802,7 @@ static ssize_t rbd_remove(struct bus_type *bus,
ret = -EBUSY;
goto done;
}
+ set_bit(rbd_dev_flag_removing, &rbd_dev->flags);
rbd_remove_all_snaps(rbd_dev);
rbd_bus_del_dev(rbd_dev);
Signature · 1 line
1.7.9.5
Re: [PATCH 1/2] rbd: define flags field, use it for exists flag
In reply to Alex ElderI see that set_bit is atomic, but I don't see that test_bit is. Am I missing a subtlety?
Quoted 76 lines · Alex Elder
Define a new rbd device flags field, manipulated using atomic bit operations. Replace the use of the current "exists" flag with a bit in this new "flags" field.
Signed-off-by Alex Elder <[email protected]>--- drivers/block/rbd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/block/rbd.c +9
-2@@ -232,7 +232,7 @@ struct rbd_device {spinlock_t lock;/* queue lock */ struct rbd_image_header header; -atomic_t exists; +unsigned long flags;struct rbd_spec *spec;char *header_name;@@ -260,6 +260,12 @@ struct rbd_device {unsigned long open_count; }; +/* Flag bits for rbd_dev->flags */ + +enum rbd_dev_flags { + rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ +}; +static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */ static LIST_HEAD (rbd_dev_list);/* devices */ @@ -756,7 +762,8 @@ static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) goto done; rbd_dev->mapping.read_only = true; } -atomic_set (&rbd_dev->exists,1 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags); + done:return ret;}drivers/block/rbd.c +2
-2@@ -1654,7 +1661,7 @@ static void rbd_rq_fn(struct request_queue *q) snapc =ceph_get_snap_context (rbd_dev->header.snapc);up_read (&rbd_dev->header_rwsem);rbd_assert (snapc !=NULL ); - }else if (!atomic_read (&rbd_dev->exists)) { + }else if (!test_bit (rbd_dev_flag_exists, &rbd_dev->flags)) {rbd_assert (rbd_dev->spec->snap_id !=CEPH_NOSNAP );dout ("request for non-existent snapshot" ); result = -ENXIO ;@@ -2270,7 +2277,7 @@ struct rbd_device *rbd_dev_create(structrbd_client *rbdc, return NULL;spin_lock_init (&rbd_dev->lock); -atomic_set (&rbd_dev->exists,0 ); + rbd_dev->flags =0 ;INIT_LIST_HEAD (&rbd_dev->node);INIT_LIST_HEAD (&rbd_dev->snaps);init_rwsem(&rbd_dev->header_rwsem);drivers/block/rbd.c +1
-1@@ -2902,7 +2909,7 @@ static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)/* Existing snapshot not in the new snap context */ if (rbd_dev->spec->snap_id == snap->id) -atomic_set (&rbd_dev->exists,0 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags);rbd_remove_snap_dev (snap); dout("%ssnap id %llu has been removed\n",
rbd_dev->spec->snap_id==snap->id?
Re: [PATCH 1/2] rbd: define flags field, use it for exists flag
Quoted 2 lines · Dan Mick
I see that
set_bitis atomic, but I don't see thattest_bitis. Am I missing a subtlety?
That's an interesting observation. I'm certain it's safe, but I needed to research it a bit, and I still haven't verified it to my satisfaction.
I *think* (but please look over the following and see if you come to the same conclusion) that this operation doesn't need to be made atomic, because the implementation of the routines that implement the "set" operations guarantee their effects are visible once they are done.
But I'm not sure whether "visible" here means precisely that another CPU will be forced to go read the updated memory when it calls test_bit().
http://www.kernel.org/doc/Documentation/atomic_ops.txt
The section of interest can be found by looking for the sentence I'm talking about:
Likewise, the atomic bit operation must be visible globally before any subsequent memory operation is made visible.
It doesn't come right and explain it though. Please let me know what you think.
-Alex
Quoted 78 lines · Alex Elder
Quoted 77 lines
Define a new rbd device flags field, manipulated using atomic bit operations. Replace the use of the current "exists" flag with a bit in this new "flags" field.
Signed-off-by Alex Elder <[email protected]>--- drivers/block/rbd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/block/rbd.c +7
-1@@ -232,7 +232,7 @@ struct rbd_device {spinlock_t lock;/* queue lock */ struct rbd_image_header header; -atomic_t exists; +unsigned long flags;struct rbd_spec *spec;char *header_name;@@ -260,6 +260,12 @@ struct rbd_device {unsigned long open_count; }; +/* Flag bits for rbd_dev->flags */ + +enum rbd_dev_flags { + rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ +}; +static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */
static
LIST_HEAD(rbd_dev_list); /* devices */drivers/block/rbd.c +2
-1@@ -756,7 +762,8 @@ static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) goto done; rbd_dev->mapping.read_only = true; } -atomic_set (&rbd_dev->exists,1 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags); + done:return ret;}drivers/block/rbd.c +2
-2@@ -1654,7 +1661,7 @@ static void rbd_rq_fn(struct request_queue *q) snapc =ceph_get_snap_context (rbd_dev->header.snapc);up_read (&rbd_dev->header_rwsem);rbd_assert (snapc !=NULL ); - }else if (!atomic_read (&rbd_dev->exists)) { + }else if (!test_bit (rbd_dev_flag_exists, &rbd_dev->flags)) {rbd_assert (rbd_dev->spec->snap_id !=CEPH_NOSNAP );dout ("request for non-existent snapshot" ); result = -ENXIO ;@@ -2270,7 +2277,7 @@ struct rbd_device *rbd_dev_create(structrbd_client *rbdc, return NULL;spin_lock_init (&rbd_dev->lock); -atomic_set (&rbd_dev->exists,0 ); + rbd_dev->flags =0 ;INIT_LIST_HEAD (&rbd_dev->node);INIT_LIST_HEAD (&rbd_dev->snaps);init_rwsem(&rbd_dev->header_rwsem);drivers/block/rbd.c +1
-1@@ -2902,7 +2909,7 @@ static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)/* Existing snapshot not in the new snap context */ if (rbd_dev->spec->snap_id == snap->id) -atomic_set (&rbd_dev->exists,0 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags);rbd_remove_snap_dev (snap); dout("%ssnap id %llu has been removed\n",rbd_dev->spec->snap_id == snap->id ?
Re: [PATCH 1/2] rbd: define flags field, use it for exists flag
I think I agree that the claim is that the onus is on the set, and so I think the proposed code is safe.
Quoted 109 lines · Alex Elder
Quoted 2 lines · Dan Mick
I see that
set_bitis atomic, but I don't see thattest_bitis. Am I missing a subtlety?That's an interesting observation. I'm certain it's safe, but I needed to research it a bit, and I still haven't verified it to my satisfaction.
I *think* (but please look over the following and see if you come to the same conclusion) that this operation doesn't need to be made atomic, because the implementation of the routines that implement the "set" operations guarantee their effects are visible once they are done.
But I'm not sure whether "visible" here means precisely that another CPU will be forced to go read the updated memory when it calls
test_bit().http://www.kernel.org/doc/Documentation/atomic_ops.txt
The section of interest can be found by looking for the sentence I'm talking about:
Likewise, the atomic bit operation must be visible globally before any subsequent memory operation is made visible.It doesn't come right and explain it though. Please let me know what you think.
-Alex
Quoted 78 lines · Alex Elder
Quoted 77 lines
Define a new rbd device flags field, manipulated using atomic bit operations. Replace the use of the current "exists" flag with a bit in this new "flags" field.
Signed-off-by Alex Elder <[email protected]>--- drivers/block/rbd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/block/rbd.c +7
-1@@ -232,7 +232,7 @@ struct rbd_device {spinlock_t lock;/* queue lock */ struct rbd_image_header header; -atomic_t exists; +unsigned long flags;struct rbd_spec *spec;char *header_name;@@ -260,6 +260,12 @@ struct rbd_device {unsigned long open_count; }; +/* Flag bits for rbd_dev->flags */ + +enum rbd_dev_flags { + rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ +}; +static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */
static
LIST_HEAD(rbd_dev_list); /* devices */drivers/block/rbd.c +2
-1@@ -756,7 +762,8 @@ static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) goto done; rbd_dev->mapping.read_only = true; } -atomic_set (&rbd_dev->exists,1 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags); + done:return ret;}drivers/block/rbd.c +2
-2@@ -1654,7 +1661,7 @@ static void rbd_rq_fn(struct request_queue *q) snapc =ceph_get_snap_context (rbd_dev->header.snapc);up_read (&rbd_dev->header_rwsem);rbd_assert (snapc !=NULL ); - }else if (!atomic_read (&rbd_dev->exists)) { + }else if (!test_bit (rbd_dev_flag_exists, &rbd_dev->flags)) {rbd_assert (rbd_dev->spec->snap_id !=CEPH_NOSNAP );dout ("request for non-existent snapshot" ); result = -ENXIO ;@@ -2270,7 +2277,7 @@ struct rbd_device *rbd_dev_create(structrbd_client *rbdc, return NULL;spin_lock_init (&rbd_dev->lock); -atomic_set (&rbd_dev->exists,0 ); + rbd_dev->flags =0 ;INIT_LIST_HEAD (&rbd_dev->node);INIT_LIST_HEAD (&rbd_dev->snaps);init_rwsem(&rbd_dev->header_rwsem);drivers/block/rbd.c +1
-1@@ -2902,7 +2909,7 @@ static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)/* Existing snapshot not in the new snap context */ if (rbd_dev->spec->snap_id == snap->id) -atomic_set (&rbd_dev->exists,0 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags);rbd_remove_snap_dev (snap); dout("%ssnap id %llu has been removed\n",rbd_dev->spec->snap_id == snap->id ?
Re: [PATCH 1/2] rbd: define flags field, use it for exists flag
In reply to Alex ElderQuoted 76 lines · Alex Elder
Define a new rbd device flags field, manipulated using atomic bit operations. Replace the use of the current "exists" flag with a bit in this new "flags" field.
Signed-off-by Alex Elder <[email protected]>--- drivers/block/rbd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/block/rbd.c +9
-2@@ -232,7 +232,7 @@ struct rbd_device {spinlock_t lock;/* queue lock */ struct rbd_image_header header; -atomic_t exists; +unsigned long flags;struct rbd_spec *spec;char *header_name;@@ -260,6 +260,12 @@ struct rbd_device {unsigned long open_count; }; +/* Flag bits for rbd_dev->flags */ + +enum rbd_dev_flags { + rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ +}; +static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */ static LIST_HEAD (rbd_dev_list);/* devices */ @@ -756,7 +762,8 @@ static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) goto done; rbd_dev->mapping.read_only = true; } -atomic_set (&rbd_dev->exists,1 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags); + done:return ret;}drivers/block/rbd.c +2
-2@@ -1654,7 +1661,7 @@ static void rbd_rq_fn(struct request_queue *q) snapc =ceph_get_snap_context (rbd_dev->header.snapc);up_read (&rbd_dev->header_rwsem);rbd_assert (snapc !=NULL ); - }else if (!atomic_read (&rbd_dev->exists)) { + }else if (!test_bit (rbd_dev_flag_exists, &rbd_dev->flags)) {rbd_assert (rbd_dev->spec->snap_id !=CEPH_NOSNAP );dout ("request for non-existent snapshot" ); result = -ENXIO ;@@ -2270,7 +2277,7 @@ struct rbd_device *rbd_dev_create(structrbd_client *rbdc, return NULL;spin_lock_init (&rbd_dev->lock); -atomic_set (&rbd_dev->exists,0 ); + rbd_dev->flags =0 ;INIT_LIST_HEAD (&rbd_dev->node);INIT_LIST_HEAD (&rbd_dev->snaps);init_rwsem(&rbd_dev->header_rwsem);drivers/block/rbd.c +1
-1@@ -2902,7 +2909,7 @@ static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)/* Existing snapshot not in the new snap context */ if (rbd_dev->spec->snap_id == snap->id) -atomic_set (&rbd_dev->exists,0 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags);rbd_remove_snap_dev (snap); dout("%ssnap id %llu has been removed\n",
rbd_dev->spec->snap_id==snap->id?
Re: [PATCH 1/2] rbd: define flags field, use it for exists flag
In reply to Alex ElderQuoted 18 lines · Alex Elder
Quoted 2 lines · Dan Mick
I see that
set_bitis atomic, but I don't see thattest_bitis. Am I missing a subtlety?That's an interesting observation. I'm certain it's safe, but I needed to research it a bit, and I still haven't verified it to my satisfaction.
I *think* (but please look over the following and see if you come to the same conclusion) that this operation doesn't need to be made atomic, because the implementation of the routines that implement the "set" operations guarantee their effects are visible once they are done.
But I'm not sure whether "visible" here means precisely that another CPU will be forced to go read the updated memory when it calls
test_bit().
Quoted 5 lines
http://www.kernel.org/doc/Documentation/atomic_ops.txt
The section of interest can be found by looking for the sentence I'm talking about:
Likewise, the atomic bit operation must be visible globally before any subsequent memory operation is made visible.
I read that differently. I think that only applies to the test_and_set style operations mentioned directly above, not set_bit.
Documentation/memory-barriers.txt confirms this interpretation:
The following operations are potential problems as they do _not_ imply memory barriers, but might be used for implementing such things as UNLOCK-class operations:
atomic_set();
set_bit();
clear_bit();
change_bit();
With these the appropriate explicit memory barrier should be used if necessary (smp_mb__before_clear_bit() for instance).
And:
Memory operations that occur after an UNLOCK operation may appear to happen before it completes.
So I think we need a memory barrier before and after set_bit for the removing flag, but we don't need barriers for the exists flag, since it's a best-effort value that can't stop already-in-flight requests.
Josh
Quoted 84 lines
It doesn't come right and explain it though. Please let me know what you think.
-Alex
Quoted 78 lines · Alex Elder
Quoted 77 lines
Define a new rbd device flags field, manipulated using atomic bit operations. Replace the use of the current "exists" flag with a bit in this new "flags" field.
Signed-off-by Alex Elder <[email protected]>--- drivers/block/rbd.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/block/rbd.c +7
-1@@ -232,7 +232,7 @@ struct rbd_device {spinlock_t lock;/* queue lock */ struct rbd_image_header header; -atomic_t exists; +unsigned long flags;struct rbd_spec *spec;char *header_name;@@ -260,6 +260,12 @@ struct rbd_device {unsigned long open_count; }; +/* Flag bits for rbd_dev->flags */ + +enum rbd_dev_flags { + rbd_dev_flag_exists,/* mapped snapshot has not been deleted */ +}; +static DEFINE_MUTEX (ctl_mutex);/* Serialize open/close/setup/teardown */
static
LIST_HEAD(rbd_dev_list); /* devices */drivers/block/rbd.c +2
-1@@ -756,7 +762,8 @@ static int rbd_dev_set_mapping(struct rbd_device *rbd_dev) goto done; rbd_dev->mapping.read_only = true; } -atomic_set (&rbd_dev->exists,1 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags); + done:return ret;}drivers/block/rbd.c +2
-2@@ -1654,7 +1661,7 @@ static void rbd_rq_fn(struct request_queue *q) snapc =ceph_get_snap_context (rbd_dev->header.snapc);up_read (&rbd_dev->header_rwsem);rbd_assert (snapc !=NULL ); - }else if (!atomic_read (&rbd_dev->exists)) { + }else if (!test_bit (rbd_dev_flag_exists, &rbd_dev->flags)) {rbd_assert (rbd_dev->spec->snap_id !=CEPH_NOSNAP );dout ("request for non-existent snapshot" ); result = -ENXIO ;@@ -2270,7 +2277,7 @@ struct rbd_device *rbd_dev_create(structrbd_client *rbdc, return NULL;spin_lock_init (&rbd_dev->lock); -atomic_set (&rbd_dev->exists,0 ); + rbd_dev->flags =0 ;INIT_LIST_HEAD (&rbd_dev->node);INIT_LIST_HEAD (&rbd_dev->snaps);init_rwsem(&rbd_dev->header_rwsem);drivers/block/rbd.c +1
-1@@ -2902,7 +2909,7 @@ static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)/* Existing snapshot not in the new snap context */ if (rbd_dev->spec->snap_id == snap->id) -atomic_set (&rbd_dev->exists,0 ); +set_bit (rbd_dev_flag_exists, &rbd_dev->flags);rbd_remove_snap_dev (snap); dout("%ssnap id %llu has been removed\n",rbd_dev->spec->snap_id == snap->id ?
Re: [PATCH 1/2] rbd: define flags field, use it for exists flag
Quoted 42 lines · Josh Durgin
Quoted 18 lines · Alex Elder
Quoted 2 lines · Dan Mick
I see that
set_bitis atomic, but I don't see thattest_bitis. Am I missing a subtlety?That's an interesting observation. I'm certain it's safe, but I needed to research it a bit, and I still haven't verified it to my satisfaction.
I *think* (but please look over the following and see if you come to the same conclusion) that this operation doesn't need to be made atomic, because the implementation of the routines that implement the "set" operations guarantee their effects are visible once they are done.
But I'm not sure whether "visible" here means precisely that another CPU will be forced to go read the updated memory when it calls
test_bit().Quoted 5 lines
http://www.kernel.org/doc/Documentation/atomic_ops.txt
The section of interest can be found by looking for the sentence I'm talking about:
Likewise, the atomic bit operation must be visible globally before any subsequent memory operation is made visible.I read that differently. I think that only applies to the
test_and_setstyle operations mentioned directly above, notset_bit.
Documentation/memory-barriers.txtconfirms this interpretation:The following operations are potential problems as they do _not_ imply memory barriers, but might be used for implementing such things as UNLOCK-class operations:
atomic_set();
set_bit();
clear_bit();
change_bit();With these the appropriate explicit memory barrier should be used if necessary (
smp_mb__before_clear_bit()for instance).
Quoted 8 lines
And:
Memory operations that occur after an UNLOCK operation may appear to happen before it completes.
So I think we need a memory barrier before and after
set_bitfor the removing flag, but we don't need barriers for the exists flag, since it's a best-effort value that can't stop already-in-flight requests.
You know, I agree with your analysis but now I'm not sure even that's enough.
Here's the code in question (from the other patch):
Test side:
mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
if (!test_bit(rbd_dev_flag_removing, &rbd_dev->flags)) {
(void) get_device(&rbd_dev->dev);
set_device_ro(bdev, rbd_dev->mapping.read_only);
rbd_dev->open_count++;
} else {
ret = -ENOENT;
}
mutex_unlock(&ctl_mutex);Set side:
if (rbd_dev->open_count) {
ret = -EBUSY;
goto done;
}
set_bit(rbd_dev_flag_removing, &rbd_dev->flags);And here's the scenario I'm thinking about. Initially, suppose rbd_dev->open_count is 0 and the removing flag is not set.
OPENING THREAD UNMAPPING THREAD
-------------- ----------------
if (rbd_dev->open_count) {
/* not taken, it's zero */
ret = -EBUSY;
goto done;
}
if (!test_bit(removing)) {
/* not set yet! */ /* barrier won't help here */
set_bit(removing);
/* clean stuff up */
rbd_dev->open_count++; /* == kablooie == */
} else {
ret = -ENOENT;
}So I think we need a spinlock, or some other thing.
In any case, I'm not going to commit this change until we've had a chance to talk about it a little more.
-Alex