-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathklp_tc_11-faulting_cat.c
219 lines (192 loc) · 4.63 KB
/
klp_tc_11-faulting_cat.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* faulting_cat - cat like program with sleep on (write) fault at copy buffer
*
* Copyright (C) 2018 SUSE
* Author: Nicolai Stange
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <limits.h>
#include <time.h>
#include <linux/userfaultfd.h>
#define ME "faulting-cat"
static void usage(void)
{
fprintf(stderr, "usage: " ME " <fault_sleep_sec> <file>\n");
}
struct do_fault_data
{
unsigned long fault_sleep_sec;
int uffd;
void *buf;
size_t buf_size;
pthread_t kill_on_error;
};
static void* do_fault(void * d)
{
struct do_fault_data const *dfd = d;
struct uffd_msg msg;
struct uffdio_zeropage zp;
struct timespec ts = {
.tv_sec = dfd->fault_sleep_sec,
};
if (read(dfd->uffd, &msg, sizeof(msg)) < 0) {
perror(ME ": read userfaultfd");
pthread_kill(dfd->kill_on_error, SIGTERM);
return (void *)-1;
}
assert(msg.event == UFFD_EVENT_PAGEFAULT);
assert((void *)msg.arg.pagefault.address == dfd->buf);
nanosleep(&ts, NULL);
zp.mode = 0;
zp.range = (struct uffdio_range){
.start = msg.arg.pagefault.address,
.len = dfd->buf_size,
};
if(ioctl(dfd->uffd, UFFDIO_ZEROPAGE, &zp)) {
perror(ME ": ioctl UFFDIO_ZEROPAGE");
pthread_kill(dfd->kill_on_error, SIGTERM);
return (void *)-1;
}
return 0;
}
int main(int argc, char *argv[])
{
int r = 0;
unsigned long fault_sleep_sec;
char *endptr;
long page_size;
int fd;
void *buf;
ssize_t bytes_written, bytes_read;
size_t bytes_left;
int uffd;
struct uffdio_api api = { .api = UFFD_API };
struct uffdio_register reg;
struct do_fault_data dfd;
pthread_t do_fault_thread;
void *do_fault_thread_retval;
if (argc != 3) {
usage();
return 1;
}
fault_sleep_sec = strtoul(argv[1], &endptr, 10);
if (*endptr || endptr == argv[1]) {
fprintf(stderr,
ME ": error: could not parse fault_sleep_sec\n");
usage();
return 1;
}
page_size = sysconf(_SC_PAGESIZE);
if (page_size < 0) {
perror(ME ": sysconf _SC_PAGESIZE");
return 2;
}
fd = open(argv[2], O_RDONLY);
if (fd < 0) {
perror(ME ": open");
return 2;
}
uffd = syscall(__NR_userfaultfd, 0);
if (uffd < 0) {
perror(ME ": userfaultfd");
close(fd);
return 2;
}
if (ioctl(uffd, UFFDIO_API, &api)) {
perror(ME ": ioctl UFFDIO_API");
close(uffd);
close(fd);
return 2;
}
buf = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (buf == (void *)-1) {
perror(ME ": mmap");
close(uffd);
close(fd);
return 2;
}
reg.range = (struct uffdio_range){
.start = (__u64)buf,
.len = (size_t)page_size,
};
reg.mode = UFFDIO_REGISTER_MODE_MISSING;
if (ioctl(uffd, UFFDIO_REGISTER, ®)) {
perror(ME ": ioctl UFFDIO_REGISTER");
munmap(buf, (size_t)page_size);
close(uffd);
close(fd);
return 2;
}
dfd = (struct do_fault_data) {
.fault_sleep_sec = fault_sleep_sec,
.uffd = uffd,
.buf = buf,
.buf_size = (size_t)page_size,
.kill_on_error = pthread_self(),
};
if (pthread_create(&do_fault_thread, NULL, do_fault, &dfd)) {
perror(ME ": pthread_create");
munmap(buf, (size_t)page_size);
close(uffd);
close(fd);
return 2;
}
bytes_written = 0;
while ((bytes_read = read(fd, buf, (size_t)page_size)) > 0) {
bytes_left = bytes_read;
while (bytes_left) {
void *p = (char *)buf + bytes_read - bytes_left;
bytes_written = write(1, p, bytes_left);
if (bytes_written < 0) {
perror(ME ": write");
break;
}
bytes_left -= bytes_written;
}
if (bytes_written < 0)
break;
}
if (bytes_read < 0)
perror(ME ": read");
if (bytes_read < 0 || bytes_written < 0) {
r = 2;
}
pthread_cancel(do_fault_thread);
if (pthread_join(do_fault_thread, &do_fault_thread_retval)) {
perror(ME ": pthread_join");
r = r ? r : 2;
}
if (do_fault_thread_retval)
r = r ? : 2;
munmap(buf, (size_t)page_size);
close(uffd);
close(fd);
return r;
}