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> |
Ingo Molnar | 8efd755a | 2016-04-28 11:39:12 +0200 | [diff] [blame] | 7 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame^] | 8 | #include <linux/sched/mm.h> |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 9 | #include <linux/mmu_context.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 11 | |
| 12 | #include <asm/mmu_context.h> |
| 13 | |
| 14 | /* |
| 15 | * use_mm |
| 16 | * Makes the calling kernel thread take on the specified |
| 17 | * mm context. |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 18 | * (Note: this routine is intended to be called only |
| 19 | * from a kernel thread context) |
| 20 | */ |
| 21 | void use_mm(struct mm_struct *mm) |
| 22 | { |
| 23 | struct mm_struct *active_mm; |
| 24 | struct task_struct *tsk = current; |
| 25 | |
| 26 | task_lock(tsk); |
| 27 | active_mm = tsk->active_mm; |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 28 | if (active_mm != mm) { |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 29 | mmgrab(mm); |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 30 | tsk->active_mm = mm; |
| 31 | } |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 32 | tsk->mm = mm; |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 33 | switch_mm(active_mm, mm, tsk); |
| 34 | task_unlock(tsk); |
Martin Schwidefsky | a53efe5 | 2012-10-26 17:17:44 +0200 | [diff] [blame] | 35 | #ifdef finish_arch_post_lock_switch |
| 36 | finish_arch_post_lock_switch(); |
| 37 | #endif |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 38 | |
Michael S. Tsirkin | f68e148 | 2009-09-21 17:03:52 -0700 | [diff] [blame] | 39 | if (active_mm != mm) |
| 40 | mmdrop(active_mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 41 | } |
Michael S. Tsirkin | 5da779c | 2010-01-14 06:17:18 +0000 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(use_mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * unuse_mm |
| 46 | * Reverses the effect of use_mm, i.e. releases the |
| 47 | * specified mm context which was earlier taken on |
| 48 | * by the calling kernel thread |
| 49 | * (Note: this routine is intended to be called only |
| 50 | * from a kernel thread context) |
| 51 | */ |
| 52 | void unuse_mm(struct mm_struct *mm) |
| 53 | { |
| 54 | struct task_struct *tsk = current; |
| 55 | |
| 56 | task_lock(tsk); |
David Rientjes | 05af2e1 | 2012-03-21 16:34:13 -0700 | [diff] [blame] | 57 | sync_mm_rss(mm); |
Michael S. Tsirkin | 3d2d827 | 2009-09-21 17:03:51 -0700 | [diff] [blame] | 58 | tsk->mm = NULL; |
| 59 | /* active_mm is still 'mm' */ |
| 60 | enter_lazy_tlb(mm, tsk); |
| 61 | task_unlock(tsk); |
| 62 | } |
Michael S. Tsirkin | 5da779c | 2010-01-14 06:17:18 +0000 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(unuse_mm); |