kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandru Elisei <alexandru.elisei@arm.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, rkrcmar@redhat.com, drjones@redhat.com,
	maz@kernel.org, andre.przywara@arm.com, vladimir.murzin@arm.com,
	mark.rutland@arm.com, Laurent Vivier <lvivier@redhat.com>,
	Thomas Huth <thuth@redhat.com>,
	David Hildenbrand <david@redhat.com>
Subject: [kvm-unit-tests PATCH v2 03/18] lib: Add WRITE_ONCE and READ_ONCE implementations in compiler.h
Date: Thu, 28 Nov 2019 18:04:03 +0000	[thread overview]
Message-ID: <20191128180418.6938-4-alexandru.elisei@arm.com> (raw)
In-Reply-To: <20191128180418.6938-1-alexandru.elisei@arm.com>

Add the WRITE_ONCE and READ_ONCE macros which are used to prevent to
prevent the compiler from optimizing a store or a load, respectively, into
something else.

Cc: Drew Jones <drjones@redhat.com>
Cc: Laurent Vivier <lvivier@redhat.com>
Cc: Thomas Huth <thuth@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
---
 lib/linux/compiler.h | 81 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 lib/linux/compiler.h

diff --git a/lib/linux/compiler.h b/lib/linux/compiler.h
new file mode 100644
index 000000000000..aac84c1d711c
--- /dev/null
+++ b/lib/linux/compiler.h
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Taken from tools/include/linux/compiler.h, with minor changes. */
+#ifndef __LINUX_COMPILER_H
+#define __LINUX_COMPILER_H
+
+#ifndef __ASSEMBLY__
+
+#include <stdint.h>
+
+#define barrier()	asm volatile("" : : : "memory")
+
+#define __always_inline	inline __attribute__((always_inline))
+
+static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
+{
+	switch (size) {
+	case 1: *(uint8_t *)res = *(volatile uint8_t *)p; break;
+	case 2: *(uint16_t *)res = *(volatile uint16_t *)p; break;
+	case 4: *(uint32_t *)res = *(volatile uint32_t *)p; break;
+	case 8: *(uint64_t *)res = *(volatile uint64_t *)p; break;
+	default:
+		barrier();
+		__builtin_memcpy((void *)res, (const void *)p, size);
+		barrier();
+	}
+}
+
+/*
+ * Prevent the compiler from merging or refetching reads or writes. The
+ * compiler is also forbidden from reordering successive instances of
+ * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
+ * particular ordering. One way to make the compiler aware of ordering is to
+ * put the two invocations of READ_ONCE or WRITE_ONCE in different C
+ * statements.
+ *
+ * These two macros will also work on aggregate data types like structs or
+ * unions. If the size of the accessed data type exceeds the word size of
+ * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
+ * fall back to memcpy(). There's at least two memcpy()s: one for the
+ * __builtin_memcpy() and then one for the macro doing the copy of variable
+ * - '__u' allocated on the stack.
+ *
+ * Their two major use cases are: (1) Mediating communication between
+ * process-level code and irq/NMI handlers, all running on the same CPU,
+ * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
+ * mutilate accesses that either do not require ordering or that interact
+ * with an explicit memory barrier or atomic instruction that provides the
+ * required ordering.
+ */
+#define READ_ONCE(x)					\
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__c = { 0 } };			\
+	__read_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
+
+static __always_inline void __write_once_size(volatile void *p, void *res, int size)
+{
+	switch (size) {
+	case 1: *(volatile uint8_t *)p = *(uint8_t *)res; break;
+	case 2: *(volatile uint16_t *)p = *(uint16_t *)res; break;
+	case 4: *(volatile uint32_t *)p = *(uint32_t *)res; break;
+	case 8: *(volatile uint64_t *)p = *(uint64_t *)res; break;
+	default:
+		barrier();
+		__builtin_memcpy((void *)p, (const void *)res, size);
+		barrier();
+	}
+}
+
+#define WRITE_ONCE(x, val) \
+({							\
+	union { typeof(x) __val; char __c[1]; } __u =	\
+		{ .__val = (typeof(x)) (val) }; 	\
+	__write_once_size(&(x), __u.__c, sizeof(x));	\
+	__u.__val;					\
+})
+
+#endif /* !__ASSEMBLY__ */
+#endif /* !__LINUX_COMPILER_H */
-- 
2.20.1


  parent reply	other threads:[~2019-11-28 18:04 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-28 18:04 [kvm-unit-tests PATCH v2 00/18] arm/arm64: Various fixes Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 01/18] lib: arm/arm64: Remove unnecessary dcache maintenance operations Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 02/18] lib: arm64: Remove barriers before TLB operations Alexandru Elisei
2019-11-28 18:04 ` Alexandru Elisei [this message]
2019-12-09 14:21   ` [kvm-unit-tests PATCH v2 03/18] lib: Add WRITE_ONCE and READ_ONCE implementations in compiler.h Thomas Huth
2019-12-16 10:15     ` Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 04/18] lib: arm/arm64: Use WRITE_ONCE to update the translation tables Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 05/18] lib: arm/arm64: Remove unused CPU_OFF parameter Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 06/18] arm/arm64: psci: Don't run C code without stack or vectors Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 07/18] lib: arm/arm64: Add missing include for alloc_page.h in pgtable.h Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 08/18] lib: arm: Implement flush_tlb_all Alexandru Elisei
2019-11-28 23:24   ` André Przywara
2019-12-30  8:50     ` Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 09/18] lib: arm/arm64: Teach mmu_clear_user about block mappings Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 10/18] arm/arm64: selftest: Add prefetch abort test Alexandru Elisei
2019-12-13 18:04   ` Andrew Jones
2019-12-30  9:19     ` Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 11/18] arm64: timer: Write to ICENABLER to disable timer IRQ Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 12/18] arm64: timer: EOIR the interrupt after masking the timer Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 13/18] arm64: timer: Test behavior when timer disabled or masked Alexandru Elisei
2019-12-13 18:28   ` Andrew Jones
2019-12-30  9:21     ` Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 14/18] lib: arm/arm64: Refuse to disable the MMU with non-identity stack pointer Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 15/18] arm/arm64: Perform dcache clean + invalidate after turning MMU off Alexandru Elisei
2019-12-13 18:42   ` Andrew Jones
2019-12-30  9:29     ` Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 16/18] arm: cstart64.S: Downgrade TLBI to non-shareable in asm_mmu_enable Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 17/18] arm/arm64: Invalidate TLB before enabling MMU Alexandru Elisei
2019-11-28 18:04 ` [kvm-unit-tests PATCH v2 18/18] arm: cstart64.S: Remove icache invalidation from asm_mmu_enable Alexandru Elisei
2019-12-13 18:51 ` [kvm-unit-tests PATCH v2 00/18] arm/arm64: Various fixes Andrew Jones

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=20191128180418.6938-4-alexandru.elisei@arm.com \
    --to=alexandru.elisei@arm.com \
    --cc=andre.przywara@arm.com \
    --cc=david@redhat.com \
    --cc=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=thuth@redhat.com \
    --cc=vladimir.murzin@arm.com \
    /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).