summaryrefslogtreecommitdiffstats
path: root/src/lib/mmap-anon.c
blob: fbb2c47f4cb672b345f108c5b86027449838940d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */

/* @UNSAFE: whole file */

#include "lib.h"
#include "mmap-util.h"

#include <fcntl.h>

#ifndef MAP_ANONYMOUS
#  ifdef MAP_ANON
#    define MAP_ANONYMOUS MAP_ANON
#  else
#    define MAP_ANONYMOUS 0
#  endif
#endif

#ifndef HAVE_LINUX_MREMAP

#include <sys/mman.h>

#define MMAP_SIGNATURE 0xdeadbeef

#define PAGE_ALIGN(size) \
	(((size) + (size_t)page_size-1) & ~(size_t)(page_size-1))

struct anon_header {
	unsigned int signature;
	size_t size;
};

static int page_size = 0;
static int header_size = 0;
static int zero_fd = -1;

static void movable_mmap_init(void)
{
#if MAP_ANONYMOUS == 0
	/* mmap()ing /dev/zero should be the same with some platforms */
	zero_fd = open("/dev/zero", O_RDWR);
	if (zero_fd == -1)
		i_fatal("Can't open /dev/zero for creating anonymous mmap: %m");
	fd_close_on_exec(zero_fd, TRUE);
#endif

	page_size = getpagesize();
	header_size = page_size;
}

void *mmap_anon(size_t length)
{
	struct anon_header *hdr;
	void *base;

	if (header_size == 0)
		movable_mmap_init();

	/* we need extra page to store the pieces which construct
	   the full mmap. also allocate only page-aligned mmap sizes. */
	length = PAGE_ALIGN(length + header_size);

	base = mmap(NULL, length, PROT_READ | PROT_WRITE,
		    MAP_ANONYMOUS | MAP_PRIVATE, zero_fd, 0);
	if (base == MAP_FAILED)
		return MAP_FAILED;

	/* initialize the header */
	hdr = base;
	hdr->signature = MMAP_SIGNATURE;
	hdr->size = length - header_size;

	return (char *) hdr + header_size;
}

static void *mremap_move(struct anon_header *hdr, size_t new_size)
{
	void *new_base;
	char *p;
	size_t block_size, old_size;

	new_base = mmap_anon(new_size);
	if (new_base == MAP_FAILED)
		return MAP_FAILED;

	/* If we're moving large memory areas, it takes less memory to
	   copy the memory pages in smaller blocks. */
	old_size = hdr->size;
	block_size = 1024*1024;

	p = (char *) hdr + header_size + hdr->size;
	do {
		if (block_size > old_size)
			block_size = old_size;
		p -= block_size;
		old_size -= block_size;

		memcpy((char *) new_base + old_size, p, block_size);
		if (munmap((void *) p, block_size) < 0)
			i_panic("munmap() failed: %m");
	} while (old_size != 0);

	if (munmap((void *) hdr, header_size) < 0)
		i_panic("munmap() failed: %m");

	return new_base;
}

void *mremap_anon(void *old_address, size_t old_size  ATTR_UNUSED,
		  size_t new_size, unsigned long flags)
{
	struct anon_header *hdr;

	if (old_address == NULL || old_address == MAP_FAILED) {
		errno = EINVAL;
		return MAP_FAILED;
	}

	hdr = (struct anon_header *) ((char *) old_address - header_size);
	if (hdr->signature != MMAP_SIGNATURE)
		i_panic("movable_mremap(): Invalid old_address");

	new_size = PAGE_ALIGN(new_size);

	if (new_size > hdr->size) {
		/* grow */
		if ((flags & MREMAP_MAYMOVE) == 0) {
			errno = ENOMEM;
			return MAP_FAILED;
		}

		return mremap_move(hdr, new_size);
	}

	if (new_size < hdr->size) {
		/* shrink */
		if (munmap((void *) ((char *) hdr + header_size + new_size),
			   hdr->size - new_size) < 0)
			i_panic("munmap() failed: %m");
		hdr->size = new_size;
	}

	return old_address;
}

int munmap_anon(void *start, size_t length ATTR_UNUSED)
{
	struct anon_header *hdr;

	if (start == NULL || start == MAP_FAILED) {
		errno = EINVAL;
		return -1;
	}

	hdr = (struct anon_header *) ((char *) start - header_size);
	if (hdr->signature != MMAP_SIGNATURE)
		i_panic("movable_munmap(): Invalid address");

	if (munmap((void *) hdr, hdr->size + header_size) < 0)
		i_panic("munmap() failed: %m");

	return 0;
}

#else

void *mmap_anon(size_t length)
{
	return mmap(NULL, length, PROT_READ | PROT_WRITE,
		    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
}

void *mremap_anon(void *old_address, size_t old_size, size_t new_size,
		  unsigned long flags)
{
	return mremap(old_address, old_size, new_size, flags);
}

int munmap_anon(void *start, size_t length)
{
	return munmap(start, length);
}

#endif