18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * This file provides wrappers with sanitizer instrumentation for bit
58c2ecf20Sopenharmony_ci * locking operations.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * To use this functionality, an arch's bitops.h file needs to define each of
88c2ecf20Sopenharmony_ci * the below bit operations with an arch_ prefix (e.g. arch_set_bit(),
98c2ecf20Sopenharmony_ci * arch___set_bit(), etc.).
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H
128c2ecf20Sopenharmony_ci#define _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/instrumented.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/**
178c2ecf20Sopenharmony_ci * clear_bit_unlock - Clear a bit in memory, for unlock
188c2ecf20Sopenharmony_ci * @nr: the bit to set
198c2ecf20Sopenharmony_ci * @addr: the address to start counting from
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * This operation is atomic and provides release barrier semantics.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_cistatic inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	instrument_atomic_write(addr + BIT_WORD(nr), sizeof(long));
268c2ecf20Sopenharmony_ci	arch_clear_bit_unlock(nr, addr);
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/**
308c2ecf20Sopenharmony_ci * __clear_bit_unlock - Clears a bit in memory
318c2ecf20Sopenharmony_ci * @nr: Bit to clear
328c2ecf20Sopenharmony_ci * @addr: Address to start counting from
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * This is a non-atomic operation but implies a release barrier before the
358c2ecf20Sopenharmony_ci * memory operation. It can be used for an unlock if no other CPUs can
368c2ecf20Sopenharmony_ci * concurrently modify other bits in the word.
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_cistatic inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	instrument_write(addr + BIT_WORD(nr), sizeof(long));
418c2ecf20Sopenharmony_ci	arch___clear_bit_unlock(nr, addr);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * test_and_set_bit_lock - Set a bit and return its old value, for lock
468c2ecf20Sopenharmony_ci * @nr: Bit to set
478c2ecf20Sopenharmony_ci * @addr: Address to count from
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * This operation is atomic and provides acquire barrier semantics if
508c2ecf20Sopenharmony_ci * the returned value is 0.
518c2ecf20Sopenharmony_ci * It can be used to implement bit locks.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_cistatic inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	instrument_atomic_read_write(addr + BIT_WORD(nr), sizeof(long));
568c2ecf20Sopenharmony_ci	return arch_test_and_set_bit_lock(nr, addr);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#if defined(arch_clear_bit_unlock_is_negative_byte)
608c2ecf20Sopenharmony_ci/**
618c2ecf20Sopenharmony_ci * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
628c2ecf20Sopenharmony_ci *                                     byte is negative, for unlock.
638c2ecf20Sopenharmony_ci * @nr: the bit to clear
648c2ecf20Sopenharmony_ci * @addr: the address to start counting from
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * This operation is atomic and provides release barrier semantics.
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * This is a bit of a one-trick-pony for the filemap code, which clears
698c2ecf20Sopenharmony_ci * PG_locked and tests PG_waiters,
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_cistatic inline bool
728c2ecf20Sopenharmony_ciclear_bit_unlock_is_negative_byte(long nr, volatile unsigned long *addr)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	instrument_atomic_write(addr + BIT_WORD(nr), sizeof(long));
758c2ecf20Sopenharmony_ci	return arch_clear_bit_unlock_is_negative_byte(nr, addr);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci/* Let everybody know we have it. */
788c2ecf20Sopenharmony_ci#define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
798c2ecf20Sopenharmony_ci#endif
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H */
82