Skip to contentSkip to conversation

9pfs: v9fs_walk() cleanup

PATCH In review Christian Schoenebeck 3 participants 11 messages Aug 17, 2021 – Aug 20, 2021

lore
Share & tools
lore
#0 lore raw

cover 9pfs: v9fs_walk() cleanup

Few cleanup patches for function v9fs_walk() as discussed last month.

In patch 2 array variables 'wnames' and 'pathes' are omitted because they contain dynamically allocated memory per array element which need to be freed individually before freeing the array.

Christian Schoenebeck (2):
hw/9pfs: avoid 'path' copy in v9fs_walk() hw/9pfs: use g_autofree in v9fs_walk() where possible

 hw/9pfs/9p.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 hw/9pfs: avoid 'path' copy in v9fs_walk()

The v9fs_walk() function resolves all client submitted path nodes to the local 'pathes' array. Using a separate string scalar variable 'path' inside the background worker thread loop and copying that local 'path' string scalar variable subsequently to the 'pathes' array (at the end of each loop iteration) is not necessary.

Instead simply resolve each path directly to the 'pathes' array and don't use the string scalar variable 'path' inside the fs worker thread loop at all.

The only advantage of the 'path' scalar was that in case of an error the respective 'pathes' element would not be filled. Right now this is not an issue as the v9fs_walk() function returns as soon as any error occurs.

---
 hw/9pfs/9p.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +4 -4
index 2815257f42..4d642ab12a 100644
@@ -1787,7 +1787,8 @@ static void coroutine_fn v9fs_walk(void *opaque)
                 strcmp("..", wnames[name_idx].data))
             {
                 err = s->ops->name_to_path(&s->ctx, &dpath,
-                                        wnames[name_idx].data, &path);
+                                           wnames[name_idx].data,
+                                           &pathes[name_idx]);
                 if (err < 0) {
                     err = -errno;
                     break;
@@ -1796,14 +1797,13 @@ static void coroutine_fn v9fs_walk(void *opaque)
                     err = -EINTR;
                     break;
                 }
-                err = s->ops->lstat(&s->ctx, &path, &stbuf);
+                err = s->ops->lstat(&s->ctx, &pathes[name_idx], &stbuf);
                 if (err < 0) {
                     err = -errno;
                     break;
                 }
                 stbufs[name_idx] = stbuf;
-                v9fs_path_copy(&dpath, &path);
-                v9fs_path_copy(&pathes[name_idx], &path);
+                v9fs_path_copy(&dpath, &pathes[name_idx]);
             }
         }
     });
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/9pfs: avoid 'path' copy in v9fs_walk()

Quoted 10 lines · On Tue, 17 Aug 2021 14:38:24 +0200 Christian Schoenebeck…

The v9fs_walk() function resolves all client submitted path nodes to the local 'pathes' array. Using a separate string scalar variable 'path' inside the background worker thread loop and copying that local 'path' string scalar variable subsequently to the 'pathes' array (at the end of each loop iteration) is not necessary.

Instead simply resolve each path directly to the 'pathes' array and don't use the string scalar variable 'path' inside the fs worker thread loop at all.

Quoted 8 lines

The only advantage of the 'path' scalar was that in case of an error the respective 'pathes' element would not be filled. Right now this is not an issue as the v9fs_walk() function returns as soon as any error occurs.

---

With this change, the path variable is no longer used at all in the first loop. I see at least an extra possible cleanup : don't set path before the first loop since it gets reset before the second one. Maybe we can even get rid of path all the way ? I'll have a look.

Quoted 34 lines
 hw/9pfs/9p.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +4 -4
index 2815257f42..4d642ab12a 100644
@@ -1787,7 +1787,8 @@ static void coroutine_fn v9fs_walk(void *opaque)
                 strcmp("..", wnames[name_idx].data))
             {
                 err = s->ops->name_to_path(&s->ctx, &dpath,
-                                        wnames[name_idx].data, &path);
+                                           wnames[name_idx].data,
+                                           &pathes[name_idx]);
                 if (err < 0) {
                     err = -errno;
                     break;
@@ -1796,14 +1797,13 @@ static void coroutine_fn v9fs_walk(void *opaque)
                     err = -EINTR;
                     break;
                 }
-                err = s->ops->lstat(&s->ctx, &path, &stbuf);
+                err = s->ops->lstat(&s->ctx, &pathes[name_idx], &stbuf);
                 if (err < 0) {
                     err = -errno;
                     break;
                 }
                 stbufs[name_idx] = stbuf;
-                v9fs_path_copy(&dpath, &path);
-                v9fs_path_copy(&pathes[name_idx], &path);
+                v9fs_path_copy(&dpath, &pathes[name_idx]);
             }
         }
     });
#3 lore raw

Re: [PATCH 1/2] hw/9pfs: avoid 'path' copy in v9fs_walk()

Quoted 18 lines · On Freitag, 20. August 2021 12:35:49 CEST Greg Kurz wrote:

On Tue, 17 Aug 2021 14:38:24 +0200

Quoted 15 lines · Christian Schoenebeck

The v9fs_walk() function resolves all client submitted path nodes to the local 'pathes' array. Using a separate string scalar variable 'path' inside the background worker thread loop and copying that local 'path' string scalar variable subsequently to the 'pathes' array (at the end of each loop iteration) is not necessary.

Instead simply resolve each path directly to the 'pathes' array and don't use the string scalar variable 'path' inside the fs worker thread loop at all.

The only advantage of the 'path' scalar was that in case of an error the respective 'pathes' element would not be filled. Right now this is not an issue as the v9fs_walk() function returns as soon as any error occurs.

Quoted 8 lines
Quoted 3 lines

---

With this change, the path variable is no longer used at all in the first loop.

Correct.

Quoted 3 lines

I see at least an extra possible cleanup : don't set path before the first loop since it gets reset before the second one.

Also correct.

Quoted 2 lines

Maybe we can even get rid of path all the way ? I'll have a look.

Yes, that's the plan.

There is still quite a bunch that can be cleaned up in that function. I just didn't want to start with a two-digit patch set right after the long summer break. ;-)

If you want to send some cleanup patches, always appreciated.

Best regards,
Christian Schoenebeck

#4 lore raw

PATCH 2/2 hw/9pfs: use g_autofree in v9fs_walk() where possible

In reply to Christian Schoenebeck
---
 hw/9pfs/9p.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +3 -4
index 4d642ab12a..c857b31321 100644
@@ -1703,11 +1703,12 @@ static bool same_stat_id(const struct stat *a, const struct stat *b)
 static void coroutine_fn v9fs_walk(void *opaque)
 {
     int name_idx;
-    V9fsQID *qids = NULL;
+    g_autofree V9fsQID *qids = NULL;
     int i, err = 0;
     V9fsPath dpath, path, *pathes = NULL;
     uint16_t nwnames;
-    struct stat stbuf, fidst, *stbufs = NULL;
+    struct stat stbuf, fidst;
+    g_autofree struct stat *stbufs = NULL;
     size_t offset = 7;
     int32_t fid, newfid;
     V9fsString *wnames = NULL;
@@ -1872,8 +1873,6 @@ out_nofid:
             v9fs_path_free(&pathes[name_idx]);
         }
         g_free(wnames);
-        g_free(qids);
-        g_free(stbufs);
         g_free(pathes);
     }
 }
Signature · 1 line
2.20.1
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] hw/9pfs: use g_autofree in v9fs_walk() where possible

In reply to Christian Schoenebeck
Quoted 26 lines · On Tue, 17 Aug 2021 15:46:50 +0200 Christian Schoenebeck…
---
 hw/9pfs/9p.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +3 -2
index 4d642ab12a..c857b31321 100644
@@ -1703,11 +1703,12 @@ static bool same_stat_id(const struct stat *a, const struct stat *b)
 static void coroutine_fn v9fs_walk(void *opaque)
 {
     int name_idx;
-    V9fsQID *qids = NULL;
+    g_autofree V9fsQID *qids = NULL;
     int i, err = 0;
     V9fsPath dpath, path, *pathes = NULL;
     uint16_t nwnames;
-    struct stat stbuf, fidst, *stbufs = NULL;
+    struct stat stbuf, fidst;
+    g_autofree struct stat *stbufs = NULL;
     size_t offset = 7;
     int32_t fid, newfid;
     V9fsString *wnames = NULL;

hw/9pfs/9p.c out_nofid: · line 1876

Quoted 6 lines
hw/9pfs/9p.c +0 -2
@@ -1872,8 +1873,6 @@ out_nofid:
             v9fs_path_free(&pathes[name_idx]);
         }
         g_free(wnames);
-        g_free(qids);
-        g_free(stbufs);
         g_free(pathes);

It seems that wnames and pathes could be converted to g_autofree as well or I'm missing something ?

Feel free to add my R-b with or without that extra change.

Quoted 2 lines
hw/9pfs/9p.c +0 -0
     }
 }
#7 lore raw

Re: [PATCH 2/2] hw/9pfs: use g_autofree in v9fs_walk() where possible

Quoted 42 lines · On Freitag, 20. August 2021 12:40:31 CEST Greg Kurz wrote:

On Tue, 17 Aug 2021 15:46:50 +0200

Quoted 39 lines · Christian Schoenebeck

---

 hw/9pfs/9p.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +0 -0
index 4d642ab12a..c857b31321 100644
@@ -1703,11 +1703,12 @@ static bool same_stat_id(const struct stat *a,

const struct stat *b)>
static void coroutine_fn v9fs_walk(void *opaque)
{

     int name_idx;
hw/9pfs/9p.c +3 -2
-    V9fsQID *qids = NULL;
+    g_autofree V9fsQID *qids = NULL;
 
     int i, err = 0;
     V9fsPath dpath, path, *pathes = NULL;
     uint16_t nwnames;
 
-    struct stat stbuf, fidst, *stbufs = NULL;
+    struct stat stbuf, fidst;
+    g_autofree struct stat *stbufs = NULL;
 
     size_t offset = 7;
     int32_t fid, newfid;
     V9fsString *wnames = NULL;
@@ -1872,8 +1873,6 @@ out_nofid:
             v9fs_path_free(&pathes[name_idx]);
 
         }
         g_free(wnames);
 
Quoted 7 lines
Quoted 4 lines
hw/9pfs/9p.c +0 -2
-        g_free(qids);
-        g_free(stbufs);
 
         g_free(pathes);

It seems that wnames and pathes could be converted to g_autofree as well or I'm missing something ?

Yeah, I mentioned that in the cover letter. Those two are omitted in this patch because they contain dynamically allocated memory per array element which need to be freed individually before freeing the array.

So those two would probably require custom cleanup handlers.

Quoted 4 lines

Feel free to add my R-b with or without that extra change.

Thanks!

Quoted 4 lines
Quoted 4 lines
     }

}

Best regards,
Christian Schoenebeck

#8 lore raw

Re: [PATCH 2/2] hw/9pfs: use g_autofree in v9fs_walk() where possible

Quoted 51 lines · On Fri, 20 Aug 2021 14:23:26 +0200 Christian Schoenebeck…
Quoted 42 lines · On Freitag, 20. August 2021 12:40:31 CEST Greg Kurz wrote:

On Tue, 17 Aug 2021 15:46:50 +0200

Quoted 39 lines · Christian Schoenebeck

---

 hw/9pfs/9p.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +0 -0
index 4d642ab12a..c857b31321 100644
@@ -1703,11 +1703,12 @@ static bool same_stat_id(const struct stat *a,

const struct stat *b)>
static void coroutine_fn v9fs_walk(void *opaque)
{

     int name_idx;
hw/9pfs/9p.c +3 -2
-    V9fsQID *qids = NULL;
+    g_autofree V9fsQID *qids = NULL;
 
     int i, err = 0;
     V9fsPath dpath, path, *pathes = NULL;
     uint16_t nwnames;
 
-    struct stat stbuf, fidst, *stbufs = NULL;
+    struct stat stbuf, fidst;
+    g_autofree struct stat *stbufs = NULL;
 
     size_t offset = 7;
     int32_t fid, newfid;
     V9fsString *wnames = NULL;
@@ -1872,8 +1873,6 @@ out_nofid:
             v9fs_path_free(&pathes[name_idx]);
 
         }
         g_free(wnames);
 
Quoted 7 lines
Quoted 4 lines
hw/9pfs/9p.c +0 -2
-        g_free(qids);
-        g_free(stbufs);
 
         g_free(pathes);

It seems that wnames and pathes could be converted to g_autofree as well or I'm missing something ?

Quoted 6 lines

Yeah, I mentioned that in the cover letter. Those two are omitted in this patch because they contain dynamically allocated memory per array element which need to be freed individually before freeing the array.

So those two would probably require custom cleanup handlers.

The freeing of the individual elements is already handled in the loop above the g_free() calls. The wnames and pathes pointers can thus be treated like the other ones.

Quoted 16 lines
Quoted 4 lines

Feel free to add my R-b with or without that extra change.

Thanks!

Quoted 4 lines
Quoted 4 lines
     }

}

Best regards,
Christian Schoenebeck

#9 lore raw

Re: [PATCH 2/2] hw/9pfs: use g_autofree in v9fs_walk() where possible

Quoted 59 lines · On Freitag, 20. August 2021 14:34:11 CEST Greg Kurz wrote:

On Fri, 20 Aug 2021 14:23:26 +0200

Quoted 56 lines · Christian Schoenebeck
Quoted 43 lines · On Freitag, 20. August 2021 12:40:31 CEST Greg Kurz wrote:

On Tue, 17 Aug 2021 15:46:50 +0200

Quoted 40 lines · Christian Schoenebeck

---

 hw/9pfs/9p.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/9pfs/9p.c +0 -0
index 4d642ab12a..c857b31321 100644
@@ -1703,11 +1703,12 @@ static bool same_stat_id(const struct stat *a,

const struct stat *b)>

static void coroutine_fn v9fs_walk(void *opaque)
{

     int name_idx;
hw/9pfs/9p.c +3 -2
-    V9fsQID *qids = NULL;
+    g_autofree V9fsQID *qids = NULL;
 
     int i, err = 0;
     V9fsPath dpath, path, *pathes = NULL;
     uint16_t nwnames;
 
-    struct stat stbuf, fidst, *stbufs = NULL;
+    struct stat stbuf, fidst;
+    g_autofree struct stat *stbufs = NULL;
 
     size_t offset = 7;
     int32_t fid, newfid;
     V9fsString *wnames = NULL;
@@ -1872,8 +1873,6 @@ out_nofid:
             v9fs_path_free(&pathes[name_idx]);
 
         }
         g_free(wnames);
 
Quoted 7 lines
Quoted 4 lines
hw/9pfs/9p.c +0 -2
-        g_free(qids);
-        g_free(stbufs);
 
         g_free(pathes);

It seems that wnames and pathes could be converted to g_autofree as well or I'm missing something ?

Yeah, I mentioned that in the cover letter. Those two are omitted in this patch because they contain dynamically allocated memory per array element which need to be freed individually before freeing the array.

Quoted 5 lines
Quoted 1 line

So those two would probably require custom cleanup handlers.

The freeing of the individual elements is already handled in the loop above the g_free() calls. The wnames and pathes pointers can thus be treated like the other ones.

Yes I know. I was just considering to make that in a safer way that would allow simple returns in future without goto out_something; branches. But yes, as it is right now they could be converted in the exact same way yet.

Best regards,
Christian Schoenebeck

#10 lore raw
In reply to Christian Schoenebeck
Quoted 6 lines · On Dienstag, 17. August 2021 15:52:39 CEST Christian Schoenebeck wrote:

Few cleanup patches for function v9fs_walk() as discussed last month.

In patch 2 array variables 'wnames' and 'pathes' are omitted because they contain dynamically allocated memory per array element which need to be freed individually before freeing the array.

Quoted 6 lines

Christian Schoenebeck (2):
hw/9pfs: avoid 'path' copy in v9fs_walk() hw/9pfs: use g_autofree in v9fs_walk() where possible

 hw/9pfs/9p.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next

Thanks!

Best regards,
Christian Schoenebeck