Skip to contentSkip to conversation

postocpy: Check that postocpy fd's are not NULL

PATCH In review Juan Quintela 2 participants 4 messages Nov 3, 2021 – Nov 4, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 postocpy: Check that postocpy fd's are not NULL

If postcopy has finished, it frees the array.
But vhost-user unregister it at cleanup time.

---
 migration/postcopy-ram.c | 4 ++++
 1 file changed, 4 insertions(+)
migration/postcopy-ram.c +4 -0
index e721f69d0f..d18b5d05b2 100644
@@ -1457,6 +1457,10 @@ void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
     MigrationIncomingState *mis = migration_incoming_get_current();
     GArray *pcrfds = mis->postcopy_remote_fds;
 
+    if (!pcrfds) {
+        /* migration has already finished and freed the array */
+        return;
+    }
     for (i = 0; i < pcrfds->len; i++) {
         struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
         if (cur->fd == pcfd->fd) {
Signature · 1 line
2.33.1
#1 lore raw
Quoted 2 lines · * Juan Quintela ([email protected]) wrote:

If postcopy has finished, it frees the array.
But vhost-user unregister it at cleanup time.

I must admit to being confused as the double migrate case vs migrate once and shutdown. It might just be an ordering thing?

I notice that 'vhost_user_backend_cleanup' does:

    if (u->postcopy_fd.handler) {
        postcopy_unregister_shared_ufd(&u->postcopy_fd);
        close(u->postcopy_fd.fd);
        u->postcopy_fd.handler = NULL;
    }

where as the other caller, 'vhost_user_postcopy_end' does:

    postcopy_unregister_shared_ufd(&u->postcopy_fd);
    close(u->postcopy_fd.fd);
    u->postcopy_fd.handler = NULL;

maybe it would be better to fix them to do the same if check?

(Also note 'post*o*cpy' typo in title, and probably worth a

Fixes c4f7538 ?)

Dave

Quoted 23 lines
---
 migration/postcopy-ram.c | 4 ++++
 1 file changed, 4 insertions(+)
migration/postcopy-ram.c +4 -0
index e721f69d0f..d18b5d05b2 100644
@@ -1457,6 +1457,10 @@ void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
     MigrationIncomingState *mis = migration_incoming_get_current();
     GArray *pcrfds = mis->postcopy_remote_fds;
 
+    if (!pcrfds) {
+        /* migration has already finished and freed the array */
+        return;
+    }
     for (i = 0; i < pcrfds->len; i++) {
         struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
         if (cur->fd == pcfd->fd) {
Signature · 1 line
2.33.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#2 lore raw
Quoted 14 lines · Dr. David Alan Gilbert
Quoted 2 lines · * Juan Quintela ([email protected]) wrote:

If postcopy has finished, it frees the array.
But vhost-user unregister it at cleanup time.

I must admit to being confused as the double migrate case vs migrate once and shutdown. It might just be an ordering thing?

I notice that 'vhost_user_backend_cleanup' does:

    if (u->postcopy_fd.handler) {
        postcopy_unregister_shared_ufd(&u->postcopy_fd);
        close(u->postcopy_fd.fd);
        u->postcopy_fd.handler = NULL;
    }
Quoted 5 lines

where as the other caller, 'vhost_user_postcopy_end' does:

    postcopy_unregister_shared_ufd(&u->postcopy_fd);
    close(u->postcopy_fd.fd);
    u->postcopy_fd.handler = NULL;

I think that we want ta make here the check to see if it has already been freed.

Quoted 1 line

maybe it would be better to fix them to do the same if check?

But even there, I think that it is more robust that we don't try to access a NULL pointer.

I.e. there are two things that we can fix here:
- postcopy unregister
- vhost use of postcopy unregister

Quoted 2 lines

(Also note 'post*o*cpy' typo in title, and probably worth a

Fixes c4f7538 ?)

Sure.

What do you think?

Later, Juan.

Quoted 26 lines

Dave

Quoted 23 lines
---
 migration/postcopy-ram.c | 4 ++++
 1 file changed, 4 insertions(+)
migration/postcopy-ram.c +4 -0
index e721f69d0f..d18b5d05b2 100644
@@ -1457,6 +1457,10 @@ void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
     MigrationIncomingState *mis = migration_incoming_get_current();
     GArray *pcrfds = mis->postcopy_remote_fds;
 
+    if (!pcrfds) {
+        /* migration has already finished and freed the array */
+        return;
+    }
     for (i = 0; i < pcrfds->len; i++) {
         struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
         if (cur->fd == pcfd->fd) {
Signature · 1 line
2.33.1
#3 lore raw
Quoted 24 lines · * Juan Quintela ([email protected]) wrote:
Quoted 14 lines · Dr. David Alan Gilbert
Quoted 2 lines · * Juan Quintela ([email protected]) wrote:

If postcopy has finished, it frees the array.
But vhost-user unregister it at cleanup time.

I must admit to being confused as the double migrate case vs migrate once and shutdown. It might just be an ordering thing?

I notice that 'vhost_user_backend_cleanup' does:

    if (u->postcopy_fd.handler) {
        postcopy_unregister_shared_ufd(&u->postcopy_fd);
        close(u->postcopy_fd.fd);
        u->postcopy_fd.handler = NULL;
    }
Quoted 5 lines

where as the other caller, 'vhost_user_postcopy_end' does:

    postcopy_unregister_shared_ufd(&u->postcopy_fd);
    close(u->postcopy_fd.fd);
    u->postcopy_fd.handler = NULL;

I think that we want ta make here the check to see if it has already been freed.

Quoted 8 lines
Quoted 1 line

maybe it would be better to fix them to do the same if check?

But even there, I think that it is more robust that we don't try to access a NULL pointer.

I.e. there are two things that we can fix here:
- postcopy unregister
- vhost use of postcopy unregister

True we could indeed fix both as a belt-and-braces.

Quoted 6 lines
Quoted 2 lines

(Also note 'post*o*cpy' typo in title, and probably worth a

Fixes c4f7538 ?)

Sure.

What do you think?

So yeh,

Please do the other if as a separate patch sometime.

Dave

Quoted 29 lines

Later, Juan.

Quoted 26 lines

Dave

Quoted 23 lines
---
 migration/postcopy-ram.c | 4 ++++
 1 file changed, 4 insertions(+)
migration/postcopy-ram.c +4 -0
index e721f69d0f..d18b5d05b2 100644
@@ -1457,6 +1457,10 @@ void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
     MigrationIncomingState *mis = migration_incoming_get_current();
     GArray *pcrfds = mis->postcopy_remote_fds;
 
+    if (!pcrfds) {
+        /* migration has already finished and freed the array */
+        return;
+    }
     for (i = 0; i < pcrfds->len; i++) {
         struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
         if (cur->fd == pcfd->fd) {
Signature · 1 line
2.33.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK