xref: /openbmc/linux/include/linux/mmap_lock.h (revision 132db935)
1 #ifndef _LINUX_MMAP_LOCK_H
2 #define _LINUX_MMAP_LOCK_H
3 
4 #include <linux/mmdebug.h>
5 
6 #define MMAP_LOCK_INITIALIZER(name) \
7 	.mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock),
8 
9 static inline void mmap_init_lock(struct mm_struct *mm)
10 {
11 	init_rwsem(&mm->mmap_lock);
12 }
13 
14 static inline void mmap_write_lock(struct mm_struct *mm)
15 {
16 	down_write(&mm->mmap_lock);
17 }
18 
19 static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
20 {
21 	down_write_nested(&mm->mmap_lock, subclass);
22 }
23 
24 static inline int mmap_write_lock_killable(struct mm_struct *mm)
25 {
26 	return down_write_killable(&mm->mmap_lock);
27 }
28 
29 static inline bool mmap_write_trylock(struct mm_struct *mm)
30 {
31 	return down_write_trylock(&mm->mmap_lock) != 0;
32 }
33 
34 static inline void mmap_write_unlock(struct mm_struct *mm)
35 {
36 	up_write(&mm->mmap_lock);
37 }
38 
39 static inline void mmap_write_downgrade(struct mm_struct *mm)
40 {
41 	downgrade_write(&mm->mmap_lock);
42 }
43 
44 static inline void mmap_read_lock(struct mm_struct *mm)
45 {
46 	down_read(&mm->mmap_lock);
47 }
48 
49 static inline int mmap_read_lock_killable(struct mm_struct *mm)
50 {
51 	return down_read_killable(&mm->mmap_lock);
52 }
53 
54 static inline bool mmap_read_trylock(struct mm_struct *mm)
55 {
56 	return down_read_trylock(&mm->mmap_lock) != 0;
57 }
58 
59 static inline void mmap_read_unlock(struct mm_struct *mm)
60 {
61 	up_read(&mm->mmap_lock);
62 }
63 
64 static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
65 {
66 	if (down_read_trylock(&mm->mmap_lock)) {
67 		rwsem_release(&mm->mmap_lock.dep_map, _RET_IP_);
68 		return true;
69 	}
70 	return false;
71 }
72 
73 static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
74 {
75 	up_read_non_owner(&mm->mmap_lock);
76 }
77 
78 static inline void mmap_assert_locked(struct mm_struct *mm)
79 {
80 	lockdep_assert_held(&mm->mmap_lock);
81 	VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
82 }
83 
84 static inline void mmap_assert_write_locked(struct mm_struct *mm)
85 {
86 	lockdep_assert_held_write(&mm->mmap_lock);
87 	VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
88 }
89 
90 #endif /* _LINUX_MMAP_LOCK_H */
91