From patchwork Sat Jul 1 19:28:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julia Lawall X-Patchwork-Id: 9820945 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5E1B7603F2 for ; Sat, 1 Jul 2017 19:52:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 50F1C283C0 for ; Sat, 1 Jul 2017 19:52:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4588028520; Sat, 1 Jul 2017 19:52:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F005C283C0 for ; Sat, 1 Jul 2017 19:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751968AbdGATww (ORCPT ); Sat, 1 Jul 2017 15:52:52 -0400 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:60373 "EHLO mail2-relais-roc.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751922AbdGATwv (ORCPT ); Sat, 1 Jul 2017 15:52:51 -0400 X-IronPort-AV: E=Sophos;i="5.40,293,1496095200"; d="scan'208";a="281617762" Received: from palace.rsr.lip6.fr (HELO localhost.localdomain) ([132.227.105.202]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/AES128-SHA256; 01 Jul 2017 21:52:49 +0200 From: Julia Lawall To: Sebastian Reichel Cc: kernel-janitors@vger.kernel.org, Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org, Benjamin Tissoires , Bastien Nocera , Stephen Just , "Rafael J . Wysocki" , Len Brown , Robert Moore , Lv Zheng , Mika Westerberg , Andy Shevchenko , 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 Message-Id: <1498937290-12285-1-git-send-email-Julia.Lawall@lip6.fr> X-Mailer: git-send-email 1.9.1 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Julia Lawall --- 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)