Linux Power Management development
 help / color / mirror / Atom feed
From: Julia Lawall <Julia.Lawall@lip6.fr>
To: Sebastian Reichel <sre@kernel.org>
Cc: kernel-janitors@vger.kernel.org,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Michal Marek <mmarek@suse.com>,
	cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	Bastien Nocera <hadess@hadess.net>,
	Stephen Just <stephenjust@gmail.com>,
	"Rafael J . Wysocki" <rafael.j.wysocki@intel.com>,
	Len Brown <lenb@kernel.org>,
	Robert Moore <robert.moore@intel.com>,
	Lv Zheng <lv.zheng@intel.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	linux-acpi@vger.kernel.org, devel@acpica.org,
	linux-pm@vger.kernel.org
Subject: [PATCH] coccinelle: api: detect unnecessary le16_to_cpu
Date: Sat,  1 Jul 2017 21:28:10 +0200	[thread overview]
Message-ID: <1498937290-12285-1-git-send-email-Julia.Lawall@lip6.fr> (raw)

As reported by Sebastian Reichel, i2c_smbus_read_word_data() returns native
endianness for little-endian bus (it basically has builtin
le16_to_cpu). Calling le16_to_cpu on the result breaks support on big
endian machines by converting it back.

This semantic patch give no reports on kernel code currently, but the
issue is somewhat obscure and has occurred in a sumitted patch, so it could
be good to have a check for it.

Suggested-by: Sebastian Reichel <sre@kernel.org>
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>

---

The rule could easily be extended with more such functions.  Let me know if
anything else should be taken into account.

 scripts/coccinelle/api/smbus_word.cocci |   45 ++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/scripts/coccinelle/api/smbus_word.cocci b/scripts/coccinelle/api/smbus_word.cocci
new file mode 100644
index 0000000..b167cf0
--- /dev/null
+++ b/scripts/coccinelle/api/smbus_word.cocci
@@ -0,0 +1,45 @@
+/// i2c_smbus_read_word_data() returns native endianness for little-endian
+/// bus (it basically has builtin le16_to_cpu). Calling le16_to_cpu on the
+/// result breaks support on big endian machines by converting it back.
+///
+// Confidence: Moderate
+// Copyright: (C) 2017 Julia Lawall, Inria. GPLv2.
+// URL: http://coccinelle.lip6.fr/
+// Options: --no-includes --include-headers
+// Keywords: i2c_smbus_read_word_data, le16_to_cpu
+
+virtual context
+virtual org
+virtual report
+
+// ----------------------------------------------------------------------------
+
+@r depends on context || org || report exists@
+expression e, x;
+position j0, j1;
+@@
+
+* x@j0 = i2c_smbus_read_word_data(...)
+... when != x = e
+* le16_to_cpu@j1(x)
+
+// ----------------------------------------------------------------------------
+
+@script:python r_org depends on org@
+j0 << r.j0;
+j1 << r.j1;
+@@
+
+msg = "le16_to_cpu not needed on i2c_smbus_read_word_data result."
+coccilib.org.print_todo(j0[0], msg)
+coccilib.org.print_link(j1[0], "")
+
+// ----------------------------------------------------------------------------
+
+@script:python r_report depends on report@
+j0 << r.j0;
+j1 << r.j1;
+@@
+
+msg = "le16_to_cpu not needed on i2c_smbus_read_word_data result around line %s." % (j1[0].line)
+coccilib.report.print_report(j0[0], msg)


             reply	other threads:[~2017-07-01 19:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-01 19:28 Julia Lawall [this message]
2017-07-03 13:36 ` [PATCH] coccinelle: api: detect unnecessary le16_to_cpu Sebastian Reichel
2017-07-03 16:37   ` Andy Shevchenko
2017-07-03 17:14     ` Sebastian Reichel
2017-07-03 17:33       ` Andy Shevchenko
2017-07-03 18:20         ` Sebastian Reichel
2017-07-04  9:11           ` Julia Lawall
2017-07-04  9:51             ` Andy Shevchenko
2017-07-04  9:53               ` Julia Lawall

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=1498937290-12285-1-git-send-email-Julia.Lawall@lip6.fr \
    --to=julia.lawall@lip6.fr \
    --cc=Gilles.Muller@lip6.fr \
    --cc=andy.shevchenko@gmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=cocci@systeme.lip6.fr \
    --cc=devel@acpica.org \
    --cc=hadess@hadess.net \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lv.zheng@intel.com \
    --cc=mika.westerberg@linux.intel.com \
    --cc=mmarek@suse.com \
    --cc=nicolas.palix@imag.fr \
    --cc=rafael.j.wysocki@intel.com \
    --cc=robert.moore@intel.com \
    --cc=sre@kernel.org \
    --cc=stephenjust@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox