blob: 1385e482fd8065451a6e5ddb619629ac9f7c36e4 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 2009 Red Hat, Inc.
2 *
3 * See ../COPYING for licensing terms.
4 */
5
6#include <linux/mm.h>
7#include <linux/mmu_context.h>
8#include <linux/export.h>
9#include <linux/sched.h>
10
11#include <asm/mmu_context.h>
12
13/*
14 * use_mm
15 * Makes the calling kernel thread take on the specified
16 * mm context.
17 * Called by the retry thread execute retries within the
18 * iocb issuer's mm context, so that copy_from/to_user
19 * operations work seamlessly for aio.
20 * (Note: this routine is intended to be called only
21 * from a kernel thread context)
22 */
23void use_mm(struct mm_struct *mm)
24{
25 struct mm_struct *active_mm;
26 struct task_struct *tsk = current;
27
28 task_lock(tsk);
29 preempt_disable_rt();
30 active_mm = tsk->active_mm;
31 if (active_mm != mm) {
32 atomic_inc(&mm->mm_count);
33 tsk->active_mm = mm;
34 }
35 tsk->mm = mm;
36 switch_mm(active_mm, mm, tsk);
37 preempt_enable_rt();
38 task_unlock(tsk);
39
40 if (active_mm != mm)
41 mmdrop(active_mm);
42}
43EXPORT_SYMBOL_GPL(use_mm);
44
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);
58 sync_mm_rss(mm);
59 tsk->mm = NULL;
60 /* active_mm is still 'mm' */
61 enter_lazy_tlb(mm, tsk);
62 task_unlock(tsk);
63}
64EXPORT_SYMBOL_GPL(unuse_mm);