1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
/*
* Copyright Red Hat
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifndef LINUX_ATOMIC_H
#define LINUX_ATOMIC_H
#include <linux/types.h>
// The atomic interfaces are chosen to exactly match those interfaces defined
// by the Linux kernel. The rest of this file is the matching user-mode
// implementation.
typedef struct {
int32_t value;
} atomic_t;
typedef struct {
int64_t value;
} atomic64_t;
#define ATOMIC_INIT(i) { (i) }
/*****************************************************************************
* Beginning of the barrier methods.
*****************************************************************************/
/**
* Stop GCC from moving memory operations across a point in the instruction
* stream. This is how the kernel uses this method.
**/
static inline void barrier(void)
{
/*
* asm volatile cannot be removed, and the memory clobber tells the
* compiler not to move memory accesses past the asm. We don't
* actually need any instructions issued on x86_64, as synchronizing
* instructions are ordered with respect to both loads and stores,
* with some irrelevant-to-us exceptions.
*/
__asm__ __volatile__("" : : : "memory");
}
/**
* Provide a memory barrier.
*
* Generate a full memory fence for the compiler and CPU. Load and store
* operations issued before the fence will not be re-ordered with operations
* issued after the fence.
*
* We also use this method in association with the __sync builtins. In earlier
* versions of GCC (at least through 4.6), the __sync operations didn't
* actually act as the memory barriers the compiler documentation says they
* should. Even as of GCC 8, it looks like the Linux kernel developers
* disagree with the compiler developers as to what constitutes a barrier at
* least on s390x, where the kernel uses explicit barriers after certain
* atomic operations and GCC does not.
*
* Rather than investigate the current status of barriers in GCC (which is an
* architecture-specific issue), and since in user mode the performance of
* these operations is not critical, we can afford to be cautious and insert
* extra barriers, until such time as we have more time to investigate and
* gain confidence in the current state of GCC barriers.
**/
static inline void smp_mb(void)
{
#if defined __x86_64__
/*
* X86 full fence. Supposedly __sync_synchronize() will do this, but
* either the GCC documentation is a lie or GCC is broken.
*
* FIXME: http://blogs.sun.com/dave/entry/atomic_fetch_and_add_vs says
* atomicAdd of zero may be a better way to spell this on current CPUs.
*/
__asm__ __volatile__("mfence" : : : "memory");
#elif defined __aarch64__
__asm__ __volatile__("dmb ish" : : : "memory");
#elif defined __s390__
__asm__ __volatile__("bcr 14,0" : : : "memory");
#elif defined __PPC__
__asm__ __volatile__("sync" : : : "memory");
#elif defined __riscv
__asm__ __volatile__("fence rw,rw" : : : "memory");
#elif defined __loongarch64
__asm__ __volatile__("dbar 0" : : : "memory");
#else
#error "no fence defined"
#endif
}
/**
* Provide a read memory barrier.
*
* Memory load operations that precede this fence will be prevented from
* changing order with any that follow this fence, by either the compiler or
* the CPU. This can be used to ensure that the load operations accessing the
* fields of a structure are not re-ordered so they actually take effect before
* a pointer to the structure is resolved.
**/
static inline void smp_rmb(void)
{
#if defined __x86_64__
// The implementation on x86 is more aggressive than necessary.
__asm__ __volatile__("lfence" : : : "memory");
#elif defined __aarch64__
__asm__ __volatile__("dmb ishld" : : : "memory");
#elif defined __s390__
__asm__ __volatile__("bcr 14,0" : : : "memory");
#elif defined __PPC__
__asm__ __volatile__("lwsync" : : : "memory");
#elif defined __riscv
__asm__ __volatile__("fence r,r" : : : "memory");
#elif defined __loongarch64
__asm__ __volatile__("dbar 0" : : : "memory");
#else
#error "no fence defined"
#endif
}
/**
* Provide a write memory barrier.
*
* Memory store operations that precede this fence will be prevented from
* changing order with any that follow this fence, by either the compiler or
* the CPU. This can be used to ensure that the store operations initializing
* the fields of a structure are not re-ordered so they actually take effect
* after a pointer to the structure is published.
**/
static inline void smp_wmb(void)
{
#if defined __x86_64__
// The implementation on x86 is more aggressive than necessary.
__asm__ __volatile__("sfence" : : : "memory");
#elif defined __aarch64__
__asm__ __volatile__("dmb ishst" : : : "memory");
#elif defined __s390__
__asm__ __volatile__("bcr 14,0" : : : "memory");
#elif defined __PPC__
__asm__ __volatile__("lwsync" : : : "memory");
#elif defined __riscv
__asm__ __volatile__("fence w,w" : : : "memory");
#elif defined __loongarch64
__asm__ __volatile__("dbar 0" : : : "memory");
#else
#error "no fence defined"
#endif
}
/**
* Provide a memory barrier before an atomic read-modify-write operation
* that does not imply one.
**/
static inline void smp_mb__before_atomic(void)
{
#if defined(__x86_64__) || defined(__s390__)
// Atomic operations are already serializing on x86 and s390
barrier();
#else
smp_mb();
#endif
}
/**
* Provide a memory barrier after an atomic read-modify-write operation
* that does not imply one.
**/
static inline void smp_mb__after_atomic(void)
{
#if defined(__x86_64__) || defined(__s390__)
// Atomic operations are already serializing on x86 and s390
barrier();
#else
smp_mb();
#endif
}
/*****************************************************************************
* Beginning of the methods for defeating compiler optimization.
*****************************************************************************/
#define READ_ONCE(x) (x)
#define WRITE_ONCE(x, val) ((x) = (val))
/*****************************************************************************
* Beginning of the 32 bit atomic support.
*****************************************************************************/
/*
* As noted above, there are a lot of explicit barriers here, in places
* where we need barriers. Ideally, GCC should just Get It Right on all the
* platforms. But there have been bugs in the past, and it looks like there
* might be one still (in GCC 8) at least on s390 (no bug report filed yet),
* and researching it may take more time than we have available before we have
* to ship. It also requires manual inspection for each platform, as there's
* no good general way to test whether the compiler gets the barriers correct.
*/
/**
* Add a signed int to a 32-bit atomic variable. The addition is atomic, but
* there are no memory barriers implied by this method.
*
* @param delta the value to be added to (or subtracted from) the variable
* @param atom a pointer to the atomic variable
**/
static inline void atomic_add(int delta, atomic_t *atom)
{
/*
* According to the kernel documentation, the addition is atomic, but there
* are no memory barriers implied by this method.
*
* The x86 implementation does do memory barriers.
*/
__sync_add_and_fetch(&atom->value, delta);
}
/**
* Add a signed int to a 32-bit atomic variable. The addition is properly
* atomic, and there are memory barriers.
*
* @param atom a pointer to the atomic variable
* @param delta the value to be added (or subtracted) from the variable
*
* @return the new value of the atom after the add operation
**/
static inline int atomic_add_return(int delta, atomic_t *atom)
{
smp_mb();
int result = __sync_add_and_fetch(&atom->value, delta);
smp_mb();
return result;
}
/**
* Compare and exchange a 32-bit atomic variable. The operation is properly
* atomic and performs a memory barrier.
*
* @param atom a pointer to the atomic variable
* @param old the value that must be present to perform the swap
* @param new the value to be swapped for the required value
*
* @return the old value
**/
static inline int atomic_cmpxchg(atomic_t *atom, int old, int new)
{
smp_mb();
int result = __sync_val_compare_and_swap(&atom->value, old, new);
smp_mb();
return result;
}
/**
* Increment a 32-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
**/
static inline void atomic_inc(atomic_t *atom)
{
/*
* According to the kernel documentation, the addition is atomic, but there
* are no memory barriers implied by this method.
*
* The x86 implementation does do memory barriers.
*/
__sync_add_and_fetch(&atom->value, 1);
}
/**
* Increment a 32-bit atomic variable. The addition is properly atomic, and
* there are memory barriers.
*
* @param atom a pointer to the atomic variable
*
* @return the new value of the atom after the increment
**/
static inline long atomic_inc_return(atomic_t *atom)
{
return atomic_add_return(1, atom);
}
/**
* Decrement a 32-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
**/
static inline void atomic_dec(atomic_t *atom)
{
/*
* According to the kernel documentation, the subtraction is atomic, but
* there are no memory barriers implied by this method.
*
* The x86 implementation does do memory barriers.
*/
__sync_sub_and_fetch(&atom->value, 1);
}
/**
* Read a 32-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
**/
static inline int atomic_read(const atomic_t *atom)
{
return READ_ONCE(atom->value);
}
/**
* Read a 32-bit atomic variable, with an acquire memory barrier.
*
* @param atom a pointer to the atomic variable
**/
static inline int atomic_read_acquire(const atomic_t *atom)
{
int value = READ_ONCE(atom->value);
smp_mb();
return value;
}
/**
* Set a 32-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
* @param value the value to set it to
**/
static inline void atomic_set(atomic_t *atom, int value)
{
atom->value = value;
}
/**
* Set a 32-bit atomic variable, with a release memory barrier.
*
* @param atom a pointer to the atomic variable
* @param value the value to set it to
**/
static inline void atomic_set_release(atomic_t *atom, int value)
{
smp_mb();
atomic_set(atom, value);
}
/*****************************************************************************
* Beginning of the 64 bit atomic support.
*****************************************************************************/
/**
* Add a signed long to a 64-bit atomic variable. The addition is atomic, but
* there are no memory barriers implied by this method.
*
* @param delta the value to be added to (or subtracted from) the variable
* @param atom a pointer to the atomic variable
**/
static inline void atomic64_add(long delta, atomic64_t *atom)
{
/*
* According to the kernel documentation, the addition is atomic, but there
* are no memory barriers implied by this method.
*
* The x86 implementation does do memory barriers.
*/
__sync_add_and_fetch(&atom->value, delta);
}
/**
* Add a signed long to a 64-bit atomic variable. The addition is properly
* atomic, and there are memory barriers.
*
* @param atom a pointer to the atomic variable
* @param delta the value to be added (or subtracted) from the variable
*
* @return the new value of the atom after the add operation
**/
static inline long atomic64_add_return(long delta, atomic64_t *atom)
{
smp_mb();
long result = __sync_add_and_fetch(&atom->value, delta);
smp_mb();
return result;
}
/**
* Compare and exchange a 64-bit atomic variable. The operation is properly
* atomic and performs a memory barrier.
*
* @param atom a pointer to the atomic variable
* @param old the value that must be present to perform the swap
* @param new the value to be swapped for the required value
*
* @return the old value
**/
static inline long atomic64_cmpxchg(atomic64_t *atom, long old, long new)
{
smp_mb();
long result = __sync_val_compare_and_swap(&atom->value, old, new);
smp_mb();
return result;
}
/**
* Increment a 64-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
**/
static inline void atomic64_inc(atomic64_t *atom)
{
/*
* According to the kernel documentation, the addition is atomic, but there
* are no memory barriers implied by this method.
*
* The x86 implementation does do memory barriers.
*/
__sync_add_and_fetch(&atom->value, 1);
}
/**
* Increment a 64-bit atomic variable. The addition is properly atomic, and
* there are memory barriers.
*
* @param atom a pointer to the atomic variable
*
* @return the new value of the atom after the increment
**/
static inline long atomic64_inc_return(atomic64_t *atom)
{
return atomic64_add_return(1, atom);
}
/**
* Read a 64-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
**/
static inline long atomic64_read(const atomic64_t *atom)
{
return READ_ONCE(atom->value);
}
/**
* Read a 64-bit atomic variable, with an acquire memory barrier.
*
* @param atom a pointer to the atomic variable
**/
static inline long atomic64_read_acquire(const atomic64_t *atom)
{
long value = READ_ONCE(atom->value);
smp_mb();
return value;
}
/**
* Set a 64-bit atomic variable, without any memory barriers.
*
* @param atom a pointer to the atomic variable
* @param value the value to set it to
**/
static inline void atomic64_set(atomic64_t *atom, long value)
{
atom->value = value;
}
/**
* Set a 64-bit atomic variable, with a release memory barrier.
*
* @param atom a pointer to the atomic variable
* @param value the value to set it to
**/
static inline void atomic64_set_release(atomic64_t *atom, long value)
{
smp_mb();
atomic64_set(atom, value);
}
/*****************************************************************************
* Generic exchange support.
*****************************************************************************/
/*
* Exchange a location's value atomically, with a full memory barrier.
*
* The location is NOT an "atomic*_t" type, but any primitive type for which
* an exchange can be done atomically. (This varies by processor, but
* generally a word-sized or pointer-sized value is supported.) As this uses a
* type-generic compiler interface, it must be implemented as a macro.
*
* @param PTR a pointer to the location to be updated
* @param NEWVAL the new value to be stored
*
* @return the old value
*/
#define xchg(PTR,NEWVAL) \
__extension__ ({ \
__typeof__(*(PTR)) __xchg_result; \
__typeof__(*(PTR)) __xchg_new_value = (NEWVAL); \
smp_mb(); /* paranoia, for old gcc bugs */ \
__xchg_result = __atomic_exchange_n((PTR), __xchg_new_value, \
__ATOMIC_SEQ_CST); \
smp_mb(); /* more paranoia */ \
__xchg_result; \
})
#endif /* LINUX_ATOMIC_H */
|