blob: 8888b124a386c5307fbb63a5380de4a8195e0919 [file] [log] [blame]
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -07001/* Copyright (C) 2009 Red Hat, Inc.
2 *
3 * See ../COPYING for licensing terms.
4 */
5
6#include <linux/mm.h>
Ingo Molnar8efd755a2016-04-28 11:39:12 +02007#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +01008#include <linux/sched/mm.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -07009#include <linux/mmu_context.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040010#include <linux/export.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070011
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. Tsirkin3d2d8272009-09-21 17:03:51 -070018 * (Note: this routine is intended to be called only
19 * from a kernel thread context)
20 */
21void 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. Tsirkinf68e1482009-09-21 17:03:52 -070028 if (active_mm != mm) {
Vegard Nossumf1f10072017-02-27 14:30:07 -080029 mmgrab(mm);
Michael S. Tsirkinf68e1482009-09-21 17:03:52 -070030 tsk->active_mm = mm;
31 }
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070032 tsk->mm = mm;
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070033 switch_mm(active_mm, mm, tsk);
34 task_unlock(tsk);
Martin Schwidefskya53efe52012-10-26 17:17:44 +020035#ifdef finish_arch_post_lock_switch
36 finish_arch_post_lock_switch();
37#endif
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070038
Michael S. Tsirkinf68e1482009-09-21 17:03:52 -070039 if (active_mm != mm)
40 mmdrop(active_mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070041}
Michael S. Tsirkin5da779c2010-01-14 06:17:18 +000042EXPORT_SYMBOL_GPL(use_mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070043
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 */
52void unuse_mm(struct mm_struct *mm)
53{
54 struct task_struct *tsk = current;
55
56 task_lock(tsk);
David Rientjes05af2e12012-03-21 16:34:13 -070057 sync_mm_rss(mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070058 tsk->mm = NULL;
59 /* active_mm is still 'mm' */
60 enter_lazy_tlb(mm, tsk);
61 task_unlock(tsk);
62}
Michael S. Tsirkin5da779c2010-01-14 06:17:18 +000063EXPORT_SYMBOL_GPL(unuse_mm);