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

ipv4: snmp: do not use SNMP_MIB_SENTINEL anymore

Use ARRAY_SIZE(), so that we know the limit at compile time.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>
Link: https://patch.msgid.link/20250905165813.1470708-5-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Dumazet and committed by
Jakub Kicinski
b7b74953 2fab94bc

+33 -32
+33 -32
net/ipv4/proc.c
··· 95 95 SNMP_MIB_ITEM("FragFails", IPSTATS_MIB_FRAGFAILS), 96 96 SNMP_MIB_ITEM("FragCreates", IPSTATS_MIB_FRAGCREATES), 97 97 SNMP_MIB_ITEM("OutTransmits", IPSTATS_MIB_OUTPKTS), 98 - SNMP_MIB_SENTINEL 99 98 }; 100 99 101 100 /* Following items are displayed in /proc/net/netstat */ ··· 118 119 SNMP_MIB_ITEM("InECT0Pkts", IPSTATS_MIB_ECT0PKTS), 119 120 SNMP_MIB_ITEM("InCEPkts", IPSTATS_MIB_CEPKTS), 120 121 SNMP_MIB_ITEM("ReasmOverlaps", IPSTATS_MIB_REASM_OVERLAPS), 121 - SNMP_MIB_SENTINEL 122 122 }; 123 123 124 124 static const struct { ··· 155 157 SNMP_MIB_ITEM("InErrs", TCP_MIB_INERRS), 156 158 SNMP_MIB_ITEM("OutRsts", TCP_MIB_OUTRSTS), 157 159 SNMP_MIB_ITEM("InCsumErrors", TCP_MIB_CSUMERRORS), 158 - SNMP_MIB_SENTINEL 159 160 }; 160 161 161 162 static const struct snmp_mib snmp4_udp_list[] = { ··· 167 170 SNMP_MIB_ITEM("InCsumErrors", UDP_MIB_CSUMERRORS), 168 171 SNMP_MIB_ITEM("IgnoredMulti", UDP_MIB_IGNOREDMULTI), 169 172 SNMP_MIB_ITEM("MemErrors", UDP_MIB_MEMERRORS), 170 - SNMP_MIB_SENTINEL 171 173 }; 172 174 173 175 static const struct snmp_mib snmp4_net_list[] = { ··· 305 309 SNMP_MIB_ITEM("TCPAOKeyNotFound", LINUX_MIB_TCPAOKEYNOTFOUND), 306 310 SNMP_MIB_ITEM("TCPAOGood", LINUX_MIB_TCPAOGOOD), 307 311 SNMP_MIB_ITEM("TCPAODroppedIcmps", LINUX_MIB_TCPAODROPPEDICMPS), 308 - SNMP_MIB_SENTINEL 309 312 }; 310 313 311 314 static void icmpmsg_put_line(struct seq_file *seq, unsigned long *vals, ··· 384 389 */ 385 390 static int snmp_seq_show_ipstats(struct seq_file *seq, void *v) 386 391 { 392 + const int cnt = ARRAY_SIZE(snmp4_ipstats_list); 393 + u64 buff64[ARRAY_SIZE(snmp4_ipstats_list)]; 387 394 struct net *net = seq->private; 388 - u64 buff64[IPSTATS_MIB_MAX]; 389 395 int i; 390 396 391 - memset(buff64, 0, IPSTATS_MIB_MAX * sizeof(u64)); 397 + memset(buff64, 0, sizeof(buff64)); 392 398 393 399 seq_puts(seq, "Ip: Forwarding DefaultTTL"); 394 - for (i = 0; snmp4_ipstats_list[i].name; i++) 400 + for (i = 0; i < cnt; i++) 395 401 seq_printf(seq, " %s", snmp4_ipstats_list[i].name); 396 402 397 403 seq_printf(seq, "\nIp: %d %d", ··· 400 404 READ_ONCE(net->ipv4.sysctl_ip_default_ttl)); 401 405 402 406 BUILD_BUG_ON(offsetof(struct ipstats_mib, mibs) != 0); 403 - snmp_get_cpu_field64_batch(buff64, snmp4_ipstats_list, 404 - net->mib.ip_statistics, 405 - offsetof(struct ipstats_mib, syncp)); 406 - for (i = 0; snmp4_ipstats_list[i].name; i++) 407 + snmp_get_cpu_field64_batch_cnt(buff64, snmp4_ipstats_list, cnt, 408 + net->mib.ip_statistics, 409 + offsetof(struct ipstats_mib, syncp)); 410 + for (i = 0; i < cnt; i++) 407 411 seq_printf(seq, " %llu", buff64[i]); 408 412 409 413 return 0; ··· 411 415 412 416 static int snmp_seq_show_tcp_udp(struct seq_file *seq, void *v) 413 417 { 418 + const int udp_cnt = ARRAY_SIZE(snmp4_udp_list); 419 + const int tcp_cnt = ARRAY_SIZE(snmp4_tcp_list); 414 420 unsigned long buff[TCPUDP_MIB_MAX]; 415 421 struct net *net = seq->private; 416 422 int i; 417 423 418 - memset(buff, 0, TCPUDP_MIB_MAX * sizeof(unsigned long)); 424 + memset(buff, 0, tcp_cnt * sizeof(unsigned long)); 419 425 420 426 seq_puts(seq, "\nTcp:"); 421 - for (i = 0; snmp4_tcp_list[i].name; i++) 427 + for (i = 0; i < tcp_cnt; i++) 422 428 seq_printf(seq, " %s", snmp4_tcp_list[i].name); 423 429 424 430 seq_puts(seq, "\nTcp:"); 425 - snmp_get_cpu_field_batch(buff, snmp4_tcp_list, 426 - net->mib.tcp_statistics); 427 - for (i = 0; snmp4_tcp_list[i].name; i++) { 431 + snmp_get_cpu_field_batch_cnt(buff, snmp4_tcp_list, 432 + tcp_cnt, 433 + net->mib.tcp_statistics); 434 + for (i = 0; i < tcp_cnt; i++) { 428 435 /* MaxConn field is signed, RFC 2012 */ 429 436 if (snmp4_tcp_list[i].entry == TCP_MIB_MAXCONN) 430 437 seq_printf(seq, " %ld", buff[i]); ··· 435 436 seq_printf(seq, " %lu", buff[i]); 436 437 } 437 438 438 - memset(buff, 0, TCPUDP_MIB_MAX * sizeof(unsigned long)); 439 + memset(buff, 0, udp_cnt * sizeof(unsigned long)); 439 440 440 - snmp_get_cpu_field_batch(buff, snmp4_udp_list, 441 - net->mib.udp_statistics); 441 + snmp_get_cpu_field_batch_cnt(buff, snmp4_udp_list, 442 + udp_cnt, 443 + net->mib.udp_statistics); 442 444 seq_puts(seq, "\nUdp:"); 443 - for (i = 0; snmp4_udp_list[i].name; i++) 445 + for (i = 0; i < udp_cnt; i++) 444 446 seq_printf(seq, " %s", snmp4_udp_list[i].name); 445 447 seq_puts(seq, "\nUdp:"); 446 - for (i = 0; snmp4_udp_list[i].name; i++) 448 + for (i = 0; i < udp_cnt; i++) 447 449 seq_printf(seq, " %lu", buff[i]); 448 450 449 - memset(buff, 0, TCPUDP_MIB_MAX * sizeof(unsigned long)); 451 + memset(buff, 0, udp_cnt * sizeof(unsigned long)); 450 452 451 453 /* the UDP and UDP-Lite MIBs are the same */ 452 454 seq_puts(seq, "\nUdpLite:"); 453 - snmp_get_cpu_field_batch(buff, snmp4_udp_list, 454 - net->mib.udplite_statistics); 455 - for (i = 0; snmp4_udp_list[i].name; i++) 455 + snmp_get_cpu_field_batch_cnt(buff, snmp4_udp_list, 456 + udp_cnt, 457 + net->mib.udplite_statistics); 458 + for (i = 0; i < udp_cnt; i++) 456 459 seq_printf(seq, " %s", snmp4_udp_list[i].name); 457 460 seq_puts(seq, "\nUdpLite:"); 458 - for (i = 0; snmp4_udp_list[i].name; i++) 461 + for (i = 0; i < udp_cnt; i++) 459 462 seq_printf(seq, " %lu", buff[i]); 460 463 461 464 seq_putc(seq, '\n'); ··· 481 480 */ 482 481 static int netstat_seq_show(struct seq_file *seq, void *v) 483 482 { 484 - const int ip_cnt = ARRAY_SIZE(snmp4_ipextstats_list) - 1; 485 - const int tcp_cnt = ARRAY_SIZE(snmp4_net_list) - 1; 483 + const int ip_cnt = ARRAY_SIZE(snmp4_ipextstats_list); 484 + const int tcp_cnt = ARRAY_SIZE(snmp4_net_list); 486 485 struct net *net = seq->private; 487 486 unsigned long *buff; 488 487 int i; ··· 495 494 buff = kzalloc(max(tcp_cnt * sizeof(long), ip_cnt * sizeof(u64)), 496 495 GFP_KERNEL); 497 496 if (buff) { 498 - snmp_get_cpu_field_batch(buff, snmp4_net_list, 499 - net->mib.net_statistics); 497 + snmp_get_cpu_field_batch_cnt(buff, snmp4_net_list, tcp_cnt, 498 + net->mib.net_statistics); 500 499 for (i = 0; i < tcp_cnt; i++) 501 500 seq_printf(seq, " %lu", buff[i]); 502 501 } else { ··· 514 513 u64 *buff64 = (u64 *)buff; 515 514 516 515 memset(buff64, 0, ip_cnt * sizeof(u64)); 517 - snmp_get_cpu_field64_batch(buff64, snmp4_ipextstats_list, 516 + snmp_get_cpu_field64_batch_cnt(buff64, snmp4_ipextstats_list, ip_cnt, 518 517 net->mib.ip_statistics, 519 518 offsetof(struct ipstats_mib, syncp)); 520 519 for (i = 0; i < ip_cnt; i++)