Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2009 Red Hat, Inc. |
| 2 | * |
| 3 | * See ../COPYING for licensing terms. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/mmu_context.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
| 10 | |
| 11 | #include <asm/mmu_context.h> |
| 12 | |
| 13 | /* |
| 14 | * use_mm |
| 15 | * Makes the calling kernel thread take on the specified |
| 16 | * mm context. |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 17 | * (Note: this routine is intended to be called only |
| 18 | * from a kernel thread context) |
| 19 | */ |
| 20 | void use_mm(struct mm_struct *mm) |
| 21 | { |
| 22 | struct mm_struct *active_mm; |
| 23 | struct task_struct *tsk = current; |
| 24 | |
| 25 | task_lock(tsk); |
| 26 | active_mm = tsk->active_mm; |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 27 | if (active_mm != mm) { |
| 28 | atomic_inc(&mm->mm_count); |
| 29 | tsk->active_mm = mm; |
| 30 | } |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 31 | tsk->mm = mm; |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 32 | switch_mm(active_mm, mm, tsk); |
| 33 | task_unlock(tsk); |
| 34 | |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 35 | if (active_mm != mm) |
| 36 | mmdrop(active_mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 37 | } |
Michael S. Tsirkin | 5da779c | 2010-01-14 06:17:18 +0000 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(use_mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * unuse_mm |
| 42 | * Reverses the effect of use_mm, i.e. releases the |
| 43 | * specified mm context which was earlier taken on |
| 44 | * by the calling kernel thread |
| 45 | * (Note: this routine is intended to be called only |
| 46 | * from a kernel thread context) |
| 47 | */ |
| 48 | void unuse_mm(struct mm_struct *mm) |
| 49 | { |
| 50 | struct task_struct *tsk = current; |
| 51 | |
| 52 | task_lock(tsk); |
David Rientjes | 05af2e1 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 53 | sync_mm_rss(mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 54 | tsk->mm = NULL; |
| 55 | /* active_mm is still 'mm' */ |
| 56 | enter_lazy_tlb(mm, tsk); |
| 57 | task_unlock(tsk); |
| 58 | } |
Michael S. Tsirkin | 5da779c | 2010-01-14 06:17:18 +0000 | [diff] [blame] | 59 | EXPORT_SYMBOL_GPL(unuse_mm); |