blob: 3e612ae748e96692522cf7d3c7c615345974fa6d [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>
Ingo Molnarf719ff92017-02-06 10:57:33 +01009#include <linux/sched/task.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070010#include <linux/mmu_context.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040011#include <linux/export.h>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070012
13#include <asm/mmu_context.h>
14
15/*
16 * use_mm
17 * Makes the calling kernel thread take on the specified
18 * mm context.
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070019 * (Note: this routine is intended to be called only
20 * from a kernel thread context)
21 */
22void use_mm(struct mm_struct *mm)
23{
24 struct mm_struct *active_mm;
25 struct task_struct *tsk = current;
26
27 task_lock(tsk);
28 active_mm = tsk->active_mm;
Michael S. Tsirkinf68e1482009-09-21 17:03:52 -070029 if (active_mm != mm) {
Vegard Nossumf1f10072017-02-27 14:30:07 -080030 mmgrab(mm);
Michael S. Tsirkinf68e1482009-09-21 17:03:52 -070031 tsk->active_mm = mm;
32 }
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070033 tsk->mm = mm;
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070034 switch_mm(active_mm, mm, tsk);
35 task_unlock(tsk);
Martin Schwidefskya53efe52012-10-26 17:17:44 +020036#ifdef finish_arch_post_lock_switch
37 finish_arch_post_lock_switch();
38#endif
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070039
Michael S. Tsirkinf68e1482009-09-21 17:03:52 -070040 if (active_mm != mm)
41 mmdrop(active_mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070042}
Michael S. Tsirkin5da779c2010-01-14 06:17:18 +000043EXPORT_SYMBOL_GPL(use_mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070044
45/*
46 * unuse_mm
47 * Reverses the effect of use_mm, i.e. releases the
48 * specified mm context which was earlier taken on
49 * by the calling kernel thread
50 * (Note: this routine is intended to be called only
51 * from a kernel thread context)
52 */
53void unuse_mm(struct mm_struct *mm)
54{
55 struct task_struct *tsk = current;
56
57 task_lock(tsk);
David Rientjes05af2e12012-03-21 16:34:13 -070058 sync_mm_rss(mm);
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070059 tsk->mm = NULL;
60 /* active_mm is still 'mm' */
61 enter_lazy_tlb(mm, tsk);
62 task_unlock(tsk);
63}
Michael S. Tsirkin5da779c2010-01-14 06:17:18 +000064EXPORT_SYMBOL_GPL(unuse_mm);