diff mbox series

[net-next] selftests: net: cleanup busy_poller.c

Message ID 20241203012838.182522-1-jdamato@fastly.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] selftests: net: cleanup busy_poller.c | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3 this patch: 3
netdev/build_tools success Errors and warnings before: 0 (+0) this patch: 0 (+0)
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 304 this patch: 304
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 156 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-12-03--06-00 (tests: 760)

Commit Message

Joe Damato Dec. 3, 2024, 1:28 a.m. UTC
Fix various integer type conversions by using strtoull and a temporary
variable which is bounds checked before being casted into the
appropriate cfg_* variable for use by the test program.

While here, free the strdup'd cfg string for overall hygenie.

Signed-off-by: Joe Damato <jdamato@fastly.com>
---
 tools/testing/selftests/net/busy_poller.c | 86 +++++++++++++----------
 1 file changed, 49 insertions(+), 37 deletions(-)


base-commit: 65ae975e97d5aab3ee9dc5ec701b12090572ed43

Comments

Stanislav Fomichev Dec. 3, 2024, 5:14 a.m. UTC | #1
On 12/03, Joe Damato wrote:
> Fix various integer type conversions by using strtoull and a temporary
> variable which is bounds checked before being casted into the
> appropriate cfg_* variable for use by the test program.
> 
> While here, free the strdup'd cfg string for overall hygenie.

Thank you for fixing this! I also saw them this morning after a net-next
pull and was about to post... I also see the following (LLVM=1):

busy_poller.c:237:6: warning: variable 'napi_id' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
  237 |         if (napi_list->obj._present.id)
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
busy_poller.c:243:38: note: uninitialized use occurs here
  243 |         netdev_napi_set_req_set_id(set_req, napi_id);
      |                                             ^~~~~~~
busy_poller.c:237:2: note: remove the 'if' if its condition is always true
  237 |         if (napi_list->obj._present.id)
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  238 |                 napi_id = napi_list->obj.id;
      |                                            ~
  239 |         else
      |         ~~~~
  240 |                 error(1, 0, "napi ID not present?");
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
busy_poller.c:226:18: note: initialize the variable 'napi_id' to silence this warning
  226 |         uint32_t napi_id;
      |                         ^
      |                          = 0
1 warning generated.

Presumably the compiler can't connect that fact that (!preset.id) ->
error. So maybe initialize napi_id to 0 to suppress it as well?
Joe Damato Dec. 3, 2024, 4:26 p.m. UTC | #2
On Mon, Dec 02, 2024 at 09:14:58PM -0800, Stanislav Fomichev wrote:
> On 12/03, Joe Damato wrote:
> > Fix various integer type conversions by using strtoull and a temporary
> > variable which is bounds checked before being casted into the
> > appropriate cfg_* variable for use by the test program.
> > 
> > While here, free the strdup'd cfg string for overall hygenie.
> 
> Thank you for fixing this! I also saw them this morning after a net-next
> pull and was about to post... I also see the following (LLVM=1):
> 
> busy_poller.c:237:6: warning: variable 'napi_id' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
>   237 |         if (napi_list->obj._present.id)
>       |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
> busy_poller.c:243:38: note: uninitialized use occurs here
>   243 |         netdev_napi_set_req_set_id(set_req, napi_id);
>       |                                             ^~~~~~~
> busy_poller.c:237:2: note: remove the 'if' if its condition is always true
>   237 |         if (napi_list->obj._present.id)
>       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   238 |                 napi_id = napi_list->obj.id;
>       |                                            ~
>   239 |         else
>       |         ~~~~
>   240 |                 error(1, 0, "napi ID not present?");
>       |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> busy_poller.c:226:18: note: initialize the variable 'napi_id' to silence this warning
>   226 |         uint32_t napi_id;
>       |                         ^
>       |                          = 0
> 1 warning generated.
> 
> Presumably the compiler can't connect that fact that (!preset.id) ->
> error. So maybe initialize napi_id to 0 to suppress it as well?

Thanks for the report! Can I ask what compiler and version you are
using so that I can test before reposting?
Joe Damato Dec. 3, 2024, 4:35 p.m. UTC | #3
On Tue, Dec 03, 2024 at 08:26:11AM -0800, Joe Damato wrote:
> On Mon, Dec 02, 2024 at 09:14:58PM -0800, Stanislav Fomichev wrote:
> > On 12/03, Joe Damato wrote:
> > > Fix various integer type conversions by using strtoull and a temporary
> > > variable which is bounds checked before being casted into the
> > > appropriate cfg_* variable for use by the test program.
> > > 
> > > While here, free the strdup'd cfg string for overall hygenie.
> > 
> > Thank you for fixing this! I also saw them this morning after a net-next
> > pull and was about to post... I also see the following (LLVM=1):
> > 
> > busy_poller.c:237:6: warning: variable 'napi_id' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
> >   237 |         if (napi_list->obj._present.id)
> >       |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
> > busy_poller.c:243:38: note: uninitialized use occurs here
> >   243 |         netdev_napi_set_req_set_id(set_req, napi_id);
> >       |                                             ^~~~~~~
> > busy_poller.c:237:2: note: remove the 'if' if its condition is always true
> >   237 |         if (napi_list->obj._present.id)
> >       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >   238 |                 napi_id = napi_list->obj.id;
> >       |                                            ~
> >   239 |         else
> >       |         ~~~~
> >   240 |                 error(1, 0, "napi ID not present?");
> >       |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > busy_poller.c:226:18: note: initialize the variable 'napi_id' to silence this warning
> >   226 |         uint32_t napi_id;
> >       |                         ^
> >       |                          = 0
> > 1 warning generated.
> > 
> > Presumably the compiler can't connect that fact that (!preset.id) ->
> > error. So maybe initialize napi_id to 0 to suppress it as well?
> 
> Thanks for the report! Can I ask what compiler and version you are
> using so that I can test before reposting?

Err, sorry. Haven't had coffee yet. I see you mentioned LLVM=1
above. When I use that I also get the same error.

FWIW: I'm using clang version 10.0.0-4ubuntu1 (which as far as I
can tell is pretty old). I'll see if I can get a newer version just
to make sure no other warnings appear.
Simon Horman Dec. 5, 2024, 9:51 a.m. UTC | #4
On Tue, Dec 03, 2024 at 08:35:53AM -0800, Joe Damato wrote:
> On Tue, Dec 03, 2024 at 08:26:11AM -0800, Joe Damato wrote:
> > On Mon, Dec 02, 2024 at 09:14:58PM -0800, Stanislav Fomichev wrote:
> > > On 12/03, Joe Damato wrote:
> > > > Fix various integer type conversions by using strtoull and a temporary
> > > > variable which is bounds checked before being casted into the
> > > > appropriate cfg_* variable for use by the test program.
> > > > 
> > > > While here, free the strdup'd cfg string for overall hygenie.
> > > 
> > > Thank you for fixing this! I also saw them this morning after a net-next
> > > pull and was about to post... I also see the following (LLVM=1):
> > > 
> > > busy_poller.c:237:6: warning: variable 'napi_id' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
> > >   237 |         if (napi_list->obj._present.id)
> > >       |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
> > > busy_poller.c:243:38: note: uninitialized use occurs here
> > >   243 |         netdev_napi_set_req_set_id(set_req, napi_id);
> > >       |                                             ^~~~~~~
> > > busy_poller.c:237:2: note: remove the 'if' if its condition is always true
> > >   237 |         if (napi_list->obj._present.id)
> > >       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > >   238 |                 napi_id = napi_list->obj.id;
> > >       |                                            ~
> > >   239 |         else
> > >       |         ~~~~
> > >   240 |                 error(1, 0, "napi ID not present?");
> > >       |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > busy_poller.c:226:18: note: initialize the variable 'napi_id' to silence this warning
> > >   226 |         uint32_t napi_id;
> > >       |                         ^
> > >       |                          = 0
> > > 1 warning generated.
> > > 
> > > Presumably the compiler can't connect that fact that (!preset.id) ->
> > > error. So maybe initialize napi_id to 0 to suppress it as well?
> > 
> > Thanks for the report! Can I ask what compiler and version you are
> > using so that I can test before reposting?
> 
> Err, sorry. Haven't had coffee yet. I see you mentioned LLVM=1
> above. When I use that I also get the same error.
> 
> FWIW: I'm using clang version 10.0.0-4ubuntu1 (which as far as I
> can tell is pretty old). I'll see if I can get a newer version just
> to make sure no other warnings appear.

Hi Joe,

If you are still looking for recent LLVM toolchains, I suggest taking
a look at https://mirrors.edge.kernel.org/pub/tools/llvm/
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/busy_poller.c b/tools/testing/selftests/net/busy_poller.c
index 99b0e8c17fca..ef62d7145598 100644
--- a/tools/testing/selftests/net/busy_poller.c
+++ b/tools/testing/selftests/net/busy_poller.c
@@ -54,16 +54,16 @@  struct epoll_params {
 #define EPIOCGPARAMS _IOR(EPOLL_IOC_TYPE, 0x02, struct epoll_params)
 #endif
 
-static uint32_t cfg_port = 8000;
+static uint16_t cfg_port = 8000;
 static struct in_addr cfg_bind_addr = { .s_addr = INADDR_ANY };
 static char *cfg_outfile;
 static int cfg_max_events = 8;
-static int cfg_ifindex;
+static uint32_t cfg_ifindex;
 
 /* busy poll params */
 static uint32_t cfg_busy_poll_usecs;
-static uint32_t cfg_busy_poll_budget;
-static uint32_t cfg_prefer_busy_poll;
+static uint16_t cfg_busy_poll_budget;
+static uint8_t cfg_prefer_busy_poll;
 
 /* IRQ params */
 static uint32_t cfg_defer_hard_irqs;
@@ -79,6 +79,7 @@  static void usage(const char *filepath)
 
 static void parse_opts(int argc, char **argv)
 {
+	unsigned long long tmp;
 	int ret;
 	int c;
 
@@ -86,31 +87,40 @@  static void parse_opts(int argc, char **argv)
 		usage(argv[0]);
 
 	while ((c = getopt(argc, argv, "p:m:b:u:P:g:o:d:r:s:i:")) != -1) {
+		/* most options take integer values, except o and b, so reduce
+		 * code duplication a bit for the common case by calling
+		 * strtoull here and leave bounds checking and casting per
+		 * option below.
+		 */
+		if (c != 'o' && c != 'b')
+			tmp = strtoull(optarg, NULL, 0);
+
 		switch (c) {
 		case 'u':
-			cfg_busy_poll_usecs = strtoul(optarg, NULL, 0);
-			if (cfg_busy_poll_usecs == ULONG_MAX ||
-			    cfg_busy_poll_usecs > UINT32_MAX)
+			if (tmp == ULLONG_MAX || tmp > UINT32_MAX)
 				error(1, ERANGE, "busy_poll_usecs too large");
+
+			cfg_busy_poll_usecs = (uint32_t)tmp;
 			break;
 		case 'P':
-			cfg_prefer_busy_poll = strtoul(optarg, NULL, 0);
-			if (cfg_prefer_busy_poll == ULONG_MAX ||
-			    cfg_prefer_busy_poll > 1)
+			if (tmp == ULLONG_MAX || tmp > 1)
 				error(1, ERANGE,
 				      "prefer busy poll should be 0 or 1");
+
+			cfg_prefer_busy_poll = (uint8_t)tmp;
 			break;
 		case 'g':
-			cfg_busy_poll_budget = strtoul(optarg, NULL, 0);
-			if (cfg_busy_poll_budget == ULONG_MAX ||
-			    cfg_busy_poll_budget > UINT16_MAX)
+			if (tmp == ULLONG_MAX || tmp > UINT16_MAX)
 				error(1, ERANGE,
 				      "busy poll budget must be [0, UINT16_MAX]");
+
+			cfg_busy_poll_budget = (uint16_t)tmp;
 			break;
 		case 'p':
-			cfg_port = strtoul(optarg, NULL, 0);
-			if (cfg_port > UINT16_MAX)
+			if (tmp == ULLONG_MAX || tmp > UINT16_MAX)
 				error(1, ERANGE, "port must be <= 65535");
+
+			cfg_port = (uint16_t)tmp;
 			break;
 		case 'b':
 			ret = inet_aton(optarg, &cfg_bind_addr);
@@ -124,41 +134,39 @@  static void parse_opts(int argc, char **argv)
 				error(1, 0, "outfile invalid");
 			break;
 		case 'm':
-			cfg_max_events = strtol(optarg, NULL, 0);
-
-			if (cfg_max_events == LONG_MIN ||
-			    cfg_max_events == LONG_MAX ||
-			    cfg_max_events <= 0)
+			if (tmp == ULLONG_MAX || tmp > INT_MAX)
 				error(1, ERANGE,
-				      "max events must be > 0 and < LONG_MAX");
+				      "max events must be > 0 and <= INT_MAX");
+
+			cfg_max_events = (int)tmp;
 			break;
 		case 'd':
-			cfg_defer_hard_irqs = strtoul(optarg, NULL, 0);
-
-			if (cfg_defer_hard_irqs == ULONG_MAX ||
-			    cfg_defer_hard_irqs > INT32_MAX)
+			if (tmp == ULLONG_MAX || tmp > INT32_MAX)
 				error(1, ERANGE,
 				      "defer_hard_irqs must be <= INT32_MAX");
+
+			cfg_defer_hard_irqs = (uint32_t)tmp;
 			break;
 		case 'r':
-			cfg_gro_flush_timeout = strtoull(optarg, NULL, 0);
-
-			if (cfg_gro_flush_timeout == ULLONG_MAX)
+			if (tmp == ULLONG_MAX || tmp > UINT64_MAX)
 				error(1, ERANGE,
-				      "gro_flush_timeout must be < ULLONG_MAX");
+				      "gro_flush_timeout must be < UINT64_MAX");
+
+			cfg_gro_flush_timeout = (uint64_t)tmp;
 			break;
 		case 's':
-			cfg_irq_suspend_timeout = strtoull(optarg, NULL, 0);
-
-			if (cfg_irq_suspend_timeout == ULLONG_MAX)
+			if (tmp == ULLONG_MAX || tmp > UINT64_MAX)
 				error(1, ERANGE,
 				      "irq_suspend_timeout must be < ULLONG_MAX");
+
+			cfg_irq_suspend_timeout = (uint64_t)tmp;
 			break;
 		case 'i':
-			cfg_ifindex = strtoul(optarg, NULL, 0);
-			if (cfg_ifindex == ULONG_MAX)
+			if (tmp == ULLONG_MAX || tmp > INT_MAX)
 				error(1, ERANGE,
-				      "ifindex must be < ULONG_MAX");
+				      "ifindex must be <= INT_MAX");
+
+			cfg_ifindex = (int)tmp;
 			break;
 		}
 	}
@@ -277,8 +285,8 @@  static void run_poller(void)
 	 * here
 	 */
 	epoll_params.busy_poll_usecs = cfg_busy_poll_usecs;
-	epoll_params.busy_poll_budget = (uint16_t)cfg_busy_poll_budget;
-	epoll_params.prefer_busy_poll = (uint8_t)cfg_prefer_busy_poll;
+	epoll_params.busy_poll_budget = cfg_busy_poll_budget;
+	epoll_params.prefer_busy_poll = cfg_prefer_busy_poll;
 	epoll_params.__pad = 0;
 
 	val = 1;
@@ -342,5 +350,9 @@  int main(int argc, char *argv[])
 	parse_opts(argc, argv);
 	setup_queue();
 	run_poller();
+
+	if (cfg_outfile)
+		free(cfg_outfile);
+
 	return 0;
 }