Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp766355imm; Thu, 26 Jul 2018 11:36:16 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe+UkSnHDzWfT58X/Gp3Td/NzGto/S/7pf9OzbTAYgwuNev5VBOWO6HZG2lVZP+DO3YzHdF X-Received: by 2002:a63:220d:: with SMTP id i13-v6mr3053598pgi.212.1532630176453; Thu, 26 Jul 2018 11:36:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532630176; cv=none; d=google.com; s=arc-20160816; b=c4OF6msPyxNAP9toHn365uZUMiuZM33/5zo5HiGxmdcobyC570RECHRcHp9Ih/fhZu NwiJaICdVf660wWb5e1jV/2l0iyx2zh02/5De3HrgFWOqlsaY1IIoXlxaGv/2txqrnYg aWxhoLQsqp1zGXc54xGUTI/aU6lMF5OSuz7eAM3+Ao8nlAKId0NWFC/LGD7KcsjpRT0A AhBl6+vCLkqEQ6nAD/JfrXh9CVAHQ7jv3BbI9EcA7ivrKwwoiyTNxWEXJx4GtTexE2aU U1kI0DnZEVqsgtU8m2Z/eNUmb6sZa3c+rHVikUynggEgp1M3jrnUDwwt0lA8AdByI0N9 B7HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :date:cc:to:from:subject:message-id:arc-authentication-results; bh=EFenOtF/ixs4IyXdlHesiOhk9VEmECErwhiYzfpU7vo=; b=vnyg+bIzzwwfTv+OG58UnTtaycwsyXwhLWjXGz4jCaSXF6z4eamME7jRiwMyyvf2Q2 CQ8JU0gFFEGlW93yynUc9cD9s9BwLv92LvEZH9WVTZRlTtq5foRLWPR/uPww6lz0zCwc NLpzrhb0cGjty1CvNoGSqhZGHslvZI4h43Gol+HGAH748FCIOIykGq4eOotN65pqI88T J0CXNGA8rNwwfzQ88MoMvCvTdSsB8wKhUkekku9vEyHcNmM0UYJVc55sM06z6URz8v8J 7fiUT71frqwj0gyax6qvnJXP/sXLHHRZ1H6q+F98dgmNVTCWBn4AQcp3nW2PDi+RYYy7 T5QA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r12-v6si2027569pfd.193.2018.07.26.11.36.01; Thu, 26 Jul 2018 11:36:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731693AbeGZTpz (ORCPT + 99 others); Thu, 26 Jul 2018 15:45:55 -0400 Received: from smtprelay0082.hostedemail.com ([216.40.44.82]:49674 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730095AbeGZTpz (ORCPT ); Thu, 26 Jul 2018 15:45:55 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id 8D59B1807F308; Thu, 26 Jul 2018 18:27:54 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::,RULES_HIT:2:41:355:379:541:968:973:982:988:989:1260:1277:1311:1313:1314:1345:1437:1515:1516:1518:1535:1593:1594:1605:1606:1730:1747:1777:1792:2194:2197:2198:2199:2200:2201:2393:2553:2559:2562:2828:2919:3138:3139:3140:3141:3142:3653:3865:3866:3867:3870:3871:3872:3874:4117:4321:4605:5007:6120:7903:10004:10848:11026:11233:11473:11658:11914:12043:12295:12296:12438:12555:12679:12760:12986:13161:13229:13439:14659:21080:21212:21221:21325:21433:21451:21627:30029:30034:30054:30070:30080:30090,0,RBL:47.151.153.53:@perches.com:.lbl8.mailshell.net-62.8.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:26,LUA_SUMMARY:none X-HE-Tag: mask95_61ba8c5090b07 X-Filterd-Recvd-Size: 6204 Received: from XPS-9350.home (unknown [47.151.153.53]) (Authenticated sender: joe@perches.com) by omf07.hostedemail.com (Postfix) with ESMTPA; Thu, 26 Jul 2018 18:27:51 +0000 (UTC) Message-ID: <1236369d28b2f1f5389ff652c4eb89e699e6481e.camel@perches.com> Subject: [RFC PATCH] checkpatch: check for function calls with struct or union on stack From: Joe Perches To: Andrew Morton Cc: Andy Whitcroft , LKML Date: Thu, 26 Jul 2018 11:27:50 -0700 Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.28.1-2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I was cc'd on a patch where structs were used on stack instead of using pointers to the structs. This can cause defects when the calling function modifies the stack struct instead of the calling function's struct. Possible patch below, but it may be overkill for the number of instances where this is actually an issue. Thoughts? There are what seems to be some false positives for a few of the .h files in include/linux/... where the false positives are for very small structs where the indirection via a pointer might be slower than than the stack use. For instance: (some duplicates removed) $ git ls-files -- "include/linux/*.h" | \ while read file ; do ./scripts/checkpatch.pl -f --types=aggregate_on_stack $file --no-summary --quiet; done WARNING: Unusual use of 'struct bvec_iter' on stack #520: FILE: include/linux/bio.h:520: +void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter); WARNING: Unusual use of 'struct fd' on stack #433: FILE: include/linux/bpf.h:433: +struct bpf_map *__bpf_map_get(struct fd f); WARNING: Unusual use of 'struct ceph_vino' on stack #465: FILE: include/linux/ceph/osd_client.h:465: +extern struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *, + struct ceph_file_layout *layout, + struct ceph_vino vino, + u64 offset, u64 *len, + unsigned int which, int num_ops, + int opcode, int flags, + struct ceph_snap_context *snapc, + u32 truncate_seq, u64 truncate_size, + bool use_mempool); [] WARNING: Unusual use of 'union extcon_property_value' on stack #63: FILE: include/linux/extcon-provider.h:63: +extern int extcon_set_property(struct extcon_dev *edev, unsigned int id, + unsigned int prop, + union extcon_property_value prop_val); [] WARNING: Unusual use of 'struct ppa_addr' on stack #528: FILE: include/linux/lightnvm.h:528: +extern int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, + struct nvm_chk_meta *meta, struct ppa_addr ppa, + int nchks); [] WARNING: Unusual use of 'struct pin_cookie' on stack #367: FILE: include/linux/lockdep.h:367: +extern void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie); [] WARNING: Unusual use of 'struct kqid' on stack #77: FILE: include/linux/quota.h:77: +extern bool qid_eq(struct kqid left, struct kqid right); [] WARNING: Unusual use of 'struct reg_field' on stack #1051: FILE: include/linux/regmap.h:1051: +struct regmap_field *devm_regmap_field_alloc(struct device *dev, + struct regmap *regmap, struct reg_field reg_field); WARNING: Unusual use of 'struct rpmsg_channel_info' on stack #121: FILE: include/linux/rpmsg.h:121: +struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *, + rpmsg_rx_cb_t cb, void *priv, + struct rpmsg_channel_info chinfo); WARNING: Unusual use of 'struct rtc_time' on stack #26: FILE: include/linux/rtc.h:26: +ktime_t rtc_tm_to_ktime(struct rtc_time tm); WARNING: Unusual use of 'struct timespec64' on stack #193: FILE: include/linux/rtc.h:193: +extern int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec); [] WARNING: Unusual use of 'struct tnum' on stack #25: FILE: include/linux/tnum.h:25: +struct tnum tnum_lshift(struct tnum a, u8 shift); [] WARNING: Unusual use of 'struct vring' on stack #81: FILE: include/linux/virtio_ring.h:81: +struct virtqueue *__vring_new_virtqueue(unsigned int index, + struct vring vring, + struct virtio_device *vdev, + bool weak_barriers, + bool ctx, + bool (*notify)(struct virtqueue *), + void (*callback)(struct virtqueue *), + const char *name); WARNING: Unusual use of 'struct vmci_handle' on stack #39: FILE: include/linux/vmw_vmci_api.h:39: +int vmci_datagram_destroy_handle(struct vmci_handle handle); --- scripts/checkpatch.pl | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 34e4683de7a3..6ec86d8a4983 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -6076,8 +6076,10 @@ sub process { # check for function definitions if ($perl_version_ok && defined $stat && - $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { + $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*([\{;])/s) { $context_function = $1; + my $args = $2; + my $term = $3; # check for multiline function definition with misplaced open brace my $ok = 0; @@ -6088,12 +6090,25 @@ sub process { $herectx .= $rl . "\n"; $ok = 1 if ($rl =~ /^[ \+]\{/); $ok = 1 if ($rl =~ /\{/ && $n == 0); - last if $rl =~ /^[ \+].*\{/; + last if ($rl =~ /^[ \+].*[\{;]/); } - if (!$ok) { + if (!$ok || $term eq '{') { ERROR("OPEN_BRACE", "open brace '{' following function definitions go on the next line\n" . $herectx); } + +# check for struct or union uses on stack that might use struct or union * + + while ($args =~ /(?:$Storage\s+)?($Type)\s*($Ident(?:\s*\[\s*\])?)?\s*,?/g) { + my $type = trim($1); + my $ident = defined($2) ? trim($2) : ""; + if ($type =~ /(?:union|struct)/ && + !($type =~ /(?:\*|\bconst|\])$/ || + $ident =~ /\]$/)) { + WARN("AGGREGATE_ON_STACK", + "Unusual use of '$type' on stack\n" . $herectx); + } + } } # checks for new __setup's