Skip to contentSkip to conversation

mm/swapfile:__swap_duplicate: drop redundant WRITE_ONCE on swap_map for err cases

PATCH In review Barry Song 2 participants 3 messages Feb 21, 2024 – Mar 11, 2024

Subsystems (2)
lore
Share & tools
lore

Barry Song

author
#0 lore raw

PATCH v2 1/1 mm/swapfile:__swap_duplicate: drop redundant WRITE_ONCE on swap_map for err cases

From: Barry Song <[email protected]>

The code is quite hard to read, we are still writing swap_map after errors happen. Though the written value is as before,

has_cache = count & SWAP_HAS_CACHE;
count &= ~SWAP_HAS_CACHE;
[snipped]
WRITE_ONCE(p->swap_map[offset], count | has_cache);

It would be better to entirely drop the WRITE_ONCE for both performance and readability.

---
-v2: drop goto according to Andrew, Thanks!

 mm/swapfile.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/swapfile.c +2 -1
index 556ff7347d5f..7cb6d9a2d51d 100644
@@ -3320,7 +3320,8 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
 	} else
 		err = -ENOENT;			/* unused swap entry */
 
-	WRITE_ONCE(p->swap_map[offset], count | has_cache);
+	if (!err)
+		WRITE_ONCE(p->swap_map[offset], count | has_cache);
 
 unlock_out:
 	unlock_cluster_or_swap_info(p, ci);
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Barry Song

From: Barry Song <[email protected]>

The code is quite hard to read, we are still writing swap_map after errors happen. Though the written value is as before,

  has_cache = count & SWAP_HAS_CACHE;
  count &= ~SWAP_HAS_CACHE;
  [snipped]
  WRITE_ONCE(p->swap_map[offset], count | has_cache);

It would be better to entirely drop the WRITE_ONCE for both performance and readability.

---
-v2: drop goto according to Andrew, Thanks!

  mm/swapfile.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/swapfile.c +0 -0
index 556ff7347d5f..7cb6d9a2d51d 100644

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry, unsigned char usage) · line 3327

Quoted 9 lines
mm/swapfile.c +2 -1
@@ -3320,7 +3320,8 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  	} else
  		err = -ENOENT;			/* unused swap entry */
 
-	WRITE_ONCE(p->swap_map[offset], count | has_cache);
+	if (!err)
+		WRITE_ONCE(p->swap_map[offset], count | has_cache);
 
  unlock_out:
  	unlock_cluster_or_swap_info(p, ci);

Maybe too late, but LGTM

Signature · 3 lines
Cheers,

David / dhildenb
author
#2 lore raw
Quoted 34 lines · David Hildenbrand
Quoted 32 lines · Barry Song

From: Barry Song <[email protected]>

The code is quite hard to read, we are still writing swap_map after errors happen. Though the written value is as before,

  has_cache = count & SWAP_HAS_CACHE;
  count &= ~SWAP_HAS_CACHE;
  [snipped]
  WRITE_ONCE(p->swap_map[offset], count | has_cache);

It would be better to entirely drop the WRITE_ONCE for both performance and readability.

---
-v2: drop goto according to Andrew, Thanks!

  mm/swapfile.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/swapfile.c +2 -1
index 556ff7347d5f..7cb6d9a2d51d 100644
@@ -3320,7 +3320,8 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
      } else
              err = -ENOENT;                  /* unused swap entry */
 
-     WRITE_ONCE(p->swap_map[offset], count | has_cache);
+     if (!err)
+             WRITE_ONCE(p->swap_map[offset], count | has_cache);
 
Quoted 6 lines

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry, unsigned char usage) · line 3327

Quoted 2 lines
mm/swapfile.c +0 -0
  unlock_out:
      unlock_cluster_or_swap_info(p, ci);

Maybe too late, but LGTM

David, thanks! Though it is probably too late as this one has been in mm-stable for a long time.

Quoted 5 lines

--
Cheers,

David / dhildenb

Thanks
Barry