linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] mm/page_alloc.c: Micro-optimisation Remove unnecessary branch
@ 2020-03-07 22:53 mateusznosek0
  2020-03-07 23:15 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: mateusznosek0 @ 2020-03-07 22:53 UTC (permalink / raw)
  To: linux-mm, linux-kernel; +Cc: Mateusz Nosek, akpm

From: Mateusz Nosek <mateusznosek0@gmail.com>

Previously if branch condition was false, the assignment was not executed.
The assignment can be safely executed even when the condition is false and
it is not incorrect as it assigns the value of 'nodemask' to 'ac.nodemask'
which already has the same value.

So as the assignment can be executed unconditionally, the branch can be
removed.

Signed-off-by: Mateusz Nosek <mateusznosek0@gmail.com>
---
 mm/page_alloc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 79e950d76ffc..75456d04b5c5 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -4819,8 +4819,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
 	 * Restore the original nodemask if it was potentially replaced with
 	 * &cpuset_current_mems_allowed to optimize the fast-path attempt.
 	 */
-	if (unlikely(ac.nodemask != nodemask))
-		ac.nodemask = nodemask;
+	ac.nodemask = nodemask;
 
 	page = __alloc_pages_slowpath(alloc_mask, order, &ac);
 
-- 
2.17.1



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

* Re: [RFC PATCH] mm/page_alloc.c: Micro-optimisation Remove unnecessary branch
  2020-03-07 22:53 [RFC PATCH] mm/page_alloc.c: Micro-optimisation Remove unnecessary branch mateusznosek0
@ 2020-03-07 23:15 ` Andrew Morton
  2020-03-08 11:50   ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2020-03-07 23:15 UTC (permalink / raw)
  To: mateusznosek0; +Cc: linux-mm, linux-kernel

On Sat,  7 Mar 2020 23:53:35 +0100 mateusznosek0@gmail.com wrote:

> From: Mateusz Nosek <mateusznosek0@gmail.com>
> 
> Previously if branch condition was false, the assignment was not executed.
> The assignment can be safely executed even when the condition is false and
> it is not incorrect as it assigns the value of 'nodemask' to 'ac.nodemask'
> which already has the same value.
> 
> So as the assignment can be executed unconditionally, the branch can be
> removed.
> 
> ...
>
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -4819,8 +4819,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
>  	 * Restore the original nodemask if it was potentially replaced with
>  	 * &cpuset_current_mems_allowed to optimize the fast-path attempt.
>  	 */
> -	if (unlikely(ac.nodemask != nodemask))
> -		ac.nodemask = nodemask;
> +	ac.nodemask = nodemask;
>  

This will now unconditionally dirty the ac.nodemask cacheline, which
means that cacheline will need to be written back.  If it is truly
unlikely that the write was needed then the thinking goes that the
test-and-branch is worthwhile, by saving on memory traffic.

At least, I assume that's why the code is the way it is.

I don't know whether this optimisation is valid on a majority of modern
platforms.  But that's the thinking!



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

* Re: [RFC PATCH] mm/page_alloc.c: Micro-optimisation Remove unnecessary branch
  2020-03-07 23:15 ` Andrew Morton
@ 2020-03-08 11:50   ` Matthew Wilcox
  0 siblings, 0 replies; 3+ messages in thread
From: Matthew Wilcox @ 2020-03-08 11:50 UTC (permalink / raw)
  To: Andrew Morton; +Cc: mateusznosek0, linux-mm, linux-kernel

On Sat, Mar 07, 2020 at 03:15:42PM -0800, Andrew Morton wrote:
> On Sat,  7 Mar 2020 23:53:35 +0100 mateusznosek0@gmail.com wrote:
> > -	if (unlikely(ac.nodemask != nodemask))
> > -		ac.nodemask = nodemask;
> > +	ac.nodemask = nodemask;
> 
> This will now unconditionally dirty the ac.nodemask cacheline, which
> means that cacheline will need to be written back.  If it is truly
> unlikely that the write was needed then the thinking goes that the
> test-and-branch is worthwhile, by saving on memory traffic.
> 
> At least, I assume that's why the code is the way it is.

The line immediately before this hunk is:

        ac.spread_dirty_pages = false;

ac is on-stack and is only 32 bytes.  I don't see a reason not to do this.

Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>


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

end of thread, other threads:[~2020-03-08 11:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-07 22:53 [RFC PATCH] mm/page_alloc.c: Micro-optimisation Remove unnecessary branch mateusznosek0
2020-03-07 23:15 ` Andrew Morton
2020-03-08 11:50   ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).