Re: + zram-switch-to-non-atomic-entry-locking.patch added to mm-unstable branch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Cc-ing Uros,

On (25/01/27 12:58), Andrew Morton wrote:
[..]
> -static int zram_slot_trylock(struct zram *zram, u32 index)
> +static int zram_slot_write_trylock(struct zram *zram, u32 index)
>  {
> -	return spin_trylock(&zram->table[index].lock);
> +	int old;
> +
> +	old = atomic_cmpxchg(&zram->table[index].lock, ZRAM_ENTRY_UNLOCKED,
> +			     ZRAM_ENTRY_WRLOCKED);
> +	return old == ZRAM_ENTRY_UNLOCKED;
>  }
>  
> -static void zram_slot_lock(struct zram *zram, u32 index)
> +static void zram_slot_write_lock(struct zram *zram, u32 index)
>  {
> -	spin_lock(&zram->table[index].lock);
> +	atomic_t *lock = &zram->table[index].lock;
> +	int old;
> +
> +	while (1) {
> +		old = atomic_cmpxchg(lock, ZRAM_ENTRY_UNLOCKED,
> +				     ZRAM_ENTRY_WRLOCKED);
> +		if (old == ZRAM_ENTRY_UNLOCKED)
> +			return;
> +
> +		cond_resched();
> +	}
>  }
>  
> -static void zram_slot_unlock(struct zram *zram, u32 index)
> +static void zram_slot_write_unlock(struct zram *zram, u32 index)
>  {
> -	spin_unlock(&zram->table[index].lock);
> +	atomic_set(&zram->table[index].lock, ZRAM_ENTRY_UNLOCKED);
> +}
> +
> +static void zram_slot_read_lock(struct zram *zram, u32 index)
> +{
> +	atomic_t *lock = &zram->table[index].lock;
> +	int old;
> +
> +	while (1) {
> +		old = atomic_read(lock);
> +		if (old == ZRAM_ENTRY_WRLOCKED) {
> +			cond_resched();
> +			continue;
> +		}
> +
> +		if (atomic_cmpxchg(lock, old, old + 1) == old)
> +			return;
> +
> +		cond_resched();
> +	}
> +}
> +
> +static void zram_slot_read_unlock(struct zram *zram, u32 index)
> +{
> +	atomic_dec(&zram->table[index].lock);
>  }

Hello Andrew,

I think I want to make these look as close to zsmalloc implementation
as possible.

A simple patch below.  No functional changes.  (also renamed
zram_slot_write_trylock() to zram_slot_try_write_lock() which is a
more common naming convention for functions of that sort.)

Would you prefer a complete re-spin of the series, with these changes
squashed, or a "fixup" patch?

---
 drivers/block/zram/zram_drv.c | 41 +++++++++++++++--------------------
 1 file changed, 17 insertions(+), 24 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index d73e8374e9cc..9622fcad2115 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -59,28 +59,26 @@ static void zram_free_page(struct zram *zram, size_t index);
 static int zram_read_from_zspool(struct zram *zram, struct page *page,
 				 u32 index);
 
-static int zram_slot_write_trylock(struct zram *zram, u32 index)
+static bool zram_slot_try_write_lock(struct zram *zram, u32 index)
 {
-	int old;
+	atomic_t *lock = &zram->table[index].lock;
+	int old = ZRAM_ENTRY_UNLOCKED;
 
-	old = atomic_cmpxchg(&zram->table[index].lock, ZRAM_ENTRY_UNLOCKED,
-			     ZRAM_ENTRY_WRLOCKED);
-	return old == ZRAM_ENTRY_UNLOCKED;
+	return atomic_try_cmpxchg(lock, &old, ZRAM_ENTRY_WRLOCKED);
 }
 
 static void zram_slot_write_lock(struct zram *zram, u32 index)
 {
 	atomic_t *lock = &zram->table[index].lock;
-	int old;
-
-	while (1) {
-		old = atomic_cmpxchg(lock, ZRAM_ENTRY_UNLOCKED,
-				     ZRAM_ENTRY_WRLOCKED);
-		if (old == ZRAM_ENTRY_UNLOCKED)
-			return;
+	int old = atomic_read(lock);
 
-		cond_resched();
-	}
+	do {
+		if (old != ZRAM_ENTRY_UNLOCKED) {
+			cond_resched();
+			old = atomic_read(lock);
+			continue;
+		}
+	} while (!atomic_try_cmpxchg(lock, &old, ZRAM_ENTRY_WRLOCKED));
 }
 
 static void zram_slot_write_unlock(struct zram *zram, u32 index)
@@ -91,20 +89,15 @@ static void zram_slot_write_unlock(struct zram *zram, u32 index)
 static void zram_slot_read_lock(struct zram *zram, u32 index)
 {
 	atomic_t *lock = &zram->table[index].lock;
-	int old;
+	int old = atomic_read(lock);
 
-	while (1) {
-		old = atomic_read(lock);
+	do {
 		if (old == ZRAM_ENTRY_WRLOCKED) {
 			cond_resched();
+			old = atomic_read(lock);
 			continue;
 		}
-
-		if (atomic_cmpxchg(lock, old, old + 1) == old)
-			return;
-
-		cond_resched();
-	}
+	} while (!atomic_try_cmpxchg(lock, &old, old + 1));
 }
 
 static void zram_slot_read_unlock(struct zram *zram, u32 index)
@@ -2315,7 +2308,7 @@ static void zram_slot_free_notify(struct block_device *bdev,
 	zram = bdev->bd_disk->private_data;
 
 	atomic64_inc(&zram->stats.notify_free);
-	if (!zram_slot_write_trylock(zram, index)) {
+	if (!zram_slot_try_write_lock(zram, index)) {
 		atomic64_inc(&zram->stats.miss_free);
 		return;
 	}
-- 
2.48.1.262.g85cc9f2d1e-goog





[Index of Archives]     [Kernel Archive]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]

  Powered by Linux