From nobody Thu Dec 18 20:37:42 2025 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (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 8B47F1FD7A5 for ; Wed, 18 Dec 2024 23:27:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734564461; cv=none; b=d3Jn4DkTX0xQCaEt9fmkv5YycCWyUOhcqkYbKj3nz7V6d+sq+tYFlegCvWS9WGvgY1xNwQa5tNR4GDv953qzILH/NC7G/i+VEPAjzuLZDD6Gt/+6mSH8qO2JIw06Da+dvpGyI6lpXneGEptqQFK6e75IhUSgLB3uIRPNitJVbOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734564461; c=relaxed/simple; bh=Otcppq+A3/tAZ+JPa2i20zxcBY/NdsJ28I62WxE0Y5I=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=QG6DUM1sFnHG+TqmWBBpFSuoLGPXqbkkV51i5xUeQaQr/3MtlQWf65RaJu5NjJBrWaxnY4B+knjectMIi6cA20OXlXS/mhAKb72zwQCtcxBqYMV8wVrj2iQLO6pzNQJdQMqMLznvlua1DeTo05nNJzC91P12qMjhyzo1Erj4qX0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=cdrzDiD7; arc=none smtp.client-ip=209.85.215.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="cdrzDiD7" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-7fcf59a41ddso52383a12.3 for ; Wed, 18 Dec 2024 15:27:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1734564459; x=1735169259; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=D6HOTFcebhwSLBcqwGZpkkd/HLxjCkjFN1KCmwBoysY=; b=cdrzDiD7+KH+29EIBL9qLXojRiKJyGzyIEqid/WA0kbxUMP6PVnY1QXkpdQEERjbs1 RzCLze1Y0nwHKFgO0pDTWNKISLGuFFCGoLM0e5qcLKrAGiYaPmGOTQnrlWlLvtkaOaTg f34Pc/yb95d/mm9ypkvqnsev4DaIPxLz2R/m8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734564459; x=1735169259; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D6HOTFcebhwSLBcqwGZpkkd/HLxjCkjFN1KCmwBoysY=; b=n3cSSoxmAbeNm0DSstH9O+KUeGhc6XusGanS5BfgHLBNWeXu6VOh3+a3or6Vbuz0mW 7UdzMS551IYrflc4sXOlm/VEXaQWrHNWklpN4f5FRLR9tL/wHkKMiKxw4Xn4dUA7AFZe S91TfdMQImFZWgupGew8AO1EU0Ea1vd0GdxuEgX6LC2OVQnWZHdfBMQc1LodJg3JUp7u MPZXWQ488qi1Wxy8qqbt4L3oLyNMWa1NmygeBw/x/kyU86HOA1XMBOa7dFwPlkje9lf2 frZX/inuaL/4lmty+oli/wXITAr3FWuwXiJpqtaGgFTE0xmpO7J0MfAAUWbK7NAhTIuj cwRg== X-Forwarded-Encrypted: i=1; AJvYcCWJ12h4y5ubsvnxxkSYKHkOI4L45I3EUdUWiN0Ur3s3AbW66Eb4BgHGsnX+KxkszrAGVj8YECHG0f9KTes=@vger.kernel.org X-Gm-Message-State: AOJu0Yxjpr6sqgYBdqWVcAgd8ztqTWZyiEwaxwPzVdxFJ1FzOpDCOmx+ 49GlGDmO7/yzqDBkepwED91NlVRMS9TFXQsyrRlaPggPOen61dgGCWwPZ9lqyw== X-Gm-Gg: ASbGncsSHrPynwxK1SMwH0ClyAogmw4HmWEFZqXkGwxcCD9oFaqDzc15QTCCTSx+610 KJ2xifS8J1jjr5/vmmW1a9Zc6UkkPko5zoZcWdyLZPdzxQwXXUwOMJJhxBf6HNx093TUpcjckiz in7hRJiyECxatClgFJz5qvV1g+ook194VSp1Bd6p6tdIy3Sc7PKrdEULEYKS+yHEnyPk5anQlZE gV6fG5+nEw3bO4/Nmoj3++FdFQaqTgXqOfDV+DI2Bj70gnrur8PxWDsCPT2OwU0L63aftx4fnmf ZRfPxvGummSrtVD80aM= X-Google-Smtp-Source: AGHT+IHF2+cXEEEKIoVmFdg0PuXzj90IZTU2c5axPAiv9AbIZZA9V8I0mIq6kF7prY9Dtg517vEaUA== X-Received: by 2002:a17:90b:548f:b0:2ee:5edc:4b2 with SMTP id 98e67ed59e1d1-2f2e91fa4c2mr6600853a91.20.1734564458745; Wed, 18 Dec 2024 15:27:38 -0800 (PST) Received: from localhost ([2a00:79e0:2e14:7:661b:b5b4:a49e:18b8]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-2f2ed52d353sm2170017a91.9.2024.12.18.15.27.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Dec 2024 15:27:38 -0800 (PST) From: Brian Norris To: Andy Whitcroft , Joe Perches Cc: Brian Norris , Dwaipayan Ray , Lukas Bulwahn , linux-kernel@vger.kernel.org Subject: [PATCH] checkpatch: Check for missing sentinels in ID arrays Date: Wed, 18 Dec 2024 15:27:00 -0800 Message-ID: <20241218232716.3624531-1-briannorris@chromium.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog 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" All of the ID tables based on (of_device_id, pci_device_id, ...) require their arrays to end in an empty sentinel value. That's usually spelled with an empty initializer entry (e.g., "{}"), but also sometimes with explicit 0 entries, field initializers (e.g., '.id =3D ""'), or even a macro entry (like PCMCIA_DEVICE_NULL). Without a sentinel, device-matching code may read out of bounds. I've found a number of such bugs in driver reviews, and we even occasionally commit one to the tree. See commit 5751eee5c620 ("i2c: nomadik: Add missing sentinel to match table") for example. Teach checkpatch to find these ID tables, and complain if it looks like there wasn't a sentinel value. Test output: $ git format-patch -1 a0d15cc47f29be6d --stdout | scripts/checkpatch.pl - ERROR: missing sentinel in ID array #57: FILE: drivers/i2c/busses/i2c-nomadik.c:1073: +static const struct of_device_id nmk_i2c_eyeq_match_table[] =3D { { .compatible =3D "XXXXXXXXXXXXXXXXXX", .data =3D (void *)(NMK_I2C_EYEQ_FLAG_32B_BUS | NMK_I2C_EYEQ_FLAG_IS_EY= EQ5), }, }; total: 1 errors, 0 warnings, 66 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inpl= ace. "[PATCH] i2c: nomadik: switch from of_device_is_compatible() to" has styl= e problems, please review. NOTE: If any of the errors are false positives, please report them to the maintainer, see CHECKPATCH in MAINTAINERS. When run across the entire tree (scripts/checkpatch.pl -q --types MISSING_SENTINEL -f ...), false positives primarily exist (approx 2 or 3 instances) where macros are used that hide the table from analysis (e.g., drivers/gpu/drm/radeon/radeon_drv.c / radeon_PCI_IDS). Signed-off-by: Brian Norris --- scripts/checkpatch.pl | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 9eed3683ad76..079586fa6cf7 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -666,6 +666,61 @@ our $tracing_logging_tags =3D qr{(?xi: [\.\!:\s]* )}; =20 +# Device ID types from include/linux/mod_devicetable.h. +our $dev_id_types =3D qr{(?x: + acpi | + ap | + apr | + auxiliary | + bcma | + ccw | + cdx | + coreboot | + css | + dfl | + eisa | + fsl_mc | + hda | + hid | + hv_vmbus | + input | + ipack | + isapnp | + ishtp | + mcb | + mdio | + mei_cl | + mhi | + mips_cdmm | + of | + parisc | + pci | + pci_epf | + pcmcia | + platform | + pnp | + pnp_card | + rio | + rpmsg | + sdio | + sdw | + serio | + slim | + spi | + spmi | + ssam | + ssb | + tee_client | + typec | + ulpi | + usb | + vchiq | + vio | + virtio | + wmi | + zorro +)_device_id}; + sub edit_distance_min { my (@arr) =3D @_; my $len =3D scalar @arr; @@ -7676,6 +7731,26 @@ sub process { WARN("DUPLICATED_SYSCTL_CONST", "duplicated sysctl range checking value '$1', consider using the share= d one in include/linux/sysctl.h\n" . $herecurr); } + +# Check that *_device_id tables have sentinel entries. + if (defined $stat && $line =3D~ /struct $dev_id_types .*\[\] =3D \{/) { + my $stripped =3D $stat; + + # Strip diff line prefixes. + $stripped =3D~ s/(^|\n)./$1/g; + # Line continuations. + $stripped =3D~ s/\\\n/\n/g; + # Strip whitespace, empty strings, zeroes, and commas. + $stripped =3D~ s/""//g; + $stripped =3D~ s/0x0//g; + $stripped =3D~ s/[\s$;,0]//g; + # Strip field assignments. + $stripped =3D~ s/\.$Ident=3D//g; + + if (!(substr($stripped, -4) eq "{}};" or $stripped =3D~ /PCMCIA_DEVICE_= NULL};$/)) { + ERROR("MISSING_SENTINEL", "missing sentinel in ID array\n" . "$here\n$= stat\n"); + } + } } =20 # If we have no input at all, then there is nothing to report on --=20 2.47.1.613.gc27f4b7a9f-goog