Skip to contentSkip to conversation

mmap_lock: Change trace and locking order

PATCH Applied Liam Howlett 5 participants 6 messages Sep 3, 2021 – Sep 7, 2021

Subsystems (2)

v2 of 3 Applied 1/1 Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mmap_lock: Change trace and locking order

Print to the trace log before releasing the lock to avoid racing with other trace log printers of the same lock type.

---
 include/linux/mmap_lock.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/mmap_lock.h +4 -4
index 0540f0156f58..b179f1e3541a 100644
@@ -101,14 +101,14 @@ static inline bool mmap_write_trylock(struct mm_struct *mm)
 
 static inline void mmap_write_unlock(struct mm_struct *mm)
 {
-	up_write(&mm->mmap_lock);
 	__mmap_lock_trace_released(mm, true);
+	up_write(&mm->mmap_lock);
 }
 
 static inline void mmap_write_downgrade(struct mm_struct *mm)
 {
-	downgrade_write(&mm->mmap_lock);
 	__mmap_lock_trace_acquire_returned(mm, false, true);
+	downgrade_write(&mm->mmap_lock);
 }
 
 static inline void mmap_read_lock(struct mm_struct *mm)
@@ -140,8 +140,8 @@ static inline bool mmap_read_trylock(struct mm_struct *mm)
 
 static inline void mmap_read_unlock(struct mm_struct *mm)
 {
-	up_read(&mm->mmap_lock);
 	__mmap_lock_trace_released(mm, false);
+	up_read(&mm->mmap_lock);
 }
 
 static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
@@ -155,8 +155,8 @@ static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
 
 static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
 {
-	up_read_non_owner(&mm->mmap_lock);
 	__mmap_lock_trace_released(mm, false);
+	up_read_non_owner(&mm->mmap_lock);
 }
 
 static inline void mmap_assert_locked(struct mm_struct *mm)
Signature · 1 line
2.30.2
#3 lore raw
Quoted 5 lines · On Mon, 6 Sep 2021 17:00:18 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 2 lines · Liam Howlett

Print to the trace log before releasing the lock to avoid racing with other trace log printers of the same lock type.

That description could use more details maybe?

Agreed, perhaps add something like this:

Due to the tracing of taking the mmap_lock happened outside the lock itself, the trace can become confusing, making it look like more than one task has the same lock:

     task-749     [006] ....     14437980: mmap_lock_acquire_returned: mm=00000000c94d28b8 memcg_path= write=true success=true
     task-750     [007] ....     14437981: mmap_lock_acquire_returned: mm=00000000c94d28b8 memcg_path= write=true success=true
     task-749     [006] ....     14437983: mmap_lock_released: mm=00000000c94d28b8 memcg_path= write=true

When in actuality the following occurred:

task-749 [006] - take mmap_lock
task-749 [006] - trace taking of mmap_lock
task-749 [006] - release mmap_lock

task-750 [007] - take mmap_lock
task-750 [007] - trace taking of mmap_lock

task-749 [006] - trace releasing of mmap_lock

Although the mmap_lock was taken and released then taken again by another process, the lack of protection around the tracing of the activity caused it to show the events out of order. If the tracing of the taking and releasing of the mmap_lock is done inside the lock, it will protect this from happening.

Andrew, I see you took this into your tree. Not sure if you sent it to Linus yet. Perhaps add the above to the patch if you have not yet sent it (with Liam's permission of course).

Seeing that the patch has this as a link in the commit, at the very least, the above explanation will be archived.

-- Steve

Quoted 5 lines
Quoted 3 lines
#4 lore raw
Quoted 36 lines · Steven Rostedt
Quoted 5 lines · On Mon, 6 Sep 2021 17:00:18 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 2 lines · Liam Howlett

Print to the trace log before releasing the lock to avoid racing with other trace log printers of the same lock type.

That description could use more details maybe?

Agreed, perhaps add something like this:

Due to the tracing of taking the mmap_lock happened outside the lock itself, the trace can become confusing, making it look like more than one task has the same lock:

     task-749     [006] ....     14437980: mmap_lock_acquire_returned: mm=00000000c94d28b8 memcg_path= write=true success=true
     task-750     [007] ....     14437981: mmap_lock_acquire_returned: mm=00000000c94d28b8 memcg_path= write=true success=true
     task-749     [006] ....     14437983: mmap_lock_released: mm=00000000c94d28b8 memcg_path= write=true

When in actuality the following occurred:

   task-749 [006] - take mmap_lock
   task-749 [006] - trace taking of mmap_lock
   task-749 [006] - release mmap_lock

   task-750 [007] - take mmap_lock
   task-750 [007] - trace taking of mmap_lock

   task-749 [006] - trace releasing of mmap_lock

Although the mmap_lock was taken and released then taken again by another process, the lack of protection around the tracing of the activity caused it to show the events out of order. If the tracing of the taking and releasing of the mmap_lock is done inside the lock, it will protect this from happening.

Quoted 3 lines

Andrew, I see you took this into your tree. Not sure if you sent it to Linus yet. Perhaps add the above to the patch if you have not yet sent it (with Liam's permission of course).

Yes, I agree this change is useful.

Let me know if you want a respin of the patch instead.

Thanks,
Liam

Quoted 14 lines

Seeing that the patch has this as a link in the commit, at the very least, the above explanation will be archived.

-- Steve

Quoted 5 lines
Quoted 3 lines
Reviewed-by on v2 1/1
#5 lore raw
In reply to Liam Howlett
Quoted 42 lines · Liam Howlett

Print to the trace log before releasing the lock to avoid racing with other trace log printers of the same lock type.

---
  include/linux/mmap_lock.h | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/mmap_lock.h +3 -3
index 0540f0156f58..b179f1e3541a 100644
@@ -101,14 +101,14 @@ static inline bool mmap_write_trylock(struct mm_struct *mm)
 
  static inline void mmap_write_unlock(struct mm_struct *mm)
  {
-	up_write(&mm->mmap_lock);
  	__mmap_lock_trace_released(mm, true);
+	up_write(&mm->mmap_lock);
  }
 
  static inline void mmap_write_downgrade(struct mm_struct *mm)
  {
-	downgrade_write(&mm->mmap_lock);
  	__mmap_lock_trace_acquire_returned(mm, false, true);
+	downgrade_write(&mm->mmap_lock);
  }
 
  static inline void mmap_read_lock(struct mm_struct *mm)
@@ -140,8 +140,8 @@ static inline bool mmap_read_trylock(struct mm_struct *mm)
 
  static inline void mmap_read_unlock(struct mm_struct *mm)
  {
-	up_read(&mm->mmap_lock);
  	__mmap_lock_trace_released(mm, false);
+	up_read(&mm->mmap_lock);
  }
 
  static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)

include/linux/mmap_lock.h static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm) · line 162

Quoted 9 lines
include/linux/mmap_lock.h +1 -1
@@ -155,8 +155,8 @@ static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
 
  static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
  {
-	up_read_non_owner(&mm->mmap_lock);
  	__mmap_lock_trace_released(mm, false);
+	up_read_non_owner(&mm->mmap_lock);
  }
 
  static inline void mmap_assert_locked(struct mm_struct *mm)
Signature · 3 lines
Thanks,

David / dhildenb