1328653Shselasky/*-
2328653Shselasky * Copyright (c) 2017 Mellanox Technologies, Ltd.
3328653Shselasky * All rights reserved.
4328653Shselasky *
5328653Shselasky * Redistribution and use in source and binary forms, with or without
6328653Shselasky * modification, are permitted provided that the following conditions
7328653Shselasky * are met:
8328653Shselasky * 1. Redistributions of source code must retain the above copyright
9328653Shselasky *    notice unmodified, this list of conditions, and the following
10328653Shselasky *    disclaimer.
11328653Shselasky * 2. Redistributions in binary form must reproduce the above copyright
12328653Shselasky *    notice, this list of conditions and the following disclaimer in the
13328653Shselasky *    documentation and/or other materials provided with the distribution.
14328653Shselasky *
15328653Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16328653Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17328653Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18328653Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19328653Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20328653Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21328653Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22328653Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23328653Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24328653Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25328653Shselasky *
26328653Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/mm_types.h 357436 2020-02-03 11:00:24Z hselasky $
27328653Shselasky */
28328653Shselasky
29328653Shselasky#ifndef _LINUX_MM_TYPES_H_
30328653Shselasky#define	_LINUX_MM_TYPES_H_
31328653Shselasky
32328653Shselasky#include <linux/types.h>
33328653Shselasky#include <linux/page.h>
34328653Shselasky#include <linux/rwsem.h>
35328653Shselasky
36328653Shselasky#include <asm/atomic.h>
37328653Shselasky
38328653Shselaskystruct vm_area_struct;
39328653Shselaskystruct task_struct;
40328653Shselasky
41328653Shselaskystruct mm_struct {
42328653Shselasky	struct vm_area_struct *mmap;
43328653Shselasky	atomic_t mm_count;
44328653Shselasky	atomic_t mm_users;
45328653Shselasky	size_t pinned_vm;
46328653Shselasky	struct rw_semaphore mmap_sem;
47328653Shselasky};
48328653Shselasky
49328653Shselaskyextern void linux_mm_dtor(struct mm_struct *mm);
50328653Shselasky
51328653Shselaskystatic inline void
52328653Shselaskymmdrop(struct mm_struct *mm)
53328653Shselasky{
54328653Shselasky	if (__predict_false(atomic_dec_and_test(&mm->mm_count)))
55328653Shselasky		linux_mm_dtor(mm);
56328653Shselasky}
57328653Shselasky
58357436Shselaskystatic inline bool
59357436Shselaskymmget_not_zero(struct mm_struct *mm)
60357436Shselasky{
61357436Shselasky	return (atomic_inc_not_zero(&mm->mm_users));
62357436Shselasky}
63357436Shselasky
64328653Shselaskystatic inline void
65328653Shselaskymmput(struct mm_struct *mm)
66328653Shselasky{
67328653Shselasky	if (__predict_false(atomic_dec_and_test(&mm->mm_users)))
68328653Shselasky		mmdrop(mm);
69328653Shselasky}
70328653Shselasky
71329960Shselaskystatic inline void
72329960Shselaskymmgrab(struct mm_struct *mm)
73329960Shselasky{
74329960Shselasky	atomic_inc(&mm->mm_count);
75329960Shselasky}
76329960Shselasky
77328653Shselaskyextern struct mm_struct *linux_get_task_mm(struct task_struct *);
78328653Shselasky#define	get_task_mm(task) linux_get_task_mm(task)
79328653Shselasky
80328653Shselasky#endif					/* _LINUX_MM_TYPES_H_ */
81