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. |
| 17 | * Called by the retry thread execute retries within the |
| 18 | * iocb issuer's mm context, so that copy_from/to_user |
| 19 | * operations work seamlessly for aio. |
| 20 | * (Note: this routine is intended to be called only |
| 21 | * from a kernel thread context) |
| 22 | */ |
| 23 | void use_mm(struct mm_struct *mm) |
| 24 | { |
| 25 | struct mm_struct *active_mm; |
| 26 | struct task_struct *tsk = current; |
| 27 | |
| 28 | task_lock(tsk); |
| 29 | active_mm = tsk->active_mm; |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 30 | if (active_mm != mm) { |
| 31 | atomic_inc(&mm->mm_count); |
| 32 | tsk->active_mm = mm; |
| 33 | } |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 34 | tsk->mm = mm; |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 35 | switch_mm(active_mm, mm, tsk); |
| 36 | task_unlock(tsk); |
| 37 | |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 38 | if (active_mm != mm) |
| 39 | mmdrop(active_mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 40 | } |
Michael S. Tsirkin | 5da779c | 2010-01-14 06:17:18 +0000 | [diff] [blame] | 41 | EXPORT_SYMBOL_GPL(use_mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * unuse_mm |
| 45 | * Reverses the effect of use_mm, i.e. releases the |
| 46 | * specified mm context which was earlier taken on |
| 47 | * by the calling kernel thread |
| 48 | * (Note: this routine is intended to be called only |
| 49 | * from a kernel thread context) |
| 50 | */ |
| 51 | void unuse_mm(struct mm_struct *mm) |
| 52 | { |
| 53 | struct task_struct *tsk = current; |
| 54 | |
| 55 | task_lock(tsk); |
Michael S. Tsirkin | 298359c | 2010-03-23 13:35:37 -0700 | [diff] [blame] | 56 | sync_mm_rss(tsk, mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 57 | tsk->mm = NULL; |
| 58 | /* active_mm is still 'mm' */ |
| 59 | enter_lazy_tlb(mm, tsk); |
| 60 | task_unlock(tsk); |
| 61 | } |
Michael S. Tsirkin | 5da779c | 2010-01-14 06:17:18 +0000 | [diff] [blame] | 62 | EXPORT_SYMBOL_GPL(unuse_mm); |