#ifndef TAIA_H #define TAIA_H #include #include #include #include #include "rnd.h" struct tai { uint64_t x; }; struct taia { struct tai sec; uint32_t nano; uint32_t atto; }; static inline void tai_pack(unsigned char *s, struct tai *t) { uint64_t x; x = t->x; s[7] = x & 255; x >>= 8; s[6] = x & 255; x >>= 8; s[5] = x & 255; x >>= 8; s[4] = x & 255; x >>= 8; s[3] = x & 255; x >>= 8; s[2] = x & 255; x >>= 8; s[1] = x & 255; x >>= 8; s[0] = x; } static inline void tai_unpack(unsigned char *s, struct tai *t) { uint64_t x; x = (unsigned char) s[0]; x <<= 8; x += (unsigned char) s[1]; x <<= 8; x += (unsigned char) s[2]; x <<= 8; x += (unsigned char) s[3]; x <<= 8; x += (unsigned char) s[4]; x <<= 8; x += (unsigned char) s[5]; x <<= 8; x += (unsigned char) s[6]; x <<= 8; x += (unsigned char) s[7]; t->x = x; } static inline void taia_pack(unsigned char *s, struct taia *t) { unsigned long x; tai_pack(s, &t->sec); s += 8; x = t->atto; s[7] = x & 255; x >>= 8; s[6] = x & 255; x >>= 8; s[5] = x & 255; x >>= 8; s[4] = x; x = t->nano; s[3] = x & 255; x >>= 8; s[2] = x & 255; x >>= 8; s[1] = x & 255; x >>= 8; s[0] = x; } static inline void taia_unpack(unsigned char *s, struct taia *t) { unsigned long x; tai_unpack(s, &t->sec); s += 8; x = (unsigned char) s[4]; x <<= 8; x += (unsigned char) s[5]; x <<= 8; x += (unsigned char) s[6]; x <<= 8; x += (unsigned char) s[7]; t->atto = x; x = (unsigned char) s[0]; x <<= 8; x += (unsigned char) s[1]; x <<= 8; x += (unsigned char) s[2]; x <<= 8; x += (unsigned char) s[3]; t->nano = x; } #define tai_unix(t, u) \ ((void) ((t)->x = 4611686018427387914ULL + \ (uint64_t) (u))) static inline void taia_now(struct taia *t) { struct timeval now; gettimeofday(&now, NULL); tai_unix(&t->sec, now.tv_sec); t->nano = 1000 * now.tv_usec + 500; /* We don't really have it, but bring some noise in. */ t->atto = secrand(); } static inline void taia_sub(struct taia *res, const struct taia *u, const struct taia *v) { unsigned long unano = u->nano; unsigned long uatto = u->atto; res->sec.x = u->sec.x - v->sec.x; res->nano = unano - v->nano; res->atto = uatto - v->atto; if (res->atto > uatto) { res->atto += 1000000000UL; --res->nano; } if (res->nano > unano) { res->nano += 1000000000UL; --res->sec.x; } } static inline void taia_add(struct taia *res, const struct taia *u, const struct taia *v) { res->sec.x = u->sec.x + v->sec.x; res->nano = u->nano + v->nano; res->atto = u->atto + v->atto; if (res->atto > 999999999UL) { res->atto -= 1000000000UL; ++res->nano; } if (res->nano > 999999999UL) { res->nano -= 1000000000UL; ++res->sec.x; } } static inline int taia_less(const struct taia *t, const struct taia *u) { if (t->sec.x < u->sec.x) return 1; if (t->sec.x > u->sec.x) return 0; if (t->nano < u->nano) return 1; if (t->nano > u->nano) return 0; return t->atto < u->atto; } extern bool taia_looks_good(struct taia *arr_taia, struct taia *pkt_taia); #endif /* TAIA_H */ lue='10'>10space:mode:
authorPaul Mackerras <paulus@ozlabs.org>2016-10-21 20:04:17 +1100
committerMichael Ellerman <mpe@ellerman.id.au>2016-10-24 19:29:47 +1100
commit09b7e37b18eecc1e347f4b1a3bc863f32801f634 (patch)
treea4dec55defb910ad1d1823c8750a45e2e8f40fbf
parent56c46222af0d09149fadec2a3ce9d4889de01cc6 (diff)
powerpc/64: Fix race condition in setting lock bit in idle/wakeup code
This fixes a race condition where one thread that is entering or leaving a power-saving state can inadvertently ignore the lock bit that was set by another thread, and potentially also clear it. The core_idle_lock_held function is called when the lock bit is seen to be set. It polls the lock bit until it is clear, then does a lwarx to load the word containing the lock bit and thread idle bits so it can be updated. However, it is possible that the value loaded with the lwarx has the lock bit set, even though an immediately preceding lwz loaded a value with the lock bit clear. If this happens then we go ahead and update the word despite the lock bit being set, and when called from pnv_enter_arch207_idle_mode, we will subsequently clear the lock bit. No identifiable misbehaviour has been attributed to this race. This fixes it by checking the lock bit in the value loaded by the lwarx. If it is set then we just go back and keep on polling. Fixes: b32aadc1a8ed ("powerpc/powernv: Fix race in updating core_idle_state") Cc: stable@vger.kernel.org # v4.2+ Signed-off-by: Paul Mackerras <paulus@ozlabs.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>