netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
@ 2025-05-08  8:44 Cosmin Ratiu
  2025-05-08 16:23 ` Stanislav Fomichev
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Cosmin Ratiu @ 2025-05-08  8:44 UTC (permalink / raw)
  To: netdev, cratiu
  Cc: David S . Miller, Jakub Kicinski, Eric Dumazet, Andrew Lunn,
	Paolo Abeni, Joe Damato, Shuah Khan, Stanislav Fomichev,
	Mina Almasry, Saeed Mahameed, Tariq Toukan, Dragos Tatulea,
	linux-kselftest

netdev_bind_rx takes ownership of the queue array passed as parameter
and frees it, so a queue array buffer cannot be reused across multiple
netdev_bind_rx calls.

This commit fixes that by always passing in a newly created queue array
to all netdev_bind_rx calls in ncdevmem.

Fixes: 85585b4bc8d8 ("selftests: add ncdevmem, netcat for devmem TCP")
Signed-off-by: Cosmin Ratiu <cratiu@nvidia.com>
---
 .../selftests/drivers/net/hw/ncdevmem.c       | 55 ++++++++-----------
 1 file changed, 22 insertions(+), 33 deletions(-)

diff --git a/tools/testing/selftests/drivers/net/hw/ncdevmem.c b/tools/testing/selftests/drivers/net/hw/ncdevmem.c
index 2bf14ac2b8c6..9d48004ff1a1 100644
--- a/tools/testing/selftests/drivers/net/hw/ncdevmem.c
+++ b/tools/testing/selftests/drivers/net/hw/ncdevmem.c
@@ -431,6 +431,22 @@ static int parse_address(const char *str, int port, struct sockaddr_in6 *sin6)
 	return 0;
 }
 
+static struct netdev_queue_id *create_queues(void)
+{
+	struct netdev_queue_id *queues;
+	size_t i = 0;
+
+	queues = calloc(num_queues, sizeof(*queues));
+	for (i = 0; i < num_queues; i++) {
+		queues[i]._present.type = 1;
+		queues[i]._present.id = 1;
+		queues[i].type = NETDEV_QUEUE_TYPE_RX;
+		queues[i].id = start_queue + i;
+	}
+
+	return queues;
+}
+
 int do_server(struct memory_buffer *mem)
 {
 	char ctrl_data[sizeof(int) * 20000];
@@ -448,7 +464,6 @@ int do_server(struct memory_buffer *mem)
 	char buffer[256];
 	int socket_fd;
 	int client_fd;
-	size_t i = 0;
 	int ret;
 
 	ret = parse_address(server_ip, atoi(port), &server_sin);
@@ -471,16 +486,7 @@ int do_server(struct memory_buffer *mem)
 
 	sleep(1);
 
-	queues = malloc(sizeof(*queues) * num_queues);
-
-	for (i = 0; i < num_queues; i++) {
-		queues[i]._present.type = 1;
-		queues[i]._present.id = 1;
-		queues[i].type = NETDEV_QUEUE_TYPE_RX;
-		queues[i].id = start_queue + i;
-	}
-
-	if (bind_rx_queue(ifindex, mem->fd, queues, num_queues, &ys))
+	if (bind_rx_queue(ifindex, mem->fd, create_queues(), num_queues, &ys))
 		error(1, 0, "Failed to bind\n");
 
 	tmp_mem = malloc(mem->size);
@@ -545,7 +551,6 @@ int do_server(struct memory_buffer *mem)
 			goto cleanup;
 		}
 
-		i++;
 		for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) {
 			if (cm->cmsg_level != SOL_SOCKET ||
 			    (cm->cmsg_type != SCM_DEVMEM_DMABUF &&
@@ -630,10 +635,8 @@ int do_server(struct memory_buffer *mem)
 
 void run_devmem_tests(void)
 {
-	struct netdev_queue_id *queues;
 	struct memory_buffer *mem;
 	struct ynl_sock *ys;
-	size_t i = 0;
 
 	mem = provider->alloc(getpagesize() * NUM_PAGES);
 
@@ -641,38 +644,24 @@ void run_devmem_tests(void)
 	if (configure_rss())
 		error(1, 0, "rss error\n");
 
-	queues = calloc(num_queues, sizeof(*queues));
-
 	if (configure_headersplit(1))
 		error(1, 0, "Failed to configure header split\n");
 
-	if (!bind_rx_queue(ifindex, mem->fd, queues, num_queues, &ys))
+	if (!bind_rx_queue(ifindex, mem->fd,
+			   calloc(num_queues, sizeof(struct netdev_queue_id)),
+			   num_queues, &ys))
 		error(1, 0, "Binding empty queues array should have failed\n");
 
-	for (i = 0; i < num_queues; i++) {
-		queues[i]._present.type = 1;
-		queues[i]._present.id = 1;
-		queues[i].type = NETDEV_QUEUE_TYPE_RX;
-		queues[i].id = start_queue + i;
-	}
-
 	if (configure_headersplit(0))
 		error(1, 0, "Failed to configure header split\n");
 
-	if (!bind_rx_queue(ifindex, mem->fd, queues, num_queues, &ys))
+	if (!bind_rx_queue(ifindex, mem->fd, create_queues(), num_queues, &ys))
 		error(1, 0, "Configure dmabuf with header split off should have failed\n");
 
 	if (configure_headersplit(1))
 		error(1, 0, "Failed to configure header split\n");
 
-	for (i = 0; i < num_queues; i++) {
-		queues[i]._present.type = 1;
-		queues[i]._present.id = 1;
-		queues[i].type = NETDEV_QUEUE_TYPE_RX;
-		queues[i].id = start_queue + i;
-	}
-
-	if (bind_rx_queue(ifindex, mem->fd, queues, num_queues, &ys))
+	if (bind_rx_queue(ifindex, mem->fd, create_queues(), num_queues, &ys))
 		error(1, 0, "Failed to bind\n");
 
 	/* Deactivating a bound queue should not be legal */
-- 
2.45.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
  2025-05-08  8:44 [PATCH net v2] tests/ncdevmem: Fix double-free of queue array Cosmin Ratiu
@ 2025-05-08 16:23 ` Stanislav Fomichev
  2025-05-08 18:31 ` Joe Damato
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Stanislav Fomichev @ 2025-05-08 16:23 UTC (permalink / raw)
  To: Cosmin Ratiu
  Cc: netdev, David S . Miller, Jakub Kicinski, Eric Dumazet,
	Andrew Lunn, Paolo Abeni, Joe Damato, Shuah Khan,
	Stanislav Fomichev, Mina Almasry, Saeed Mahameed, Tariq Toukan,
	Dragos Tatulea, linux-kselftest

On 05/08, Cosmin Ratiu wrote:
> netdev_bind_rx takes ownership of the queue array passed as parameter
> and frees it, so a queue array buffer cannot be reused across multiple
> netdev_bind_rx calls.
> 
> This commit fixes that by always passing in a newly created queue array
> to all netdev_bind_rx calls in ncdevmem.
> 
> Fixes: 85585b4bc8d8 ("selftests: add ncdevmem, netcat for devmem TCP")
> Signed-off-by: Cosmin Ratiu <cratiu@nvidia.com>

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
  2025-05-08  8:44 [PATCH net v2] tests/ncdevmem: Fix double-free of queue array Cosmin Ratiu
  2025-05-08 16:23 ` Stanislav Fomichev
@ 2025-05-08 18:31 ` Joe Damato
  2025-05-09  7:21   ` Cosmin Ratiu
  2025-05-08 20:42 ` Mina Almasry
  2025-05-09 23:30 ` patchwork-bot+netdevbpf
  3 siblings, 1 reply; 7+ messages in thread
From: Joe Damato @ 2025-05-08 18:31 UTC (permalink / raw)
  To: Cosmin Ratiu
  Cc: netdev, David S . Miller, Jakub Kicinski, Eric Dumazet,
	Andrew Lunn, Paolo Abeni, Shuah Khan, Stanislav Fomichev,
	Mina Almasry, Saeed Mahameed, Tariq Toukan, Dragos Tatulea,
	linux-kselftest

On Thu, May 08, 2025 at 11:44:34AM +0300, Cosmin Ratiu wrote:
> netdev_bind_rx takes ownership of the queue array passed as parameter
> and frees it, so a queue array buffer cannot be reused across multiple
> netdev_bind_rx calls.
> 
> This commit fixes that by always passing in a newly created queue array
> to all netdev_bind_rx calls in ncdevmem.
> 
> Fixes: 85585b4bc8d8 ("selftests: add ncdevmem, netcat for devmem TCP")
> Signed-off-by: Cosmin Ratiu <cratiu@nvidia.com>
> ---
>  .../selftests/drivers/net/hw/ncdevmem.c       | 55 ++++++++-----------
>  1 file changed, 22 insertions(+), 33 deletions(-)
> 
> diff --git a/tools/testing/selftests/drivers/net/hw/ncdevmem.c b/tools/testing/selftests/drivers/net/hw/ncdevmem.c
> index 2bf14ac2b8c6..9d48004ff1a1 100644
> --- a/tools/testing/selftests/drivers/net/hw/ncdevmem.c
> +++ b/tools/testing/selftests/drivers/net/hw/ncdevmem.c
> @@ -431,6 +431,22 @@ static int parse_address(const char *str, int port, struct sockaddr_in6 *sin6)

> +	queues = calloc(num_queues, sizeof(*queues));

> -	queues = malloc(sizeof(*queues) * num_queues);

> +	if (!bind_rx_queue(ifindex, mem->fd,
> +			   calloc(num_queues, sizeof(struct netdev_queue_id)),

Nit: it looks like in the original we didn't care about malloc
potentially failing. Do we care about checking for that now with
this cleanup?

Otherwise:

Reviewed-by: Joe Damato <jdamato@fastly.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
  2025-05-08  8:44 [PATCH net v2] tests/ncdevmem: Fix double-free of queue array Cosmin Ratiu
  2025-05-08 16:23 ` Stanislav Fomichev
  2025-05-08 18:31 ` Joe Damato
@ 2025-05-08 20:42 ` Mina Almasry
  2025-05-09  7:24   ` Cosmin Ratiu
  2025-05-09 23:30 ` patchwork-bot+netdevbpf
  3 siblings, 1 reply; 7+ messages in thread
From: Mina Almasry @ 2025-05-08 20:42 UTC (permalink / raw)
  To: Cosmin Ratiu
  Cc: netdev, David S . Miller, Jakub Kicinski, Eric Dumazet,
	Andrew Lunn, Paolo Abeni, Joe Damato, Shuah Khan,
	Stanislav Fomichev, Saeed Mahameed, Tariq Toukan, Dragos Tatulea,
	linux-kselftest

On Thu, May 8, 2025 at 1:45 AM Cosmin Ratiu <cratiu@nvidia.com> wrote:
>
> netdev_bind_rx takes ownership of the queue array passed as parameter
> and frees it, so a queue array buffer cannot be reused across multiple
> netdev_bind_rx calls.
>
> This commit fixes that by always passing in a newly created queue array
> to all netdev_bind_rx calls in ncdevmem.
>
> Fixes: 85585b4bc8d8 ("selftests: add ncdevmem, netcat for devmem TCP")
> Signed-off-by: Cosmin Ratiu <cratiu@nvidia.com>

Thank you very much.

Reviewed-by: Mina Almasry <almasrymina@google.com>

Also, I think there was a discussion in v1 about increasing the amount
of memory that ncdevmem uses by default (currently it's 64MB) as Stan
suggested. I have it in my TODO list to implement that change but I
don't think I'll get to it soon. If you (or anyone) gets to it before
me, it's a welcome change. AFAIU it'll unblock you from running
ncdevmem on your driver which expects much more dmabuf memory
available.

But to be clear, that can be a follow up change. I think this is good as-is.

-- 
Thanks,
Mina

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
  2025-05-08 18:31 ` Joe Damato
@ 2025-05-09  7:21   ` Cosmin Ratiu
  0 siblings, 0 replies; 7+ messages in thread
From: Cosmin Ratiu @ 2025-05-09  7:21 UTC (permalink / raw)
  To: andrew+netdev@lunn.ch, davem@davemloft.net, jdamato@fastly.com,
	Tariq Toukan, Dragos Tatulea, shuah@kernel.org, sdf@fomichev.me,
	linux-kselftest@vger.kernel.org, pabeni@redhat.com,
	kuba@kernel.org, edumazet@google.com, netdev@vger.kernel.org,
	almasrymina@google.com, Saeed Mahameed

On Thu, 2025-05-08 at 11:31 -0700, Joe Damato wrote:
> 
> Nit: it looks like in the original we didn't care about malloc
> potentially failing. Do we care about checking for that now with
> this cleanup?

Thank you for the review, Joe.

I looked a bit into adding error messages and I think it wouldn't make
sense just for these allocations, as there are others which do not
check for malloc/calloc error (e.g. all generated ynl libs).

Furthermore, I am under the impression that on the kind of systems
ncdevmem would run, malloc/calloc would almost never fail because of
overcommit.

Finally, even if they did fail, the user program would just segfault
(not very user friendly, but good enough).

Cosmin.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
  2025-05-08 20:42 ` Mina Almasry
@ 2025-05-09  7:24   ` Cosmin Ratiu
  0 siblings, 0 replies; 7+ messages in thread
From: Cosmin Ratiu @ 2025-05-09  7:24 UTC (permalink / raw)
  To: almasrymina@google.com
  Cc: andrew+netdev@lunn.ch, davem@davemloft.net, jdamato@fastly.com,
	Tariq Toukan, Dragos Tatulea, shuah@kernel.org, sdf@fomichev.me,
	linux-kselftest@vger.kernel.org, pabeni@redhat.com,
	netdev@vger.kernel.org, edumazet@google.com, kuba@kernel.org,
	Saeed Mahameed

On Thu, 2025-05-08 at 13:42 -0700, Mina Almasry wrote:
> 
> Thank you very much.
> 
> Reviewed-by: Mina Almasry <almasrymina@google.com>

Thank you for the review.

> 
> Also, I think there was a discussion in v1 about increasing the
> amount
> of memory that ncdevmem uses by default (currently it's 64MB) as Stan
> suggested. I have it in my TODO list to implement that change but I
> don't think I'll get to it soon. If you (or anyone) gets to it before
> me, it's a welcome change. AFAIU it'll unblock you from running
> ncdevmem on your driver which expects much more dmabuf memory
> available.
> 
> But to be clear, that can be a follow up change. I think this is good
> as-is.
> 

I do plan to follow-up on that topic, mlx5 will require some
reengineering to not fail rx rings refill when the pool is too small,
and most likely some touch-ups in ncdevmem/udmabuf to make this better.

Cosmin.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2] tests/ncdevmem: Fix double-free of queue array
  2025-05-08  8:44 [PATCH net v2] tests/ncdevmem: Fix double-free of queue array Cosmin Ratiu
                   ` (2 preceding siblings ...)
  2025-05-08 20:42 ` Mina Almasry
@ 2025-05-09 23:30 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-05-09 23:30 UTC (permalink / raw)
  To: Cosmin Ratiu
  Cc: netdev, davem, kuba, edumazet, andrew+netdev, pabeni, jdamato,
	shuah, sdf, almasrymina, saeedm, tariqt, dtatulea,
	linux-kselftest

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Thu, 8 May 2025 11:44:34 +0300 you wrote:
> netdev_bind_rx takes ownership of the queue array passed as parameter
> and frees it, so a queue array buffer cannot be reused across multiple
> netdev_bind_rx calls.
> 
> This commit fixes that by always passing in a newly created queue array
> to all netdev_bind_rx calls in ncdevmem.
> 
> [...]

Here is the summary with links:
  - [net,v2] tests/ncdevmem: Fix double-free of queue array
    https://git.kernel.org/netdev/net/c/97c4e094a4b2

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-05-09 23:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-08  8:44 [PATCH net v2] tests/ncdevmem: Fix double-free of queue array Cosmin Ratiu
2025-05-08 16:23 ` Stanislav Fomichev
2025-05-08 18:31 ` Joe Damato
2025-05-09  7:21   ` Cosmin Ratiu
2025-05-08 20:42 ` Mina Almasry
2025-05-09  7:24   ` Cosmin Ratiu
2025-05-09 23:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).