source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-page.c @ 028bf82

55-freebsd-126-freebsd-12
Last change on this file since 028bf82 was 91fb6e3, checked in by Sebastian Huber <sebastian.huber@…>, on 11/21/16 at 13:57:57

rtems-kernel-page: Reclaim pages earlier

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2015 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-kernel-space.h>
41#include <machine/rtems-bsd-page.h>
42
43#include <sys/param.h>
44#include <sys/types.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <vm/uma.h>
50
51#include <stdlib.h>
52
53#include <rtems/bsd/bsd.h>
54#include <rtems/malloc.h>
55#include <rtems/rbheap.h>
56
57void **rtems_bsd_page_object_table;
58
59uintptr_t rtems_bsd_page_area_begin;
60
61static struct {
62        struct mtx mtx;
63        rtems_rbheap_control heap;
64        size_t free;
65        uint32_t reclaims;
66} page_alloc;
67
68void *
69rtems_bsd_page_alloc(uintptr_t size_in_bytes, int flags)
70{
71        void *addr;
72
73        mtx_lock(&page_alloc.mtx);
74
75        addr = rtems_rbheap_allocate(&page_alloc.heap, size_in_bytes);
76        if (addr == NULL && ((flags & M_WAITOK) != 0 ||
77            page_alloc.free < 32)) {
78                int i;
79
80                for (i = 0; i < 8; i++) {
81                        ++page_alloc.reclaims;
82                        mtx_unlock(&page_alloc.mtx);
83                        uma_reclaim();
84                        mtx_lock(&page_alloc.mtx);
85
86                        addr = rtems_rbheap_allocate(&page_alloc.heap,
87                            size_in_bytes);
88                        if (addr != NULL)
89                                break;
90
91                        msleep(&page_alloc.heap, &page_alloc.mtx, 0,
92                            "page alloc", (hz / 4) * (i + 1));
93                }
94
95                if (addr == NULL && (flags & M_WAITOK) != 0) {
96                        panic("rtems_bsd_page_alloc: page starvation");
97                }
98        }
99
100        page_alloc.free -= (addr != NULL) ? 1 : 0;
101        mtx_unlock(&page_alloc.mtx);
102
103#ifdef INVARIANTS
104        flags |= M_ZERO;
105#endif
106
107        if (addr != NULL && (flags & M_ZERO) != 0) {
108                memset(addr, 0, size_in_bytes);
109        }
110
111        return (addr);
112}
113
114void
115rtems_bsd_page_free(void *addr)
116{
117
118        mtx_lock(&page_alloc.mtx);
119        ++page_alloc.free;
120        rtems_rbheap_free(&page_alloc.heap, addr);
121        wakeup(&page_alloc.heap);
122        mtx_unlock(&page_alloc.mtx);
123}
124
125static void
126rtems_bsd_page_init(void *arg)
127{
128        rtems_status_code sc;
129        void *area;
130        void **obj_table;
131        rtems_rbheap_chunk *chunks;
132        size_t i;
133        size_t n;
134        uintptr_t heap_size;
135
136        mtx_init(&page_alloc.mtx, "page heap", NULL, MTX_RECURSE);
137
138        heap_size = rtems_bsd_get_allocator_domain_size(
139            RTEMS_BSD_ALLOCATOR_DOMAIN_PAGE);
140
141        area = rtems_heap_allocate_aligned_with_boundary(heap_size, PAGE_SIZE,
142            0);
143        BSD_ASSERT(area != NULL);
144
145        sc = rtems_rbheap_initialize(&page_alloc.heap, area, heap_size,
146            PAGE_SIZE, rtems_rbheap_extend_descriptors_with_malloc, NULL);
147        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
148
149        rtems_rbheap_set_extend_descriptors(&page_alloc.heap,
150            rtems_rbheap_extend_descriptors_never);
151
152        n = heap_size / PAGE_SIZE;
153        page_alloc.free = n;
154
155        chunks = malloc(n * sizeof(*chunks), M_RTEMS_HEAP, M_NOWAIT);
156        BSD_ASSERT(chunks != NULL);
157
158        for (i = 0; i < n; ++i) {
159                rtems_rbheap_add_to_spare_descriptor_chain(&page_alloc.heap,
160                    &chunks[i]);
161        }
162
163        obj_table = calloc(n, sizeof(*obj_table));
164
165        rtems_bsd_page_area_begin = (uintptr_t)area;
166        rtems_bsd_page_object_table = obj_table;
167}
168
169SYSINIT(rtems_bsd_page, SI_SUB_VM, SI_ORDER_FIRST, rtems_bsd_page_init, NULL);
Note: See TracBrowser for help on using the repository browser.