Skip to contentSkip to conversation

checkpatch: ignore warning designated initializers using NR_CPUS

PATCH Superseded Peng Wang 2 participants 7 messages Dec 30, 2020 Checkpatch

lore
Share & tools
lore

Peng Wang

author
#0 lore raw

PATCH 1/1 checkpatch: ignore warning designated initializers using NR_CPUS

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. As an example below:

kernel/cgroup/cpuset.c:

static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show,
                .write = cpuset_write_resmask,
                .max_write_len = (100U + 6 * NR_CPUS),
                .private = FILE_CPULIST,
        },
	...
}
---
 scripts/checkpatch.pl | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +4 -1
index 0008530..da8fc48 100755
@@ -7022,12 +7022,15 @@ sub process {
 
 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
+# ignore designated initializers using NR_CPUS
+
 		if ($line =~ /\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
 		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /\s*\..*=.*\bNR_CPUS\b.*,$/)
 		{
 			WARN("NR_CPUS",
 			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Signature · 1 line
2.9.5
maintainer
#1 lore raw
Quoted 13 lines · Peng Wang

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. As an example below:

kernel/cgroup/cpuset.c:
static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show, .write = cpuset_write_resmask, .max_write_len = (100U + 6 * NR_CPUS), .private = FILE_CPULIST,
        },

Quoted 2 lines
	...
}

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 15 lines
scripts/checkpatch.pl +0 -0
@@ -7022,12 +7022,15 @@ sub process {

 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
scripts/checkpatch.pl +2 -0
+# ignore designated initializers using NR_CPUS
+

  if ($line =~ /\bNR_CPUS\b/ &&
  $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&

scripts/checkpatch.pl +2 -1
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /\s*\..*=.*\bNR_CPUS\b.*,$/)
Quoted 3 lines

  {
  WARN("NR_CPUS",
  "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);

I wonder if it's worth it as there just aren't many of these: (6 total)

$ git grep -P '\..*=.*\bNR_CPUS\b.*,\s*$'
drivers/firmware/efi/efi.c:     .cpu_bitmap             = { [BITS_TO_LONGS(NR__CPUS)] = 0},
drivers/infiniband/hw/hfi1/sdma.c:      .max_size = NR_CPUS,
init/init_task.c:       .nr_cpus_allowed= NR_CPUS,
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/sysctl.c:                .maxlen         = NR_CPUS,

It also seems it might be more sensible as:

	$line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/

Which finds a couple more:

$ git grep -P '^\s*\.\w+\s*=\s*.*\bNR_CPUS\b'
arch/arm64/include/asm/processor.h:     .fpsimd_cpu = NR_CPUS,                  \
arch/powerpc/kernel/prom_init.c:                .max_cpus = cpu_to_be32(NR_CPUS),       /* number of cores supported */
drivers/firmware/efi/efi.c:     .cpu_bitmap             = { [BITS_TO_LONGS(NR_CPUS)] = 0},
drivers/infiniband/hw/hfi1/sdma.c:      .max_size = NR_CPUS,
init/init_task.c:       .nr_cpus_allowed= NR_CPUS,
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/sysctl.c:                .maxlen         = NR_CPUS,
author
#2 lore raw

Hi, Joe

Quoted 31 lines · Joe Perches
Quoted 13 lines · Peng Wang

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. As an example below:

kernel/cgroup/cpuset.c:
static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show, .write = cpuset_write_resmask, .max_write_len = (100U + 6 * NR_CPUS), .private = FILE_CPULIST,
        },

Quoted 2 lines
	...
}

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 12 lines
scripts/checkpatch.pl +2 -0
@@ -7022,12 +7022,15 @@ sub process {
 
 
  # use of NR_CPUS is usually wrong
  # ignore definitions of NR_CPUS and usage to define arrays as likely right
+# ignore designated initializers using NR_CPUS
+
  		if ($line =~ /\bNR_CPUS\b/ &&
  		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
  		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
  		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
  		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
Quoted 8 lines
Quoted 6 lines
scripts/checkpatch.pl +2 -1
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /\s*\..*=.*\bNR_CPUS\b.*,$/)
  		{
  			WARN("NR_CPUS",

  "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);

I wonder if it's worth it as there just aren't many of these: (6 total)

It does not look that much, but checkpatch.pl makes warning on this especially when "0 errors, 0 warnings" is needed.

Quoted 9 lines
$ git grep -P '\..*=.*\bNR_CPUS\b.*,\s*$'
drivers/firmware/efi/efi.c:     .cpu_bitmap             = { [BITS_TO_LONGS(NR__CPUS)] = 0},
drivers/infiniband/hw/hfi1/sdma.c:      .max_size = NR_CPUS,
init/init_task.c:       .nr_cpus_allowed= NR_CPUS,
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/sysctl.c:                .maxlen         = NR_CPUS,
Quoted 3 lines

It also seems it might be more sensible as:

	$line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/

Aha, this is better!
Will send a v2 patch including this if you don't mind, Thanks.

Quoted 13 lines

Which finds a couple more:

$ git grep -P '^\s*\.\w+\s*=\s*.*\bNR_CPUS\b'
arch/arm64/include/asm/processor.h:     .fpsimd_cpu = NR_CPUS,                  \
arch/powerpc/kernel/prom_init.c:                .max_cpus = cpu_to_be32(NR_CPUS),       /* number of cores supported */
drivers/firmware/efi/efi.c:     .cpu_bitmap             = { [BITS_TO_LONGS(NR_CPUS)] = 0},
drivers/infiniband/hw/hfi1/sdma.c:      .max_size = NR_CPUS,
init/init_task.c:       .nr_cpus_allowed= NR_CPUS,
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/cgroup/cpuset.c:         .max_write_len = (100U + 6 * NR_CPUS),
kernel/sysctl.c:                .maxlen         = NR_CPUS,
author
#3 lore raw

PATCH v2 1/1 checkpatch: ignore warning designated initializers using NR_CPUS

In reply to Joe Perches

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. An example below:

kernel/cgroup/cpuset.c:

static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show,
                .write = cpuset_write_resmask,
                .max_write_len = (100U + 6 * NR_CPUS),
                .private = FILE_CPULIST,
        },
	...
}
---
 scripts/checkpatch.pl | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +4 -1
index 0008530..fdbbab3 100755
@@ -7022,12 +7022,15 @@ sub process {
 
 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
+# ignore designated initializers using NR_CPUS
+
 		if ($line =~ /\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
 		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
 		{
 			WARN("NR_CPUS",
 			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Signature · 1 line
2.9.5
maintainer
#4 lore raw
Quoted 13 lines · Peng Wang

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. An example below:

kernel/cgroup/cpuset.c:
static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show, .write = cpuset_write_resmask, .max_write_len = (100U + 6 * NR_CPUS), .private = FILE_CPULIST,
        },

Quoted 2 lines
	...
}

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 7026

Quoted 7 lines
scripts/checkpatch.pl +0 -0
@@ -7022,12 +7022,15 @@ sub process {

 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
scripts/checkpatch.pl +2 -0
+# ignore designated initializers using NR_CPUS
+

Please remove this empty line, other than that, looks OK to me.

Andrew? Care to take this and remove the empty line or wait for v3?

Quoted 11 lines

  if ($line =~ /\bNR_CPUS\b/ &&
  $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&

scripts/checkpatch.pl +2 -1
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)

  {
  WARN("NR_CPUS",
  "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);

author
#5 lore raw

PATCH v3 1/1 checkpatch: ignore warning designated initializers using NR_CPUS

In reply to Joe Perches

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. An example below:

kernel/cgroup/cpuset.c:

static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show,
                .write = cpuset_write_resmask,
                .max_write_len = (100U + 6 * NR_CPUS),
                .private = FILE_CPULIST,
        },
	...
}
---
 scripts/checkpatch.pl | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +3 -1
index 92e888e..6f112fb 100755
@@ -7028,12 +7028,14 @@ sub process {
 
 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
+# ignore designated initializers using NR_CPUS
 		if ($line =~ /\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
 		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
 		{
 			WARN("NR_CPUS",
 			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Signature · 1 line
2.9.5
maintainer Acked-by on v3 1/1
#6 lore raw
Quoted 13 lines · Peng Wang

Some max_length wants to hold as large room as possible to ensure enough size to tackle with the biggest NR_CPUS. An example below:

kernel/cgroup/cpuset.c:
static struct cftype legacy_files[] = {
        {
                .name = "cpus",
                .seq_show = cpuset_common_seq_show, .write = cpuset_write_resmask, .max_write_len = (100U + 6 * NR_CPUS), .private = FILE_CPULIST,
        },

Quoted 5 lines
	...
}

---

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 13 lines
scripts/checkpatch.pl +0 -0
@@ -7028,12 +7028,14 @@ sub process {

 # use of NR_CPUS is usually wrong
 # ignore definitions of NR_CPUS and usage to define arrays as likely right
+# ignore designated initializers using NR_CPUS
 		if ($line =~ /\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
 		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
 		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
scripts/checkpatch.pl +2 -1
-		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
+		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
+		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
Quoted 3 lines

  {
  WARN("NR_CPUS",
  "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);

Thanks.