OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [mmnommu/] [page_alloc.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/mm/page_alloc.c
3
 *
4
 *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
5
 *  Swap reorganised 29.12.95, Stephen Tweedie
6
 */
7
 
8
/*
9
 * uClinux revisions for NO_MM
10
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
11
 *                     The Silver Hammer Group, Ltd.
12
 * Copyright (C) 1999  D. Jeff Dionne <jeff@uclinux.org>,
13
 *                     Rt-Control, Inc.
14
 */
15
 
16
#include <linux/config.h>
17
#include <linux/mm.h>
18
#include <linux/sched.h>
19
#include <linux/head.h>
20
#include <linux/kernel.h>
21
#include <linux/kernel_stat.h>
22
#include <linux/errno.h>
23
#include <linux/string.h>
24
#include <linux/stat.h>
25
#include <linux/swap.h>
26
#include <linux/fs.h>
27
#include <linux/swapctl.h>
28
#include <linux/interrupt.h>
29
 
30
#include <asm/dma.h>
31
#include <asm/system.h> /* for cli()/sti() */
32
#include <asm/segment.h> /* for memcpy_to/fromfs */
33
#include <asm/bitops.h>
34
#include <asm/pgtable.h>
35
 
36
int nr_swap_pages = 0;
37
int nr_free_pages = 0;
38
 
39
extern struct wait_queue *buffer_wait;
40
 
41
/*
42
 * Free area management
43
 *
44
 * The free_area_list arrays point to the queue heads of the free areas
45
 * of different sizes
46
 */
47
 
48
#ifdef BIGALLOCS
49
#define NR_MEM_LISTS 9
50
#else
51
#define NR_MEM_LISTS 7
52
#endif
53
 
54
/* The start of this MUST match the start of "struct page" */
55
struct free_area_struct {
56
        struct page *next;
57
        struct page *prev;
58
        unsigned int * map;
59
};
60
 
61
#define memory_head(x) ((struct page *)(x))
62
 
63
static struct free_area_struct free_area[NR_MEM_LISTS];
64
 
65
static inline void init_mem_queue(struct free_area_struct * head)
66
{
67
        head->next = memory_head(head);
68
        head->prev = memory_head(head);
69
}
70
 
71
static inline void add_mem_queue(struct free_area_struct * head, struct page * entry)
72
{
73
        struct page * next = head->next;
74
 
75
        entry->prev = memory_head(head);
76
        entry->next = next;
77
        next->prev = entry;
78
        head->next = entry;
79
}
80
 
81
static inline void remove_mem_queue(struct page * entry)
82
{
83
        struct page * next = entry->next;
84
        struct page * prev = entry->prev;
85
        next->prev = prev;
86
        prev->next = next;
87
}
88
 
89
/*
90
 * Free_page() adds the page to the free lists. This is optimized for
91
 * fast normal cases (no error jumps taken normally).
92
 *
93
 * The way to optimize jumps for gcc-2.2.2 is to:
94
 *  - select the "normal" case and put it inside the if () { XXX }
95
 *  - no else-statements if you can avoid them
96
 *
97
 * With the above two rules, you get a straight-line execution path
98
 * for the normal case, giving better asm-code.
99
 *
100
 * free_page() may sleep since the page being freed may be a buffer
101
 * page or present in the swap cache. It will not sleep, however,
102
 * for a freshly allocated page (get_free_page()).
103
 */
104
 
105
/*
106
 * Buddy system. Hairy. You really aren't expected to understand this
107
 *
108
 * Hint: -mask = 1+~mask
109
 */
110
static inline void free_pages_ok(unsigned long map_nr, unsigned long order)
111
{
112
        struct free_area_struct *area = free_area + order;
113
        unsigned long index = map_nr >> (1 + order);
114
        unsigned long mask = (~0UL) << order;
115
        unsigned long flags;
116
 
117
        save_flags(flags);
118
        cli();
119
 
120
#define list(x) (mem_map+(x))
121
 
122
        map_nr &= mask;
123
        nr_free_pages -= mask;
124
        while (mask + (1 << (NR_MEM_LISTS-1))) {
125
                if (!change_bit(index, area->map))
126
                        break;
127
                remove_mem_queue(list(map_nr ^ -mask));
128
                mask <<= 1;
129
                area++;
130
                index >>= 1;
131
                map_nr &= mask;
132
        }
133
        add_mem_queue(area, list(map_nr));
134
 
135
#undef list
136
 
137
        restore_flags(flags);
138
        if (!waitqueue_active(&buffer_wait))
139
                return;
140
        wake_up(&buffer_wait);
141
}
142
 
143
#ifdef DEBUG_FREE_PAGES
144
 
145
#undef __free_page
146
void __free_page_flf(struct page *page, char*file, int line, char*function)
147
{
148
        printk("Freeing page %p from %s @%s:%d\n", page, function, file, line);
149
        __free_page(page);
150
}
151
 
152
#undef free_pages
153
void free_pages_flf(unsigned long addr, unsigned long order, char*file, int line, char*function)
154
{
155
        printk("Freeing %lu byte page %lx from %s @%s:%d\n", 4096 << order, addr, function, file, line);
156
        free_pages(addr, order);
157
}
158
 
159
#undef __get_free_pages
160
unsigned long __get_free_pages_flf(int priority, unsigned long order, int dma, char * file, int line, char * function)
161
{
162
        printk("Allocating %d byte page from %s @%s:%d\n", 4096 << order, function, file, line);
163
        return __get_free_pages(priority, order, dma);
164
}
165
 
166
#undef get_free_page
167
unsigned long get_free_page_flf(int priority, char * file, int line, char * function)
168
{
169
        void * result = (void*)__get_free_pages_flf(priority, 0, 0, file, line, function);
170
        if (result)
171
                memset(result, 0, PAGE_SIZE);
172
        return result;
173
}
174
 
175
#endif /* DEBUG_FREE_PAGES */
176
 
177
void __free_page(struct page *page)
178
{
179
        if (!PageReserved(page) && atomic_dec_and_test(&page->count)) {
180
                unsigned long map_nr = page->map_nr;
181
 
182
                free_pages_ok(map_nr, 0);
183
        }
184
}
185
 
186
void free_pages(unsigned long addr, unsigned long order)
187
{
188
        unsigned long map_nr = MAP_NR(addr);
189
 
190
        if (map_nr < MAP_NR(high_memory)) {
191
                mem_map_t * map = mem_map + map_nr;
192
                if (PageReserved(map))
193
                        return;
194
                if (atomic_dec_and_test(&map->count)) {
195
 
196
                        free_pages_ok(map_nr, order);
197
                        return;
198
                }
199
        }
200
}
201
 
202
/*
203
 * Some ugly macros to speed up __get_free_pages()..
204
 */
205
#define MARK_USED(index, order, area) \
206
        change_bit((index) >> (1+(order)), (area)->map)
207
#define CAN_DMA(x) (PageDMA(x))
208
#define ADDRESS(x) (PAGE_OFFSET + ((x) << PAGE_SHIFT))
209
#define RMQUEUE(order, dma) \
210
do { struct free_area_struct * area = free_area+order; \
211
     unsigned long new_order = order; \
212
        do { struct page *prev = memory_head(area), *ret; \
213
                while (memory_head(area) != (ret = prev->next)) { \
214
                        if (!dma || CAN_DMA(ret)) { \
215
                                unsigned long map_nr = ret->map_nr; \
216
                                (prev->next = ret->next)->prev = prev; \
217
                                MARK_USED(map_nr, new_order, area); \
218
                                nr_free_pages -= 1 << order; \
219
                                EXPAND(ret, map_nr, order, new_order, area); \
220
                                restore_flags(flags); \
221
                                return ADDRESS(map_nr); \
222
                        } \
223
                        prev = ret; \
224
                } \
225
                new_order++; area++; \
226
        } while (new_order < NR_MEM_LISTS); \
227
} while (0)
228
 
229
#define EXPAND(map,index,low,high,area) \
230
do { unsigned long size = 1 << high; \
231
        while (high > low) { \
232
                area--; high--; size >>= 1; \
233
                add_mem_queue(area, map); \
234
                MARK_USED(index, high, area); \
235
                index += size; \
236
                map += size; \
237
        } \
238
        map->count = 1; \
239
        map->age = PAGE_INITIAL_AGE; \
240
} while (0)
241
 
242
unsigned long __get_free_pages(int priority, unsigned long order, int dma)
243
{
244
        unsigned long flags;
245
        int reserved_pages;
246
 
247
        if (order >= NR_MEM_LISTS)
248
                return 0;
249
        if (intr_count && priority != GFP_ATOMIC) {
250
                static int count = 0;
251
                if (++count < 5) {
252
                        printk("gfp called nonatomically from interrupt %p\n",
253
                                __builtin_return_address(0));
254
                        priority = GFP_ATOMIC;
255
                }
256
        }
257
        reserved_pages = 5;
258
#ifndef CONFIG_REDUCED_MEMORY
259
        if (priority != GFP_NFS)
260
                reserved_pages = min_free_pages;
261
        if ((priority == GFP_BUFFER || priority == GFP_IO) && reserved_pages >= 48)
262
                reserved_pages -= (12 + (reserved_pages>>3));
263
#endif /* !CONFIG_REDUCED_MEMORY */
264
        save_flags(flags);
265
repeat:
266
        cli();
267
        if ((priority==GFP_ATOMIC) || nr_free_pages > reserved_pages) {
268
                RMQUEUE(order, dma);
269
                restore_flags(flags);
270
#ifdef DEBUG
271
                printk("fragmentation preventing allocation, re-attempting to free\n");
272
#endif
273
        }
274
        restore_flags(flags);
275
        if (priority != GFP_BUFFER && try_to_free_page(priority, dma, 1))
276
                goto repeat;
277
        return 0;
278
}
279
 
280
/*
281
 * Show free area list (used inside shift_scroll-lock stuff)
282
 * We also calculate the percentage fragmentation. We do this by counting the
283
 * memory on each free list with the exception of the first item on the list.
284
 */
285
 
286
/*
287
 * That's as may be, but I added an explicit fragmentation percentage, just
288
 * to make it obvious. -kja
289
 */
290
 
291
/* totals held by do_mmap to compute memory wastage */
292
unsigned long realalloc, askedalloc;
293
 
294
void show_free_areas(void)
295
{
296
        unsigned long order, flags;
297
        unsigned long total = 0;
298
        unsigned long fragmented = 0;
299
        unsigned long slack;
300
 
301
        printk("Free pages:      %6dkB\n ( ",nr_free_pages<<(PAGE_SHIFT-10));
302
        save_flags(flags);
303
        cli();
304
        for (order=0 ; order < NR_MEM_LISTS; order++) {
305
                struct page * tmp;
306
                unsigned long nr = 0;
307
                for (tmp = free_area[order].next ; tmp != memory_head(free_area+order) ; tmp = tmp->next) {
308
                        nr ++;
309
                }
310
                total += nr * ((PAGE_SIZE>>10) << order);
311
                if ((nr > 1) && (order < (NR_MEM_LISTS-1)))
312
                        fragmented += (nr-1) * (1 << order);
313
                printk("%lu*%lukB ", nr, (PAGE_SIZE>>10) << order);
314
        }
315
        restore_flags(flags);
316
        fragmented *= 100;
317
        fragmented /= nr_free_pages;
318
 
319
        if (realalloc)
320
                slack = (realalloc-askedalloc) * 100 / realalloc;
321
        else
322
                slack = 0;
323
 
324
        printk("= %lukB, %%%lu frag, %%%lu slack)\n", total, fragmented, slack);
325
 
326
#ifdef SWAP_CACHE_INFO
327
        show_swap_cache_info();
328
#endif  
329
}
330
 
331
#define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
332
 
333
/*
334
 * set up the free-area data structures:
335
 *   - mark all pages reserved
336
 *   - mark all memory queues empty
337
 *   - clear the memory bitmaps
338
 */
339
unsigned long free_area_init(unsigned long start_mem, unsigned long end_mem)
340
{
341
        mem_map_t * p;
342
        unsigned long mask = PAGE_MASK;
343
        int i;
344
 
345
        /*
346
         * select nr of pages we try to keep free for important stuff
347
         * with a minimum of 48 pages. This is totally arbitrary
348
         */
349
        i = (end_mem - PAGE_OFFSET) >> (PAGE_SHIFT+7);
350
        if (i < 24)
351
                i = 24;
352
        i += 24;   /* The limit for buffer pages in __get_free_pages is
353
                    * decreased by 12+(i>>3) */
354
        min_free_pages = i;
355
        free_pages_low = i + (i>>1);
356
        free_pages_high = i + i;
357
 
358
        mem_map = (mem_map_t *) start_mem;
359
        p = mem_map + MAP_NR(end_mem);
360
        start_mem = LONG_ALIGN((unsigned long) p);
361
        memset(mem_map, 0, start_mem - (unsigned long) mem_map);
362
        do {
363
                --p;
364
                p->flags = (1 << PG_DMA) | (1 << PG_reserved);
365
                p->map_nr = p - mem_map;
366
        } while (p > mem_map);
367
 
368
        for (i = 0 ; i < NR_MEM_LISTS ; i++) {
369
                unsigned long bitmap_size;
370
                init_mem_queue(free_area+i);
371
                mask += mask;
372
                end_mem = (end_mem + ~mask) & mask;
373
                bitmap_size = (end_mem - PAGE_OFFSET) >> (PAGE_SHIFT + i);
374
                bitmap_size = (bitmap_size + 7) >> 3;
375
                bitmap_size = LONG_ALIGN(bitmap_size);
376
                free_area[i].map = (unsigned int *) start_mem;
377
                memset((void *) start_mem, 0, bitmap_size);
378
                start_mem += bitmap_size;
379
        }
380
        return start_mem;
381
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.