linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] driver core: Add branch prediction hints in really_probe()
@ 2018-11-06 13:46 Muchun Song
  2018-11-06 14:30 ` Rafael J. Wysocki
  2018-11-06 20:26 ` Greg KH
  0 siblings, 2 replies; 6+ messages in thread
From: Muchun Song @ 2018-11-06 13:46 UTC (permalink / raw)
  To: gregkh, rafael; +Cc: linux-kernel

If condition is false in most cases. So, add an unlikely() to the if
condition, so that the optimizer assumes that the condition is false.

Signed-off-by: Muchun Song <smuchun@gmail.com>
---
 drivers/base/dd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 169412ee4ae8..8eba453c4e5d 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -450,7 +450,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
 	bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
 			   !drv->suppress_bind_attrs;
 
-	if (defer_all_probes) {
+	if (unlikely(defer_all_probes)) {
 		/*
 		 * Value of defer_all_probes can be set only by
 		 * device_defer_all_probes_enable() which, in turn, will call
@@ -508,7 +508,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
 			goto probe_failed;
 	}
 
-	if (test_remove) {
+	if (unlikely(test_remove)) {
 		test_remove = false;
 
 		if (dev->bus->remove)
-- 
2.17.1


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

* Re: [PATCH] driver core: Add branch prediction hints in really_probe()
  2018-11-06 13:46 [PATCH] driver core: Add branch prediction hints in really_probe() Muchun Song
@ 2018-11-06 14:30 ` Rafael J. Wysocki
  2018-11-06 14:43   ` Muchun Song
  2018-11-06 20:26 ` Greg KH
  1 sibling, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2018-11-06 14:30 UTC (permalink / raw)
  To: smuchun; +Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Linux Kernel Mailing List

On Tue, Nov 6, 2018 at 2:47 PM Muchun Song <smuchun@gmail.com> wrote:
>
> If condition is false in most cases. So, add an unlikely() to the if
> condition, so that the optimizer assumes that the condition is false.
>
> Signed-off-by: Muchun Song <smuchun@gmail.com>

Have you measured the practical impact of this patch in any way?

> ---
>  drivers/base/dd.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> index 169412ee4ae8..8eba453c4e5d 100644
> --- a/drivers/base/dd.c
> +++ b/drivers/base/dd.c
> @@ -450,7 +450,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
>         bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
>                            !drv->suppress_bind_attrs;
>
> -       if (defer_all_probes) {
> +       if (unlikely(defer_all_probes)) {
>                 /*
>                  * Value of defer_all_probes can be set only by
>                  * device_defer_all_probes_enable() which, in turn, will call
> @@ -508,7 +508,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
>                         goto probe_failed;
>         }
>
> -       if (test_remove) {
> +       if (unlikely(test_remove)) {
>                 test_remove = false;
>
>                 if (dev->bus->remove)
> --
> 2.17.1
>

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

* Re: [PATCH] driver core: Add branch prediction hints in really_probe()
  2018-11-06 14:30 ` Rafael J. Wysocki
@ 2018-11-06 14:43   ` Muchun Song
  2018-11-06 14:59     ` Rafael J. Wysocki
  0 siblings, 1 reply; 6+ messages in thread
From: Muchun Song @ 2018-11-06 14:43 UTC (permalink / raw)
  To: rafael; +Cc: gregkh, linux-kernel

Hi Rafael,

If we want the driver core to test driver remove functions, we can
enable CONFIG_DEBUG_TEST_DRIVER_REMOVE. This option is
just for testing it. So, in most cases, the option is disabled and the if
condition is false. So I think we can add an unlikely() to it.

Rafael J. Wysocki <rafael@kernel.org> 于2018年11月6日周二 下午10:30写道:
>
> On Tue, Nov 6, 2018 at 2:47 PM Muchun Song <smuchun@gmail.com> wrote:
> >
> > If condition is false in most cases. So, add an unlikely() to the if
> > condition, so that the optimizer assumes that the condition is false.
> >
> > Signed-off-by: Muchun Song <smuchun@gmail.com>
>
> Have you measured the practical impact of this patch in any way?
>
> > ---
> >  drivers/base/dd.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> > index 169412ee4ae8..8eba453c4e5d 100644
> > --- a/drivers/base/dd.c
> > +++ b/drivers/base/dd.c
> > @@ -450,7 +450,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
> >         bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
> >                            !drv->suppress_bind_attrs;
> >
> > -       if (defer_all_probes) {
> > +       if (unlikely(defer_all_probes)) {
> >                 /*
> >                  * Value of defer_all_probes can be set only by
> >                  * device_defer_all_probes_enable() which, in turn, will call
> > @@ -508,7 +508,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
> >                         goto probe_failed;
> >         }
> >
> > -       if (test_remove) {
> > +       if (unlikely(test_remove)) {
> >                 test_remove = false;
> >
> >                 if (dev->bus->remove)
> > --
> > 2.17.1
> >

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

* Re: [PATCH] driver core: Add branch prediction hints in really_probe()
  2018-11-06 14:43   ` Muchun Song
@ 2018-11-06 14:59     ` Rafael J. Wysocki
  2018-11-06 15:12       ` Muchun Song
  0 siblings, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2018-11-06 14:59 UTC (permalink / raw)
  To: smuchun; +Cc: Rafael J. Wysocki, Greg Kroah-Hartman, Linux Kernel Mailing List

On Tue, Nov 6, 2018 at 3:43 PM Muchun Song <smuchun@gmail.com> wrote:
>
> Hi Rafael,
>
> If we want the driver core to test driver remove functions, we can
> enable CONFIG_DEBUG_TEST_DRIVER_REMOVE. This option is
> just for testing it. So, in most cases, the option is disabled and the if
> condition is false. So I think we can add an unlikely() to it.

Yes, it can be added there, but does it really need to be added?

If the conditions are false all the time, the branch predictor in the
processor should be able to deal with it just fine.

And if they are false already at build time, the compiler should just
optimize them away.

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

* Re: [PATCH] driver core: Add branch prediction hints in really_probe()
  2018-11-06 14:59     ` Rafael J. Wysocki
@ 2018-11-06 15:12       ` Muchun Song
  0 siblings, 0 replies; 6+ messages in thread
From: Muchun Song @ 2018-11-06 15:12 UTC (permalink / raw)
  To: rafael; +Cc: gregkh, linux-kernel

Hi Rafael,

>
> On Tue, Nov 6, 2018 at 3:43 PM Muchun Song <smuchun@gmail.com> wrote:
> >
> > Hi Rafael,
> >
> > If we want the driver core to test driver remove functions, we can
> > enable CONFIG_DEBUG_TEST_DRIVER_REMOVE. This option is
> > just for testing it. So, in most cases, the option is disabled and the if
> > condition is false. So I think we can add an unlikely() to it.
>
> Yes, it can be added there, but does it really need to be added?
>
> If the conditions are false all the time, the branch predictor in the
> processor should be able to deal with it just fine.
>
> And if they are false already at build time, the compiler should just
> optimize them away.

Thank you for your explanation. I really didn't take into account the
situation you said. Yeah, the compiler can optimize them away or
the processor can deal with it.

So,  we don't need to add unlikely() to it. The patch does't make sense.
Thanks.

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

* Re: [PATCH] driver core: Add branch prediction hints in really_probe()
  2018-11-06 13:46 [PATCH] driver core: Add branch prediction hints in really_probe() Muchun Song
  2018-11-06 14:30 ` Rafael J. Wysocki
@ 2018-11-06 20:26 ` Greg KH
  1 sibling, 0 replies; 6+ messages in thread
From: Greg KH @ 2018-11-06 20:26 UTC (permalink / raw)
  To: Muchun Song; +Cc: rafael, linux-kernel

On Tue, Nov 06, 2018 at 09:46:30PM +0800, Muchun Song wrote:
> If condition is false in most cases. So, add an unlikely() to the if
> condition, so that the optimizer assumes that the condition is false.
> 
> Signed-off-by: Muchun Song <smuchun@gmail.com>

As Rafael said, don't do stuff like this unless you can actually measure
the difference.  Last time we tested the kernel for this a few years
ago, about 75% of the unlikely/likely additions were incorrect, removing
them made the kernel faster.  Turns out the compiler and cpu know better
than developers do :)

Also, the probe() path is never a hot-path to worry about stuff like
this.

thanks,

greg k-h

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

end of thread, other threads:[~2018-11-06 20:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-06 13:46 [PATCH] driver core: Add branch prediction hints in really_probe() Muchun Song
2018-11-06 14:30 ` Rafael J. Wysocki
2018-11-06 14:43   ` Muchun Song
2018-11-06 14:59     ` Rafael J. Wysocki
2018-11-06 15:12       ` Muchun Song
2018-11-06 20:26 ` Greg KH

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).