mm/swapfile:__swap_duplicate: drop redundant WRITE_ONCE on swap_map for err cases
Subsystems (2)
PATCH 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.
--- mm/swapfile.c | 3 +++ 1 file changed, 3 insertions(+)
mm/swapfile.c +3 -0
@@ -3320,6 +3320,9 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage) }else err = -ENOENT ;/* unused swap entry */ +if (err) + goto unlock_out; +WRITE_ONCE (p->swap_map[offset], count | has_cache); unlock_out:
Signature · 1 line
2.34.1
Andrew Morton
maintainerQuoted 18 lines · On Wed, 21 Feb 2024 22:10:28 +1300 Barry Song <[email protected]> wrote:
From: Barry Song <[email protected]>
The code is quite hard to read, we are still writing
swap_mapafter 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_ONCEfor both performance and readability....
mm/swapfile.c static int __swap_duplicate(swp_entry_t entry, unsigned char usage) · line 3328
Quoted 9 lines
mm/swapfile.c +3
-0@@ -3320,6 +3320,9 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage) }else err = -ENOENT ;/* unused swap entry */ +if (err) + goto unlock_out; +WRITE_ONCE (p->swap_map[offset], count | has_cache); unlock_out:
I agree, but why add the goto?
mm/swapfile.c renamed from mm/swapfile.c~mm-swapfile-__swap_duplicate-drop-redundant-write_once-on-swap_map-for-err-cases-fix +2 -4
@@ -3335,10 +3335,8 @@ static int __swap_duplicate(swp_entry_t }else err = -ENOENT ;/* unused swap entry */ -if (err) - goto unlock_out; - -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);
_