aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavidlohr Bueso <dave@stgolabs.net>2015-04-16 15:49:12 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2015-04-17 09:04:11 -0400
commit11163348a23cdbcdca5fb42485418e75f8566a5c (patch)
tree591adeddbcc89b51e047223b60b45500764fcc89
parent15beb694c66146e1133b9ff81d54e3ef3daa1d7c (diff)
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>
-rw-r--r--drivers/oprofile/buffer_sync.c30
-rw-r--r--kernel/fork.c1
2 files changed, 17 insertions, 14 deletions
diff --git a/drivers/oprofile/buffer_sync.c b/drivers/oprofile/buffer_sync.c
index d93b2b6b1f7a..82f7000a285d 100644
--- a/drivers/oprofile/buffer_sync.c
+++ b/drivers/oprofile/buffer_sync.c
@@ -21,6 +21,7 @@
21 * objects. 21 * objects.
22 */ 22 */
23 23
24#include <linux/file.h>
24#include <linux/mm.h> 25#include <linux/mm.h>
25#include <linux/workqueue.h> 26#include <linux/workqueue.h>
26#include <linux/notifier.h> 27#include <linux/notifier.h>
@@ -224,10 +225,18 @@ static inline unsigned long fast_get_dcookie(struct path *path)
224static unsigned long get_exec_dcookie(struct mm_struct *mm) 225static unsigned long get_exec_dcookie(struct mm_struct *mm)
225{ 226{
226 unsigned long cookie = NO_COOKIE; 227 unsigned long cookie = NO_COOKIE;
228 struct file *exe_file;
227 229
228 if (mm && mm->exe_file) 230 if (!mm)
229 cookie = fast_get_dcookie(&mm->exe_file->f_path); 231 goto done;
232
233 exe_file = get_mm_exe_file(mm);
234 if (!exe_file)
235 goto done;
230 236
237 cookie = fast_get_dcookie(&exe_file->f_path);
238 fput(exe_file);
239done:
231 return cookie; 240 return cookie;
232} 241}
233 242
@@ -236,6 +245,8 @@ static unsigned long get_exec_dcookie(struct mm_struct *mm)
236 * pair that can then be added to the global event buffer. We make 245 * pair that can then be added to the global event buffer. We make
237 * sure to do this lookup before a mm->mmap modification happens so 246 * sure to do this lookup before a mm->mmap modification happens so
238 * we don't lose track. 247 * we don't lose track.
248 *
249 * The caller must ensure the mm is not nil (ie: not a kernel thread).
239 */ 250 */
240static unsigned long 251static unsigned long
241lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset) 252lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
@@ -243,6 +254,7 @@ lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
243 unsigned long cookie = NO_COOKIE; 254 unsigned long cookie = NO_COOKIE;
244 struct vm_area_struct *vma; 255 struct vm_area_struct *vma;
245 256
257 down_read(&mm->mmap_sem);
246 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) { 258 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
247 259
248 if (addr < vma->vm_start || addr >= vma->vm_end) 260 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)
262 274
263 if (!vma) 275 if (!vma)
264 cookie = INVALID_COOKIE; 276 cookie = INVALID_COOKIE;
277 up_read(&mm->mmap_sem);
265 278
266 return cookie; 279 return cookie;
267} 280}
@@ -402,20 +415,9 @@ static void release_mm(struct mm_struct *mm)
402{ 415{
403 if (!mm) 416 if (!mm)
404 return; 417 return;
405 up_read(&mm->mmap_sem);
406 mmput(mm); 418 mmput(mm);
407} 419}
408 420
409
410static 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
419static inline int is_code(unsigned long val) 421static inline int is_code(unsigned long val)
420{ 422{
421 return val == ESCAPE_CODE; 423 return val == ESCAPE_CODE;
@@ -532,7 +534,7 @@ void sync_buffer(int cpu)
532 new = (struct task_struct *)val; 534 new = (struct task_struct *)val;
533 oldmm = mm; 535 oldmm = mm;
534 release_mm(oldmm); 536 release_mm(oldmm);
535 mm = take_tasks_mm(new); 537 mm = get_task_mm(new);
536 if (mm != oldmm) 538 if (mm != oldmm)
537 cookie = get_exec_dcookie(mm); 539 cookie = get_exec_dcookie(mm);
538 add_user_ctx_switch(new, cookie); 540 add_user_ctx_switch(new, cookie);
diff --git a/kernel/fork.c b/kernel/fork.c
index 0d23e76a0c61..03c1eaaa6ef5 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -752,6 +752,7 @@ struct file *get_mm_exe_file(struct mm_struct *mm)
752 rcu_read_unlock(); 752 rcu_read_unlock();
753 return exe_file; 753 return exe_file;
754} 754}
755EXPORT_SYMBOL(get_mm_exe_file);
755 756
756/** 757/**
757 * get_task_mm - acquire a reference to the task's mm 758 * get_task_mm - acquire a reference to the task's mm