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