Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

checkpatch: check for missing sentinels in ID arrays

All of the ID tables based on <linux/mod_devicetable.h> (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 = ""'), 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[] = {
{
.compatible = "XXXXXXXXXXXXXXXXXX",
.data = (void *)(NMK_I2C_EYEQ_FLAG_32B_BUS | NMK_I2C_EYEQ_FLAG_IS_EYEQ5),
},
};

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-inplace.

"[PATCH] i2c: nomadik: switch from of_device_is_compatible() to" has style 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 exist:

* where macros are used that hide the table from analysis
(e.g., drivers/gpu/drm/radeon/radeon_drv.c / radeon_PCI_IDS).
There are fewer than 5 of these.

* where such tables are processed correctly via ARRAY_SIZE() (fewer than
5 instances). This is by far not the typical usage of *_device_id
arrays.

* some odd parsing artifacts, where ctx_statement_block() seems to quit
in the middle of a block due to #if/#else/#endif.

Also, not every "struct *_device_id" is in fact a sentinel-requiring
structure, but even with such types, false positives are very rare.

Link: https://lkml.kernel.org/r/20250702235245.1007351-1-briannorris@chromium.org
Signed-off-by: Brian Norris <briannorris@chromium.org>
Acked-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Brian Norris <briannorris@chromium.org>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Brian Norris and committed by
Andrew Morton
22c2ed69 1f04e0e6

+28
+28
scripts/checkpatch.pl
··· 685 685 [\.\!:\s]* 686 686 )}; 687 687 688 + # Device ID types like found in include/linux/mod_devicetable.h. 689 + our $dev_id_types = qr{\b[a-z]\w*_device_id\b}; 690 + 688 691 sub edit_distance_min { 689 692 my (@arr) = @_; 690 693 my $len = scalar @arr; ··· 7681 7678 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { 7682 7679 WARN("DUPLICATED_SYSCTL_CONST", 7683 7680 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); 7681 + } 7682 + 7683 + # Check that *_device_id tables have sentinel entries. 7684 + if (defined $stat && $line =~ /struct\s+$dev_id_types\s+\w+\s*\[\s*\]\s*=\s*\{/) { 7685 + my $stripped = $stat; 7686 + 7687 + # Strip diff line prefixes. 7688 + $stripped =~ s/(^|\n)./$1/g; 7689 + # Line continuations. 7690 + $stripped =~ s/\\\n/\n/g; 7691 + # Strip whitespace, empty strings, zeroes, and commas. 7692 + $stripped =~ s/""//g; 7693 + $stripped =~ s/0x0//g; 7694 + $stripped =~ s/[\s$;,0]//g; 7695 + # Strip field assignments. 7696 + $stripped =~ s/\.$Ident=//g; 7697 + 7698 + if (!(substr($stripped, -4) eq "{}};" || 7699 + substr($stripped, -6) eq "{{}}};" || 7700 + $stripped =~ /ISAPNP_DEVICE_SINGLE_END}};$/ || 7701 + $stripped =~ /ISAPNP_CARD_END}};$/ || 7702 + $stripped =~ /NULL};$/ || 7703 + $stripped =~ /PCMCIA_DEVICE_NULL};$/)) { 7704 + ERROR("MISSING_SENTINEL", "missing sentinel in ID array\n" . "$here\n$stat\n"); 7705 + } 7684 7706 } 7685 7707 } 7686 7708