wok view linux/stuff/linux-freeinitrd.u @ rev 20721
updated arp-scan (1.9 -> 1.9.5)
author | Hans-G?nter Theisgen |
---|---|
date | Fri Feb 08 15:48:54 2019 +0100 (2019-02-08) |
parents | 708b5293dd29 |
children |
line source
1 --- linux-3.16.53/init/initramfs.c
2 +++ linux-3.16.53/init/initramfs.c
3 @@ -384,6 +384,52 @@
4 [Reset] = do_reset,
5 };
7 +#include <linux/initrd.h>
8 +#define INITRD_PAGE ((PAGE_SIZE > 64*1024) ? PAGE_SIZE : 64*1024)
9 +#define INITRD_DOT (1024*1024)
10 +
11 +static void free_rootfs_mem(unsigned long start, unsigned long end)
12 +{
13 + free_init_pages(NULL, start, end);
14 +}
15 +
16 +static void _free_initrd(unsigned long initrd_start, unsigned long initrd_end,
17 + void (*free_initrd_mem)(unsigned long, unsigned long));
18 +
19 +static struct {
20 + unsigned long offset, last, inptr, freed;
21 + char *max;
22 +} fill;
23 +
24 +static void release_inbuf(unsigned n)
25 +{
26 + if (n >= INITRD_PAGE) {
27 + unsigned long rem = n % INITRD_PAGE;
28 + unsigned long end = initrd_start + n - rem;
29 + _free_initrd(initrd_start, end, free_rootfs_mem);
30 + fill.freed += n - rem;
31 + if (fill.freed >= INITRD_DOT) {
32 + fill.freed -= INITRD_DOT;
33 + printk(".");
34 + }
35 + initrd_start = end;
36 + fill.offset = rem;
37 + }
38 +}
39 +
40 +static unsigned long fill_buffer(void *buffer, unsigned size)
41 +{
42 + unsigned long max = fill.max - (char *) initrd_start - fill.offset;
43 + if (max > size) max = size;
44 + if (max > INITRD_PAGE) max = INITRD_PAGE;
45 + memcpy(buffer, (void *)(initrd_start + fill.offset), max);
46 + release_inbuf(fill.offset);
47 + fill.offset += max;
48 + fill.inptr += fill.last;
49 + fill.last = max;
50 + return max;
51 +}
52 +
53 static int __init write_buffer(char *buf, unsigned len)
54 {
55 count = len;
56 @@ -427,6 +473,7 @@
57 decompress_fn decompress;
58 const char *compress_name;
59 static __initdata char msg_buf[64];
60 + int early_free_initrd = (buf == (char *) initrd_start);
62 header_buf = kmalloc(110, GFP_KERNEL);
63 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
64 @@ -440,11 +487,16 @@
65 message = NULL;
66 while (!message && len) {
67 loff_t saved_offset = this_header;
68 + fill.offset = buf - (char *) initrd_start;
69 + fill.max = buf + len;
70 + fill.inptr = fill.last = fill.freed = 0;
71 if (*buf == '0' && !(this_header & 3)) {
72 state = Start;
73 written = write_buffer(buf, len);
74 buf += written;
75 len -= written;
76 + if (early_free_initrd)
77 + release_inbuf(buf - (char *) initrd_start);
78 continue;
79 }
80 if (!*buf) {
81 @@ -457,7 +509,12 @@
82 decompress = decompress_method(buf, len, &compress_name);
83 pr_debug("Detected %s compressed data\n", compress_name);
84 if (decompress) {
85 - res = decompress(buf, len, NULL, flush_buffer, NULL,
86 + if (early_free_initrd) {
87 + res = decompress(NULL, 0, fill_buffer,
88 + flush_buffer, NULL, &my_inptr, error);
89 + my_inptr += fill.inptr;
90 + }
91 + else res = decompress(buf, len, NULL, flush_buffer, NULL,
92 &my_inptr, error);
93 if (res)
94 error("decompressor failed");
95 @@ -499,7 +556,8 @@
96 #include <linux/initrd.h>
97 #include <linux/kexec.h>
99 -static void __init free_initrd(void)
100 +static void _free_initrd(unsigned long initrd_start, unsigned long initrd_end,
101 + void (*free_initrd_mem)(unsigned long, unsigned long))
102 {
103 #ifdef CONFIG_KEXEC
104 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
105 @@ -527,6 +585,12 @@
106 #endif
107 free_initrd_mem(initrd_start, initrd_end);
108 skip:
109 + ;
110 +}
111 +
112 +static void __init free_initrd(void)
113 +{
114 + _free_initrd(initrd_start, initrd_end, free_initrd_mem);
115 initrd_start = 0;
116 initrd_end = 0;
117 }