LCOV - code coverage report
Current view: top level - include/linux - mmap_lock.h (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 47 50 94.0 %
Date: 2023-07-31 20:08:12 Functions: 10 10 100.0 %

          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   282070352 : static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
      26             :                                                    bool write)
      27             : {
      28   282070352 :         if (tracepoint_enabled(mmap_lock_start_locking))
      29           0 :                 __mmap_lock_do_trace_start_locking(mm, write);
      30   282110389 : }
      31             : 
      32   321371602 : static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
      33             :                                                       bool write, bool success)
      34             : {
      35   321371602 :         if (tracepoint_enabled(mmap_lock_acquire_returned))
      36           0 :                 __mmap_lock_do_trace_acquire_returned(mm, write, success);
      37   321371098 : }
      38             : 
      39   289316084 : static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
      40             : {
      41   289316084 :         if (tracepoint_enabled(mmap_lock_released))
      42           0 :                 __mmap_lock_do_trace_released(mm, write);
      43   289313822 : }
      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             : static inline void mmap_assert_locked(struct mm_struct *mm)
      64             : {
      65             :         lockdep_assert_held(&mm->mmap_lock);
      66             :         VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
      67             : }
      68             : 
      69             : static inline void mmap_assert_write_locked(struct mm_struct *mm)
      70             : {
      71             :         lockdep_assert_held_write(&mm->mmap_lock);
      72             :         VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
      73             : }
      74             : 
      75             : #ifdef CONFIG_PER_VMA_LOCK
      76             : static inline void vma_end_write_all(struct mm_struct *mm)
      77             : {
      78   140883361 :         mmap_assert_write_locked(mm);
      79             :         /* No races during update due to exclusive mmap_lock being held */
      80   140883361 :         WRITE_ONCE(mm->mm_lock_seq, mm->mm_lock_seq + 1);
      81             : }
      82             : #else
      83             : static inline void vma_end_write_all(struct mm_struct *mm) {}
      84             : #endif
      85             : 
      86             : static inline void mmap_init_lock(struct mm_struct *mm)
      87             : {
      88             :         init_rwsem(&mm->mmap_lock);
      89             : }
      90             : 
      91    39262550 : static inline void mmap_write_lock(struct mm_struct *mm)
      92             : {
      93    39262550 :         __mmap_lock_trace_start_locking(mm, true);
      94    39265119 :         down_write(&mm->mmap_lock);
      95    39267563 :         __mmap_lock_trace_acquire_returned(mm, true, true);
      96    39266236 : }
      97             : 
      98             : static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
      99             : {
     100             :         __mmap_lock_trace_start_locking(mm, true);
     101             :         down_write_nested(&mm->mmap_lock, subclass);
     102             :         __mmap_lock_trace_acquire_returned(mm, true, true);
     103             : }
     104             : 
     105   101682042 : static inline int mmap_write_lock_killable(struct mm_struct *mm)
     106             : {
     107   101682042 :         int ret;
     108             : 
     109   101682042 :         __mmap_lock_trace_start_locking(mm, true);
     110   101679253 :         ret = down_write_killable(&mm->mmap_lock);
     111   101690470 :         __mmap_lock_trace_acquire_returned(mm, true, ret == 0);
     112   101686919 :         return ret;
     113             : }
     114             : 
     115             : static inline bool mmap_write_trylock(struct mm_struct *mm)
     116             : {
     117             :         bool ret;
     118             : 
     119             :         __mmap_lock_trace_start_locking(mm, true);
     120             :         ret = down_write_trylock(&mm->mmap_lock) != 0;
     121             :         __mmap_lock_trace_acquire_returned(mm, true, ret);
     122             :         return ret;
     123             : }
     124             : 
     125   101654428 : static inline void mmap_write_unlock(struct mm_struct *mm)
     126             : {
     127   101654428 :         __mmap_lock_trace_released(mm, true);
     128   101630289 :         vma_end_write_all(mm);
     129   101630289 :         up_write(&mm->mmap_lock);
     130   101651002 : }
     131             : 
     132    39257515 : static inline void mmap_write_downgrade(struct mm_struct *mm)
     133             : {
     134    39257515 :         __mmap_lock_trace_acquire_returned(mm, false, true);
     135    39253072 :         vma_end_write_all(mm);
     136    39253072 :         downgrade_write(&mm->mmap_lock);
     137    39253152 : }
     138             : 
     139   109015920 : static inline void mmap_read_lock(struct mm_struct *mm)
     140             : {
     141   109015920 :         __mmap_lock_trace_start_locking(mm, false);
     142   109007580 :         down_read(&mm->mmap_lock);
     143   109018619 :         __mmap_lock_trace_acquire_returned(mm, false, true);
     144   109012138 : }
     145             : 
     146    32166299 : static inline int mmap_read_lock_killable(struct mm_struct *mm)
     147             : {
     148    32166299 :         int ret;
     149             : 
     150    32166299 :         __mmap_lock_trace_start_locking(mm, false);
     151    32162603 :         ret = down_read_killable(&mm->mmap_lock);
     152    32165392 :         __mmap_lock_trace_acquire_returned(mm, false, ret == 0);
     153    32161588 :         return ret;
     154             : }
     155             : 
     156             : static inline bool mmap_read_trylock(struct mm_struct *mm)
     157             : {
     158             :         bool ret;
     159             : 
     160             :         __mmap_lock_trace_start_locking(mm, false);
     161             :         ret = down_read_trylock(&mm->mmap_lock) != 0;
     162             :         __mmap_lock_trace_acquire_returned(mm, false, ret);
     163             :         return ret;
     164             : }
     165             : 
     166   187692005 : static inline void mmap_read_unlock(struct mm_struct *mm)
     167             : {
     168   187692005 :         __mmap_lock_trace_released(mm, false);
     169   187674157 :         up_read(&mm->mmap_lock);
     170   187688067 : }
     171             : 
     172             : static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
     173             : {
     174             :         __mmap_lock_trace_released(mm, false);
     175             :         up_read_non_owner(&mm->mmap_lock);
     176             : }
     177             : 
     178             : static inline int mmap_lock_is_contended(struct mm_struct *mm)
     179             : {
     180             :         return rwsem_is_contended(&mm->mmap_lock);
     181             : }
     182             : 
     183             : #endif /* _LINUX_MMAP_LOCK_H */

Generated by: LCOV version 1.14