]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/ppc64/mm/imalloc.c
2 * c 2001 PPC 64 Team, IBM Corp
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
13 #include <asm/uaccess.h>
14 #include <asm/pgalloc.h>
15 #include <asm/pgtable.h>
16 #include <asm/semaphore.h>
18 static DECLARE_MUTEX(imlist_sem
);
19 struct vm_struct
* imlist
= NULL
;
21 static int get_free_im_addr(unsigned long size
, unsigned long *im_addr
)
24 struct vm_struct
**p
, *tmp
;
27 for (p
= &imlist
; (tmp
= *p
) ; p
= &tmp
->next
) {
28 if (size
+ addr
< (unsigned long) tmp
->addr
)
30 if ((unsigned long)tmp
->addr
>= IMALLOC_START
)
31 addr
= tmp
->size
+ (unsigned long) tmp
->addr
;
32 if (addr
> IMALLOC_END
-size
)
40 /* Return whether the region described by v_addr and size is a subset
41 * of the region described by parent
43 static inline int im_region_is_subset(unsigned long v_addr
, unsigned long size
,
44 struct vm_struct
*parent
)
46 return (int) (v_addr
>= (unsigned long) parent
->addr
&&
47 v_addr
< (unsigned long) parent
->addr
+ parent
->size
&&
51 /* Return whether the region described by v_addr and size is a superset
52 * of the region described by child
54 static int im_region_is_superset(unsigned long v_addr
, unsigned long size
,
55 struct vm_struct
*child
)
57 struct vm_struct parent
;
59 parent
.addr
= (void *) v_addr
;
62 return im_region_is_subset((unsigned long) child
->addr
, child
->size
,
66 /* Return whether the region described by v_addr and size overlaps
67 * the region described by vm. Overlapping regions meet the
68 * following conditions:
69 * 1) The regions share some part of the address space
70 * 2) The regions aren't identical
71 * 3) Neither region is a subset of the other
73 static int im_region_overlaps(unsigned long v_addr
, unsigned long size
,
76 if (im_region_is_superset(v_addr
, size
, vm
))
79 return (v_addr
+ size
> (unsigned long) vm
->addr
+ vm
->size
&&
80 v_addr
< (unsigned long) vm
->addr
+ vm
->size
) ||
81 (v_addr
< (unsigned long) vm
->addr
&&
82 v_addr
+ size
> (unsigned long) vm
->addr
);
85 /* Determine imalloc status of region described by v_addr and size.
86 * Can return one of the following:
87 * IM_REGION_UNUSED - Entire region is unallocated in imalloc space.
88 * IM_REGION_SUBSET - Region is a subset of a region that is already
89 * allocated in imalloc space.
90 * vm will be assigned to a ptr to the parent region.
91 * IM_REGION_EXISTS - Exact region already allocated in imalloc space.
92 * vm will be assigned to a ptr to the existing imlist
94 * IM_REGION_OVERLAPS - Region overlaps an allocated region in imalloc space.
95 * IM_REGION_SUPERSET - Region is a superset of a region that is already
96 * allocated in imalloc space.
98 static int im_region_status(unsigned long v_addr
, unsigned long size
,
99 struct vm_struct
**vm
)
101 struct vm_struct
*tmp
;
103 for (tmp
= imlist
; tmp
; tmp
= tmp
->next
)
104 if (v_addr
< (unsigned long) tmp
->addr
+ tmp
->size
)
108 if (im_region_overlaps(v_addr
, size
, tmp
))
109 return IM_REGION_OVERLAP
;
112 if (im_region_is_subset(v_addr
, size
, tmp
)) {
113 /* Return with tmp pointing to superset */
114 return IM_REGION_SUBSET
;
116 if (im_region_is_superset(v_addr
, size
, tmp
)) {
117 /* Return with tmp pointing to first subset */
118 return IM_REGION_SUPERSET
;
120 else if (v_addr
== (unsigned long) tmp
->addr
&&
122 /* Return with tmp pointing to exact region */
123 return IM_REGION_EXISTS
;
128 return IM_REGION_UNUSED
;
131 static struct vm_struct
* split_im_region(unsigned long v_addr
,
132 unsigned long size
, struct vm_struct
*parent
)
134 struct vm_struct
*vm1
= NULL
;
135 struct vm_struct
*vm2
= NULL
;
136 struct vm_struct
*new_vm
= NULL
;
138 vm1
= (struct vm_struct
*) kmalloc(sizeof(*vm1
), GFP_KERNEL
);
140 printk(KERN_ERR
"%s() out of memory\n", __FUNCTION__
);
144 if (v_addr
== (unsigned long) parent
->addr
) {
145 /* Use existing parent vm_struct to represent child, allocate
146 * new one for the remainder of parent range
148 vm1
->size
= parent
->size
- size
;
149 vm1
->addr
= (void *) (v_addr
+ size
);
150 vm1
->next
= parent
->next
;
155 } else if (v_addr
+ size
== (unsigned long) parent
->addr
+
157 /* Allocate new vm_struct to represent child, use existing
158 * parent one for remainder of parent range
161 vm1
->addr
= (void *) v_addr
;
162 vm1
->next
= parent
->next
;
165 parent
->size
-= size
;
168 /* Allocate two new vm_structs for the new child and
169 * uppermost remainder, and use existing parent one for the
170 * lower remainder of parent range
172 vm2
= (struct vm_struct
*) kmalloc(sizeof(*vm2
), GFP_KERNEL
);
174 printk(KERN_ERR
"%s() out of memory\n", __FUNCTION__
);
180 vm1
->addr
= (void *) v_addr
;
184 vm2
->size
= ((unsigned long) parent
->addr
+ parent
->size
) -
186 vm2
->addr
= (void *) v_addr
+ size
;
187 vm2
->next
= parent
->next
;
189 parent
->size
= v_addr
- (unsigned long) parent
->addr
;
196 static struct vm_struct
* __add_new_im_area(unsigned long req_addr
,
199 struct vm_struct
**p
, *tmp
, *area
;
201 for (p
= &imlist
; (tmp
= *p
) ; p
= &tmp
->next
) {
202 if (req_addr
+ size
<= (unsigned long)tmp
->addr
)
206 area
= (struct vm_struct
*) kmalloc(sizeof(*area
), GFP_KERNEL
);
210 area
->addr
= (void *)req_addr
;
218 static struct vm_struct
* __im_get_area(unsigned long req_addr
,
222 struct vm_struct
*tmp
;
225 status
= im_region_status(req_addr
, size
, &tmp
);
226 if ((criteria
& status
) == 0) {
231 case IM_REGION_UNUSED
:
232 tmp
= __add_new_im_area(req_addr
, size
);
234 case IM_REGION_SUBSET
:
235 tmp
= split_im_region(req_addr
, size
, tmp
);
237 case IM_REGION_EXISTS
:
238 /* Return requested region */
240 case IM_REGION_SUPERSET
:
241 /* Return first existing subset of requested region */
244 printk(KERN_ERR
"%s() unexpected imalloc region status\n",
252 struct vm_struct
* im_get_free_area(unsigned long size
)
254 struct vm_struct
*area
;
258 if (get_free_im_addr(size
, &addr
)) {
259 printk(KERN_ERR
"%s() cannot obtain addr for size 0x%lx\n",
265 area
= __im_get_area(addr
, size
, IM_REGION_UNUSED
);
268 "%s() cannot obtain area for addr 0x%lx size 0x%lx\n",
269 __FUNCTION__
, addr
, size
);
276 struct vm_struct
* im_get_area(unsigned long v_addr
, unsigned long size
,
279 struct vm_struct
*area
;
282 area
= __im_get_area(v_addr
, size
, criteria
);
287 unsigned long im_free(void * addr
)
289 struct vm_struct
**p
, *tmp
;
290 unsigned long ret_size
= 0;
294 if ((PAGE_SIZE
-1) & (unsigned long) addr
) {
295 printk(KERN_ERR
"Trying to %s bad address (%p)\n", __FUNCTION__
, addr
);
299 for (p
= &imlist
; (tmp
= *p
) ; p
= &tmp
->next
) {
300 if (tmp
->addr
== addr
) {
301 ret_size
= tmp
->size
;
309 printk(KERN_ERR
"Trying to %s nonexistent area (%p)\n", __FUNCTION__
,