All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stanislav Fomichev <stfomichev@gmail.com>
To: Jakub Kicinski <kuba@kernel.org>
Cc: davem@davemloft.net, netdev@vger.kernel.org, edumazet@google.com,
	pabeni@redhat.com, andrew+netdev@lunn.ch, horms@kernel.org,
	almasrymina@google.com, sdf@fomichev.me, joe@dama.to,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH net-next 1/4] selftests: drv-net: ncdevmem: remove use of error()
Date: Fri, 22 Aug 2025 14:25:45 -0700	[thread overview]
Message-ID: <aKjgWZnfWShtvi8m@mini-arch> (raw)
In-Reply-To: <20250822200052.1675613-2-kuba@kernel.org>

On 08/22, Jakub Kicinski wrote:
> Using error() makes it impossible for callers to unwind their
> changes. Replace error() calls with proper error handling.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Acked-by: Stanislav Fomichev <sdf@fomichev.me>

> ---
>  .../selftests/drivers/net/hw/ncdevmem.c       | 528 ++++++++++++------
>  1 file changed, 364 insertions(+), 164 deletions(-)
> 
> diff --git a/tools/testing/selftests/drivers/net/hw/ncdevmem.c b/tools/testing/selftests/drivers/net/hw/ncdevmem.c
> index 71961a7688e6..e75adfed33ac 100644
> --- a/tools/testing/selftests/drivers/net/hw/ncdevmem.c
> +++ b/tools/testing/selftests/drivers/net/hw/ncdevmem.c
> @@ -115,6 +115,21 @@ struct memory_provider {
>  				   size_t off, int n);
>  };
>  
> +static void pr_err(const char *fmt, ...)
> +{
> +	va_list args;
> +
> +	fprintf(stderr, "%s: ", TEST_PREFIX);
> +
> +	va_start(args, fmt);
> +	vfprintf(stderr, fmt, args);
> +	va_end(args);
> +
> +	if (errno != 0)
> +		fprintf(stderr, ": %s", strerror(errno));
> +	fprintf(stderr, "\n");
> +}
> +
>  static struct memory_buffer *udmabuf_alloc(size_t size)
>  {
>  	struct udmabuf_create create;
> @@ -123,27 +138,33 @@ static struct memory_buffer *udmabuf_alloc(size_t size)
>  
>  	ctx = malloc(sizeof(*ctx));
>  	if (!ctx)
> -		error(1, ENOMEM, "malloc failed");
> +		return NULL;
>  
>  	ctx->size = size;
>  
>  	ctx->devfd = open("/dev/udmabuf", O_RDWR);
> -	if (ctx->devfd < 0)
> -		error(1, errno,
> -		      "%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n",
> -		      TEST_PREFIX);
> +	if (ctx->devfd < 0) {
> +		pr_err("[skip,no-udmabuf: Unable to access DMA buffer device file]");
> +		goto err_free_ctx;
> +	}
>  
>  	ctx->memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING);
> -	if (ctx->memfd < 0)
> -		error(1, errno, "%s: [skip,no-memfd]\n", TEST_PREFIX);
> +	if (ctx->memfd < 0) {
> +		pr_err("[skip,no-memfd]");
> +		goto err_close_dev;
> +	}
>  
>  	ret = fcntl(ctx->memfd, F_ADD_SEALS, F_SEAL_SHRINK);
> -	if (ret < 0)
> -		error(1, errno, "%s: [skip,fcntl-add-seals]\n", TEST_PREFIX);
> +	if (ret < 0) {
> +		pr_err("[skip,fcntl-add-seals]");
> +		goto err_close_memfd;
> +	}
>  
>  	ret = ftruncate(ctx->memfd, size);
> -	if (ret == -1)
> -		error(1, errno, "%s: [FAIL,memfd-truncate]\n", TEST_PREFIX);
> +	if (ret == -1) {
> +		pr_err("[FAIL,memfd-truncate]");
> +		goto err_close_memfd;
> +	}
>  
>  	memset(&create, 0, sizeof(create));
>  
> @@ -151,15 +172,29 @@ static struct memory_buffer *udmabuf_alloc(size_t size)
>  	create.offset = 0;
>  	create.size = size;
>  	ctx->fd = ioctl(ctx->devfd, UDMABUF_CREATE, &create);
> -	if (ctx->fd < 0)
> -		error(1, errno, "%s: [FAIL, create udmabuf]\n", TEST_PREFIX);
> +	if (ctx->fd < 0) {
> +		pr_err("[FAIL, create udmabuf]");
> +		goto err_close_fd;
> +	}
>  
>  	ctx->buf_mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
>  			    ctx->fd, 0);
> -	if (ctx->buf_mem == MAP_FAILED)
> -		error(1, errno, "%s: [FAIL, map udmabuf]\n", TEST_PREFIX);
> +	if (ctx->buf_mem == MAP_FAILED) {
> +		pr_err("[FAIL, map udmabuf]");
> +		goto err_close_fd;
> +	}
>  
>  	return ctx;
> +
> +err_close_fd:
> +	close(ctx->fd);
> +err_close_memfd:
> +	close(ctx->memfd);
> +err_close_dev:
> +	close(ctx->devfd);
> +err_free_ctx:
> +	free(ctx);
> +	return NULL;
>  }
>  
>  static void udmabuf_free(struct memory_buffer *ctx)
> @@ -217,7 +252,7 @@ static void print_nonzero_bytes(void *ptr, size_t size)
>  		putchar(p[i]);
>  }
>  
> -void validate_buffer(void *line, size_t size)
> +int validate_buffer(void *line, size_t size)
>  {
>  	static unsigned char seed = 1;
>  	unsigned char *ptr = line;
> @@ -232,8 +267,10 @@ void validate_buffer(void *line, size_t size)
>  				"Failed validation: expected=%u, actual=%u, index=%lu\n",
>  				expected, ptr[i], i);
>  			errors++;
> -			if (errors > 20)
> -				error(1, 0, "validation failed.");
> +			if (errors > 20) {
> +				pr_err("validation failed");
> +				return -1;
> +			}
>  		}
>  		seed++;
>  		if (seed == do_validation)
> @@ -241,6 +278,7 @@ void validate_buffer(void *line, size_t size)
>  	}
>  
>  	fprintf(stdout, "Validated buffer\n");
> +	return 0;
>  }
>  
>  static int rxq_num(int ifindex)
> @@ -279,7 +317,7 @@ static int rxq_num(int ifindex)
>  		system(command);                                        \
>  	})
>  
> -static int reset_flow_steering(void)
> +static void reset_flow_steering(void)
>  {
>  	/* Depending on the NIC, toggling ntuple off and on might not
>  	 * be allowed. Additionally, attempting to delete existing filters
> @@ -292,7 +330,6 @@ static int reset_flow_steering(void)
>  	run_command(
>  		"ethtool -n %s | grep 'Filter:' | awk '{print $2}' | xargs -n1 ethtool -N %s delete >&2",
>  		ifname, ifname);
> -	return 0;
>  }
>  
>  static const char *tcp_data_split_str(int val)
> @@ -354,6 +391,11 @@ static int configure_rss(void)
>  	return run_command("ethtool -X %s equal %d >&2", ifname, start_queue);
>  }
>  
> +static void reset_rss(void)
> +{
> +	run_command("ethtool -X %s default >&2", ifname, start_queue);
> +}
> +
>  static int configure_channels(unsigned int rx, unsigned int tx)
>  {
>  	struct ethtool_channels_get_req *gchan;
> @@ -479,6 +521,7 @@ static int bind_rx_queue(unsigned int ifindex, unsigned int dmabuf_fd,
>  
>  	*ys = ynl_sock_create(&ynl_netdev_family, &yerr);
>  	if (!*ys) {
> +		netdev_queue_id_free(queues);

Funny how you spotted this.. Ownership of these is complicated with ynl :-( 

  reply	other threads:[~2025-08-22 21:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-22 20:00 [PATCH net-next 0/4] selftests: drv-net: ncdevmem: fix error paths Jakub Kicinski
2025-08-22 20:00 ` [PATCH net-next 1/4] selftests: drv-net: ncdevmem: remove use of error() Jakub Kicinski
2025-08-22 21:25   ` Stanislav Fomichev [this message]
2025-08-22 20:00 ` [PATCH net-next 2/4] selftests: drv-net: ncdevmem: save IDs of flow rules we added Jakub Kicinski
2025-08-22 21:27   ` Stanislav Fomichev
2025-08-22 22:15     ` Jakub Kicinski
2025-08-22 20:00 ` [PATCH net-next 3/4] selftests: drv-net: ncdevmem: restore old channel config Jakub Kicinski
2025-08-22 22:14   ` Stanislav Fomichev
2025-08-22 20:00 ` [PATCH net-next 4/4] selftests: drv-net: ncdevmem: configure and restore HDS threshold Jakub Kicinski
2025-08-22 22:26   ` Stanislav Fomichev
2025-08-22 22:34     ` Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aKjgWZnfWShtvi8m@mini-arch \
    --to=stfomichev@gmail.com \
    --cc=almasrymina@google.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=joe@dama.to \
    --cc=kuba@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.