LCOV - code coverage report
Current view: top level - include/linux - mmap_lock.h (source / functions) Hit Total Coverage
Test: landlock.info Lines: 64 73 87.7 %
Date: 2021-04-22 12:43:58 Functions: 14 15 93.3 %

          Line data    Source code
       1             : #ifndef _LINUX_MMAP_LOCK_H
       2             : #define _LINUX_MMAP_LOCK_H
       3             : 
       4             : #include <linux/lockdep.h>
       5             : #include <linux/mm_types.h>
       6             : #include <linux/mmdebug.h>
       7             : #include <linux/rwsem.h>
       8             : #include <linux/tracepoint-defs.h>
       9             : #include <linux/types.h>
      10             : 
      11             : #define MMAP_LOCK_INITIALIZER(name) \
      12             :         .mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock),
      13             : 
      14             : DECLARE_TRACEPOINT(mmap_lock_start_locking);
      15             : DECLARE_TRACEPOINT(mmap_lock_acquire_returned);
      16             : DECLARE_TRACEPOINT(mmap_lock_released);
      17             : 
      18             : #ifdef CONFIG_TRACING
      19             : 
      20             : void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write);
      21             : void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
      22             :                                            bool success);
      23             : void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write);
      24             : 
      25      348481 : static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
      26             :                                                    bool write)
      27             : {
      28      348481 :         if (tracepoint_enabled(mmap_lock_start_locking))
      29           0 :                 __mmap_lock_do_trace_start_locking(mm, write);
      30      348480 : }
      31             : 
      32      350048 : static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
      33             :                                                       bool write, bool success)
      34             : {
      35      350048 :         if (tracepoint_enabled(mmap_lock_acquire_returned))
      36           0 :                 __mmap_lock_do_trace_acquire_returned(mm, write, success);
      37      350043 : }
      38             : 
      39      347593 : static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
      40             : {
      41      347593 :         if (tracepoint_enabled(mmap_lock_released))
      42           0 :                 __mmap_lock_do_trace_released(mm, write);
      43      347586 : }
      44             : 
      45             : #else /* !CONFIG_TRACING */
      46             : 
      47             : static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
      48             :                                                    bool write)
      49             : {
      50             : }
      51             : 
      52             : static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
      53             :                                                       bool write, bool success)
      54             : {
      55             : }
      56             : 
      57             : static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
      58             : {
      59             : }
      60             : 
      61             : #endif /* CONFIG_TRACING */
      62             : 
      63        2019 : static inline void mmap_init_lock(struct mm_struct *mm)
      64             : {
      65        2019 :         init_rwsem(&mm->mmap_lock);
      66             : }
      67             : 
      68           2 : static inline void mmap_write_lock(struct mm_struct *mm)
      69             : {
      70           2 :         __mmap_lock_trace_start_locking(mm, true);
      71           2 :         down_write(&mm->mmap_lock);
      72           2 :         __mmap_lock_trace_acquire_returned(mm, true, true);
      73           2 : }
      74             : 
      75         858 : static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
      76             : {
      77         858 :         __mmap_lock_trace_start_locking(mm, true);
      78         858 :         down_write_nested(&mm->mmap_lock, subclass);
      79         858 :         __mmap_lock_trace_acquire_returned(mm, true, true);
      80         858 : }
      81             : 
      82       48661 : static inline int mmap_write_lock_killable(struct mm_struct *mm)
      83             : {
      84       48661 :         int ret;
      85             : 
      86       48661 :         __mmap_lock_trace_start_locking(mm, true);
      87       48661 :         ret = down_write_killable(&mm->mmap_lock);
      88       48660 :         __mmap_lock_trace_acquire_returned(mm, true, ret == 0);
      89       48661 :         return ret;
      90             : }
      91             : 
      92           0 : static inline bool mmap_write_trylock(struct mm_struct *mm)
      93             : {
      94           0 :         bool ret;
      95             : 
      96           0 :         __mmap_lock_trace_start_locking(mm, true);
      97           0 :         ret = down_write_trylock(&mm->mmap_lock) != 0;
      98           0 :         __mmap_lock_trace_acquire_returned(mm, true, ret);
      99           0 :         return ret;
     100             : }
     101             : 
     102       47947 : static inline void mmap_write_unlock(struct mm_struct *mm)
     103             : {
     104       47947 :         up_write(&mm->mmap_lock);
     105       47947 :         __mmap_lock_trace_released(mm, true);
     106       47947 : }
     107             : 
     108        1574 : static inline void mmap_write_downgrade(struct mm_struct *mm)
     109             : {
     110        1574 :         downgrade_write(&mm->mmap_lock);
     111        1574 :         __mmap_lock_trace_acquire_returned(mm, false, true);
     112        1574 : }
     113             : 
     114        2448 : static inline void mmap_read_lock(struct mm_struct *mm)
     115             : {
     116        2448 :         __mmap_lock_trace_start_locking(mm, false);
     117        2448 :         down_read(&mm->mmap_lock);
     118        2448 :         __mmap_lock_trace_acquire_returned(mm, false, true);
     119        2448 : }
     120             : 
     121        1349 : static inline int mmap_read_lock_killable(struct mm_struct *mm)
     122             : {
     123        1349 :         int ret;
     124             : 
     125        1349 :         __mmap_lock_trace_start_locking(mm, false);
     126        1349 :         ret = down_read_killable(&mm->mmap_lock);
     127        1349 :         __mmap_lock_trace_acquire_returned(mm, false, ret == 0);
     128        1349 :         return ret;
     129             : }
     130             : 
     131      295166 : static inline bool mmap_read_trylock(struct mm_struct *mm)
     132             : {
     133      295166 :         bool ret;
     134             : 
     135      295166 :         __mmap_lock_trace_start_locking(mm, false);
     136      295164 :         ret = down_read_trylock(&mm->mmap_lock) != 0;
     137      295157 :         __mmap_lock_trace_acquire_returned(mm, false, ret);
     138      295154 :         return ret;
     139             : }
     140             : 
     141      299636 : static inline void mmap_read_unlock(struct mm_struct *mm)
     142             : {
     143      299636 :         up_read(&mm->mmap_lock);
     144      299646 :         __mmap_lock_trace_released(mm, false);
     145      299638 : }
     146             : 
     147             : static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
     148             : {
     149             :         if (mmap_read_trylock(mm)) {
     150             :                 rwsem_release(&mm->mmap_lock.dep_map, _RET_IP_);
     151             :                 return true;
     152             :         }
     153             :         return false;
     154             : }
     155             : 
     156             : static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
     157             : {
     158             :         up_read_non_owner(&mm->mmap_lock);
     159             :         __mmap_lock_trace_released(mm, false);
     160             : }
     161             : 
     162          21 : static inline void mmap_assert_locked(struct mm_struct *mm)
     163             : {
     164          42 :         lockdep_assert_held(&mm->mmap_lock);
     165          21 :         VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
     166          21 : }
     167             : 
     168       26793 : static inline void mmap_assert_write_locked(struct mm_struct *mm)
     169             : {
     170       53586 :         lockdep_assert_held_write(&mm->mmap_lock);
     171       26792 :         VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
     172       26793 : }
     173             : 
     174             : static inline int mmap_lock_is_contended(struct mm_struct *mm)
     175             : {
     176             :         return rwsem_is_contended(&mm->mmap_lock);
     177             : }
     178             : 
     179             : #endif /* _LINUX_MMAP_LOCK_H */

Generated by: LCOV version 1.14