staging: zram: minimize `slot_free_lock' usage (v2)
PATCH 1/2 staging: zram: minimize `slot_free_lock' usage (v2)
Calling handle_pending_slot_free() for every RW operation may cause unneccessary slot_free_lock locking, because most likely process will see NULL slot_free_rq. handle_pending_slot_free() only when current detects that slot_free_rq is not NULL.
v2: protect handle_pending_slot_free() with zram rw_lock.
---
drivers/staging/zram/zram_drv.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +5 -2
@@ -517,6 +517,7 @@ static void handle_pending_slot_free(struct zram *zram) {struct zram_slot_free *free_rq; +down_write (&zram->lock);spin_lock (&zram->slot_free_lock);while (zram->slot_free_rq) { free_rq = zram->slot_free_rq;
@@ -525,6 +526,7 @@ static void handle_pending_slot_free(struct zram *zram)kfree (free_rq); }spin_unlock (&zram->slot_free_lock); +up_write (&zram->lock); } static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
@@ -532,14 +534,15 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, {int ret; +if (zram->slot_free_rq) +handle_pending_slot_free (zram); +if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock); }
Quoted 7 lines · Sergey Senozhatsky
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
zram->slot_free_lock protects zram->slot_free_rq but shouldn't the zram rw_lock be wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.
Is the down_read() in the original code be changed to down_write()?
regards,
dan carpenter
Quoted 8 lines
Quoted 7 lines
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
Quoted 7 lines
zram->slot_free_lockprotectszram->slot_free_rqbut shouldn't the zramrw_lockbe wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.Is the
down_read()in the original code be changed todown_write()?
I'm not touching locking around existing READ/WRITE commands.
the original code:
static void handle_pending_slot_free(struct zram *zram)
{
struct zram_slot_free *free_rq;
spin_lock(&zram->slot_free_lock);
while (zram->slot_free_rq) {
free_rq = zram->slot_free_rq;
zram->slot_free_rq = free_rq->next;
zram_free_page(zram, free_rq->index);
kfree(free_rq);
}
spin_unlock(&zram->slot_free_lock);
}static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
int offset, struct bio *bio, int rw)
{
int ret;
if (rw == READ) {
down_read(&zram->lock);
handle_pending_slot_free(zram);
ret = zram_bvec_read(zram, bvec, index, offset, bio);
up_read(&zram->lock);
} else {
down_write(&zram->lock);
handle_pending_slot_free(zram);
ret = zram_bvec_write(zram, bvec, index, offset);
up_write(&zram->lock);
}
return ret;
}the new one:
static void handle_pending_slot_free(struct zram *zram)
{
struct zram_slot_free *free_rq;
down_write(&zram->lock);
spin_lock(&zram->slot_free_lock);
while (zram->slot_free_rq) {
free_rq = zram->slot_free_rq;
zram->slot_free_rq = free_rq->next;
zram_free_page(zram, free_rq->index);
kfree(free_rq);
}
spin_unlock(&zram->slot_free_lock);
up_write(&zram->lock);
}static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
int offset, struct bio *bio, int rw)
{
int ret;
if (zram->slot_free_rq)
handle_pending_slot_free(zram);
if (rw == READ) {
down_read(&zram->lock);
ret = zram_bvec_read(zram, bvec, index, offset, bio);
up_read(&zram->lock);
} else {
down_write(&zram->lock);
ret = zram_bvec_write(zram, bvec, index, offset);
up_write(&zram->lock);
}
return ret;
}both READ and WRITE operations are still protected by down_read() for READ path and down_write() for WRITE path. however, there is no handle_pending_slot_free() and zram->slot_free_lock locking on every READ/WRITE, instead handle_pending_slot_free() is called only when zram->slot_free_rq is not NULL. handle_pending_slot_free() in turn protects zram_free_page() call by down_write(), so no READ/WRITE operations are affected.
-ss
Quoted 13 lines · Sergey Senozhatsky
Quoted 13 lines
Quoted 7 lines
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
zram->slot_free_lockprotectszram->slot_free_rqbut shouldn't the zramrw_lockbe wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.
Quoted 5 lines
Quoted 2 lines
Is the
down_read()in the original code be changed todown_write()?I'm not touching locking around existing READ/WRITE commands.
Your patch does change the locking because now instead of taking the zram lock once it takes it and then drops it and then retakes it. This looks potentially racy to me but I don't know the code so I will defer to any zram maintainer.
1) You haven't given us any performance numbers so it's not clear if the locking is even a problem.
2) The v2 patch introduces an obvious deadlock in zram_slot_free() because now we take the rw_lock twice. Fix your testing to catch this kind of bug next time.
3) Explain why it is safe to test zram->slot_free_rq when we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.
regards,
dan carpenter
Quoted 17 lines · Dan Carpenter
Quoted 16 lines · Sergey Senozhatsky
Quoted 15 lines
Quoted 7 lines
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
zram->slot_free_lockprotectszram->slot_free_rqbut shouldn't the zramrw_lockbe wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.Is the
down_read()in the original code be changed todown_write()?
Quoted 7 lines
Quoted 2 lines
I'm not touching locking around existing READ/WRITE commands.
Your patch does change the locking because now instead of taking the zram lock once it takes it and then drops it and then retakes it. This looks potentially racy to me but I don't know the code so I will defer to any zram maintainer.
You're right. Nothing prevents zram_slot_free_notify() to repopulate the free slot queue while we drop the lock.
Actually, the original code is already racy. handle_pending_slot_free() modifies zram->table while holding only a read lock. It needs to hold a write lock to do that. Using down_write for all requests would obviously fix that, but at the cost of read performance.
Quoted 15 lines
1) You haven't given us any performance numbers so it's not clear if the locking is even a problem.
2) The v2 patch introduces an obvious deadlock in
zram_slot_free()because now we take therw_locktwice. Fix your testing to catch this kind of bug next time.3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.regards,
dan carpenter
Quoted 26 lines · Jerome Marchand
Quoted 17 lines · Dan Carpenter
Quoted 16 lines · Sergey Senozhatsky
Quoted 15 lines
Quoted 7 lines
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
zram->slot_free_lockprotectszram->slot_free_rqbut shouldn't the zramrw_lockbe wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.Is the
down_read()in the original code be changed todown_write()?Quoted 7 lines
Quoted 2 lines
I'm not touching locking around existing READ/WRITE commands.
Your patch does change the locking because now instead of taking the zram lock once it takes it and then drops it and then retakes it. This looks potentially racy to me but I don't know the code so I will defer to any zram maintainer.
Quoted 7 lines
You're right. Nothing prevents
zram_slot_free_notify()to repopulate the free slot queue while we drop the lock.Actually, the original code is already racy.
handle_pending_slot_free()modifieszram->tablewhile holding only a read lock. It needs to hold a write lock to do that. Usingdown_writefor all requests would obviously fix that, but at the cost of read performance.
Now I think we can drop the call to handle_pending_slot_free() in zram_bvec_rw() altogether. As long as the write lock is held when handle_pending_slot_free() is called, there is no race. It's no different from any write request and the current code handles R/W concurrency already.
Jerome
Quoted 23 lines
Quoted 9 lines
1) You haven't given us any performance numbers so it's not clear if the locking is even a problem.
2) The v2 patch introduces an obvious deadlock in
zram_slot_free()because now we take therw_locktwice. Fix your testing to catch this kind of bug next time.Quoted 7 lines
3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.regards,
dan carpenter--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 35 lines · Jerome Marchand
Quoted 26 lines · Jerome Marchand
Quoted 17 lines · Dan Carpenter
Quoted 16 lines · Sergey Senozhatsky
Quoted 15 lines
Quoted 7 lines
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
zram->slot_free_lockprotectszram->slot_free_rqbut shouldn't the zramrw_lockbe wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.Is the
down_read()in the original code be changed todown_write()?Quoted 7 lines
Quoted 2 lines
I'm not touching locking around existing READ/WRITE commands.
Your patch does change the locking because now instead of taking the zram lock once it takes it and then drops it and then retakes it. This looks potentially racy to me but I don't know the code so I will defer to any zram maintainer.
Quoted 7 lines
You're right. Nothing prevents
zram_slot_free_notify()to repopulate the free slot queue while we drop the lock.Actually, the original code is already racy.
handle_pending_slot_free()modifieszram->tablewhile holding only a read lock. It needs to hold a write lock to do that. Usingdown_writefor all requests would obviously fix that, but at the cost of read performance.
Quoted 5 lines
Now I think we can drop the call to
handle_pending_slot_free()inzram_bvec_rw()altogether. As long as the write lock is held whenhandle_pending_slot_free()is called, there is no race. It's no different from any write request and the current code handles R/W concurrency already.
Yes, I think that can work.
To summarize, there should be 3 patches:
1) handle_pending_slot_free() in zram_bvec_rw() (as suggested by Jerome Marchand)
2) handle_pending_slot_free() race with reset (found by Dan Carpenter)
3) drop init_done and use init_done()
I'll prepare a patches later today.
-ss
Quoted 26 lines
Jerome
Quoted 23 lines
Quoted 9 lines
1) You haven't given us any performance numbers so it's not clear if the locking is even a problem.
2) The v2 patch introduces an obvious deadlock in
zram_slot_free()because now we take therw_locktwice. Fix your testing to catch this kind of bug next time.Quoted 7 lines
3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.regards,
dan carpenter--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Btw, the [email protected] list seems to be down again. I still have not recieved the v3 patch. Use the [email protected] email list instead.
regards,
dan carpenter
Greg Kroah-Hartman
maintainerQuoted 3 lines · Dan Carpenter
Btw, the [email protected] list seems to be down again. I still have not recieved the v3 patch. Use the [email protected] email list instead.
They are the same "list", just different DNS entries. I'll go poke the sysadmin to find out what's going on...
PATCH 1/2 staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
In reply to Dan CarpenterDan Carpenter noted that handle_pending_slot_free() is racy with zram_reset_device(). Take write init_lock in zram_slot_free(), thus preventing any concurrent zram_slot_free(), zram_bvec_rw() or zram_reset_device(). This also allows to safely check zram->init_done in handle_pending_slot_free().
Initial intention was to minimze number of handle_pending_slot_free() call from zram_bvec_rw(), which were slowing down READ requests due to slot_free_lock spin lock. Jerome Marchand suggested to remove handle_pending_slot_free() from zram_bvec_rw().
---
drivers/staging/zram/zram_drv.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/staging/zram/zram_drv.c +5 -8
@@ -521,7 +521,8 @@ static void handle_pending_slot_free(struct zram *zram)while (zram->slot_free_rq) { free_rq = zram->slot_free_rq; zram->slot_free_rq = free_rq->next; -zram_free_page (zram, free_rq->index); +if (zram->init_done) +zram_free_page (zram, free_rq->index);kfree (free_rq); }spin_unlock (&zram->slot_free_lock);
@@ -534,16 +535,13 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock); } -return ret; }
@@ -750,12 +748,11 @@ error:static void zram_slot_free (struct work_struct *work) { -struct zram *zram; + struct zram *zram = container_of(work, struct zram, free_work); - zram = container_of(work, struct zram, free_work); -down_write (&zram->lock); +down_write (&zram->init_lock);handle_pending_slot_free (zram); -up_write (&zram->lock); +up_write (&zram->init_lock); }static void add_slot_free (struct zram *zram,struct zram_slot_free *free_rq)
Re: [PATCH 1/2] staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
Quoted 6 lines · Sergey Senozhatsky
Dan Carpenter noted that
handle_pending_slot_free()is racy withzram_reset_device(). Take writeinit_lockinzram_slot_free(), thus preventing any concurrentzram_slot_free(),zram_bvec_rw()orzram_reset_device(). This also allows to safely checkzram->init_doneinhandle_pending_slot_free().
Quoted 7 lines
Initial intention was to minimze number of
handle_pending_slot_free()call fromzram_bvec_rw(), which were slowing down READ requests due toslot_free_lockspin lock. Jerome Marchand suggested to removehandle_pending_slot_free()fromzram_bvec_rw().Signed-off-by Sergey Senozhatsky <[email protected]>
I have multiple versions of this and the other zram patches from you, with no idea of which to accept.
So, I'm going to drop them all, can you please resend what you wish to submit, and in the future, be a bit more obvious with your "vN" markings?
thanks,
greg k-h
Re: [PATCH 1/2] staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
Quoted 12 lines · Greg KH
Quoted 11 lines · Sergey Senozhatsky
Dan Carpenter noted that
handle_pending_slot_free()is racy withzram_reset_device(). Take writeinit_lockinzram_slot_free(), thus preventing any concurrentzram_slot_free(),zram_bvec_rw()orzram_reset_device(). This also allows to safely checkzram->init_doneinhandle_pending_slot_free().Initial intention was to minimze number of
handle_pending_slot_free()call fromzram_bvec_rw(), which were slowing down READ requests due toslot_free_lockspin lock. Jerome Marchand suggested to removehandle_pending_slot_free()fromzram_bvec_rw().
Quoted 5 lines
Quoted 2 lines
Signed-off-by Sergey Senozhatsky <[email protected]>I have multiple versions of this and the other zram patches from you, with no idea of which to accept.
yes, please, drop all patches. I did not Cc you in these two patches to stop spamming your inbox with multiply versions. I will send them back to you as soon as I get positive feedback.
Quoted 4 lines
So, I'm going to drop them all, can you please resend what you wish to submit, and in the future, be a bit more obvious with your "vN" markings?
sorry for that.
-ss
Quoted 4 lines
thanks,
greg k-h
Re: [PATCH 1/2] staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
In reply to Sergey SenozhatskyHello Sergey,
Sorry for really slow response. I was really busy by internal works and Thanks for pointing the BUG, Dan, Jerome and Sergey. I read your threads roughly so I may miss something. If so, sorry for that. Anyway I will put my opinion.
Quoted 2 lines · Sergey Senozhatsky
Dan Carpenter noted that
handle_pending_slot_free()is racy withzram_reset_device(). Take writeinit_lockinzram_slot_free(), thus
Right but "init_lock" is what I really want to remove.
Yes. It's just read-side lock so most of time it doesn't hurt us but it makes code very complicated and deadlock prone so I'd like to replace it with RCU. Yeah, It's off topic but just let me put my opinion in future direction.
Abought the bug, how about moving flush_work below down_write(init_lock)? zram_make_request is already closed by init_lock and we have a rule about lock ordering as following so I don't see any problem.
init_lock
zram->lock
Quoted 32 lines
preventing any concurrent
zram_slot_free(),zram_bvec_rw()orzram_reset_device(). This also allows to safely checkzram->init_doneinhandle_pending_slot_free().Initial intention was to minimze number of
handle_pending_slot_free()call fromzram_bvec_rw(), which were slowing down READ requests due toslot_free_lockspin lock. Jerome Marchand suggested to removehandle_pending_slot_free()fromzram_bvec_rw().Signed-off-by Sergey Senozhatsky <[email protected]>---
drivers/staging/zram/zram_drv.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/staging/zram/zram_drv.c +2
-1@@ -521,7 +521,8 @@ static void handle_pending_slot_free(struct zram *zram)while (zram->slot_free_rq) { free_rq = zram->slot_free_rq; zram->slot_free_rq = free_rq->next; -zram_free_page (zram, free_rq->index); +if (zram->init_done) +zram_free_page (zram, free_rq->index);kfree (free_rq); }spin_unlock (&zram->slot_free_lock);
drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537
Quoted 4 lines
drivers/staging/zram/zram_drv.c +0
-1@@ -534,16 +535,13 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram);
Read side is okay but actually I have a nitpick.
If someone poll a block in zram-swap device, he would see a block has zero value suddenly although there was no I/O.(I don't want to argue it's sane user or not, anyway) it never happens on real block device and it never happens on zram-block device. Only it can happen zram-swap device. And such behavior was there since we introduced swap_slot_free_notify. (off-topic: I'd like to remove it because it makes tight coupling between zram and swap and obviously, it was layering violation function) so now, I don't have strong objection.
The idea is to remove swap_slot_free_notify is to use frontswap when user want to use zram as swap so zram can be notified when the block lose the owner but still we should solve the mutex problem in notify handler.
drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 542
Quoted 5 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); -handle_pending_slot_free (zram);
Why did you remove this in write-side?
We can't expect when the work will trigger. It means the work could remove valid block under the us.
Quoted 8 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock); } -return ret; }
drivers/staging/zram/zram_drv.c error: · line 753
Quoted 9 lines
drivers/staging/zram/zram_drv.c +2
-3@@ -750,12 +748,11 @@ error:static void zram_slot_free (struct work_struct *work) { -struct zram *zram; + struct zram *zram = container_of(work, struct zram, free_work); - zram = container_of(work, struct zram, free_work); -down_write (&zram->lock); +down_write (&zram->init_lock);
I don't like this.
Primary problem is we should handle it as atomic so that we should use spinlock instead of mutex. Yeah, /me kicks his ass. From the beginning, I should solve this problem as that way.
The simple solution popped from my mind is that
drivers/staging/zram/zram_drv.c +5 -2
@@ -534,11 +534,14 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); +/* +* We should free pending slot. Otherwise it would +* free valid blocks under the us. +*/ handle_pending_slot_free (zram); ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock);
@@ -552,7 +555,6 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)size_t index;struct zram_meta *meta; -flush_work (&zram->free_work);down_write (&zram->init_lock);if (!zram->init_done) {
@@ -560,6 +562,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)return ; } +flush_work (&zram->free_work); meta = zram->meta; zram->init_done =0 ;
But more ideal way I am thinking now is
1) replace init_lock with RCU lock
2) introduce new meta atmoic lock instead of zram->mutex, which is very coarse-grained.
3) use atmoic lock in notify handler.
Signature · 2 lines
Kind regards, Minchan Kim
Re: [PATCH 1/2] staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
Hello,
Quoted 77 lines · Minchan Kim
Hello Sergey,
Sorry for really slow response. I was really busy by internal works and Thanks for pointing the BUG, Dan, Jerome and Sergey. I read your threads roughly so I may miss something. If so, sorry for that. Anyway I will put my opinion.
Quoted 2 lines · Sergey Senozhatsky
Dan Carpenter noted that
handle_pending_slot_free()is racy withzram_reset_device(). Take writeinit_lockinzram_slot_free(), thusRight but "
init_lock" is what I really want to remove.
Yes. It's just read-side lock so most of time it doesn't hurt us but it makes code very complicated and deadlock prone so I'd like to replace it with RCU. Yeah, It's off topic but just let me put my opinion in future direction.Abought the bug, how about moving
flush_workbelowdown_write(init_lock)?zram_make_requestis already closed byinit_lockand we have a rule about lock ordering as following so I don't see any problem.
init_lockzram->lockQuoted 32 lines
preventing any concurrent
zram_slot_free(),zram_bvec_rw()orzram_reset_device(). This also allows to safely checkzram->init_doneinhandle_pending_slot_free().Initial intention was to minimze number of
handle_pending_slot_free()call fromzram_bvec_rw(), which were slowing down READ requests due toslot_free_lockspin lock. Jerome Marchand suggested to removehandle_pending_slot_free()fromzram_bvec_rw().Signed-off-by Sergey Senozhatsky <[email protected]>---
drivers/staging/zram/zram_drv.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/staging/zram/zram_drv.c +2
-1@@ -521,7 +521,8 @@ static void handle_pending_slot_free(struct zram *zram)while (zram->slot_free_rq) { free_rq = zram->slot_free_rq; zram->slot_free_rq = free_rq->next; -zram_free_page (zram, free_rq->index); +if (zram->init_done) +zram_free_page (zram, free_rq->index);kfree (free_rq); }spin_unlock (&zram->slot_free_lock);drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537
Quoted 4 lines
drivers/staging/zram/zram_drv.c +0
-1@@ -534,16 +535,13 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram);Read side is okay but actually I have a nitpick.
If someone poll a block in zram-swap device, he would see a block has zero value suddenly although there was no I/O.(I don't want to argue it's sane user or not, anyway) it never happens on real block device and it never happens on zram-block device. Only it can happen zram-swap device. And such behavior was there since we introducedswap_slot_free_notify. (off-topic: I'd like to remove it because it makes tight coupling between zram and swap and obviously, it was layering violation function) so now, I don't have strong objection.The idea is to remove
swap_slot_free_notifyis to use frontswap when user want to use zram as swap so zram can be notified when the block lose the owner but still we should solve the mutex problem in notify handler.
Quoted 10 lines
drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Quoted 5 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); -handle_pending_slot_free (zram);Why did you remove this in write-side?
We can't expect when the work will trigger. It means the work could remove valid block under the us.
not sure I understand how.zram_slot_free() takes down_write(&zram->init_lock) and zram_make_request() takes down_read(&zram->init_lock), thus zram_slot_free() can not concurrently work with any RW requests. RW requests are under read() lock and zram_slot_free() is under write() lock.
Quoted 55 lines
Quoted 8 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock); } -return ret; }drivers/staging/zram/zram_drv.c error: · line 753
Quoted 9 lines
drivers/staging/zram/zram_drv.c +2
-3@@ -750,12 +748,11 @@ error:static void zram_slot_free (struct work_struct *work) { -struct zram *zram; + struct zram *zram = container_of(work, struct zram, free_work); - zram = container_of(work, struct zram, free_work); -down_write (&zram->lock); +down_write (&zram->init_lock);I don't like this.
Primary problem is we should handle it as atomic so that we should use spinlock instead of mutex. Yeah, /me kicks his ass. From the beginning, I should solve this problem as that way.The simple solution popped from my mind is that
drivers/staging/zram/zram_drv.c +4
-2@@ -534,11 +534,14 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); +/* +* We should free pending slot. Otherwise it would +* free valid blocks under the us. +*/ handle_pending_slot_free (zram); ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock);@@ -552,7 +555,6 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)size_t index;struct zram_meta *meta; -flush_work (&zram->free_work);down_write (&zram->init_lock);if (!zram->init_done) {
drivers/staging/zram/zram_drv.c static void zram_reset_device(struct zram *zram, bool reset_capacity) · line 567
Quoted 6 lines
drivers/staging/zram/zram_drv.c +1
-0@@ -560,6 +562,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)return ; } +flush_work (&zram->free_work); meta = zram->meta; zram->init_done =0 ;
this one looks ok to me.
-ss
Quoted 10 lines
drivers/staging/zram/zram_drv.c +0
-0But more ideal way I am thinking now is1) replace
init_lockwith RCU lock
2) introduce new meta atmoic lock instead ofzram->mutex, which is very coarse-grained.
3) use atmoic lock in notify handler.Signature · 2 lines
Kind regards, Minchan Kim
Re: [PATCH 1/2] staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
Quoted 93 lines · Sergey Senozhatsky
Hello,
Quoted 77 lines · Minchan Kim
Hello Sergey,
Sorry for really slow response. I was really busy by internal works and Thanks for pointing the BUG, Dan, Jerome and Sergey. I read your threads roughly so I may miss something. If so, sorry for that. Anyway I will put my opinion.
Quoted 2 lines · Sergey Senozhatsky
Dan Carpenter noted that
handle_pending_slot_free()is racy withzram_reset_device(). Take writeinit_lockinzram_slot_free(), thusRight but "
init_lock" is what I really want to remove.
Yes. It's just read-side lock so most of time it doesn't hurt us but it makes code very complicated and deadlock prone so I'd like to replace it with RCU. Yeah, It's off topic but just let me put my opinion in future direction.Abought the bug, how about moving
flush_workbelowdown_write(init_lock)?zram_make_requestis already closed byinit_lockand we have a rule about lock ordering as following so I don't see any problem.
init_lockzram->lockQuoted 32 lines
preventing any concurrent
zram_slot_free(),zram_bvec_rw()orzram_reset_device(). This also allows to safely checkzram->init_doneinhandle_pending_slot_free().Initial intention was to minimze number of
handle_pending_slot_free()call fromzram_bvec_rw(), which were slowing down READ requests due toslot_free_lockspin lock. Jerome Marchand suggested to removehandle_pending_slot_free()fromzram_bvec_rw().Signed-off-by Sergey Senozhatsky <[email protected]>---
drivers/staging/zram/zram_drv.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/staging/zram/zram_drv.c +2
-1@@ -521,7 +521,8 @@ static void handle_pending_slot_free(struct zram *zram)while (zram->slot_free_rq) { free_rq = zram->slot_free_rq; zram->slot_free_rq = free_rq->next; -zram_free_page (zram, free_rq->index); +if (zram->init_done) +zram_free_page (zram, free_rq->index);kfree (free_rq); }spin_unlock (&zram->slot_free_lock);drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537
Quoted 4 lines
drivers/staging/zram/zram_drv.c +0
-1@@ -534,16 +535,13 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram);Read side is okay but actually I have a nitpick.
If someone poll a block in zram-swap device, he would see a block has zero value suddenly although there was no I/O.(I don't want to argue it's sane user or not, anyway) it never happens on real block device and it never happens on zram-block device. Only it can happen zram-swap device. And such behavior was there since we introducedswap_slot_free_notify. (off-topic: I'd like to remove it because it makes tight coupling between zram and swap and obviously, it was layering violation function) so now, I don't have strong objection.The idea is to remove
swap_slot_free_notifyis to use frontswap when user want to use zram as swap so zram can be notified when the block lose the owner but still we should solve the mutex problem in notify handler.Quoted 10 lines
drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Quoted 5 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); -handle_pending_slot_free (zram);Why did you remove this in write-side?
We can't expect when the work will trigger. It means the work could remove valid block under the us.
Quoted 5 lines
not sure I understand how.
zram_slot_free()takesdown_write(&zram->init_lock)andzram_make_request()takesdown_read(&zram->init_lock), thuszram_slot_free()can not concurrently work with any RW requests. RW requests are underread()lock andzram_slot_free()is underwrite()lock.
Let's consider example.
Swap subsystem asked to zram "A" block free from now by swap_slot_free_notify but zram had been pended it without real freeing.
Swap reused "A" block for new data because "A" block was free but request pended for a long time just handled and zram blindly free new data on the "A" block. :( That's why we should handle pending free request right before zram-write.
Another try to optimize the lock overhead is to check the block is pending for free right before zram_free_page in write path. If so, we should remove pending reuqest from slot_free_rq list to prevent valid block later. But for that case, we need more complex data structure to find the block fast and many checking code right before zram_free_page so that it would make code rather complicated.
So, do you have any real workload for us to consider it's really troublesome? Otherwise, I'd like to keep the code simple.
Quoted 59 lines
Quoted 59 lines
Quoted 9 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock); } -return ret; }drivers/staging/zram/zram_drv.c error: · line 753
Quoted 9 lines
drivers/staging/zram/zram_drv.c +2
-3@@ -750,12 +748,11 @@ error:static void zram_slot_free (struct work_struct *work) { -struct zram *zram; + struct zram *zram = container_of(work, struct zram, free_work); - zram = container_of(work, struct zram, free_work); -down_write (&zram->lock); +down_write (&zram->init_lock);I don't like this.
Primary problem is we should handle it as atomic so that we should use spinlock instead of mutex. Yeah, /me kicks his ass. From the beginning, I should solve this problem as that way.The simple solution popped from my mind is that
drivers/staging/zram/zram_drv.c +4
-2@@ -534,11 +534,14 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); +/* +* We should free pending slot. Otherwise it would +* free valid blocks under the us. +*/ handle_pending_slot_free (zram); ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock);@@ -552,7 +555,6 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)size_t index;struct zram_meta *meta; -flush_work (&zram->free_work);down_write (&zram->init_lock);if (!zram->init_done) {@@ -560,6 +562,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)return ; }
Quoted 5 lines
drivers/staging/zram/zram_drv.c static void zram_reset_device(struct zram *zram, bool reset_capacity) · line 567
Quoted 3 lines
drivers/staging/zram/zram_drv.c +1
-0+flush_work (&zram->free_work); meta = zram->meta; zram->init_done =0 ;this one looks ok to me.
If you don't mind, I'd like to go with this.
Thanks.
Quoted 18 lines
-ssQuoted 10 lines
drivers/staging/zram/zram_drv.c +0
-0But more ideal way I am thinking now is1) replace
init_lockwith RCU lock
2) introduce new meta atmoic lock instead ofzram->mutex, which is very coarse-grained.
3) use atmoic lock in notify handler.Signature · 2 lines
Kind regards, Minchan Kim--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 2 lines
Kind regards, Minchan Kim
Re: [PATCH 1/2] staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
Quoted 171 lines · Minchan Kim
Quoted 90 lines · Minchan Kim
Quoted 77 lines
Hello Sergey,
Sorry for really slow response. I was really busy by internal works and Thanks for pointing the BUG, Dan, Jerome and Sergey. I read your threads roughly so I may miss something. If so, sorry for that. Anyway I will put my opinion.
Quoted 2 lines · Sergey Senozhatsky
Dan Carpenter noted that
handle_pending_slot_free()is racy withzram_reset_device(). Take writeinit_lockinzram_slot_free(), thusRight but "
init_lock" is what I really want to remove.
Yes. It's just read-side lock so most of time it doesn't hurt us but it makes code very complicated and deadlock prone so I'd like to replace it with RCU. Yeah, It's off topic but just let me put my opinion in future direction.Abought the bug, how about moving
flush_workbelowdown_write(init_lock)?zram_make_requestis already closed byinit_lockand we have a rule about lock ordering as following so I don't see any problem.
init_lockzram->lockQuoted 32 lines
preventing any concurrent
zram_slot_free(),zram_bvec_rw()orzram_reset_device(). This also allows to safely checkzram->init_doneinhandle_pending_slot_free().Initial intention was to minimze number of
handle_pending_slot_free()call fromzram_bvec_rw(), which were slowing down READ requests due toslot_free_lockspin lock. Jerome Marchand suggested to removehandle_pending_slot_free()fromzram_bvec_rw().Signed-off-by Sergey Senozhatsky <[email protected]>---
drivers/staging/zram/zram_drv.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/staging/zram/zram_drv.c +2
-1@@ -521,7 +521,8 @@ static void handle_pending_slot_free(struct zram *zram)while (zram->slot_free_rq) { free_rq = zram->slot_free_rq; zram->slot_free_rq = free_rq->next; -zram_free_page (zram, free_rq->index); +if (zram->init_done) +zram_free_page (zram, free_rq->index);kfree (free_rq); }spin_unlock (&zram->slot_free_lock);drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537
Quoted 4 lines
drivers/staging/zram/zram_drv.c +0
-1@@ -534,16 +535,13 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram);Read side is okay but actually I have a nitpick.
If someone poll a block in zram-swap device, he would see a block has zero value suddenly although there was no I/O.(I don't want to argue it's sane user or not, anyway) it never happens on real block device and it never happens on zram-block device. Only it can happen zram-swap device. And such behavior was there since we introducedswap_slot_free_notify. (off-topic: I'd like to remove it because it makes tight coupling between zram and swap and obviously, it was layering violation function) so now, I don't have strong objection.The idea is to remove
swap_slot_free_notifyis to use frontswap when user want to use zram as swap so zram can be notified when the block lose the owner but still we should solve the mutex problem in notify handler.Quoted 10 lines
drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Quoted 5 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); -handle_pending_slot_free (zram);Why did you remove this in write-side?
We can't expect when the work will trigger. It means the work could remove valid block under the us.Quoted 5 lines
not sure I understand how.
zram_slot_free()takesdown_write(&zram->init_lock)andzram_make_request()takesdown_read(&zram->init_lock), thuszram_slot_free()can not concurrently work with any RW requests. RW requests are underread()lock andzram_slot_free()is underwrite()lock.Let's consider example.
Swap subsystem asked to zram "A" block free from now byswap_slot_free_notifybut zram had been pended it without real freeing.
Swap reused "A" block for new data because "A" block was free but request pended for a long time just handled and zram blindly free new data on the "A" block. :( That's why we should handle pending free request right before zram-write.Another try to optimize the lock overhead is to check the block is pending for free right before
zram_free_pagein write path. If so, we should remove pending reuqest fromslot_free_rqlist to prevent valid block later. But for that case, we need more complex data structure to find the block fast and many checking code right beforezram_free_pageso that it would make code rather complicated.So, do you have any real workload for us to consider it's really troublesome? Otherwise, I'd like to keep the code simple.
Quoted 59 lines
Quoted 59 lines
Quoted 9 lines
drivers/staging/zram/zram_drv.c +0
-1ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock); } -return ret; }drivers/staging/zram/zram_drv.c error: · line 753
Quoted 9 lines
drivers/staging/zram/zram_drv.c +2
-3@@ -750,12 +748,11 @@ error:static void zram_slot_free (struct work_struct *work) { -struct zram *zram; + struct zram *zram = container_of(work, struct zram, free_work); - zram = container_of(work, struct zram, free_work); -down_write (&zram->lock); +down_write (&zram->init_lock);I don't like this.
Primary problem is we should handle it as atomic so that we should use spinlock instead of mutex. Yeah, /me kicks his ass. From the beginning, I should solve this problem as that way.The simple solution popped from my mind is that
drivers/staging/zram/zram_drv.c +4
-2@@ -534,11 +534,14 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,if (rw ==READ ) {down_read (&zram->lock); -handle_pending_slot_free (zram); ret =zram_bvec_read (zram, bvec, index, offset, bio);up_read (&zram->lock); }else {down_write (&zram->lock); +/* +* We should free pending slot. Otherwise it would +* free valid blocks under the us. +*/ handle_pending_slot_free (zram); ret =zram_bvec_write (zram, bvec, index, offset);up_write (&zram->lock);@@ -552,7 +555,6 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)size_t index;struct zram_meta *meta; -flush_work (&zram->free_work);down_write (&zram->init_lock);if (!zram->init_done) {@@ -560,6 +562,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)return ; }
Quoted 8 lines
Quoted 5 lines
drivers/staging/zram/zram_drv.c static void zram_reset_device(struct zram *zram, bool reset_capacity) · line 567
Quoted 3 lines
drivers/staging/zram/zram_drv.c +1
-0+flush_work (&zram->free_work); meta = zram->meta; zram->init_done =0 ;this one looks ok to me.
If you don't mind, I'd like to go with this.
Thanks.
sure, no objections.
-ss
Quoted 24 lines
Quoted 19 lines
-ssQuoted 10 lines
drivers/staging/zram/zram_drv.c +0
-0But more ideal way I am thinking now is1) replace
init_lockwith RCU lock
2) introduce new meta atmoic lock instead ofzram->mutex, which is very coarse-grained.
3) use atmoic lock in notify handler.Signature · 2 lines
Kind regards, Minchan Kim--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/Signature · 2 lines
Kind regards, Minchan Kim
PATCH 2/2 staging: zram: remove init_done from zram struct (v3)
In reply to Dan Carpenterzram->init_done' in fact mimics zram->meta != NULL' value. Introduce init_done() function that checks zram->meta (iow, checks if initialisation was performed), so `zram->init_done' can be removed.
v3: init_done() in handle_pending_slot_free()
v2: introduce init_done()
---
drivers/staging/zram/zram_drv.c | 23 ++++++++++++----------- drivers/staging/zram/zram_drv.h | 1 - 2 files changed, 12 insertions(+), 12 deletions(-)
drivers/staging/zram/zram_drv.c +12 -11
@@ -42,6 +42,11 @@ static struct zram *zram_devices;/* Module params (documentation at end) */ static unsigned int num_devices =1 ; +static inline int init_done (struct zram *zram) +{ +return zram->meta !=NULL ; +} +static inline struct zram *dev_to_zram (struct device *dev) {return (struct zram *)dev_to_disk (dev)->private_data;
@@ -60,7 +65,7 @@ static ssize_t initstate_show(struct device *dev, {struct zram *zram =dev_to_zram (dev); -return sprintf (buf,"%u\n" , zram->init_done); +return sprintf (buf,"%u\n" ,init_done (zram)); } static ssize_t num_reads_show(struct device *dev,
@@ -133,7 +138,7 @@ static ssize_t mem_used_total_show(struct device *dev,struct zram_meta *meta = zram->meta;down_read (&zram->init_lock); -if (zram->init_done) +if (init_done (zram)) val =zs_get_total_size_bytes (meta->mem_pool);up_read (&zram->init_lock);
@@ -521,7 +526,7 @@ static void handle_pending_slot_free(struct zram *zram)while (zram->slot_free_rq) { free_rq = zram->slot_free_rq; zram->slot_free_rq = free_rq->next; -if (zram->init_done) +if (init_done (zram))zram_free_page (zram, free_rq->index);kfree (free_rq); }
@@ -553,14 +558,12 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)flush_work (&zram->free_work);down_write (&zram->init_lock); -if (!zram->init_done) { +if (!init_done (zram)) {up_write (&zram->init_lock);return ; } meta = zram->meta; - zram->init_done =0 ; -/* Free all pages that are still in this zram device */ for (index =0 ; index < zram->disksize >>PAGE_SHIFT ; index++) {unsigned long handle = meta->table[index].handle;
@@ -599,9 +602,7 @@ static void zram_init_device(struct zram *zram, struct zram_meta *meta)/* zram devices sort of resembles non-rotational disks */ queue_flag_set_unlocked (QUEUE_FLAG_NONROT , zram->disk->queue); - zram->meta = meta; - zram->init_done =1 ;pr_debug ("Initialization done!\n" ); }
@@ -620,7 +621,7 @@ static ssize_t disksize_store(struct device *dev, disksize =PAGE_ALIGN (disksize); meta =zram_meta_alloc (disksize);down_write (&zram->init_lock); -if (zram->init_done) { +if (init_done (zram)) {up_write (&zram->init_lock);zram_meta_free (meta);pr_info ("Cannot change disksize for initialized device\n" );
@@ -728,7 +729,7 @@ static void zram_make_request(struct request_queue *queue, struct bio *bio)struct zram *zram = queue->queuedata;down_read (&zram->init_lock); -if (unlikely (!zram->init_done)) +if (unlikely (!init_done (zram))) goto error;if (!valid_io_request (zram, bio)) {
@@ -876,7 +877,7 @@ static int create_device(struct zram *zram, int device_id) goto out_free_disk; } - zram->init_done =0 ; + zram->meta =NULL ;return 0 ; out_free_disk:
drivers/staging/zram/zram_drv.h +0 -1
@@ -110,7 +110,6 @@ struct zram {struct request_queue *queue;struct gendisk *disk; -int init_done;/* Prevent concurrent execution of device init, reset and R/W request */ struct rw_semaphore init_lock;/*
On (09/10/13 17:34), Sergey Senozhatsky wrote:
[..]
Quoted 7 lines
Quoted 6 lines
Now I think we can drop the call to
handle_pending_slot_free()inzram_bvec_rw()altogether. As long as the write lock is held whenhandle_pending_slot_free()is called, there is no race. It's no different from any write request and the current code handles R/W concurrency already.
Quoted 8 lines
Yes, I think that can work.
To summarize, there should be 3 patches:
1)handle_pending_slot_free()inzram_bvec_rw()(as suggested by Jerome Marchand)
2)handle_pending_slot_free()race with reset (found by Dan Carpenter)
3) dropinit_doneand useinit_done()I'll prepare a patches later today.
I've sent two patches:
staging: zram: fix handle_pending_slot_free() and zram_reset_device() race
staging: zram: remove init_done from zram struct (v3)
Cc'd [email protected] as suggested by Dan.
please discard any previous patches and sorry for the noise.
Thanks,
-ss
Quoted 27 lines
Quoted 27 lines
Jerome
Quoted 23 lines
Quoted 9 lines
1) You haven't given us any performance numbers so it's not clear if the locking is even a problem.
2) The v2 patch introduces an obvious deadlock in
zram_slot_free()because now we take therw_locktwice. Fix your testing to catch this kind of bug next time.Quoted 7 lines
3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.regards,
dan carpenter--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 17 lines · Dan Carpenter
Quoted 16 lines · Sergey Senozhatsky
Quoted 15 lines
Quoted 7 lines
Calling
handle_pending_slot_free()for every RW operation may cause unneccessaryslot_free_locklocking, because most likely process will see NULLslot_free_rq.handle_pending_slot_free()only when current detects thatslot_free_rqis not NULL.v2: protect
handle_pending_slot_free()with zramrw_lock.
zram->slot_free_lockprotectszram->slot_free_rqbut shouldn't the zramrw_lockbe wrapped around the whole operation like the original code does? I don't know the zram code, but the original looks like it makes sense but in this one it looks like the locks are duplicative.Is the
down_read()in the original code be changed todown_write()?
Quoted 8 lines
Quoted 2 lines
I'm not touching locking around existing READ/WRITE commands.
Your patch does change the locking because now instead of taking the zram lock once it takes it and then drops it and then retakes it. This looks potentially racy to me but I don't know the code so I will defer to any zram maintainer.
it takes it only when there is zram->slot_free_rq. otherwise it just pointer comparison. original code does (schematically for READ case)
down_read()
spin_lock()
if (!NULL) {...}
spin_unlock();up_read();
patch adds the `if (!NULL)' check before N concurrent readers will stuck on spin_lock() to just unlock spin_lock because zram->slot_free_rq is NULL.
Quoted 2 lines
1) You haven't given us any performance numbers so it's not clear if the locking is even a problem.
good point, I did not perform any testing. here they are:
./iozone -t -T -R -l 3 -u 3 -r 16K -s 60M -I +Z
Record Size 16 KB
File size set to 61440 KB
O_DIRECT feature enabled
Command line used: ./iozone -t -T -R -l 3 -u 3 -r 16K -s 60M -I +Z Output is in Kbytes/sec
Time Resolution = 0.000001 seconds.
Processor cache size set to 1024 Kbytes.
Processor cache line size set to 32 bytes.
File stride size set to 17 * record size.
Min process = 3
Max process = 3
Throughput test with 3 processes
Each process writes a 61440 Kbyte file in 16 Kbyte records
test | w/o patch | w/ patch
--------------------------------------------------
Read | 1895278.31 | 2778439.78
Re-read | 2638231.06 | 2630383.88
Reverse Read | 1378348.80 | 1538697.89
Stride read | 1698457.96 | 2043402.42
Random read | 1818998.33 | 2038670.38
Mixed workload | 376585.98 | 435064.57
Pread | 1402478.04 | 992575.22
Fread | 4955286.31 | 5199061.31Quoted 3 lines
2) The v2 patch introduces an obvious deadlock in
zram_slot_free()because now we take therw_locktwice. Fix your testing to catch this kind of bug next time.
yes it is. and I'm sorry about that. I sent v3 yesterday https://lkml.org/lkml/2013/9/8/42
Quoted 3 lines
3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.
atomic pointer test, which is either NULL or !NULL.
for NULL case we don't take spin lock and just skip the whole handle_pending_slot_free() thing.
for !NULL we call handle_pending_slot_free(). any operations with zram->slot_free_rq (walking, removal or addition of element) are protected by spin lock within handle_pending_slot_free(). most requests will see NULL zram->slot_free_rq.
if someone set zram->slot_free_rq to !NULL right after current process checked it, then next request will see it !NULL and perform handle_pending_slot_free().
-ss
Quoted 3 lines
regards,
dan carpenter
Quoted 6 lines · Sergey Senozhatsky
Quoted 3 lines
3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.atomic pointer test, which is either NULL or !NULL.
That's not how concurency works. Atomic types are complicated than that. Anyway, the zram maintainers don't need me to explain that to them so I'll let them take over from here.
regards,
dan carpenter
Quoted 5 lines · Dan Carpenter
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines
3) Explain why it is safe to test
zram->slot_free_rqwhen we are not holding the lock. I think it is unsafe. I don't want to even think about it without the numbers.
Quoted 7 lines
Quoted 2 lines
atomic pointer test, which is either NULL or !NULL.
That's not how concurency works. Atomic types are complicated than that. Anyway, the zram maintainers don't need me to explain that to them so I'll let them take over from here.
yes, I understand that. but can't we check slot_free_rq pointer (32 or 64 bit read) w/o locking to just decide if we must:
-- call handle_pending_slot_free()
-- take the slot_free_lock
-- check slot_free_rq again (this time under the slot_free_lock) and perform
slot_free_rq operations while it is !NULL.
-ss
Quoted 3 lines
regards,
dan carpenter