blob: a1da47e027479680afb3ebc58091c92b5413aa6b [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* Copyright (C) 2009 Red Hat, Inc.
2 *
3 * See ../COPYING for licensing terms.
4 */
5
6#include <linux/mm.h>
7#include <linux/sched.h>
8#include <linux/sched/mm.h>
9#include <linux/sched/task.h>
10#include <linux/mmu_context.h>
11#include <linux/export.h>
12
13#include <asm/mmu_context.h>
14
15/*
16 * use_mm
17 * Makes the calling kernel thread take on the specified
18 * mm context.
19 * (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);
Olivier Deprez0e641232021-09-23 10:07:05 +020028 /* Hold off tlb flush IPIs while switching mm's */
29 local_irq_disable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000030 active_mm = tsk->active_mm;
31 if (active_mm != mm) {
32 mmgrab(mm);
33 tsk->active_mm = mm;
34 }
35 tsk->mm = mm;
Olivier Deprez0e641232021-09-23 10:07:05 +020036 switch_mm_irqs_off(active_mm, mm, tsk);
37 local_irq_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 task_unlock(tsk);
39#ifdef finish_arch_post_lock_switch
40 finish_arch_post_lock_switch();
41#endif
42
43 if (active_mm != mm)
44 mmdrop(active_mm);
45}
46EXPORT_SYMBOL_GPL(use_mm);
47
48/*
49 * unuse_mm
50 * Reverses the effect of use_mm, i.e. releases the
51 * specified mm context which was earlier taken on
52 * by the calling kernel thread
53 * (Note: this routine is intended to be called only
54 * from a kernel thread context)
55 */
56void unuse_mm(struct mm_struct *mm)
57{
58 struct task_struct *tsk = current;
59
60 task_lock(tsk);
61 sync_mm_rss(mm);
Olivier Deprez0e641232021-09-23 10:07:05 +020062 local_irq_disable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 tsk->mm = NULL;
64 /* active_mm is still 'mm' */
65 enter_lazy_tlb(mm, tsk);
Olivier Deprez0e641232021-09-23 10:07:05 +020066 local_irq_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 task_unlock(tsk);
68}
69EXPORT_SYMBOL_GPL(unuse_mm);