Skip to content

Commit f1f1007

Browse files
vegardtorvalds
authored andcommitted
mm: add new mmgrab() helper
Apart from adding the helper function itself, the rest of the kernel is converted mechanically using: git grep -l 'atomic_inc.*mm_count' | xargs sed -i 's/atomic_inc(&\(.*\)->mm_count);/mmgrab\(\1\);/' git grep -l 'atomic_inc.*mm_count' | xargs sed -i 's/atomic_inc(&\(.*\)\.mm_count);/mmgrab\(\&\1\);/' This is needed for a later patch that hooks into the helper, but might be a worthwhile cleanup on its own. (Michal Hocko provided most of the kerneldoc comment.) Link: http://lkml.kernel.org/r/20161218123229.22952-1-vegard.nossum@oracle.com Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com> Acked-by: Michal Hocko <mhocko@suse.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 522b837 commit f1f1007

File tree

40 files changed

+65
-43
lines changed

40 files changed

+65
-43
lines changed

arch/alpha/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ smp_callin(void)
144144
alpha_mv.smp_callin();
145145

146146
/* All kernel threads share the same mm context. */
147-
atomic_inc(&init_mm.mm_count);
147+
mmgrab(&init_mm);
148148
current->active_mm = &init_mm;
149149

150150
/* inform the notifiers about the new cpu */

arch/arc/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void start_kernel_secondary(void)
140140
setup_processor();
141141

142142
atomic_inc(&mm->mm_users);
143-
atomic_inc(&mm->mm_count);
143+
mmgrab(mm);
144144
current->active_mm = mm;
145145
cpumask_set_cpu(cpu, mm_cpumask(mm));
146146

arch/arm/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ asmlinkage void secondary_start_kernel(void)
371371
* reference and switch to it.
372372
*/
373373
cpu = smp_processor_id();
374-
atomic_inc(&mm->mm_count);
374+
mmgrab(mm);
375375
current->active_mm = mm;
376376
cpumask_set_cpu(cpu, mm_cpumask(mm));
377377

arch/arm64/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ asmlinkage void secondary_start_kernel(void)
222222
* All kernel threads share the same mm context; grab a
223223
* reference and switch to it.
224224
*/
225-
atomic_inc(&mm->mm_count);
225+
mmgrab(mm);
226226
current->active_mm = mm;
227227

228228
/*

arch/blackfin/mach-common/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ void secondary_start_kernel(void)
308308

309309
/* Attach the new idle task to the global mm. */
310310
atomic_inc(&mm->mm_users);
311-
atomic_inc(&mm->mm_count);
311+
mmgrab(mm);
312312
current->active_mm = mm;
313313

314314
preempt_disable();

arch/hexagon/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void start_secondary(void)
162162
);
163163

164164
/* Set the memory struct */
165-
atomic_inc(&init_mm.mm_count);
165+
mmgrab(&init_mm);
166166
current->active_mm = &init_mm;
167167

168168
cpu = smp_processor_id();

arch/ia64/kernel/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ cpu_init (void)
994994
*/
995995
ia64_setreg(_IA64_REG_CR_DCR, ( IA64_DCR_DP | IA64_DCR_DK | IA64_DCR_DX | IA64_DCR_DR
996996
| IA64_DCR_DA | IA64_DCR_DD | IA64_DCR_LC));
997-
atomic_inc(&init_mm.mm_count);
997+
mmgrab(&init_mm);
998998
current->active_mm = &init_mm;
999999
BUG_ON(current->mm);
10001000

arch/m32r/kernel/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ void __init cpu_init (void)
403403
printk(KERN_INFO "Initializing CPU#%d\n", cpu_id);
404404

405405
/* Set up and load the per-CPU TSS and LDT */
406-
atomic_inc(&init_mm.mm_count);
406+
mmgrab(&init_mm);
407407
current->active_mm = &init_mm;
408408
if (current->mm)
409409
BUG();

arch/metag/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ asmlinkage void secondary_start_kernel(void)
345345
* reference and switch to it.
346346
*/
347347
atomic_inc(&mm->mm_users);
348-
atomic_inc(&mm->mm_count);
348+
mmgrab(mm);
349349
current->active_mm = mm;
350350
cpumask_set_cpu(cpu, mm_cpumask(mm));
351351
enter_lazy_tlb(mm, current);

arch/mips/kernel/traps.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@ void per_cpu_trap_init(bool is_boot_cpu)
22322232
if (!cpu_data[cpu].asid_cache)
22332233
cpu_data[cpu].asid_cache = asid_first_version(cpu);
22342234

2235-
atomic_inc(&init_mm.mm_count);
2235+
mmgrab(&init_mm);
22362236
current->active_mm = &init_mm;
22372237
BUG_ON(current->mm);
22382238
enter_lazy_tlb(&init_mm, current);

arch/mn10300/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static void __init smp_cpu_init(void)
589589
}
590590
printk(KERN_INFO "Initializing CPU#%d\n", cpu_id);
591591

592-
atomic_inc(&init_mm.mm_count);
592+
mmgrab(&init_mm);
593593
current->active_mm = &init_mm;
594594
BUG_ON(current->mm);
595595

arch/parisc/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ smp_cpu_init(int cpunum)
279279
set_cpu_online(cpunum, true);
280280

281281
/* Initialise the idle task for this CPU */
282-
atomic_inc(&init_mm.mm_count);
282+
mmgrab(&init_mm);
283283
current->active_mm = &init_mm;
284284
BUG_ON(current->mm);
285285
enter_lazy_tlb(&init_mm, current);

arch/powerpc/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ void start_secondary(void *unused)
707707
unsigned int cpu = smp_processor_id();
708708
int i, base;
709709

710-
atomic_inc(&init_mm.mm_count);
710+
mmgrab(&init_mm);
711711
current->active_mm = &init_mm;
712712

713713
smp_store_cpu_info(cpu);

arch/s390/kernel/processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void cpu_init(void)
7373
get_cpu_id(id);
7474
if (machine_has_cpu_mhz)
7575
update_cpu_mhz(NULL);
76-
atomic_inc(&init_mm.mm_count);
76+
mmgrab(&init_mm);
7777
current->active_mm = &init_mm;
7878
BUG_ON(current->mm);
7979
enter_lazy_tlb(&init_mm, current);

arch/score/kernel/traps.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void __init trap_init(void)
336336
set_except_vector(18, handle_dbe);
337337
flush_icache_range(DEBUG_VECTOR_BASE_ADDR, IRQ_VECTOR_BASE_ADDR);
338338

339-
atomic_inc(&init_mm.mm_count);
339+
mmgrab(&init_mm);
340340
current->active_mm = &init_mm;
341341
cpu_cache_init();
342342
}

arch/sh/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ asmlinkage void start_secondary(void)
178178
struct mm_struct *mm = &init_mm;
179179

180180
enable_mmu();
181-
atomic_inc(&mm->mm_count);
181+
mmgrab(mm);
182182
atomic_inc(&mm->mm_users);
183183
current->active_mm = mm;
184184
#ifdef CONFIG_MMU

arch/sparc/kernel/leon_smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void leon_cpu_pre_online(void *arg)
9393
: "memory" /* paranoid */);
9494

9595
/* Attach to the address space of init_task. */
96-
atomic_inc(&init_mm.mm_count);
96+
mmgrab(&init_mm);
9797
current->active_mm = &init_mm;
9898

9999
while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))

arch/sparc/kernel/smp_64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void smp_callin(void)
122122
current_thread_info()->new_child = 0;
123123

124124
/* Attach to the address space of init_task. */
125-
atomic_inc(&init_mm.mm_count);
125+
mmgrab(&init_mm);
126126
current->active_mm = &init_mm;
127127

128128
/* inform the notifiers about the new cpu */

arch/sparc/kernel/sun4d_smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void sun4d_cpu_pre_online(void *arg)
9393
show_leds(cpuid);
9494

9595
/* Attach to the address space of init_task. */
96-
atomic_inc(&init_mm.mm_count);
96+
mmgrab(&init_mm);
9797
current->active_mm = &init_mm;
9898

9999
local_ops->cache_all();

arch/sparc/kernel/sun4m_smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void sun4m_cpu_pre_online(void *arg)
5959
: "memory" /* paranoid */);
6060

6161
/* Attach to the address space of init_task. */
62-
atomic_inc(&init_mm.mm_count);
62+
mmgrab(&init_mm);
6363
current->active_mm = &init_mm;
6464

6565
while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))

arch/sparc/kernel/traps_32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void trap_init(void)
448448
thread_info_offsets_are_bolixed_pete();
449449

450450
/* Attach to the address space of init_task. */
451-
atomic_inc(&init_mm.mm_count);
451+
mmgrab(&init_mm);
452452
current->active_mm = &init_mm;
453453

454454
/* NOTE: Other cpus have this done as they are started

arch/sparc/kernel/traps_64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,6 @@ void __init trap_init(void)
28372837
/* Attach to the address space of init_task. On SMP we
28382838
* do this in smp.c:smp_callin for other cpus.
28392839
*/
2840-
atomic_inc(&init_mm.mm_count);
2840+
mmgrab(&init_mm);
28412841
current->active_mm = &init_mm;
28422842
}

arch/tile/kernel/smpboot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static void start_secondary(void)
160160
__this_cpu_write(current_asid, min_asid);
161161

162162
/* Set up this thread as another owner of the init_mm */
163-
atomic_inc(&init_mm.mm_count);
163+
mmgrab(&init_mm);
164164
current->active_mm = &init_mm;
165165
if (current->mm)
166166
BUG();

arch/x86/kernel/cpu/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ void cpu_init(void)
15101510
for (i = 0; i <= IO_BITMAP_LONGS; i++)
15111511
t->io_bitmap[i] = ~0UL;
15121512

1513-
atomic_inc(&init_mm.mm_count);
1513+
mmgrab(&init_mm);
15141514
me->active_mm = &init_mm;
15151515
BUG_ON(me->mm);
15161516
enter_lazy_tlb(&init_mm, me);
@@ -1561,7 +1561,7 @@ void cpu_init(void)
15611561
/*
15621562
* Set up and load the per-CPU TSS and LDT
15631563
*/
1564-
atomic_inc(&init_mm.mm_count);
1564+
mmgrab(&init_mm);
15651565
curr->active_mm = &init_mm;
15661566
BUG_ON(curr->mm);
15671567
enter_lazy_tlb(&init_mm, curr);

arch/xtensa/kernel/smp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void secondary_start_kernel(void)
136136
/* All kernel threads share the same mm context. */
137137

138138
atomic_inc(&mm->mm_users);
139-
atomic_inc(&mm->mm_count);
139+
mmgrab(mm);
140140
current->active_mm = mm;
141141
cpumask_set_cpu(cpu, mm_cpumask(mm));
142142
enter_lazy_tlb(mm, current);

drivers/gpu/drm/amd/amdkfd/kfd_process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static void kfd_process_notifier_release(struct mmu_notifier *mn,
262262
* and because the mmu_notifier_unregister function also drop
263263
* mm_count we need to take an extra count here.
264264
*/
265-
atomic_inc(&p->mm->mm_count);
265+
mmgrab(p->mm);
266266
mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
267267
mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
268268
}

drivers/gpu/drm/i915/i915_gem_userptr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
334334
mm->i915 = to_i915(obj->base.dev);
335335

336336
mm->mm = current->mm;
337-
atomic_inc(&current->mm->mm_count);
337+
mmgrab(current->mm);
338338

339339
mm->mn = NULL;
340340

drivers/infiniband/hw/hfi1/file_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static int hfi1_file_open(struct inode *inode, struct file *fp)
185185
if (fd) {
186186
fd->rec_cpu_num = -1; /* no cpu affinity by default */
187187
fd->mm = current->mm;
188-
atomic_inc(&fd->mm->mm_count);
188+
mmgrab(fd->mm);
189189
fp->private_data = fd;
190190
} else {
191191
fp->private_data = NULL;

fs/proc/base.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
766766

767767
if (!IS_ERR_OR_NULL(mm)) {
768768
/* ensure this mm_struct can't be freed */
769-
atomic_inc(&mm->mm_count);
769+
mmgrab(mm);
770770
/* but do not pin its memory */
771771
mmput(mm);
772772
}
@@ -1064,7 +1064,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
10641064
if (p) {
10651065
if (atomic_read(&p->mm->mm_users) > 1) {
10661066
mm = p->mm;
1067-
atomic_inc(&mm->mm_count);
1067+
mmgrab(mm);
10681068
}
10691069
task_unlock(p);
10701070
}

fs/userfaultfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ static struct file *userfaultfd_file_create(int flags)
18471847
ctx->released = false;
18481848
ctx->mm = current->mm;
18491849
/* prevent the mm struct to be freed */
1850-
atomic_inc(&ctx->mm->mm_count);
1850+
mmgrab(ctx->mm);
18511851

18521852
file = anon_inode_getfile("[userfaultfd]", &userfaultfd_fops, ctx,
18531853
O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS));

include/linux/sched.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,28 @@ static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
29042904
*/
29052905
extern struct mm_struct * mm_alloc(void);
29062906

2907+
/**
2908+
* mmgrab() - Pin a &struct mm_struct.
2909+
* @mm: The &struct mm_struct to pin.
2910+
*
2911+
* Make sure that @mm will not get freed even after the owning task
2912+
* exits. This doesn't guarantee that the associated address space
2913+
* will still exist later on and mmget_not_zero() has to be used before
2914+
* accessing it.
2915+
*
2916+
* This is a preferred way to to pin @mm for a longer/unbounded amount
2917+
* of time.
2918+
*
2919+
* Use mmdrop() to release the reference acquired by mmgrab().
2920+
*
2921+
* See also <Documentation/vm/active_mm.txt> for an in-depth explanation
2922+
* of &mm_struct.mm_count vs &mm_struct.mm_users.
2923+
*/
2924+
static inline void mmgrab(struct mm_struct *mm)
2925+
{
2926+
atomic_inc(&mm->mm_count);
2927+
}
2928+
29072929
/* mmdrop drops the mm and the page tables */
29082930
extern void __mmdrop(struct mm_struct *);
29092931
static inline void mmdrop(struct mm_struct *mm)

kernel/exit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ static void exit_mm(void)
539539
__set_current_state(TASK_RUNNING);
540540
down_read(&mm->mmap_sem);
541541
}
542-
atomic_inc(&mm->mm_count);
542+
mmgrab(mm);
543543
BUG_ON(mm != current->active_mm);
544544
/* more a memory barrier than a real lock */
545545
task_lock(current);

kernel/futex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static inline bool should_fail_futex(bool fshared)
338338

339339
static inline void futex_get_mm(union futex_key *key)
340340
{
341-
atomic_inc(&key->private.mm->mm_count);
341+
mmgrab(key->private.mm);
342342
/*
343343
* Ensure futex_get_mm() implies a full barrier such that
344344
* get_futex_key() implies a full barrier. This is relied upon

kernel/sched/core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
28472847

28482848
if (!mm) {
28492849
next->active_mm = oldmm;
2850-
atomic_inc(&oldmm->mm_count);
2850+
mmgrab(oldmm);
28512851
enter_lazy_tlb(oldmm, next);
28522852
} else
28532853
switch_mm_irqs_off(oldmm, mm, next);
@@ -6098,7 +6098,7 @@ void __init sched_init(void)
60986098
/*
60996099
* The boot idle thread does lazy MMU switching as well:
61006100
*/
6101-
atomic_inc(&init_mm.mm_count);
6101+
mmgrab(&init_mm);
61026102
enter_lazy_tlb(&init_mm, current);
61036103

61046104
/*

mm/khugepaged.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ int __khugepaged_enter(struct mm_struct *mm)
420420
list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
421421
spin_unlock(&khugepaged_mm_lock);
422422

423-
atomic_inc(&mm->mm_count);
423+
mmgrab(mm);
424424
if (wakeup)
425425
wake_up_interruptible(&khugepaged_wait);
426426

mm/ksm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ int __ksm_enter(struct mm_struct *mm)
18541854
spin_unlock(&ksm_mmlist_lock);
18551855

18561856
set_bit(MMF_VM_MERGEABLE, &mm->flags);
1857-
atomic_inc(&mm->mm_count);
1857+
mmgrab(mm);
18581858

18591859
if (needs_wakeup)
18601860
wake_up_interruptible(&ksm_thread_wait);

mm/mmu_context.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void use_mm(struct mm_struct *mm)
2525
task_lock(tsk);
2626
active_mm = tsk->active_mm;
2727
if (active_mm != mm) {
28-
atomic_inc(&mm->mm_count);
28+
mmgrab(mm);
2929
tsk->active_mm = mm;
3030
}
3131
tsk->mm = mm;

mm/mmu_notifier.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static int do_mmu_notifier_register(struct mmu_notifier *mn,
275275
mm->mmu_notifier_mm = mmu_notifier_mm;
276276
mmu_notifier_mm = NULL;
277277
}
278-
atomic_inc(&mm->mm_count);
278+
mmgrab(mm);
279279

280280
/*
281281
* Serialize the update against mmu_notifier_unregister. A

0 commit comments

Comments
 (0)