docs/rcu: Distinguish rcu_dereference and atomic_rcu_read
PATCH 1/2 docs/rcu: Distinguish rcu_dereference and atomic_rcu_read
--- docs/rcu.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
docs/rcu.txt +3 -1
@@ -197,7 +197,9 @@ DIFFERENCES WITH LINUX critical section to become an updater. - atomic_rcu_read and atomic_rcu_set replace rcu_dereference and - rcu_assign_pointer. They take a _pointer_ to the variable being accessed. + rcu_assign_pointer. Note that although both atomic_rcu_read and + rcu_dereference take a _pointer_ to the variable being accessed, + atomic_rcu_read dereferences the pointer whereas rcu_dereference does not. - call_rcu is a macro that has an extra argument (the name of the first field in the struct, which must be a struct rcu_head), and expects the
Signature · 1 line
2.10.1
PATCH 2/2 translate-all: Use proper type
gcc does not warn about the wrong type since it is a void pointer which can be cast to any type.
--- translate-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
translate-all.c +1 -1
@@ -412,7 +412,7 @@ static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)/* Level 2..N-1. */ for (i =V_L1_SHIFT /V_L2_BITS -1 ; i >0 ; i--) { -void **p =atomic_rcu_read (lp); +void *p =atomic_rcu_read (lp);if (p ==NULL ) {if (!alloc) {
Signature · 1 line
2.10.1
Re: [Qemu-devel] [PATCH 2/2] translate-all: Use proper type
Quoted 13 lines · Pranith Kumar
gcc does not warn about the wrong type since it is a void pointer which can be cast to any type.
Signed-off-by Pranith Kumar <[email protected]>--- translate-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)translate-all.c +0
-0
translate-all.c static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) · line 418
Quoted 8 lines
translate-all.c +1
-1@@ -412,7 +412,7 @@ static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)/* Level 2..N-1. */ for (i =V_L1_SHIFT /V_L2_BITS -1 ; i >0 ; i--) { -void **p =atomic_rcu_read (lp); +void *p =atomic_rcu_read (lp);if (p ==NULL ) {if (!alloc) {
Let me redo your patch with more context (for patches like this using format-patch -U<n> is useful):
$ git diff -U11 translate-all.c
translate-all.c +1 -1
@@ -405,23 +405,23 @@ static void page_init(void)static PageDesc *page_find_alloc (tb_page_addr_t index,int alloc) {PageDesc *pd;void **lp;int i;/* Level 1. Always allocated. */ lp = l1_map + ((index >>V_L1_SHIFT ) & (V_L1_SIZE -1 ));/* Level 2..N-1. */ for (i =V_L1_SHIFT /V_L2_BITS -1 ; i >0 ; i--) { -void **p =atomic_rcu_read (lp); +void *p =atomic_rcu_read (lp);if (p ==NULL ) {if (!alloc) {return NULL ; } p = g_new0(void *, V_L2_SIZE);atomic_rcu_set (lp, p); } lp = p + ((index >> (i *V_L2_BITS )) & (V_L2_SIZE -1 )); }
I prefer void **p since that matches lp's and l1_map's type.
It's true that since we're dealing with void * the compiler won't complain either way.
Emilio
Re: [Qemu-devel] [PATCH 2/2] translate-all: Use proper type
Quoted 15 lines · Emilio G. Cota
+++
b/translate-all.ctranslate-all.c +1
-1@@ -405,23 +405,23 @@ static void page_init(void)static PageDesc *page_find_alloc (tb_page_addr_t index,int alloc) {PageDesc *pd;void **lp;int i;/* Level 1. Always allocated. */ lp = l1_map + ((index >>V_L1_SHIFT ) & (V_L1_SIZE -1 ));/* Level 2..N-1. */ for (i =V_L1_SHIFT /V_L2_BITS -1 ; i >0 ; i--) { -void **p =atomic_rcu_read (lp); +void *p =atomic_rcu_read (lp);
translate-all.c static void page_init(void) · line 426
Quoted 10 lines
translate-all.c +0
-0if (p ==NULL ) {if (!alloc) {return NULL ; } p = g_new0(void *, V_L2_SIZE);atomic_rcu_set (lp, p); } lp = p + ((index >> (i *V_L2_BITS )) & (V_L2_SIZE -1 ));
Pointer addition of 'void *' plus an offset is undefined (gcc, and presumably clang, have an extension that treats it the same as computing an offset to a 'char *'; but some compilers choke); this is because sizeof(void) is unknown, so you don't know what stride to make for each offset. Or put another way, 'p + offset' is the same as '&((*p)[offset])', but (*p)[offset] is ill-defined when p is the opaque type void.
Pointer addition of 'void **' plus an offset is well-defined, because sizeof(void*) is well-defined and therefore the stride (4 or 8) makes sense. Or in array notation, computing '&((*p)[offset]) means we are skipping to the offset array entry where p is the start of the array of void* pointers.
In that regards, changing the type of p from 'void **' (where you stride by the size of a pointer when computing lp) to 'void *' (where you stride by 1 under gcc when computing lp) is WRONG.
Quoted 1 line
I prefer void **p since that matches lp's and l1_map's type.
Not just prefer, but require.
Quoted 3 lines
It's true that since we're dealing with void * the compiler won't complain either way.
It's a shame that void* relaxes typing so much, but this is one case where we HAVE to use the right type.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 2/2] translate-all: Use proper type
Quoted 35 lines · Eric Blake
Quoted 15 lines · Emilio G. Cota
+++
b/translate-all.ctranslate-all.c +1
-1@@ -405,23 +405,23 @@ static void page_init(void)static PageDesc *page_find_alloc (tb_page_addr_t index,int alloc) {PageDesc *pd;void **lp;int i;/* Level 1. Always allocated. */ lp = l1_map + ((index >>V_L1_SHIFT ) & (V_L1_SIZE -1 ));/* Level 2..N-1. */ for (i =V_L1_SHIFT /V_L2_BITS -1 ; i >0 ; i--) { -void **p =atomic_rcu_read (lp); +void *p =atomic_rcu_read (lp);translate-all.c static void page_init(void) · line 426
Quoted 10 lines
translate-all.c +0
-0if (p ==NULL ) {if (!alloc) {return NULL ; } p = g_new0(void *, V_L2_SIZE);atomic_rcu_set (lp, p); } lp = p + ((index >> (i *V_L2_BITS )) & (V_L2_SIZE -1 ));Pointer addition of 'void *' plus an offset is undefined (gcc, and presumably clang, have an extension that treats it the same as computing an offset to a 'char *'; but some compilers choke); this is because sizeof(void) is unknown, so you don't know what stride to make for each offset. Or put another way, 'p + offset' is the same as '&((*p)[offset])', but (*p)[offset] is ill-defined when p is the opaque type void.
Quoted 6 lines
Pointer addition of 'void **' plus an offset is well-defined, because
sizeof(void*)is well-defined and therefore the stride (4 or 8) makes sense. Or in array notation, computing '&((*p)[offset]) means we are skipping to the offset array entry where p is the start of the array of void* pointers.
Indeed. I missed that crucial detail. I would prefer explicitly casting to 'void **' for p, since that is not the type of what is being returned by atomic_rcu_read().
The joys of void pointer arithmetic, TIL.
Signature · 1 line
Pranith
----- Original Message -----
Quoted 16 lines
From: "Pranith Kumar" <[email protected]>
To: "Paolo Bonzini" <[email protected]>, "Sergey Fedorov" <[email protected]>, "Cao jin" <[email protected]>, "open list:All patches CC here" <[email protected]>
Sent: Tuesday, October 18, 2016 4:56:19 PM
Subject: [PATCH 1/2] docs/rcu: Distinguishrcu_dereferenceandatomic_rcu_readSigned-off-by Pranith Kumar <[email protected]>--- docs/rcu.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)docs/rcu.txt +0
-0
docs/rcu.txt DIFFERENCES WITH LINUX · line 202
Quoted 7 lines
docs/rcu.txt +3
-1@@ -197,7 +197,9 @@ DIFFERENCES WITH LINUX critical section to become an updater. - atomic_rcu_read and atomic_rcu_set replace rcu_dereference and - rcu_assign_pointer. They take a _pointer_ to the variable being accessed. + rcu_assign_pointer. Note that although both atomic_rcu_read and + rcu_dereference take a _pointer_ to the variable being accessed, + atomic_rcu_read dereferences the pointer whereas rcu_dereference does not.
No, neither rcu_dereference nor rcu-assign_pointer take a pointer. You use them like rcu_dereference(p) versus QEMU's atomic_rcu_read(&p).
Paolo
Quoted 6 lines
docs/rcu.txt +0
-1- call_rcu is a macro that has an extra argument (the name of the first field in the struct, which must be a struct rcu_head), and expects the --2.10.1
Re: [Qemu-devel] [PATCH 2/2] translate-all: Use proper type
In reply to Pranith KumarQuoted 13 lines · Pranith Kumar
gcc does not warn about the wrong type since it is a void pointer which can be cast to any type.
Signed-off-by Pranith Kumar <[email protected]>--- translate-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)translate-all.c +0
-0
translate-all.c static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) · line 415
Quoted 5 lines
translate-all.c +1
-1@@ -412,7 +412,7 @@ static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)/* Level 2..N-1. */ for (i =V_L1_SHIFT /V_L2_BITS -1 ; i >0 ; i--) { -void **p =atomic_rcu_read (lp); +void *p =atomic_rcu_read (lp);
Wrong; you can see below that p is initialized with
p = g_new0(void *, V_L2_SIZE);
so it must be a pointer to "void *". You are introducing exactly the bug that is mentioned in the commit message, and it would have screwed up this statement:
lp = p + ((index >> (i * V_L2_BITS)) & (V_L2_SIZE - 1));
because it would then omit the multiplication of the RHS by sizeof(void *).
How did you test the patch? Coverity would have caught this, but please be more careful.
Thanks,
Paolo
Quoted 3 lines
translate-all.c +0
-0if (p ==NULL ) {if (!alloc) {