linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas()
@ 2024-10-04 16:48 Breno Leitao
  2024-10-04 17:01 ` Lorenzo Stoakes
  2024-10-07 11:50 ` Vlastimil Babka
  0 siblings, 2 replies; 5+ messages in thread
From: Breno Leitao @ 2024-10-04 16:48 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Vlastimil Babka, Lorenzo Stoakes
  Cc: kernel-team, open list:VMA, open list

Performance analysis using branch annotation on a fleet of 200 hosts
running web servers revealed that the 'likely' hint in
vms_gather_munmap_vmas() was 100% consistently incorrect. In all
observed cases, the branch behavior contradicted the hint.

Remove the 'unlikely' qualifier from the condition checking 'vms->uf'.
By doing so, we allow the compiler to make optimization decisions based
on its own heuristics and profiling data, rather than relying on a
static hint that has proven to be inaccurate in real-world scenarios.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 mm/vma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/vma.c b/mm/vma.c
index 4737afcb064c..9d4fe794dd07 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -1250,7 +1250,7 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
 		else if (is_data_mapping(next->vm_flags))
 			vms->data_vm += nrpages;
 
-		if (unlikely(vms->uf)) {
+		if (vms->uf) {
 			/*
 			 * If userfaultfd_unmap_prep returns an error the vmas
 			 * will remain split, but userland will get a
-- 
2.43.5



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas()
  2024-10-04 16:48 [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas() Breno Leitao
@ 2024-10-04 17:01 ` Lorenzo Stoakes
  2024-10-29 11:52   ` Breno Leitao
  2024-10-07 11:50 ` Vlastimil Babka
  1 sibling, 1 reply; 5+ messages in thread
From: Lorenzo Stoakes @ 2024-10-04 17:01 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Andrew Morton, Liam R. Howlett, Vlastimil Babka, kernel-team,
	open list:VMA, open list

On Fri, Oct 04, 2024 at 09:48:31AM -0700, Breno Leitao wrote:
> Performance analysis using branch annotation on a fleet of 200 hosts
> running web servers revealed that the 'likely' hint in

To be pedantic: *unlikely

> vms_gather_munmap_vmas() was 100% consistently incorrect. In all
> observed cases, the branch behavior contradicted the hint.

OK so this is probably because vm_mmap_pgoff() declares the userfaultfd
list head on the stack then passes it into do_mmap() and threads all the
way to this code... and yeah, so that would be 100%.

There are other code paths that aren't 100%, but the system call one is.

Nice spot!

>
> Remove the 'unlikely' qualifier from the condition checking 'vms->uf'.
> By doing so, we allow the compiler to make optimization decisions based
> on its own heuristics and profiling data, rather than relying on a
> static hint that has proven to be inaccurate in real-world scenarios.

Yeah I'm generally not in favour of 'vibes' based likely()/unlikely(), I
think it should always be based on profiling.

It's understandable that there would be this expectation, and it may have
migrated from other code that already had this check in where perhaps it
wasn't always referencing a stack object, but yeah this is just wrong.

>
> Signed-off-by: Breno Leitao <leitao@debian.org>

Liam will want a look too when he's back next week.

Looks good to me though!

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  mm/vma.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/vma.c b/mm/vma.c
> index 4737afcb064c..9d4fe794dd07 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -1250,7 +1250,7 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
>  		else if (is_data_mapping(next->vm_flags))
>  			vms->data_vm += nrpages;
>
> -		if (unlikely(vms->uf)) {
> +		if (vms->uf) {
>  			/*
>  			 * If userfaultfd_unmap_prep returns an error the vmas
>  			 * will remain split, but userland will get a
> --
> 2.43.5
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas()
  2024-10-04 16:48 [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas() Breno Leitao
  2024-10-04 17:01 ` Lorenzo Stoakes
@ 2024-10-07 11:50 ` Vlastimil Babka
  1 sibling, 0 replies; 5+ messages in thread
From: Vlastimil Babka @ 2024-10-07 11:50 UTC (permalink / raw)
  To: Breno Leitao, Andrew Morton, Liam R. Howlett, Lorenzo Stoakes
  Cc: kernel-team, open list:VMA, open list

On 10/4/24 6:48 PM, Breno Leitao wrote:
> Performance analysis using branch annotation on a fleet of 200 hosts
> running web servers revealed that the 'likely' hint in
> vms_gather_munmap_vmas() was 100% consistently incorrect. In all
> observed cases, the branch behavior contradicted the hint.
> 
> Remove the 'unlikely' qualifier from the condition checking 'vms->uf'.
> By doing so, we allow the compiler to make optimization decisions based
> on its own heuristics and profiling data, rather than relying on a
> static hint that has proven to be inaccurate in real-world scenarios.
> 
> Signed-off-by: Breno Leitao <leitao@debian.org>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/vma.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/vma.c b/mm/vma.c
> index 4737afcb064c..9d4fe794dd07 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -1250,7 +1250,7 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
>  		else if (is_data_mapping(next->vm_flags))
>  			vms->data_vm += nrpages;
>  
> -		if (unlikely(vms->uf)) {
> +		if (vms->uf) {
>  			/*
>  			 * If userfaultfd_unmap_prep returns an error the vmas
>  			 * will remain split, but userland will get a


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas()
  2024-10-04 17:01 ` Lorenzo Stoakes
@ 2024-10-29 11:52   ` Breno Leitao
  2024-10-29 14:18     ` Liam R. Howlett
  0 siblings, 1 reply; 5+ messages in thread
From: Breno Leitao @ 2024-10-29 11:52 UTC (permalink / raw)
  To: Lorenzo Stoakes, Liam.Howlett
  Cc: Andrew Morton, Liam R. Howlett, Vlastimil Babka, kernel-team,
	open list:VMA, open list

On Fri, Oct 04, 2024 at 06:01:07PM +0100, Lorenzo Stoakes wrote:
> On Fri, Oct 04, 2024 at 09:48:31AM -0700, Breno Leitao wrote:
> > Performance analysis using branch annotation on a fleet of 200 hosts
> > running web servers revealed that the 'likely' hint in
> 
> To be pedantic: *unlikely
> 
> > vms_gather_munmap_vmas() was 100% consistently incorrect. In all
> > observed cases, the branch behavior contradicted the hint.
> 
> OK so this is probably because vm_mmap_pgoff() declares the userfaultfd
> list head on the stack then passes it into do_mmap() and threads all the
> way to this code... and yeah, so that would be 100%.
> 
> There are other code paths that aren't 100%, but the system call one is.
> 
> Nice spot!
> 
> >
> > Remove the 'unlikely' qualifier from the condition checking 'vms->uf'.
> > By doing so, we allow the compiler to make optimization decisions based
> > on its own heuristics and profiling data, rather than relying on a
> > static hint that has proven to be inaccurate in real-world scenarios.
> 
> Yeah I'm generally not in favour of 'vibes' based likely()/unlikely(), I
> think it should always be based on profiling.
> 
> It's understandable that there would be this expectation, and it may have
> migrated from other code that already had this check in where perhaps it
> wasn't always referencing a stack object, but yeah this is just wrong.
> 
> >
> > Signed-off-by: Breno Leitao <leitao@debian.org>
> 
> Liam will want a look too when he's back next week.

Liam, are you OK with this one? I suspect that Andrew is waiting for
your review before merging it.

> 
> Looks good to me though!
> 
> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> 
> > ---
> >  mm/vma.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/mm/vma.c b/mm/vma.c
> > index 4737afcb064c..9d4fe794dd07 100644
> > --- a/mm/vma.c
> > +++ b/mm/vma.c
> > @@ -1250,7 +1250,7 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
> >  		else if (is_data_mapping(next->vm_flags))
> >  			vms->data_vm += nrpages;
> >
> > -		if (unlikely(vms->uf)) {
> > +		if (vms->uf) {
> >  			/*
> >  			 * If userfaultfd_unmap_prep returns an error the vmas
> >  			 * will remain split, but userland will get a
> > --
> > 2.43.5
> >


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas()
  2024-10-29 11:52   ` Breno Leitao
@ 2024-10-29 14:18     ` Liam R. Howlett
  0 siblings, 0 replies; 5+ messages in thread
From: Liam R. Howlett @ 2024-10-29 14:18 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Lorenzo Stoakes, Andrew Morton, Vlastimil Babka, kernel-team,
	open list:VMA, open list

* Breno Leitao <leitao@debian.org> [241029 07:52]:
> On Fri, Oct 04, 2024 at 06:01:07PM +0100, Lorenzo Stoakes wrote:
> > On Fri, Oct 04, 2024 at 09:48:31AM -0700, Breno Leitao wrote:
> > > Performance analysis using branch annotation on a fleet of 200 hosts
> > > running web servers revealed that the 'likely' hint in
> > 
> > To be pedantic: *unlikely
> > 
> > > vms_gather_munmap_vmas() was 100% consistently incorrect. In all
> > > observed cases, the branch behavior contradicted the hint.
> > 
> > OK so this is probably because vm_mmap_pgoff() declares the userfaultfd
> > list head on the stack then passes it into do_mmap() and threads all the
> > way to this code... and yeah, so that would be 100%.
> > 
> > There are other code paths that aren't 100%, but the system call one is.
> > 
> > Nice spot!
> > 
> > >
> > > Remove the 'unlikely' qualifier from the condition checking 'vms->uf'.
> > > By doing so, we allow the compiler to make optimization decisions based
> > > on its own heuristics and profiling data, rather than relying on a
> > > static hint that has proven to be inaccurate in real-world scenarios.
> > 
> > Yeah I'm generally not in favour of 'vibes' based likely()/unlikely(), I
> > think it should always be based on profiling.
> > 
> > It's understandable that there would be this expectation, and it may have
> > migrated from other code that already had this check in where perhaps it
> > wasn't always referencing a stack object, but yeah this is just wrong.
> > 
> > >
> > > Signed-off-by: Breno Leitao <leitao@debian.org>
> > 
> > Liam will want a look too when he's back next week.
> 
> Liam, are you OK with this one? I suspect that Andrew is waiting for
> your review before merging it.

I'm fine with removing the unlikely.

This begs for a refactoring considering we are calling a function in
fs/userfaultfd on every vma on every munmap operation in this call path.

Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>

> 
> > 
> > Looks good to me though!
> > 
> > Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> > 
> > > ---
> > >  mm/vma.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/mm/vma.c b/mm/vma.c
> > > index 4737afcb064c..9d4fe794dd07 100644
> > > --- a/mm/vma.c
> > > +++ b/mm/vma.c
> > > @@ -1250,7 +1250,7 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
> > >  		else if (is_data_mapping(next->vm_flags))
> > >  			vms->data_vm += nrpages;
> > >
> > > -		if (unlikely(vms->uf)) {
> > > +		if (vms->uf) {
> > >  			/*
> > >  			 * If userfaultfd_unmap_prep returns an error the vmas
> > >  			 * will remain split, but userland will get a
> > > --
> > > 2.43.5
> > >


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-10-29 14:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-04 16:48 [PATCH] mm: Remove misleading 'unlikely' hint in vms_gather_munmap_vmas() Breno Leitao
2024-10-04 17:01 ` Lorenzo Stoakes
2024-10-29 11:52   ` Breno Leitao
2024-10-29 14:18     ` Liam R. Howlett
2024-10-07 11:50 ` Vlastimil Babka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox