diff mbox series

[09/15] mm/swap: avoid calling swp_swap_info when try to check SWP_STABLE_WRITES

Message ID 20220509131416.17553-10-linmiaohe@huawei.com (mailing list archive)
State New
Headers show
Series A few cleanup patches for swap | expand

Commit Message

Miaohe Lin May 9, 2022, 1:14 p.m. UTC
Use flags of si directly to check SWP_STABLE_WRITES to avoid possible
READ_ONCE and thus save some cpu cycles.

Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
 mm/memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

NeilBrown May 10, 2022, 12:28 a.m. UTC | #1
On Mon, 09 May 2022, Miaohe Lin wrote:
> Use flags of si directly to check SWP_STABLE_WRITES to avoid possible
> READ_ONCE and thus save some cpu cycles.
> 
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
>  mm/memory.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/memory.c b/mm/memory.c
> index 9c3e7e6ac202..89dd15504f3d 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -3892,7 +3892,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
>  			 */
>  			exclusive = true;
>  		} else if (exclusive && PageWriteback(page) &&
> -			  (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) {
> +			  (si->flags & SWP_STABLE_WRITES)) {

Should this have a data_race() annotation.  Other bit-tests of si->flags
do because scan_swap_map_slots can update it asynchronously, but we know
that won't matter in practice.

Thanks,
NeilBrown


>  			/*
>  			 * This is tricky: not all swap backends support
>  			 * concurrent page modifications while under writeback.
> -- 
> 2.23.0
> 
>
Miaohe Lin May 10, 2022, 2:21 a.m. UTC | #2
On 2022/5/10 8:28, NeilBrown wrote:
> On Mon, 09 May 2022, Miaohe Lin wrote:
>> Use flags of si directly to check SWP_STABLE_WRITES to avoid possible
>> READ_ONCE and thus save some cpu cycles.
>>
>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>> ---
>>  mm/memory.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/mm/memory.c b/mm/memory.c
>> index 9c3e7e6ac202..89dd15504f3d 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -3892,7 +3892,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
>>  			 */
>>  			exclusive = true;
>>  		} else if (exclusive && PageWriteback(page) &&
>> -			  (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) {
>> +			  (si->flags & SWP_STABLE_WRITES)) {
> 
> Should this have a data_race() annotation.  Other bit-tests of si->flags
> do because scan_swap_map_slots can update it asynchronously, but we know
> that won't matter in practice.

Yes, you're right. scan_swap_map_slots can update si->flags asynchronously while
do_swap_page tests SWP_STABLE_WRITES here. We know this is harmless because
SWP_STABLE_WRITES is only changed at swapon/swapoff.

Will add data_race() annotation in next version to avoid possible KCSAN data-race complaint.

Many thanks for pointing this out! :)

> 
> Thanks,
> NeilBrown
> 
> 
>>  			/*
>>  			 * This is tricky: not all swap backends support
>>  			 * concurrent page modifications while under writeback.
>> -- 
>> 2.23.0
>>
>>
> .
>
Andrew Morton May 17, 2022, 11:39 p.m. UTC | #3
On Tue, 10 May 2022 10:21:21 +0800 Miaohe Lin <linmiaohe@huawei.com> wrote:

> On 2022/5/10 8:28, NeilBrown wrote:
> > On Mon, 09 May 2022, Miaohe Lin wrote:
> >> Use flags of si directly to check SWP_STABLE_WRITES to avoid possible
> >> READ_ONCE and thus save some cpu cycles.
> >>
> >> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> >> ---
> >>  mm/memory.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/mm/memory.c b/mm/memory.c
> >> index 9c3e7e6ac202..89dd15504f3d 100644
> >> --- a/mm/memory.c
> >> +++ b/mm/memory.c
> >> @@ -3892,7 +3892,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> >>  			 */
> >>  			exclusive = true;
> >>  		} else if (exclusive && PageWriteback(page) &&
> >> -			  (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) {
> >> +			  (si->flags & SWP_STABLE_WRITES)) {
> > 
> > Should this have a data_race() annotation.  Other bit-tests of si->flags
> > do because scan_swap_map_slots can update it asynchronously, but we know
> > that won't matter in practice.
> 
> Yes, you're right. scan_swap_map_slots can update si->flags asynchronously while
> do_swap_page tests SWP_STABLE_WRITES here. We know this is harmless because
> SWP_STABLE_WRITES is only changed at swapon/swapoff.
> 
> Will add data_race() annotation in next version to avoid possible KCSAN data-race complaint.
> 

I did this:

--- a/mm/memory.c~mm-swap-avoid-calling-swp_swap_info-when-try-to-check-swp_stable_writes-fix
+++ a/mm/memory.c
@@ -3889,7 +3889,7 @@ vm_fault_t do_swap_page(struct vm_fault
 			 */
 			exclusive = true;
 		} else if (exclusive && PageWriteback(page) &&
-			  (si->flags & SWP_STABLE_WRITES)) {
+			  data_race(si->flags & SWP_STABLE_WRITES)) {
 			/*
 			 * This is tricky: not all swap backends support
 			 * concurrent page modifications while under writeback.
diff mbox series

Patch

diff --git a/mm/memory.c b/mm/memory.c
index 9c3e7e6ac202..89dd15504f3d 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3892,7 +3892,7 @@  vm_fault_t do_swap_page(struct vm_fault *vmf)
 			 */
 			exclusive = true;
 		} else if (exclusive && PageWriteback(page) &&
-			  (swp_swap_info(entry)->flags & SWP_STABLE_WRITES)) {
+			  (si->flags & SWP_STABLE_WRITES)) {
 			/*
 			 * This is tricky: not all swap backends support
 			 * concurrent page modifications while under writeback.