public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: Laurent Vivier <lvivier@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>, passt-dev@passt.top
Subject: Re: [PATCH] tcp_vu: head_cnt need not be global
Date: Tue, 18 Feb 2025 11:22:06 +0100	[thread overview]
Message-ID: <20250218112206.7af5d711@elisabeth> (raw)
In-Reply-To: <53e1b351-da19-4491-b438-1e77e0dc836c@redhat.com>

On Tue, 18 Feb 2025 09:01:30 +0100
Laurent Vivier <lvivier@redhat.com> wrote:

> On 18/02/2025 03:50, David Gibson wrote:
> > head_cnt is a global variable which tracks how many entries in head[] are
> > currently used.  The fact that it's global obscures the fact that the
> > lifetime over which it has a meaningful value is quite short: a single
> > call to of tcp_vu_data_from_sock().
> > 
> > Make it a local to tcp_vu_data_from_sock() to make that lifetime clearer.
> > We keep the head[] array global for now - although technically it has the
> > same valid lifetime - because it's large enough we might not want to put
> > it on the stack.  
> 
> We can make the array local but on the heap using a static declaration in the function.

Well, it would still be in the data segment, not actually on the heap
[1], but it would be somewhat hidden in the function.

I'd rather suggest that we keep this stuff visible at the "top level".


[1] I had a doubt for a moment so I tried it:

$ git diff
diff --git a/tcp_vu.c b/tcp_vu.c
index 0622f17..c40b102 100644
--- a/tcp_vu.c
+++ b/tcp_vu.c
@@ -187,6 +187,7 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c,
 {
 	struct vu_dev *vdev = c->vdev;
 	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+	static char am_i_heap_or_am_i_data[64 << 20];
 	struct msghdr mh_sock = { 0 };
 	uint16_t mss = MSS_GET(conn);
 	int s = conn->sock;
@@ -195,6 +196,8 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c,
 	int elem_cnt;
 	int i;
 
+	memset(am_i_heap_or_am_i_data, 0, sizeof(am_i_heap_or_am_i_data));
+
 	*iov_cnt = 0;
 
 	hdrlen = tcp_vu_hdrlen(v6);

$ nm -td -S passt | grep am_i_heap_or_am_i_data
0000000202163552 0000000067108864 b am_i_heap_or_am_i_data.2

-- 
@@ -187,6 +187,7 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c,
 {
 	struct vu_dev *vdev = c->vdev;
 	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
+	static char am_i_heap_or_am_i_data[64 << 20];
 	struct msghdr mh_sock = { 0 };
 	uint16_t mss = MSS_GET(conn);
 	int s = conn->sock;
@@ -195,6 +196,8 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c,
 	int elem_cnt;
 	int i;
 
+	memset(am_i_heap_or_am_i_data, 0, sizeof(am_i_heap_or_am_i_data));
+
 	*iov_cnt = 0;
 
 	hdrlen = tcp_vu_hdrlen(v6);

$ nm -td -S passt | grep am_i_heap_or_am_i_data
0000000202163552 0000000067108864 b am_i_heap_or_am_i_data.2

-- 
Stefano


  reply	other threads:[~2025-02-18 10:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-18  2:50 [PATCH] tcp_vu: head_cnt need not be global David Gibson
2025-02-18  8:01 ` Laurent Vivier
2025-02-18 10:22   ` Stefano Brivio [this message]
2025-02-20 10:06   ` David Gibson
2025-02-18 15:30 ` Stefano Brivio

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=20250218112206.7af5d711@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).