Path: blob/master/include/asm-generic/bitops/instrumented-non-atomic.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */12/*3* This file provides wrappers with sanitizer instrumentation for non-atomic4* bit operations.5*6* To use this functionality, an arch's bitops.h file needs to define each of7* the below bit operations with an arch_ prefix (e.g. arch_set_bit(),8* arch___set_bit(), etc.).9*/10#ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H11#define _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H1213#include <linux/instrumented.h>1415/**16* ___set_bit - Set a bit in memory17* @nr: the bit to set18* @addr: the address to start counting from19*20* Unlike set_bit(), this function is non-atomic. If it is called on the same21* region of memory concurrently, the effect may be that only one operation22* succeeds.23*/24static __always_inline void25___set_bit(unsigned long nr, volatile unsigned long *addr)26{27instrument_write(addr + BIT_WORD(nr), sizeof(long));28arch___set_bit(nr, addr);29}3031/**32* ___clear_bit - Clears a bit in memory33* @nr: the bit to clear34* @addr: the address to start counting from35*36* Unlike clear_bit(), this function is non-atomic. If it is called on the same37* region of memory concurrently, the effect may be that only one operation38* succeeds.39*/40static __always_inline void41___clear_bit(unsigned long nr, volatile unsigned long *addr)42{43instrument_write(addr + BIT_WORD(nr), sizeof(long));44arch___clear_bit(nr, addr);45}4647/**48* ___change_bit - Toggle a bit in memory49* @nr: the bit to change50* @addr: the address to start counting from51*52* Unlike change_bit(), this function is non-atomic. If it is called on the same53* region of memory concurrently, the effect may be that only one operation54* succeeds.55*/56static __always_inline void57___change_bit(unsigned long nr, volatile unsigned long *addr)58{59instrument_write(addr + BIT_WORD(nr), sizeof(long));60arch___change_bit(nr, addr);61}6263static __always_inline void __instrument_read_write_bitop(long nr, volatile unsigned long *addr)64{65if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC)) {66/*67* We treat non-atomic read-write bitops a little more special.68* Given the operations here only modify a single bit, assuming69* non-atomicity of the writer is sufficient may be reasonable70* for certain usage (and follows the permissible nature of the71* assume-plain-writes-atomic rule):72* 1. report read-modify-write races -> check read;73* 2. do not report races with marked readers, but do report74* races with unmarked readers -> check "atomic" write.75*/76kcsan_check_read(addr + BIT_WORD(nr), sizeof(long));77/*78* Use generic write instrumentation, in case other sanitizers79* or tools are enabled alongside KCSAN.80*/81instrument_write(addr + BIT_WORD(nr), sizeof(long));82} else {83instrument_read_write(addr + BIT_WORD(nr), sizeof(long));84}85}8687/**88* ___test_and_set_bit - Set a bit and return its old value89* @nr: Bit to set90* @addr: Address to count from91*92* This operation is non-atomic. If two instances of this operation race, one93* can appear to succeed but actually fail.94*/95static __always_inline bool96___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)97{98__instrument_read_write_bitop(nr, addr);99return arch___test_and_set_bit(nr, addr);100}101102/**103* ___test_and_clear_bit - Clear a bit and return its old value104* @nr: Bit to clear105* @addr: Address to count from106*107* This operation is non-atomic. If two instances of this operation race, one108* can appear to succeed but actually fail.109*/110static __always_inline bool111___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)112{113__instrument_read_write_bitop(nr, addr);114return arch___test_and_clear_bit(nr, addr);115}116117/**118* ___test_and_change_bit - Change a bit and return its old value119* @nr: Bit to change120* @addr: Address to count from121*122* This operation is non-atomic. If two instances of this operation race, one123* can appear to succeed but actually fail.124*/125static __always_inline bool126___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)127{128__instrument_read_write_bitop(nr, addr);129return arch___test_and_change_bit(nr, addr);130}131132/**133* _test_bit - Determine whether a bit is set134* @nr: bit number to test135* @addr: Address to start counting from136*/137static __always_inline bool138_test_bit(unsigned long nr, const volatile unsigned long *addr)139{140instrument_atomic_read(addr + BIT_WORD(nr), sizeof(long));141return arch_test_bit(nr, addr);142}143144/**145* _test_bit_acquire - Determine, with acquire semantics, whether a bit is set146* @nr: bit number to test147* @addr: Address to start counting from148*/149static __always_inline bool150_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)151{152instrument_atomic_read(addr + BIT_WORD(nr), sizeof(long));153return arch_test_bit_acquire(nr, addr);154}155156#endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H */157158159