Skip to contentSkip to conversation

staging: zram: minimize `slot_free_lock' usage

PATCH In review Sergey Senozhatsky 3 participants 5 messages Sep 6, 2013 – Sep 9, 2013 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 staging: zram: minimize `slot_free_lock' usage

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.

---

 drivers/staging/zram/zram_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +3 -2
index 91d94b5..17386e2 100644
@@ -532,14 +532,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);
 	}
#1 lore raw
Quoted 17 lines · Sergey Senozhatsky

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.

---

 drivers/staging/zram/zram_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 91d94b5..17386e2 100644

drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537

Quoted 6 lines
drivers/staging/zram/zram_drv.c +3 -0
@@ -532,14 +532,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);
+

Calling handle_pending_slot_free() without holding zram->lock? That's racy.

Jerome

Quoted 18 lines
drivers/staging/zram/zram_drv.c +0 -3
 	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);
 	}
 
--

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/

#2 lore raw
Quoted 21 lines · Jerome Marchand
Quoted 20 lines · Sergey Senozhatsky

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.

---

 drivers/staging/zram/zram_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 91d94b5..17386e2 100644
@@ -532,14 +532,15 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
 {
 	int ret;
 
Quoted 6 lines

drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537

Quoted 3 lines
drivers/staging/zram/zram_drv.c +3 -0
+	if (zram->slot_free_rq)
+		handle_pending_slot_free(zram);
+

Calling handle_pending_slot_free() without holding zram->lock? That's racy.

sorry, my bad. it should take down_write() lock.

	-ss
Quoted 21 lines

Jerome

Quoted 18 lines
drivers/staging/zram/zram_drv.c +0 -3
 	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);
 	}
 
--

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/

#3 lore raw
Quoted 22 lines · Sergey Senozhatsky
Quoted 21 lines · Jerome Marchand
Quoted 20 lines · Sergey Senozhatsky

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.

---

 drivers/staging/zram/zram_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 91d94b5..17386e2 100644
@@ -532,14 +532,15 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
 {
 	int ret;
 
Quoted 9 lines
Quoted 6 lines

drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537

Quoted 3 lines
drivers/staging/zram/zram_drv.c +3 -0
+	if (zram->slot_free_rq)
+		handle_pending_slot_free(zram);
+

Calling handle_pending_slot_free() without holding zram->lock? That's racy.

sorry, my bad. it should take down_write() lock.

Or down_read() on the read path. We leave the original as-is?

regards,
dan carpenter

#4 lore raw
Quoted 30 lines · Dan Carpenter
Quoted 29 lines · Sergey Senozhatsky
Quoted 27 lines · Jerome Marchand
Quoted 17 lines · Sergey Senozhatsky

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.

---

 drivers/staging/zram/zram_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 91d94b5..17386e2 100644

drivers/staging/zram/zram_drv.c static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, · line 537

Quoted 6 lines
drivers/staging/zram/zram_drv.c +3 -0
@@ -532,14 +532,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);
+

Calling handle_pending_slot_free() without holding zram->lock? That's racy.

Quoted 5 lines
Quoted 2 lines

sorry, my bad. it should take down_write() lock.

Or down_read() on the read path. We leave the original as-is?

Hello,

down_write() for both READ and WRITE looks ok to me (+down_write() for zram_slot_free()). is there something I miss?

down_read() for READ in case of N active readers will force N-1 processes to spin on zram->slot_free_lock in handle_pending_slot_free().

it probably makes sense to add extra zram->slot_free_rq check for the case when process slept on rw lock while someone was freeing pages:

static void handle_pending_slot_free(struct zram *zram)

{
        struct zram_slot_free *free_rq;

        down_write(&zram->lock);
+	if (!zram->slot_free_rq)
+		goto out;
        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);
+out:
        up_write(&zram->lock);
}
	-ss
Quoted 3 lines

regards,
dan carpenter