mempolicy.h 8.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * NUMA memory policies for Linux.
 * Copyright 2003,2004 Andi Kleen SuSE Labs
 */
5 6
#ifndef _LINUX_MEMPOLICY_H
#define _LINUX_MEMPOLICY_H 1
L
Linus Torvalds 已提交
7 8 9 10 11 12


#include <linux/mmzone.h>
#include <linux/slab.h>
#include <linux/rbtree.h>
#include <linux/spinlock.h>
13
#include <linux/nodemask.h>
14
#include <linux/pagemap.h>
15
#include <uapi/linux/mempolicy.h>
L
Linus Torvalds 已提交
16

17
struct mm_struct;
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31

#ifdef CONFIG_NUMA

/*
 * Describe a memory policy.
 *
 * A mempolicy can be either associated with a process or with a VMA.
 * For VMA related allocations the VMA policy is preferred, otherwise
 * the process policy is used. Interrupts ignore the memory policy
 * of the current process.
 *
 * Locking policy for interlave:
 * In process context there is no locking because only the process accesses
 * its own state. All vma manipulation is somewhat protected by a down_read on
32
 * mmap_sem.
L
Linus Torvalds 已提交
33 34
 *
 * Freeing policy:
35
 * Mempolicy objects are reference counted.  A mempolicy will be freed when
36
 * mpol_put() decrements the reference count to zero.
L
Linus Torvalds 已提交
37
 *
38 39
 * Duplicating policy objects:
 * mpol_dup() allocates a new mempolicy and copies the specified mempolicy
40
 * to the new storage.  The reference count of the new object is initialized
41
 * to 1, representing the caller of mpol_dup().
L
Linus Torvalds 已提交
42 43 44
 */
struct mempolicy {
	atomic_t refcnt;
45
	unsigned short mode; 	/* See MPOL_* above */
46
	unsigned short flags;	/* See set_mempolicy() MPOL_F_* above */
L
Linus Torvalds 已提交
47 48
	union {
		short 		 preferred_node; /* preferred */
49
		nodemask_t	 nodes;		/* interleave/bind */
L
Linus Torvalds 已提交
50 51
		/* undefined for default */
	} v;
52 53 54 55
	union {
		nodemask_t cpuset_mems_allowed;	/* relative to these nodes */
		nodemask_t user_nodemask;	/* nodemask passed by user */
	} w;
L
Linus Torvalds 已提交
56 57 58 59 60 61 62
};

/*
 * Support for managing mempolicy data objects (clone, copy, destroy)
 * The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
 */

63 64
extern void __mpol_put(struct mempolicy *pol);
static inline void mpol_put(struct mempolicy *pol)
L
Linus Torvalds 已提交
65 66
{
	if (pol)
67
		__mpol_put(pol);
L
Linus Torvalds 已提交
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
 * Does mempolicy pol need explicit unref after use?
 * Currently only needed for shared policies.
 */
static inline int mpol_needs_cond_ref(struct mempolicy *pol)
{
	return (pol && (pol->flags & MPOL_F_SHARED));
}

static inline void mpol_cond_put(struct mempolicy *pol)
{
	if (mpol_needs_cond_ref(pol))
		__mpol_put(pol);
}

extern struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
					  struct mempolicy *frompol);
static inline struct mempolicy *mpol_cond_copy(struct mempolicy *tompol,
						struct mempolicy *frompol)
{
	if (!frompol)
		return frompol;
	return __mpol_cond_copy(tompol, frompol);
}

95 96
extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
L
Linus Torvalds 已提交
97 98
{
	if (pol)
99
		pol = __mpol_dup(pol);
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109 110 111
	return pol;
}

#define vma_policy(vma) ((vma)->vm_policy)
#define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))

static inline void mpol_get(struct mempolicy *pol)
{
	if (pol)
		atomic_inc(&pol->refcnt);
}

112 113
extern bool __mpol_equal(struct mempolicy *a, struct mempolicy *b);
static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
L
Linus Torvalds 已提交
114 115
{
	if (a == b)
116
		return true;
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	return __mpol_equal(a, b);
}

/*
 * Tree of shared policies for a shared memory region.
 * Maintain the policies in a pseudo mm that contains vmas. The vmas
 * carry the policy. As a special twist the pseudo mm is indexed in pages, not
 * bytes, so that we can work with shared memory segments bigger than
 * unsigned long.
 */

struct sp_node {
	struct rb_node nd;
	unsigned long start, end;
	struct mempolicy *policy;
};

struct shared_policy {
	struct rb_root root;
136
	struct mutex mutex;
L
Linus Torvalds 已提交
137 138
};

139
void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol);
L
Linus Torvalds 已提交
140 141 142 143 144 145 146
int mpol_set_shared_policy(struct shared_policy *info,
				struct vm_area_struct *vma,
				struct mempolicy *new);
void mpol_free_shared_policy(struct shared_policy *p);
struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
					    unsigned long idx);

S
Stephen Wilson 已提交
147 148 149
struct mempolicy *get_vma_policy(struct task_struct *tsk,
		struct vm_area_struct *vma, unsigned long addr);

L
Linus Torvalds 已提交
150 151
extern void numa_default_policy(void);
extern void numa_policy_init(void);
152 153
extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
				enum mpol_rebind_step step);
154
extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
155
extern void mpol_fix_fork_child_flag(struct task_struct *p);
156

157
extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
158 159
				unsigned long addr, gfp_t gfp_flags,
				struct mempolicy **mpol, nodemask_t **nodemask);
160
extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
161 162
extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
				const nodemask_t *mask);
163
extern unsigned slab_node(void);
L
Linus Torvalds 已提交
164

165
extern enum zone_type policy_zone;
166

167
static inline void check_highest_zone(enum zone_type k)
168
{
169
	if (k > policy_zone && k != ZONE_MOVABLE)
170 171 172
		policy_zone = k;
}

173 174
int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
		     const nodemask_t *to, int flags);
175

176 177

#ifdef CONFIG_TMPFS
178
extern int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context);
179
#endif
180

181 182
extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
			int no_context);
183 184 185 186

/* Check if a vma is migratable */
static inline int vma_migratable(struct vm_area_struct *vma)
{
187
	if (vma->vm_flags & (VM_IO | VM_HUGETLB | VM_PFNMAP))
188 189 190 191 192 193 194 195 196 197 198 199 200
		return 0;
	/*
	 * Migration allocates pages in the highest zone. If we cannot
	 * do so then migration (at least from node to node) is not
	 * possible.
	 */
	if (vma->vm_file &&
		gfp_zone(mapping_gfp_mask(vma->vm_file->f_mapping))
								< policy_zone)
			return 0;
	return 1;
}

201 202
extern int mpol_misplaced(struct page *, struct vm_area_struct *, unsigned long);

L
Linus Torvalds 已提交
203 204 205 206
#else

struct mempolicy {};

207
static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
L
Linus Torvalds 已提交
208
{
209
	return true;
L
Linus Torvalds 已提交
210 211
}

212
static inline void mpol_put(struct mempolicy *p)
L
Linus Torvalds 已提交
213 214 215
{
}

216 217 218 219 220 221 222 223 224 225
static inline void mpol_cond_put(struct mempolicy *pol)
{
}

static inline struct mempolicy *mpol_cond_copy(struct mempolicy *to,
						struct mempolicy *from)
{
	return from;
}

L
Linus Torvalds 已提交
226 227 228 229
static inline void mpol_get(struct mempolicy *pol)
{
}

230
static inline struct mempolicy *mpol_dup(struct mempolicy *old)
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243
{
	return NULL;
}

struct shared_policy {};

static inline int mpol_set_shared_policy(struct shared_policy *info,
					struct vm_area_struct *vma,
					struct mempolicy *new)
{
	return -EINVAL;
}

244 245
static inline void mpol_shared_policy_init(struct shared_policy *sp,
						struct mempolicy *mpol)
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
{
}

static inline void mpol_free_shared_policy(struct shared_policy *p)
{
}

static inline struct mempolicy *
mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
{
	return NULL;
}

#define vma_policy(vma) NULL
#define vma_set_policy(vma, pol) do {} while(0)

static inline void numa_policy_init(void)
{
}

static inline void numa_default_policy(void)
{
}

270
static inline void mpol_rebind_task(struct task_struct *tsk,
271 272
				const nodemask_t *new,
				enum mpol_rebind_step step)
273 274 275
{
}

276 277 278 279
static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
{
}

280 281 282 283
static inline void mpol_fix_fork_child_flag(struct task_struct *p)
{
}

284
static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
285 286
				unsigned long addr, gfp_t gfp_flags,
				struct mempolicy **mpol, nodemask_t **nodemask)
287
{
288 289
	*mpol = NULL;
	*nodemask = NULL;
290
	return node_zonelist(0, gfp_flags);
291 292
}

293 294 295 296 297 298 299 300 301 302
static inline bool init_nodemask_of_mempolicy(nodemask_t *m)
{
	return false;
}

static inline bool mempolicy_nodemask_intersects(struct task_struct *tsk,
			const nodemask_t *mask)
{
	return false;
}
303

304 305
static inline int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
				   const nodemask_t *to, int flags)
306 307 308 309
{
	return 0;
}

310 311 312
static inline void check_highest_zone(int k)
{
}
313 314

#ifdef CONFIG_TMPFS
315 316
static inline int mpol_parse_str(char *str, struct mempolicy **mpol,
				int no_context)
317
{
318
	return 1;	/* error */
319
}
320
#endif
321

322 323
static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
				int no_context)
324 325 326 327
{
	return 0;
}

328 329 330 331 332 333
static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
				 unsigned long address)
{
	return -1; /* no node preference */
}

L
Linus Torvalds 已提交
334 335
#endif /* CONFIG_NUMA */
#endif