qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: "Pranith Kumar" <bobby.prani@gmail.com>,
	"Richard Henderson" <rth@twiddle.net>,
	"Emilio G. Cota" <cota@braap.org>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Sergey Fedorov" <sergey.fedorov@linaro.org>,
	"Markus Armbruster" <armbru@redhat.com>,
	"open list:All patches CC here" <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH 1/1] seqlock: Fix warning reg. incompatible cast
Date: Mon, 8 Aug 2016 11:55:26 +0200	[thread overview]
Message-ID: <561336e2-41b3-d2a8-600e-e777747cac62@redhat.com> (raw)
In-Reply-To: <f26958a6-5b37-0cf0-2166-16bb4c673fa8@redhat.com>



On 08/08/2016 11:27, Paolo Bonzini wrote:
> 
> 
> On 08/08/2016 11:05, Paolo Bonzini wrote:
>>>> /home/pranith/devops/code/qemu/include/qemu/seqlock.h:62:21: warning: passing 'typeof (*&sl->sequence) *' (aka 'const unsigned int *') to parameter of type 'unsigned int *' discards qualifier
>>>> s [-Wincompatible-pointer-types-discards-qualifiers]
>>>>     return unlikely(atomic_read(&sl->sequence) != start);
>>>>                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
>>>> /home/pranith/devops/code/qemu/include/qemu/atomic.h:58:25: note: expanded from macro 'atomic_read'
>>>>      __atomic_load(ptr, &_val, __ATOMIC_RELAXED);     \
>>>>                         ^~~~~
>>>> /home/pranith/devops/code/qemu/include/qemu/compiler.h:62:43: note: expanded from macro 'unlikely'
>>>> #define unlikely(x)   __builtin_expect(!!(x), 0)
>>>>
>>>> Signed-off-by: Pranith Kumar <bobby.prani@gmail.com>
>> This is a compiler bug, isn't it?  Atomic loads of a const pointer
>> should be allowed.
> 
> Oh, this is &_val having a const type.  That makes more sense indeed,
> but it should be worked around in qemu/atomic.h.
> 
> Can you check if this works?

Better:

diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
index 7e13fca..81cd33d 100644
--- a/include/qemu/atomic.h
+++ b/include/qemu/atomic.h
@@ -18,6 +18,26 @@
 /* Compiler barrier */
 #define barrier()   ({ asm volatile("" ::: "memory"); (void)0; })
 
+#define typeof_strip_const(expr)                                               \
+  typeof(                                                                      \
+    __builtin_choose_expr(                                                     \
+      __builtin_types_compatible_p(typeof(expr), const short) ||               \
+        __builtin_types_compatible_p(typeof(expr), short),                     \
+      (short)1,                                                                \
+      __builtin_choose_expr(                                                   \
+        __builtin_types_compatible_p(typeof(expr), const unsigned short) ||    \
+          __builtin_types_compatible_p(typeof(expr), unsigned short),          \
+        (unsigned short)1,                                                     \
+        __builtin_choose_expr(                                                 \
+          __builtin_types_compatible_p(typeof(expr), const char) ||            \
+            __builtin_types_compatible_p(typeof(expr), char),                  \
+          (char)1,                                                             \
+          __builtin_choose_expr(                                               \
+            __builtin_types_compatible_p(typeof(expr), const unsigned char) || \
+              __builtin_types_compatible_p(typeof(expr), unsigned char),       \
+            (unsigned char)1,                                                  \
+            expr+0)))))
+
 #ifdef __ATOMIC_RELAXED
 /* For C11 atomic ops */
 
@@ -54,7 +74,7 @@
 #define atomic_read(ptr)                              \
     ({                                                \
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \
-    typeof(*ptr) _val;                                \
+    typeof_strip_const(*ptr) _val;                    \
      __atomic_load(ptr, &_val, __ATOMIC_RELAXED);     \
     _val;                                             \
     })
@@ -80,7 +100,7 @@
 #define atomic_rcu_read(ptr)                          \
     ({                                                \
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \
-    typeof(*ptr) _val;                                \
+    typeof_strip_const(*ptr) _val;                    \
     atomic_rcu_read__nocheck(ptr, &_val);             \
     _val;                                             \
     })
@@ -103,7 +123,7 @@
 #define atomic_mb_read(ptr)                             \
     ({                                                  \
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *));   \
-    typeof(*ptr) _val;                                  \
+    typeof_strip_const(*ptr) _val;                      \
      __atomic_load(ptr, &_val, __ATOMIC_RELAXED);       \
      smp_rmb();                                         \
     _val;                                               \
@@ -120,7 +140,7 @@
 #define atomic_mb_read(ptr)                             \
     ({                                                  \
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *));   \
-    typeof(*ptr) _val;                                  \
+    typeof_strip_const(*ptr) _val;                      \
     __atomic_load(ptr, &_val, __ATOMIC_SEQ_CST);        \
     _val;                                               \
     })
@@ -137,7 +157,7 @@
 
 #define atomic_xchg(ptr, i)    ({                           \
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *));       \
-    typeof(*ptr) _new = (i), _old;                          \
+    typeof_strip_const(*ptr) _new = (i), _old;              \
     __atomic_exchange(ptr, &_new, &_old, __ATOMIC_SEQ_CST); \
     _old;                                                   \
 })
@@ -146,7 +166,7 @@
 #define atomic_cmpxchg(ptr, old, new)                                   \
     ({                                                                  \
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *));                   \
-    typeof(*ptr) _old = (old), _new = (new);                            \
+    typeof_strip_const(*ptr) _old = (old), _new = (new);                \
     __atomic_compare_exchange(ptr, &_old, &_new, false,                 \
                               __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);      \
     _old;                                                               \


Paolo

  reply	other threads:[~2016-08-08  9:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-07  1:41 [Qemu-devel] [PATCH 1/1] seqlock: Fix warning reg. incompatible cast Pranith Kumar
2016-08-08  9:05 ` Paolo Bonzini
2016-08-08  9:27   ` Paolo Bonzini
2016-08-08  9:55     ` Paolo Bonzini [this message]
2016-08-08 10:00       ` Richard Henderson
2016-08-08 10:02         ` Paolo Bonzini
2016-08-08 10:25           ` Richard Henderson

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=561336e2-41b3-d2a8-600e-e777747cac62@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=armbru@redhat.com \
    --cc=bobby.prani@gmail.com \
    --cc=cota@braap.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=sergey.fedorov@linaro.org \
    /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.
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 NNTP newsgroup(s).