All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Machek <pavel@ucw.cz>
To: Joe Perches <joe@perches.com>
Cc: Denis Efremov <efremov@linux.com>,
	cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org,
	Julia Lawall <Julia.Lawall@lip6.fr>,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Michal Marek <michal.lkml@markovi.net>
Subject: Re: [PATCH] scripts: coccinelle: check for !(un)?likely usage
Date: Sun, 1 Sep 2019 19:24:03 +0200	[thread overview]
Message-ID: <20190901172403.GA1047@bug> (raw)
In-Reply-To: <b5bae2981e27d133b61d99b08ee60244bf7aabe3.camel@perches.com>

Hi!

> > This patch adds coccinelle script for detecting !likely and !unlikely
> > usage. It's better to use unlikely instead of !likely and vice versa.
> 
> Please explain _why_ is it better in the changelog.
> 
> btw: there are relatively few uses like this in the kernel.
> 
> $ git grep -P '!\s*(?:un)?likely\s*\(' | wc -l
> 40
> 
> afaict: It may save 2 bytes of x86/64 object code.
> 
> For instance:
> 
> $ diff -urN kernel/tsacct.lst.old kernel/tsacct.lst.new|less
> --- kernel/tsacct.lst.old       2019-08-25 09:21:39.936570183 -0700
> +++ kernel/tsacct.lst.new       2019-08-25 09:22:20.774324886 -0700
> @@ -24,158 +24,153 @@
>    15:  48 89 fb                mov    %rdi,%rbx
>         u64 time, delta;
>  
> -       if (!likely(tsk->mm))
> +       if (unlikely(tsk->mm))

Are you sure this is equivalent?

									Pavel



>    18:  4c 8d ab 28 02 00 00    lea    0x228(%rbx),%r13
>    1f:  e8 00 00 00 00          callq  24 <__acct_update_integrals+0x24>
>                         20: R_X86_64_PLT32      __sanitizer_cov_trace_pc-0x4
>    24:  4c 89 ef                mov    %r13,%rdi
>    27:  e8 00 00 00 00          callq  2c <__acct_update_integrals+0x2c>
>                         28: R_X86_64_PLT32      __asan_load8_noabort-0x4
> -  2c:  4c 8b bb 28 02 00 00    mov    0x228(%rbx),%r15
> -  33:  4d 85 ff                test   %r15,%r15
> -  36:  74 34                   je     6c <__acct_update_integrals+0x6c>
> +  2c:  48 83 bb 28 02 00 00    cmpq   $0x0,0x228(%rbx)
> +  33:  00 
> +  34:  75 34                   jne    6a <__acct_update_integrals+0x6a>
>                 return;
> 
> And here's a possible equivalent checkpatch test.
> ---
>  scripts/checkpatch.pl | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 287fe73688f0..364603ad1a47 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -6529,6 +6529,24 @@ sub process {
>  			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
>  		}
>  
> +# !(likely|unlikely)(condition) use should be (unlikely|likely)(condition)
> +		if ($perl_version_ok &&
> +		    $line =~ /(\!\s*((?:un)?likely))\s*$balanced_parens/) {
> +			my $match = $1;
> +			my $type =  $2;
> +			my $reverse;
> +			if ($type eq "likely") {
> +				$reverse = "unlikely";
> +			} else {
> +				$reverse = "likely";
> +			}
> +			if (WARN("LIKELY_MISUSE",
> +				 "Prefer $reverse over $match\n" . $herecurr) &&
> +			    $fix) {
> +				$fixed[$fixlinenr] =~ s/\Q$match\E\s*\(/$reverse(/;
> +			}
> +		}
> +
>  # whine mightly about in_atomic
>  		if ($line =~ /\bin_atomic\s*\(/) {
>  			if ($realfile =~ m@^drivers/@) {
> 

-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

  parent reply	other threads:[~2019-09-01 17:24 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-25 13:05 [Cocci] [PATCH] scripts: coccinelle: check for !(un)?likely usage Denis Efremov
2019-08-25 13:05 ` Denis Efremov
2019-08-25 15:30 ` [Cocci] " Markus Elfring
2019-08-25 15:30   ` Markus Elfring
2019-08-25 21:06   ` [Cocci] " Denis Efremov
2019-08-25 21:06     ` Denis Efremov
2019-08-25 15:30 ` [Cocci] " Markus Elfring
2019-08-25 15:30   ` Markus Elfring
2019-08-25 16:37 ` [Cocci] " Joe Perches
2019-08-25 16:37   ` Joe Perches
2019-08-25 18:59   ` [Cocci] " Denis Efremov
2019-08-25 18:59     ` Denis Efremov
2019-08-25 19:19     ` [Cocci] " Julia Lawall
2019-08-25 19:19       ` Julia Lawall
2019-08-28 11:33       ` [Cocci] " Rasmus Villemoes
2019-08-28 11:33         ` Rasmus Villemoes
2019-08-28 11:59         ` [Cocci] " Joe Perches
2019-08-28 11:59           ` Joe Perches
2019-08-28 12:33         ` [Cocci] " Denis Efremov
2019-08-28 12:33           ` Denis Efremov
2019-08-28 13:05           ` Rasmus Villemoes
2019-08-28 13:14             ` Denis Efremov
2019-08-28 12:33         ` [Cocci] " Julia Lawall
2019-08-28 12:33           ` Julia Lawall
2019-08-28 12:41       ` [Cocci] " Denis Efremov
2019-08-28 12:41         ` Denis Efremov
2019-08-28 13:57         ` [Cocci] " Denis Efremov
2019-08-28 13:57           ` Denis Efremov
2019-08-25 21:19     ` [Cocci] " Denis Efremov
2019-08-25 21:19       ` Denis Efremov
2019-09-01 17:24   ` Pavel Machek [this message]
2019-09-01 17:39     ` [Cocci] " Denis Efremov
2019-09-01 17:39       ` Denis Efremov
2019-08-29 17:10 ` [Cocci] [PATCH v2] " Denis Efremov
2019-08-29 17:10   ` Denis Efremov
2019-08-29 17:13   ` [Cocci] " Denis Efremov
2019-08-29 17:13     ` Denis Efremov
2019-08-30  0:42     ` [Cocci] " Julia Lawall
2019-08-30  0:42       ` Julia Lawall
2019-08-30  6:56       ` [Cocci] " Denis Efremov
2019-08-30  6:56         ` Denis Efremov
2019-08-30  8:06         ` [Cocci] " Rasmus Villemoes
2019-08-30  8:06           ` Rasmus Villemoes
2019-08-29 20:07   ` [Cocci] " Markus Elfring
2019-08-29 20:07     ` Markus Elfring
2019-08-30  7:55   ` [Cocci] " Markus Elfring
2019-08-30  7:55     ` Markus Elfring
2019-08-30  7:55     ` Markus Elfring
2019-09-06 20:19   ` [Cocci] " Julia Lawall
2019-09-06 20:19     ` Julia Lawall
2019-09-06 20:55     ` [Cocci] " Denis Efremov
2019-09-06 20:55       ` Denis Efremov
2019-09-07  8:05       ` [Cocci] [v2] " Markus Elfring
2019-09-07  8:05         ` Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190901172403.GA1047@bug \
    --to=pavel@ucw.cz \
    --cc=Gilles.Muller@lip6.fr \
    --cc=Julia.Lawall@lip6.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=efremov@linux.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.lkml@markovi.net \
    --cc=nicolas.palix@imag.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.