gfp.h revision 239065
1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef	_LINUX_GFP_H_
30#define	_LINUX_GFP_H_
31
32#include <sys/systm.h>
33#include <sys/malloc.h>
34
35#include <linux/page.h>
36
37#include <vm/vm_param.h>
38#include <vm/vm_object.h>
39#include <vm/vm_extern.h>
40#include <vm/vm_kern.h>
41
42#define	__GFP_NOWARN	0
43#define	__GFP_HIGHMEM	0
44#define	__GFP_ZERO	M_ZERO
45
46#define	GFP_NOWAIT	M_NOWAIT
47#define	GFP_ATOMIC	(M_NOWAIT | M_USE_RESERVE)
48#define	GFP_KERNEL	M_WAITOK
49#define	GFP_USER	M_WAITOK
50#define	GFP_HIGHUSER	M_WAITOK
51#define	GFP_HIGHUSER_MOVABLE	M_WAITOK
52#define	GFP_IOFS	M_NOWAIT
53
54static inline void *
55page_address(struct page *page)
56{
57
58	if (page->object != kmem_object && page->object != kernel_object)
59		return (NULL);
60	return ((void *)(uintptr_t)(VM_MIN_KERNEL_ADDRESS +
61	    IDX_TO_OFF(page->pindex)));
62}
63
64static inline unsigned long
65_get_page(gfp_t mask)
66{
67
68	return kmem_malloc(kmem_map, PAGE_SIZE, mask);
69}
70
71#define	get_zeroed_page(mask)	_get_page((mask) | M_ZERO)
72#define	alloc_page(mask)	virt_to_page(_get_page((mask)))
73#define	__get_free_page(mask)	_get_page((mask))
74
75static inline void
76free_page(unsigned long page)
77{
78
79	if (page == 0)
80		return;
81	kmem_free(kmem_map, page, PAGE_SIZE);
82}
83
84static inline void
85__free_page(struct page *m)
86{
87
88	if (m->object != kmem_object)
89		panic("__free_page:  Freed page %p not allocated via wrappers.",
90		    m);
91	kmem_free(kmem_map, (vm_offset_t)page_address(m), PAGE_SIZE);
92}
93
94static inline void
95__free_pages(void *p, unsigned int order)
96{
97	size_t size;
98
99	if (p == 0)
100		return;
101	size = PAGE_SIZE << order;
102	kmem_free(kmem_map, (vm_offset_t)p, size);
103}
104
105/*
106 * Alloc pages allocates directly from the buddy allocator on linux so
107 * order specifies a power of two bucket of pages and the results
108 * are expected to be aligned on the size as well.
109 */
110static inline struct page *
111alloc_pages(gfp_t gfp_mask, unsigned int order)
112{
113	unsigned long page;
114	size_t size;
115
116	size = PAGE_SIZE << order;
117	page = kmem_alloc_contig(kmem_map, size, gfp_mask, 0, -1,
118	    size, 0, VM_MEMATTR_DEFAULT);
119	if (page == 0)
120		return (NULL);
121        return (virt_to_page(page));
122}
123
124#endif	/* _LINUX_GFP_H_ */
125