blob: 6f4d27c5bb325f6468461b17cd8800cc1e473308 [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>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -07008#include <linux/mmu_context.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -04009#include <linux/export.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070010
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. Tsirkin3d2d8272009-09-21 17:03:51 -070017 * (Note: this routine is intended to be called only
18 * from a kernel thread context)
19 */
20void 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. Tsirkinf68e1482009-09-21 17:03:52 -070027 if (active_mm != mm) {
28 atomic_inc(&mm->mm_count);
29 tsk->active_mm = mm;
30 }
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070031 tsk->mm = mm;
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070032 switch_mm(active_mm, mm, tsk);
33 task_unlock(tsk);
Martin Schwidefskya53efe52012-10-26 17:17:44 +020034#ifdef finish_arch_post_lock_switch
35 finish_arch_post_lock_switch();
36#endif
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070037
Michael S. Tsirkinf68e1482009-09-21 17:03:52 -070038 if (active_mm != mm)
39 mmdrop(active_mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070040}
Michael S. Tsirkin5da779c2010-01-14 06:17:18 +000041EXPORT_SYMBOL_GPL(use_mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070042
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 */
51void unuse_mm(struct mm_struct *mm)
52{
53 struct task_struct *tsk = current;
54
55 task_lock(tsk);
David Rientjes05af2e12012-03-21 16:34:13 -070056 sync_mm_rss(mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070057 tsk->mm = NULL;
58 /* active_mm is still 'mm' */
59 enter_lazy_tlb(mm, tsk);
60 task_unlock(tsk);
61}
Michael S. Tsirkin5da779c2010-01-14 06:17:18 +000062EXPORT_SYMBOL_GPL(unuse_mm);