Skip to contentSkip to conversation

qemu/queue.h: clear linked list pointers on remove

PATCH Applied Stefan Hajnoczi 4 participants 11 messages Feb 24, 2020 – Mar 9, 2020

lore
Share & tools
lore
#0 lore raw

cover qemu/queue.h: clear linked list pointers on remove

QLIST_REMOVE() and friends leave dangling linked list pointers in the node that was removed. This makes it impossible to decide whether a node is currently in a list or not. It also makes debugging harder.

Based-on: [email protected]

          ("[PULL 00/31] Block patches")

Stefan Hajnoczi (2):
qemu/queue.h: clear linked list pointers on remove
aio-posix: remove confusing QLIST_SAFE_REMOVE()

 include/qemu/queue.h | 19 +++++++++++++++----
 util/aio-posix.c     |  2 +-
 2 files changed, 16 insertions(+), 5 deletions(-)
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/2 qemu/queue.h: clear linked list pointers on remove

Do not leave stale linked list pointers around after removal. It's safer to set them to NULL so that use-after-removal results in an immediate segfault.

The RCU queue removal macros are unchanged since nodes may still be traversed after removal.

---
 include/qemu/queue.h | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
include/qemu/queue.h +15 -4
index 294db54eb1..456a5b01ee 100644
@@ -142,6 +142,8 @@ struct {                                                                \
                 (elm)->field.le_next->field.le_prev =                   \
                     (elm)->field.le_prev;                               \
         *(elm)->field.le_prev = (elm)->field.le_next;                   \
+        (elm)->field.le_next = NULL;                                    \
+        (elm)->field.le_prev = NULL;                                    \
 } while (/*CONSTCOND*/0)
 
 /*
@@ -225,12 +227,15 @@ struct {                                                                \
 } while (/*CONSTCOND*/0)
 
 #define QSLIST_REMOVE_HEAD(head, field) do {                             \
-        (head)->slh_first = (head)->slh_first->field.sle_next;          \
+        typeof((head)->slh_first) elm = (head)->slh_first;               \
+        (head)->slh_first = elm->field.sle_next;                         \
+        elm->field.sle_next = NULL;                                      \
 } while (/*CONSTCOND*/0)
 
 #define QSLIST_REMOVE_AFTER(slistelm, field) do {                       \
-        (slistelm)->field.sle_next =                                    \
-            QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field);         \
+        typeof(slistelm) next = (slistelm)->field.sle_next;             \
+        (slistelm)->field.sle_next = next->field.sle_next;              \
+        next->field.sle_next = NULL;                                    \
 } while (/*CONSTCOND*/0)
 
 #define QSLIST_REMOVE(head, elm, type, field) do {                      \
@@ -241,6 +246,7 @@ struct {                                                                \
         while (curelm->field.sle_next != (elm))                         \
             curelm = curelm->field.sle_next;                            \
         curelm->field.sle_next = curelm->field.sle_next->field.sle_next; \
+        (elm)->field.sle_next = NULL;                                   \
     }                                                                   \
 } while (/*CONSTCOND*/0)
 
@@ -304,8 +310,10 @@ struct {                                                                \
 } while (/*CONSTCOND*/0)
 
 #define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
-    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+    typeof((head)->sqh_first) elm = (head)->sqh_first;                  \
+    if (((head)->sqh_first = elm->field.sqe_next) == NULL)              \
         (head)->sqh_last = &(head)->sqh_first;                          \
+    elm->field.sqe_next = NULL;                                         \
 } while (/*CONSTCOND*/0)
 
 #define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do {            \
@@ -329,6 +337,7 @@ struct {                                                                \
         if ((curelm->field.sqe_next =                                   \
             curelm->field.sqe_next->field.sqe_next) == NULL)            \
                 (head)->sqh_last = &(curelm)->field.sqe_next;           \
+        (elm)->field.sqe_next = NULL;                                   \
     }                                                                   \
 } while (/*CONSTCOND*/0)
 
@@ -446,6 +455,8 @@ union {                                                                 \
             (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \
         (elm)->field.tqe_circ.tql_prev->tql_next = (elm)->field.tqe_next; \
         (elm)->field.tqe_circ.tql_prev = NULL;                          \
+        (elm)->field.tqe_circ.tql_next = NULL;                          \
+        (elm)->field.tqe_next = NULL;                                   \
 } while (/*CONSTCOND*/0)
 
 /* remove @left, @right and all elements in between from @head */
Signature · 1 line
2.24.1
#2 lore raw
Quoted 18 lines · Stefan Hajnoczi

Do not leave stale linked list pointers around after removal. It's safer to set them to NULL so that use-after-removal results in an immediate segfault.

The RCU queue removal macros are unchanged since nodes may still be traversed after removal.

---
  include/qemu/queue.h | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)
include/qemu/queue.h +0 -0
index 294db54eb1..456a5b01ee 100644

include/qemu/queue.h struct { \ · line 147

Quoted 6 lines
include/qemu/queue.h +2 -0
@@ -142,6 +142,8 @@ struct {                                                                \
                  (elm)->field.le_next->field.le_prev =                   \
                      (elm)->field.le_prev;                               \
          *(elm)->field.le_prev = (elm)->field.le_next;                   \
+        (elm)->field.le_next = NULL;                                    \
+        (elm)->field.le_prev = NULL;                                    \
  } while (/*CONSTCOND*/0)

OK.

Quoted 30 lines
include/qemu/queue.h +7 -3
  /*
@@ -225,12 +227,15 @@ struct {                                                                \
  } while (/*CONSTCOND*/0)
 
  #define QSLIST_REMOVE_HEAD(head, field) do {                             \
-        (head)->slh_first = (head)->slh_first->field.sle_next;          \
+        typeof((head)->slh_first) elm = (head)->slh_first;               \
+        (head)->slh_first = elm->field.sle_next;                         \
+        elm->field.sle_next = NULL;                                      \
  } while (/*CONSTCOND*/0)
 
  #define QSLIST_REMOVE_AFTER(slistelm, field) do {                       \
-        (slistelm)->field.sle_next =                                    \
-            QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field);         \
+        typeof(slistelm) next = (slistelm)->field.sle_next;             \
+        (slistelm)->field.sle_next = next->field.sle_next;              \
+        next->field.sle_next = NULL;                                    \
  } while (/*CONSTCOND*/0)
 
  #define QSLIST_REMOVE(head, elm, type, field) do {                      \
@@ -241,6 +246,7 @@ struct {                                                                \
          while (curelm->field.sle_next != (elm))                         \
              curelm = curelm->field.sle_next;                            \
          curelm->field.sle_next = curelm->field.sle_next->field.sle_next; \
+        (elm)->field.sle_next = NULL;                                   \
      }                                                                   \
  } while (/*CONSTCOND*/0)
 

include/qemu/queue.h struct { \ · line 315

Quoted 7 lines
include/qemu/queue.h +2 -1
@@ -304,8 +310,10 @@ struct {                                                                \
  } while (/*CONSTCOND*/0)
 
  #define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
-    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+    typeof((head)->sqh_first) elm = (head)->sqh_first;                  \
+    if (((head)->sqh_first = elm->field.sqe_next) == NULL)              \
          (head)->sqh_last = &(head)->sqh_first;                          \

Here you check elm for NULL ...

include/qemu/queue.h struct { \ · line 316

Quoted 1 line
include/qemu/queue.h +1 -0
+    elm->field.sqe_next = NULL;                                         \

... then you assign it.

Quoted 21 lines
include/qemu/queue.h +3 -0
  } while (/*CONSTCOND*/0)
 
  #define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do {            \
@@ -329,6 +337,7 @@ struct {                                                                \
          if ((curelm->field.sqe_next =                                   \
              curelm->field.sqe_next->field.sqe_next) == NULL)            \
                  (head)->sqh_last = &(curelm)->field.sqe_next;           \
+        (elm)->field.sqe_next = NULL;                                   \
      }                                                                   \
  } while (/*CONSTCOND*/0)
 
@@ -446,6 +455,8 @@ union {                                                                 \
              (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \
          (elm)->field.tqe_circ.tql_prev->tql_next = (elm)->field.tqe_next; \
          (elm)->field.tqe_circ.tql_prev = NULL;                          \
+        (elm)->field.tqe_circ.tql_next = NULL;                          \
+        (elm)->field.tqe_next = NULL;                                   \
  } while (/*CONSTCOND*/0)
 
  /* remove @left, @right and all elements in between from @head */
#3 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé

include/qemu/queue.h struct { \ · line 314

Quoted 7 lines · Stefan Hajnoczi
include/qemu/queue.h +2 -1
@@ -304,8 +310,10 @@ struct {                                                                \
  } while (/*CONSTCOND*/0)
  #define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
-    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+    typeof((head)->sqh_first) elm = (head)->sqh_first;                  \
+    if (((head)->sqh_first = elm->field.sqe_next) == NULL)              \
          (head)->sqh_last = &(head)->sqh_first;                          \

Here you check elm for NULL ...

Quoted 3 lines

include/qemu/queue.h struct { \ · line 316

Quoted 1 line
include/qemu/queue.h +1 -0
+    elm->field.sqe_next = NULL;                                         \

... then you assign it.

The sqe_next field is copied into the head. If this was the last element in the list then the head's sqh_last needs to be fixed up.

Finally we clear the linked list sqe_next pointer inside the element itself (which is no longer in the list).

Is there an issue?

Stefan

#4 lore raw

PATCH 2/2 aio-posix: remove confusing QLIST_SAFE_REMOVE()

In reply to Stefan Hajnoczi

QLIST_SAFE_REMOVE() is confusing here because the node must be on the list. We actually just wanted to clear the linked list pointers when removing it from the list. QLIST_REMOVE() now does this, so switch to it.

---
 util/aio-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/aio-posix.c +1 -1
index 7e6c9835bb..026c1f5286 100644
@@ -493,7 +493,7 @@ static bool aio_dispatch_ready_handlers(AioContext *ctx,
     AioHandler *node;
 
     while ((node = QLIST_FIRST(ready_list))) {
-        QLIST_SAFE_REMOVE(node, node_ready);
+        QLIST_REMOVE(node, node_ready);
         progress = aio_dispatch_handler(ctx, node) || progress;
     }
 
Signature · 1 line
2.24.1
#5 lore raw
In reply to Stefan Hajnoczi
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Subject: [PATCH 0/2] qemu/queue.h: clear linked list pointers on remove
Message-id: [email protected]
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
56b6529 aio-posix: remove confusing QLIST_SAFE_REMOVE() f913b24 qemu/queue.h: clear linked list pointers on remove

=== OUTPUT BEGIN ===
1/2 Checking commit f913b2430ad3 (qemu/queue.h: clear linked list pointers on remove)
ERROR: do not use assignment in if condition
#65: FILE: include/qemu/queue.h:314:
+ if (((head)->sqh_first = elm->field.sqe_next) == NULL) \

total: 1 errors, 0 warnings, 59 lines checked

Patch 1/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

2/2 Checking commit 56b6529b1894 (aio-posix: remove confusing QLIST_SAFE_REMOVE()) === OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#6 lore raw
Quoted 7 lines · On Mon, Feb 24, 2020 at 02:55:33AM -0800, [email protected] wrote:

=== OUTPUT BEGIN ===
1/2 Checking commit f913b2430ad3 (qemu/queue.h: clear linked list pointers on remove)
ERROR: do not use assignment in if condition
#65: FILE: include/qemu/queue.h:314:
+ if (((head)->sqh_first = elm->field.sqe_next) == NULL) \

total: 1 errors, 0 warnings, 59 lines checked

The same pattern is used elsewhere in this file. This code comes from BSD and doesn't comply with QEMU's coding style.

Stefan

#7 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 7 lines · On Mon, Feb 24, 2020 at 02:55:33AM -0800, [email protected] wrote:

=== OUTPUT BEGIN ===
1/2 Checking commit f913b2430ad3 (qemu/queue.h: clear linked list pointers on remove)
ERROR: do not use assignment in if condition
#65: FILE: include/qemu/queue.h:314:
+ if (((head)->sqh_first = elm->field.sqe_next) == NULL) \

total: 1 errors, 0 warnings, 59 lines checked

The same pattern is used elsewhere in this file. This code comes from BSD and doesn't comply with QEMU's coding style.

Checkpatch is right, assigning out of the if statement makes the review easier, and we can avoid the 'elm' null deref:

#define QSIMPLEQ_REMOVE_HEAD(head, field) do { \

-    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+    typeof((head)->sqh_first) elm = (head)->sqh_first; \
+    (head)->sqh_first = elm->field.sqe_next; \
+    if (elm == NULL) { \
         (head)->sqh_last = &(head)->sqh_first; \

+ } else { \
+ elm->field.sqe_next = NULL; \
+ } \
} while (/*CONSTCOND*/0)

#8 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Stefan Hajnoczi
Quoted 6 lines · On Mon, Feb 24, 2020 at 02:55:33AM -0800, [email protected] wrote:

=== OUTPUT BEGIN ===
1/2 Checking commit f913b2430ad3 (qemu/queue.h: clear linked list pointers on remove)
ERROR: do not use assignment in if condition
#65: FILE: include/qemu/queue.h:314:
+ if (((head)->sqh_first = elm->field.sqe_next) == NULL) \

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

total: 1 errors, 0 warnings, 59 lines checked

The same pattern is used elsewhere in this file. This code comes from BSD and doesn't comply with QEMU's coding style.

Checkpatch is right, assigning out of the if statement makes the review easier, and we can avoid the 'elm' null deref:

The rest of the file uses if ((a = b) == NULL), so making it inconsistent in this one instance isn't very satisfying.

Review

Quoted 5 lines

#define QSIMPLEQ_REMOVE_HEAD(head, field) do { \

(quoted diff) +3 -1
-    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+    typeof((head)->sqh_first) elm = (head)->sqh_first; \
+    (head)->sqh_first = elm->field.sqe_next; \
+    if (elm == NULL) { \

The previous line would have segfaulted if elm was NULL so this check doesn't make sense.

This macro assumes there is at least one element in the list.

The point of the check is to fix up the sqh_last pointer in the head when the final element is removed from the list.

Quoted 5 lines
         (head)->sqh_last = &(head)->sqh_first; \
(quoted diff) +3 -0
+    } else { \
+        elm->field.sqe_next = NULL; \
+    } \
 } while (/*CONSTCOND*/0)
#9 lore raw
Quoted 26 lines · Stefan Hajnoczi
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Stefan Hajnoczi
Quoted 6 lines · On Mon, Feb 24, 2020 at 02:55:33AM -0800, [email protected] wrote:

=== OUTPUT BEGIN ===
1/2 Checking commit f913b2430ad3 (qemu/queue.h: clear linked list pointers on remove)
ERROR: do not use assignment in if condition
#65: FILE: include/qemu/queue.h:314:
+ if (((head)->sqh_first = elm->field.sqe_next) == NULL) \

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

total: 1 errors, 0 warnings, 59 lines checked

The same pattern is used elsewhere in this file. This code comes from BSD and doesn't comply with QEMU's coding style.

Checkpatch is right, assigning out of the if statement makes the review easier, and we can avoid the 'elm' null deref:

The rest of the file uses if ((a = b) == NULL), so making it inconsistent in this one instance isn't very satisfying.

Review

Quoted 5 lines

#define QSIMPLEQ_REMOVE_HEAD(head, field) do { \

(quoted diff) +3 -1
-    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+    typeof((head)->sqh_first) elm = (head)->sqh_first; \
+    (head)->sqh_first = elm->field.sqe_next; \
+    if (elm == NULL) { \
Quoted 4 lines

The previous line would have segfaulted if elm was NULL so this check doesn't make sense.

This macro assumes there is at least one element in the list.

Ah good point, thanks.

Quoted 9 lines

The point of the check is to fix up the sqh_last pointer in the head when the final element is removed from the list.

Quoted 5 lines
          (head)->sqh_last = &(head)->sqh_first; \
(quoted diff) +3 -0
+    } else { \
+        elm->field.sqe_next = NULL; \
+    } \
  } while (/*CONSTCOND*/0)
author Applied
#10 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

QLIST_REMOVE() and friends leave dangling linked list pointers in the node that was removed. This makes it impossible to decide whether a node is currently in a list or not. It also makes debugging harder.

Based-on: [email protected]

          ("[PULL 00/31] Block patches")
Quoted 11 lines

Stefan Hajnoczi (2):
qemu/queue.h: clear linked list pointers on remove
aio-posix: remove confusing QLIST_SAFE_REMOVE()

 include/qemu/queue.h | 19 +++++++++++++++----
 util/aio-posix.c     |  2 +-
 2 files changed, 16 insertions(+), 5 deletions(-)
Signature · 1 line
2.24.1

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan