Skip to content

Commit 1116334

Browse files
Davidlohr Buesotorvalds
authored andcommitted
oprofile: reduce mmap_sem hold for mm->exe_file
sync_buffer() needs the mmap_sem for two distinct operations, both only occurring upon user context switch handling: 1) Dealing with the exe_file. 2) Adding the dcookie data as we need to lookup the vma that backs it. This is done via add_sample() and add_data(). This patch isolates 1), for it will no longer need the mmap_sem for serialization. However, for now, make of the more standard get_mm_exe_file(), requiring only holding the mmap_sem to read the value, and relying on reference counting to make sure that the exe file won't dissappear underneath us while doing the get dcookie. As a consequence, for 2) we move the mmap_sem locking into where we really need it, in lookup_dcookie(). The benefits are twofold: reduce mmap_sem hold times, and cleaner code. [akpm@linux-foundation.org: export get_mm_exe_file for arch/x86/oprofile/oprofile.ko] Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Cc: Robert Richter <rric@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 15beb69 commit 1116334

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎drivers/oprofile/buffer_sync.c‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* objects.
2222
*/
2323

24+
#include <linux/file.h>
2425
#include <linux/mm.h>
2526
#include <linux/workqueue.h>
2627
#include <linux/notifier.h>
@@ -224,10 +225,18 @@ static inline unsigned long fast_get_dcookie(struct path *path)
224225
static unsigned long get_exec_dcookie(struct mm_struct *mm)
225226
{
226227
unsigned long cookie = NO_COOKIE;
228+
struct file *exe_file;
227229

228-
if (mm && mm->exe_file)
229-
cookie = fast_get_dcookie(&mm->exe_file->f_path);
230+
if (!mm)
231+
goto done;
232+
233+
exe_file = get_mm_exe_file(mm);
234+
if (!exe_file)
235+
goto done;
230236

237+
cookie = fast_get_dcookie(&exe_file->f_path);
238+
fput(exe_file);
239+
done:
231240
return cookie;
232241
}
233242

@@ -236,13 +245,16 @@ static unsigned long get_exec_dcookie(struct mm_struct *mm)
236245
* pair that can then be added to the global event buffer. We make
237246
* sure to do this lookup before a mm->mmap modification happens so
238247
* we don't lose track.
248+
*
249+
* The caller must ensure the mm is not nil (ie: not a kernel thread).
239250
*/
240251
static unsigned long
241252
lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
242253
{
243254
unsigned long cookie = NO_COOKIE;
244255
struct vm_area_struct *vma;
245256

257+
down_read(&mm->mmap_sem);
246258
for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
247259

248260
if (addr < vma->vm_start || addr >= vma->vm_end)
@@ -262,6 +274,7 @@ lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
262274

263275
if (!vma)
264276
cookie = INVALID_COOKIE;
277+
up_read(&mm->mmap_sem);
265278

266279
return cookie;
267280
}
@@ -402,20 +415,9 @@ static void release_mm(struct mm_struct *mm)
402415
{
403416
if (!mm)
404417
return;
405-
up_read(&mm->mmap_sem);
406418
mmput(mm);
407419
}
408420

409-
410-
static struct mm_struct *take_tasks_mm(struct task_struct *task)
411-
{
412-
struct mm_struct *mm = get_task_mm(task);
413-
if (mm)
414-
down_read(&mm->mmap_sem);
415-
return mm;
416-
}
417-
418-
419421
static inline int is_code(unsigned long val)
420422
{
421423
return val == ESCAPE_CODE;
@@ -532,7 +534,7 @@ void sync_buffer(int cpu)
532534
new = (struct task_struct *)val;
533535
oldmm = mm;
534536
release_mm(oldmm);
535-
mm = take_tasks_mm(new);
537+
mm = get_task_mm(new);
536538
if (mm != oldmm)
537539
cookie = get_exec_dcookie(mm);
538540
add_user_ctx_switch(new, cookie);

‎kernel/fork.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ struct file *get_mm_exe_file(struct mm_struct *mm)
752752
rcu_read_unlock();
753753
return exe_file;
754754
}
755+
EXPORT_SYMBOL(get_mm_exe_file);
755756

756757
/**
757758
* get_task_mm - acquire a reference to the task's mm

0 commit comments

Comments
 (0)