From patchwork Tue Aug 8 03:30:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Cromie X-Patchwork-Id: 132620 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2290010vqr; Tue, 8 Aug 2023 10:47:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLDfwWGbVHV4xCPUjz3btCixCg+Cd+fXv6BvRrEHkjhRwveHAxGktiqP3/6nmxet6ofw+8 X-Received: by 2002:a17:902:e884:b0:1bc:7001:6e5e with SMTP id w4-20020a170902e88400b001bc70016e5emr491319plg.32.1691516847371; Tue, 08 Aug 2023 10:47:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691516847; cv=none; d=google.com; s=arc-20160816; b=k4rBKxk6AovLkmthA9olHTMkFCqVAGe3CsGLyHMg2p2+2vbt3YGHshc9QiGKaRsFh6 JQB1efcNbEHNeuDYVymcsG1W/ezvHM5E7T6n1sHO83JM1BeqaFPZVFJThHeHBrJB01al xIn+xt5mqoMyeTJWHzKyAAjoesEG9WDTe9RkjRasRJrkvrEzMTC8uFIQ3OHWaEgAp6/T Lv2K+uh0Y3bJjt0LBYl8AEs52E0mHrxIbl74JjUJC22DOKPhYtD0VjmEYxsuzTVjijsT uoTTUhyRhsGK1phd/56zRoe/UmWYbY8Ud/Vo6uHRAyyobwCufKft/bfVY6tI/7DnHgxP aYyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Qq8u+P5B599QNMU/hMo97WZZje4v2kxUMmhAQURJTHU=; fh=4slIhm+5cB7alRcvLtQEEbnq2MWKGpVa7fokhEANJjI=; b=j5nOOgd2xwA+DGCoQZd+0fYyS9awZLWrEtygaWqJ73Ru6TZSpxgi+6V1T6sQWOmE5F usXnKXQjd6r3NvJnl/js/1+WNRdOkTZHqoyeY1Aa7vB12aDZEbZ2WyTq7Qpi+QMcgQzz J1S4pd2e6Rsg67r+75V3/tvLZdWwDDLVvYEsyWwHMOrebnwcviikJ7Tau9jC9mdvNp4Y FC2pIg/wP98QjyfdZSpuYJWoQxgCi9ILMUSDpRG+DgS9LucKPXatBOgoFteL/AfU85QX M70aZYZaPMrPiVJk/VGLtSPslqEve+xhmCKV24IH7+YbvLGIK3Rb+Xmwfh84VcV/1ugT k/4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="lrC69s/9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n4-20020a170902d2c400b001b8987f4004si7966467plc.541.2023.08.08.10.47.13; Tue, 08 Aug 2023 10:47:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="lrC69s/9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229655AbjHHPhv (ORCPT + 99 others); Tue, 8 Aug 2023 11:37:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229582AbjHHPg4 (ORCPT ); Tue, 8 Aug 2023 11:36:56 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C3D02113 for ; Mon, 7 Aug 2023 20:30:27 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-76c845dc5beso312577785a.1 for ; Mon, 07 Aug 2023 20:30:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691465426; x=1692070226; 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=Qq8u+P5B599QNMU/hMo97WZZje4v2kxUMmhAQURJTHU=; b=lrC69s/9+ztDiIY1TLG6g1c6x7Nx2jLCtsJtQTTX7FNKTGGAl7mvxs2oCQVyBQocCA N3AxMNtBHlyn0Un41DO29tDJibRHSUk6rjR7G19/9mJpTYsI06Kw7zjjzyOoAD6Z4GCm +KTlhbUw+6Lt2Jsp9fUOuxugb5wHQjsVj8JkduXP9wiY0qO3AfGcuoP8VawFResSaCti 0enkDVNEKcgkx4uY3A55Z4zMTngcYayl2eHWhyGajNcftM/fNLUJOqrkqs3ef8yr/Uvz IdAibuiXBwxVYeUXxYN1q+keEWqE0X7yZ3LQTSEzhMD7jR1U3a6XDh7uTvJAfqUd3XqK 587w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691465426; x=1692070226; 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=Qq8u+P5B599QNMU/hMo97WZZje4v2kxUMmhAQURJTHU=; b=E4FWwoy0DVgvRgYPc8/312skrRyXfzyJyuIcTDOZU+3woo/lFUQ7zG8Ozfi8zMorNC AAmnnPes5SBpfJFWMcUSV1Q3qlEepAKYWL6JyxBmM3oapHqVGmJSdFU04DFU4LJ7el12 poDTo7wOU9VjTIbH3xhz5pdvxNwL/B1dp+cWCwItw0dlZRJjkKKrlf4RAo48g4lw0mSq OjNHj+B/J1IpqEE45nMr4IEWoPm+oiaReiuGvGSUaf5wWjod86KTn/h0tH6CKgkijzpu 1mn6WhARYlFiPbsJ0FF+aicTZtNoeYro4S8hb+VyX3Dr37Di2ywnVTkKMOoYAbaeYlYT +OYQ== X-Gm-Message-State: AOJu0YyzebIAXH7WW8ErozFTmuPRlwElLOs82kh0G8DZJfBM2i99pfkR 9Mk909V8tJrmHgMqZj8NHEFxaXafVeypXQ== X-Received: by 2002:a05:620a:258a:b0:765:3ca0:3708 with SMTP id x10-20020a05620a258a00b007653ca03708mr8876142qko.75.1691465425812; Mon, 07 Aug 2023 20:30:25 -0700 (PDT) Received: from frodo.. (c-73-78-62-130.hsd1.co.comcast.net. [73.78.62.130]) by smtp.googlemail.com with ESMTPSA id em12-20020a0566384dac00b0042bb5a8e074sm2854031jab.8.2023.08.07.20.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 20:30:25 -0700 (PDT) From: Jim Cromie To: linux-kernel@vger.kernel.org Cc: akpm@linux-foundation.org, Jim Cromie , apw@canonical.com, joe@perches.com Subject: [PATCH 1/2] checkpatch: special case extern struct in .c Date: Mon, 7 Aug 2023 21:30:18 -0600 Message-ID: <20230808033019.21911-2-jim.cromie@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230808033019.21911-1-jim.cromie@gmail.com> References: <20230808033019.21911-1-jim.cromie@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773683969810754060 X-GMAIL-MSGID: 1773683969810754060 "externs should be avoided in .c files" needs an exception for linker symbols, like those that mark the start, stop of many kernel sections. Since checkpatch already checks REALNAME to avoid looking at fragments changing vmlinux.lds.h, add a new else-if block to look at them instead. As a simple heuristic, treat all words (in the patch-line) as possible symbols, to screen later warnings. For my test case, the possible-symbols included BOUNDED_BY (a macro), which is extra, but not troublesome - these are just to screen WARNINGS that might be issued on later fragments (changing .c files) Where the WARN is issued, precede it with an else-if block to catch one common extern-in-c use case: "extern struct foo bar[]". Here we can at least issue a softer warning, after checking for a match with a maybe-linker-symbol parsed earlier from the patch. Though heuristic, it worked for my test-case, allowing both start__, stop__ $symbol's (wo the prefixes specifically named). I've coded it narrowly, it can be expanded later to cover any other expressions. It does require that the externs in .c's have the additions to vmlinux.lds.h in the same patch. And requires vmlinux.lds.h before .c fragments. Cc: apw@canonical.com Cc: joe@perches.com Signed-off-by: Jim Cromie --- scripts/checkpatch.pl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 880fde13d9b8..6aabcc1f66c1 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -74,6 +74,8 @@ my $git_command ='export LANGUAGE=en_US.UTF-8; git'; my $tabsize = 8; my ${CONFIG_} = "CONFIG_"; +my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h + sub help { my ($exitcode) = @_; @@ -6051,6 +6053,9 @@ sub process { # check for line continuations outside of #defines, preprocessor #, and asm + } elsif ($realfile =~ m@/vmlinux.lds.h$@) { + $line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge; + #print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol; } else { if ($prevline !~ /^..*\\$/ && $line !~ /^\+\s*\#.*\\$/ && # preprocessor @@ -7119,6 +7124,21 @@ sub process { "arguments for function declarations should follow identifier\n" . $herecurr); } + } elsif ($realfile =~ /\.c$/ && defined $stat && + $stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/) + { + my ($st_type, $st_name) = ($1, $2); + + for my $s (keys %maybe_linker_symbol) { + #print "Linker symbol? $st_name : $s\n"; + goto LIKELY_LINKER_SYMBOL + if $st_name =~ /$s/; + } + WARN("AVOID_EXTERNS", + "found a file-scoped extern type:$st_type name:$st_name in .c file\n" + . "is this a linker symbol ?\n" . $herecurr); + LIKELY_LINKER_SYMBOL: + } elsif ($realfile =~ /\.c$/ && defined $stat && $stat =~ /^.\s*extern\s+/) {