From nobody Wed Dec 17 15:36:08 2025 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1ED1D4C94; Tue, 7 May 2024 03:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715052504; cv=none; b=hJxrRAikffZxWmOTnAMvrXwiGxN324PsN0JylBNr9c2QJ4halmjJ2ngHPr4jGY8QgcdA+ORD+brvHOjHKX8wFcVEK9od4v/mSJym6vwMPzmW2buM142+st2aYx8ipwyI0CTI5U7sQi+TzTYQm9MBwm/bX9oBDYVpE70YsYlczwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715052504; c=relaxed/simple; bh=VM7uBtgDykOSCEFvxGZTHdHh834Ycngh2ZDz0aAHDjQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Xs8I2tefgqqjYbt7OySHOhMdFzfv5dZ01AjO/fceOGjSeH3GTGcl2C6p5uM6WBf1MdGXGUbCUylMA+Gmy6itPC2fUIATGjaQqrfOU7UGsiMiJ4G3N7Vyg4TyUrFRdv4YxrQ1slXWL9qTm6SdOeR1qTDP00I2u2itQhScrD+jJ34= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jNOz6v75; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jNOz6v75" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6f453d2c5a1so2543874b3a.2; Mon, 06 May 2024 20:28:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715052502; x=1715657302; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O1yoif+153AIw3iNaB4BG80MPHwUmzwez6HYhpKPz5Y=; b=jNOz6v75AKuM7kuVcRxNWPWErhAZJHv1v5kXS/yynL5+HbEVQkVyHQZC1Qoa2TIdTb 6nyOjCNv3hUB49ev4RgqF+6qRqHMz/8/ofnhhIiYynitVwfQdUq+U3GbLpZMprkXf1Li F/euTWUlcG5eM1W3o0umYO0Igc/orm/EayHWdRh3EpDG45wBvj2jsSpuXit1KmjC6Y5l FNCfenRHSknwKkusT25H7Yabz4VJVkwuP2LTzzecX3IHKHOwBA8NRyMGv++3gN8k6+EX MOoxPpE+ceC60RXyQm5TM0mTFFGCa4BI31veqBevmDZsOuRkKYNwd1s5vmOvPcKfoLM+ 4+xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715052502; x=1715657302; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O1yoif+153AIw3iNaB4BG80MPHwUmzwez6HYhpKPz5Y=; b=F431ORCoslLT7+2nLpkSK7cblpZUR45TVbXrzcpdvjt6YAXSkLFLp6ov+WGKbw9xpI 7KeIBKRqFOlJ3v1KLGwZE0j/BTVgN2iAWkkVdGWXAhVqCNJ6sGqy+IDxTY1B/GJm8/mx 2O4S0oqqb+RHK9Y5y9Zns60QrCoSL2QbYwfhj3al1ppiuJsMdUrEVw2sx8fxm8jFMicS qjG3dQ4bTHFPHIUdcAy5cIAWrjVu88cd2Q7DwollFa4DomAFINy35hQsFuSfE1dNzR+n MxMu57/VjFO2cNYNS4uJaqDcAvklT4vT28mkQCoO2IzXjm4+B71hNoySN96Wt9845lEy ADeA== X-Forwarded-Encrypted: i=1; AJvYcCVFdHfeos1gHK6+ebGUYiyRZWSaOi47PGXmOCdwDE55tmOLcqomP8T/X0IlWfWx9+LPLBYt90q/0kH8nhPKj/VUwZ94QxGe9RKsRtUXD5lkMuuPV3PuWrXbxYaNYRjzG7XZXxZpzsEH5o7yVcg1AM+Bevovsd83fEBvC+QKCsC9nTtL X-Gm-Message-State: AOJu0Yz2S4OqjPhY7oGEKhB3Fnq9zkP4vAnhjQZ134m0DBC1HynYmd5n cT8Tb3qXGe9CNjjQFwqNufVAXAFvV/qjxoKoWIft7YN6yN64OKB2eiNjGA== X-Google-Smtp-Source: AGHT+IHKL3g+3kTUa1oQMquQVXpn+yfHlbbziGb6kfxG2mhrQGITfNK2pPt0dtfvj3MsH1zU89N41g== X-Received: by 2002:a05:6a21:8cc6:b0:1a9:a820:c604 with SMTP id ta6-20020a056a218cc600b001a9a820c604mr16337860pzb.48.1715052502344; Mon, 06 May 2024 20:28:22 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id p16-20020a170902ebd000b001ebd7bdaaffsm8988409plg.288.2024.05.06.20.28.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 20:28:22 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-doc@vger.kernel.org Cc: apw@canonical.com, broonie@kernel.org, chenhuacai@loongson.cn, chris@zankel.net, corbet@lwn.net, dwaipayanray1@gmail.com, herbert@gondor.apana.org.au, joe@perches.com, linux-kernel@vger.kernel.org, linux@roeck-us.net, lukas.bulwahn@gmail.com, mac.xxn@outlook.com, sfr@canb.auug.org.au, v-songbaohua@oppo.com, workflows@vger.kernel.org, Max Filippov Subject: [PATCH v7 1/2] Documentation: coding-style: ask function-like macros to evaluate parameters Date: Tue, 7 May 2024 15:27:56 +1200 Message-Id: <20240507032757.146386-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240507032757.146386-1-21cnbao@gmail.com> References: <20240507032757.146386-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Barry Song Recent commit 77292bb8ca69c80 ("crypto: scomp - remove memcpy if sg_nents is 1 and pages are lowmem") leads to warnings on xtensa and loongarch, In file included from crypto/scompress.c:12: include/crypto/scatterwalk.h: In function 'scatterwalk_pagedone': include/crypto/scatterwalk.h:76:30: warning: variable 'page' set but not= used [-Wunused-but-set-variable] 76 | struct page *page; | ^~~~ crypto/scompress.c: In function 'scomp_acomp_comp_decomp': >> crypto/scompress.c:174:38: warning: unused variable 'dst_page' [-Wunused= -variable] 174 | struct page *dst_page =3D sg_page(req->d= st); | The reason is that flush_dcache_page() is implemented as a noop macro on these platforms as below, #define flush_dcache_page(page) do { } while (0) The driver code, for itself, seems be quite innocent and placing maybe_unused seems pointless, struct page *dst_page =3D sg_page(req->dst); for (i =3D 0; i < nr_pages; i++) flush_dcache_page(dst_page + i); And it should be independent of architectural implementation differences. Let's provide guidance on coding style for requesting parameter evaluation or proposing the migration to a static inline function. Signed-off-by: Barry Song Suggested-by: Max Filippov Reviewed-by: Mark Brown Acked-by: Joe Perches Cc: Chris Zankel Cc: Huacai Chen Cc: Herbert Xu Cc: Guenter Roeck Cc: Stephen Rothwell Cc: Andy Whitcroft Cc: Dwaipayan Ray Cc: Joe Perches Cc: Jonathan Corbet Cc: Lukas Bulwahn Cc: Xining Xu --- Documentation/process/coding-style.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Documentation/process/coding-style.rst b/Documentation/process= /coding-style.rst index 9c7cf7347394..7e768c65aa92 100644 --- a/Documentation/process/coding-style.rst +++ b/Documentation/process/coding-style.rst @@ -827,6 +827,29 @@ Macros with multiple statements should be enclosed in = a do - while block: do_this(b, c); \ } while (0) =20 +Function-like macros with unused parameters should be replaced by static +inline functions to avoid the issue of unused variables: + +.. code-block:: c + + static inline void fun(struct foo *foo) + { + } + +Due to historical practices, many files still employ the "cast to (void)" +approach to evaluate parameters. However, this method is not advisable. +Inline functions address the issue of "expression with side effects +evaluated more than once", circumvent unused-variable problems, and +are generally better documented than macros for some reason. + +.. code-block:: c + + /* + * Avoid doing this whenever possible and instead opt for static + * inline functions + */ + #define macrofun(foo) do { (void) (foo); } while (0) + Things to avoid when using macros: =20 1) macros that affect control flow: --=20 2.34.1 From nobody Wed Dec 17 15:36:08 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 073E751010; Tue, 7 May 2024 03:28:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715052511; cv=none; b=beZP7sJVLx7UkQ5CNm0NtQM3oxxUFz/l7CHhL/gCO4fzMk2SxUx8wqV/CKTTT0Y1we7tvzQ7TIIxPq/fL7XWUk2vL0z1/l3rB8wUClMxc+wrcTMMPk2coIFITWH+mX+TernWCSkRmsMeeHVSQ9ZVEO3QxIZoxBqwfPeGvbczQZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715052511; c=relaxed/simple; bh=RJFOiqz4QzNMe5fUaO49lQKw+BLYKHJV5ZAJCUpwMoU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Niciju62KqKrFDIRM7OE43uCEauU4nyjvf6ti8pEQnGwQmfG1M/u4PdVB/LtT9s94NoX/buLiG/2Kob9AHRjnArfIydNWc5HIUxGUOU67Z967KW6qjjtsf0Nn/kBdyvOsjR4nKyXhN7geWsZQUqTi0vyfonjF2mBDWl3xga7rE8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SuczOKv2; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SuczOKv2" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1ed41eb3382so19917215ad.0; Mon, 06 May 2024 20:28:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715052509; x=1715657309; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SO4svkG2NjlN6TUmciRZsz/RMioTaNbu5km5tihixL8=; b=SuczOKv2EEwxVN3jnA//e3vuukpTB1USQJtZo4DxwPcyfrlaBVj3P9T6+nfeZ6qUEl Vj3gnX+nVa7p3/iXtzlzr9Erq6uaXUsWYeeid6esYR8dFG+xrgltIviI6j2dUlhgbULN I2dvEO5PiI/0fZuUqj3SWxb+sPhvPj68u7QdqnX65hdUkll37hAKV2b4KXeQnuWzOZQi 5JK/XYRB9H8LzsPKx2TBJW+s+GLkpcyUngvrKcfnS0fZyX/ezpgZFGLWmx2iQaJ9/Uz4 6XOPVnKS3RO6nnVMtKm2NV3S5Vwa/mGcDP81TqycSOZEjb6A5xxE5KKK+ty6R8ks+3Xr Wedw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715052509; x=1715657309; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SO4svkG2NjlN6TUmciRZsz/RMioTaNbu5km5tihixL8=; b=mzhRaH1sD7kUJvx47yWuZonuKWeX2TTYbZqE5nwR3dgbL7u4OCAh5A/FENnSfpr9A6 YxM3CtMfFKhZJ6C9BD9FXq7wR9hyQ1dA2TdUY/b4yJB9k5s6m2Ge4Dws9xh6h4xsIwxH DtuVWAAhq0G7AdS7FhEpuo0BYhZGwZnAGpRiUxzAf64nGaahxoGBl4Yy21TWsyzxFkJR JdiOoeLJGenzR3AgO3LnuAg82KxigF8uiO6/qVZY286aK5R2LFUFrGY8Nyr353eiOwfv rL5UOlM1bXgPRdEKugZ90d1yLyxWapNoKSuxCSEWTYGi5/RMdc7lH/SKGe2q9SLMyTEO p9FQ== X-Forwarded-Encrypted: i=1; AJvYcCVrLlM/yooDDVkw17FHtsjElqCdjCJtf84XSq04lfUFYSVRxMm8PPNgQt6sOl6RwD+1PwSx8Rb95c8LV8+JyOLpp5Ya9K76o4x19kknXod0R/JXaY4yiqFjcdq9Rn/zlxZmKIw2oolgXR+JqBSnAiED5KKJNfNuffrDBRE2Tax5/MaM X-Gm-Message-State: AOJu0Yy2+hja8Ml+JCw67Rm1Ig4gW3QJSUufZhcuXxPXwXStMxxO70tu Qvf/JZ2Ssbbow+/ZTqEo8MlzFFaEmyEUrUB2rVMV06mzo5IOaYF4 X-Google-Smtp-Source: AGHT+IG3qDqL1LI9mMmJY13LJ3nnxFJ9FUPUeAEV+FMwxCHYwOzUfyhz2TCvIPCdZnfdRqvb8d6a8A== X-Received: by 2002:a17:902:e5c7:b0:1e8:92:c5e2 with SMTP id u7-20020a170902e5c700b001e80092c5e2mr16956588plf.47.1715052509309; Mon, 06 May 2024 20:28:29 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id p16-20020a170902ebd000b001ebd7bdaaffsm8988409plg.288.2024.05.06.20.28.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 20:28:28 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-doc@vger.kernel.org Cc: apw@canonical.com, broonie@kernel.org, chenhuacai@loongson.cn, chris@zankel.net, corbet@lwn.net, dwaipayanray1@gmail.com, herbert@gondor.apana.org.au, joe@perches.com, linux-kernel@vger.kernel.org, linux@roeck-us.net, lukas.bulwahn@gmail.com, mac.xxn@outlook.com, sfr@canb.auug.org.au, v-songbaohua@oppo.com, workflows@vger.kernel.org, Max Filippov , Jeff Johnson , Charlemagne Lasse Subject: [PATCH v7 2/2] scripts: checkpatch: check unused parameters for function-like macro Date: Tue, 7 May 2024 15:27:57 +1200 Message-Id: <20240507032757.146386-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240507032757.146386-1-21cnbao@gmail.com> References: <20240507032757.146386-1-21cnbao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Xining Xu If function-like macros do not utilize a parameter, it might result in a build warning. In our coding style guidelines, we advocate for utilizing static inline functions to replace such macros. This patch verifies compliance with the new rule. For a macro such as the one below, #define test(a) do { } while (0) The test result is as follows. WARNING: Argument 'a' is not used in function-like macro #21: FILE: mm/init-mm.c:20: +#define test(a) do { } while (0) total: 0 errors, 1 warnings, 8 lines checked Signed-off-by: Xining Xu Tested-by: Barry Song Signed-off-by: Barry Song Acked-by: Joe Perches Cc: Chris Zankel Cc: Huacai Chen Cc: Herbert Xu Cc: Guenter Roeck Cc: Stephen Rothwell Cc: Mark Brown Cc: Andy Whitcroft Cc: Dwaipayan Ray Cc: Jonathan Corbet Cc: Lukas Bulwahn Cc: Max Filippov Cc: Jeff Johnson Cc: Charlemagne Lasse --- Documentation/dev-tools/checkpatch.rst | 14 ++++++++++++++ scripts/checkpatch.pl | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-too= ls/checkpatch.rst index 127968995847..a9fac978a525 100644 --- a/Documentation/dev-tools/checkpatch.rst +++ b/Documentation/dev-tools/checkpatch.rst @@ -906,6 +906,20 @@ Macros, Attributes and Symbols =20 See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/ =20 + **MACRO_ARG_UNUSED** + If function-like macros do not utilize a parameter, it might result + in a build warning. We advocate for utilizing static inline functions + to replace such macros. + For example, for a macro such as the one below:: + + #define test(a) do { } while (0) + + there would be a warning like below:: + + WARNING: Argument 'a' is not used in function-like macro. + + See: https://www.kernel.org/doc/html/latest/process/coding-style.html#= macros-enums-and-rtl + **SINGLE_STATEMENT_DO_WHILE_MACRO** For the multi-statement macros, it is necessary to use the do-while loop to avoid unpredictable code paths. The do-while loop helps to diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 9c4c4a61bc83..2b812210b412 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -6040,6 +6040,12 @@ sub process { CHK("MACRO_ARG_PRECEDENCE", "Macro argument '$arg' may be better as '($arg)' to avoid precede= nce issues\n" . "$herectx"); } + +# check if this is an unused argument + if ($define_stmt !~ /\b$arg\b/) { + WARN("MACRO_ARG_UNUSED", + "Argument '$arg' is not used in function-like macro\n" . "$herec= tx"); + } } =20 # check for macros with flow control, but without ## concatenation --=20 2.34.1