-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_record.c
487 lines (413 loc) · 11.1 KB
/
io_record.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include <linux/export.h>
#include <linux/compiler.h>
#include <linux/dax.h>
#include <linux/fs.h>
#include <linux/sched/signal.h>
#include <linux/uaccess.h>
#include <linux/capability.h>
#include <linux/kernel_stat.h>
#include <linux/gfp.h>
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/file.h>
#include <linux/uio.h>
#include <linux/hash.h>
#include <linux/writeback.h>
#include <linux/backing-dev.h>
#include <linux/pagevec.h>
#include <linux/blkdev.h>
#include <linux/security.h>
#include <linux/cpuset.h>
#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
#include <linux/hugetlb.h>
#include <linux/memcontrol.h>
#include <linux/cleancache.h>
#include <linux/rmap.h>
#include <linux/module.h>
#include <linux/io_record.h>
#include "internal.h"
struct io_info {
struct file *file;
struct inode *inode;
int offset;
int nr_pages;
};
#define NUM_IO_INFO_IN_BUF (128 * 1024) /* # of struct io_info */
#define RESULT_BUF_SIZE_IN_BYTES (5 * 1024 * 1024) /* 5MB */
#define RESULT_BUF_END_MAGIC (~0) /* -1 */
struct io_info *record_buf; /* array of struct io_info */
void *result_buf; /* buffer used for post processing result */
/*
* format in result buf per file:
* <A = length of "path", (size = sizeof(int))>
* <"path" string, (size = A)>
* <tuple array, (size = B * sizeof(int) * 2>
* <end MAGIC, (val = -1, size = sizeof(int) * 2>
*/
#define MAX_FILEPATH_LEN 256
/* return bytes written to the path. if buffer full, return < 0 */
void *result_buf_cursor; /* this is touched by post processing only */
void write_to_result_buf(void *src, int size)
{
memcpy(result_buf_cursor, src, size);
result_buf_cursor = result_buf_cursor + size;
}
/* this assumes that start_idx~end_idx belong to the same inode */
int fill_result_buf(int start_idx, int end_idx)
{
int ret = 0;
int i;
int size_expected;
char strbuf[MAX_FILEPATH_LEN];
char *path;
int pathsize;
int result_buf_used;
int prev_offset = -1;
int long max_size = 0;
void *buf_start;
struct file *file;
if (start_idx >= end_idx)
BUG_ON(1); /* this case is not in consideration */
file = record_buf[start_idx].file;
path = d_path(&file->f_path, strbuf, MAX_FILEPATH_LEN);
if (!path || IS_ERR(path))
goto out;
/* max size check (not strict) */
result_buf_used = result_buf_cursor - result_buf;
size_expected = sizeof(int) * 2 + /* end magic of this attempt */
sizeof(int) + strlen(path) + /* for path string */
sizeof(int) * 2 * (end_idx - start_idx) + /* data */
sizeof(int); /* end magic of post-processing */
if (size_expected > (RESULT_BUF_SIZE_IN_BYTES - result_buf_used))
return -EINVAL;
buf_start = result_buf_cursor;
pathsize = strlen(path);
write_to_result_buf(&pathsize, sizeof(int));
write_to_result_buf(path, pathsize);
/* fill the result buf using the record buf */
for (i = start_idx; i < end_idx; i++) {
if (prev_offset == -1) {
prev_offset = record_buf[i].offset;
max_size = record_buf[i].nr_pages;
continue;
}
/* in the last range */
if ((prev_offset + max_size) >=
(record_buf[i].offset + record_buf[i].nr_pages)) {
continue;
} else {
if ((prev_offset + max_size) >= record_buf[i].offset) {
max_size = (record_buf[i].offset +
record_buf[i].nr_pages) -
prev_offset;
} else {
write_to_result_buf(&prev_offset,
sizeof(int));
write_to_result_buf(&max_size,
sizeof(int));
prev_offset = record_buf[i].offset;
max_size = record_buf[i].nr_pages;
}
}
}
/* fill the record buf */
write_to_result_buf(&prev_offset, sizeof(int));
write_to_result_buf(&max_size, sizeof(int));
/* fill the record buf with final magic */
prev_offset = RESULT_BUF_END_MAGIC;
max_size = RESULT_BUF_END_MAGIC;
write_to_result_buf(&prev_offset, sizeof(int));
write_to_result_buf(&max_size, sizeof(int));
/* return # of bytes written to result buf */
ret = result_buf_cursor - buf_start;
out:
return ret;
}
/* idx record_buf_cursor of the array (record_buf) */
static atomic_t record_buf_cursor = ATOMIC_INIT(-1);
static DEFINE_RWLOCK(record_rwlock);
int record_target; /* pid # of group leader */
bool record_enable;
static DEFINE_MUTEX(status_lock);
enum io_record_cmd_types current_status = IO_RECORD_INIT;
static inline void set_record_status(bool enable)
{
write_lock(&record_rwlock);
record_enable = enable;
write_unlock(&record_rwlock);
}
/* assume caller has read lock of record_rwlock */
static inline bool __get_record_status(void)
{
return record_enable;
}
static inline void set_record_target(int pid)
{
write_lock(&record_rwlock);
record_target = pid;
write_unlock(&record_rwlock);
}
void release_records(void);
/* change the current status, and do the init jobs for the status */
static void change_current_status(enum io_record_cmd_types status)
{
switch (status) {
case IO_RECORD_INIT:
set_record_status(false);
set_record_target(-1);
release_records();
atomic_set(&record_buf_cursor, 0);
result_buf_cursor = result_buf;
break;
case IO_RECORD_START:
set_record_status(true);
break;
case IO_RECORD_STOP:
set_record_status(false);
break;
case IO_RECORD_POST_PROCESSING:
break;
case IO_RECORD_POST_PROCESSING_DONE:
break;
}
current_status = status;
}
/* Only this function contains the status change rules */
/* Assume that the caller has the status lock */
static inline bool change_status_if_valid(enum io_record_cmd_types next_status)
{
bool ret = false;
if (!record_buf)
return false;
if (next_status == IO_RECORD_INIT &&
current_status != IO_RECORD_POST_PROCESSING)
ret = true;
else if (next_status == (current_status + 1))
ret = true;
if (ret)
change_current_status(next_status);
return ret;
}
/*
* control :
* - record start
* - record end
* - data read
* hook :
* - syscall, or pagecache add or ...
* - filemap fault
* buffer : (to store records between record start and record end)
* post-processing : merge, etc.
* output : return post processing result to userspace...
*/
/* return 0 on success */
bool start_record(int pid)
{
bool ret = false;
mutex_lock(&status_lock);
if (!change_status_if_valid(IO_RECORD_START))
goto out;
set_record_target(pid);
ret = true;
out:
mutex_unlock(&status_lock);
return ret;
}
bool stop_record(void)
{
bool ret = false;
mutex_lock(&status_lock);
if (!change_status_if_valid(IO_RECORD_STOP))
goto out;
ret = true;
out:
mutex_unlock(&status_lock);
return ret;
}
#include <linux/sort.h>
static void io_info_swap(void *lhs, void *rhs, int size)
{
struct io_info tmp;
struct io_info *linfo = (struct io_info *)lhs;
struct io_info *rinfo = (struct io_info *)rhs;
memcpy(&tmp, linfo, sizeof(struct io_info));
memcpy(linfo, rinfo, sizeof(struct io_info));
memcpy(rinfo, &tmp, sizeof(struct io_info));
}
static int io_info_compare(const void *lhs, const void *rhs)
{
struct io_info *linfo = (struct io_info *)lhs;
struct io_info *rinfo = (struct io_info *)rhs;
if ((unsigned long)linfo->inode > (unsigned long)rinfo->inode)
return 1;
else if ((unsigned long)linfo->inode < (unsigned long)rinfo->inode)
return -1;
else {
if ((unsigned long)linfo->offset > (unsigned long)rinfo->offset)
return 1;
else if ((unsigned long)linfo->offset <
(unsigned long)rinfo->offset)
return -1;
else
return 0;
}
}
bool post_processing_records(void)
{
bool ret = false;
int i;
struct inode *prev_inode = NULL;
int start_idx = -1, end_idx = -1;
int last_magic = RESULT_BUF_END_MAGIC;
mutex_lock(&status_lock);
if (!change_status_if_valid(IO_RECORD_POST_PROCESSING))
goto out;
/* From this point, we assume that no one touches record buf */
/* sort based on inode pointer address */
sort(record_buf, atomic_read(&record_buf_cursor),
sizeof(struct io_info), &io_info_compare, &io_info_swap);
/* fill the result buf per inode */
for (i = 0; i < atomic_read(&record_buf_cursor); i++) {
if (prev_inode != record_buf[i].inode) {
end_idx = i;
if (prev_inode && (fill_result_buf(start_idx,
end_idx) < 0))
/* if result buf full, break without write */
break;
prev_inode = record_buf[i].inode;
start_idx = i;
}
}
if (start_idx != -1)
fill_result_buf(start_idx, i);
/* fill the last magic to indicate end of result */
write_to_result_buf(&last_magic, sizeof(int));
if (!change_status_if_valid(IO_RECORD_POST_PROCESSING_DONE))
BUG_ON(1); /* this is the case not in consideration */
ret = true;
out:
mutex_unlock(&status_lock);
return ret;
}
ssize_t read_record(char __user *buf, size_t count, loff_t *ppos)
{
int result_buf_size;
int ret;
mutex_lock(&status_lock);
if (current_status != IO_RECORD_POST_PROCESSING_DONE) {
ret = -EFAULT;
goto out;
}
result_buf_size = result_buf_cursor - result_buf;
if (*ppos >= result_buf_size) {
ret = 0;
goto out;
}
ret = (*ppos + count < result_buf_size) ? count :
(result_buf_size - *ppos);
if (copy_to_user(buf, result_buf + *ppos, ret)) {
ret = -EFAULT;
goto out;
}
*ppos = *ppos + ret;
out:
mutex_unlock(&status_lock);
return ret;
}
/*
* if this is not called explicitly by user processes, kernel should call this
* at some point.
*/
bool init_record(void)
{
bool ret = false;
mutex_lock(&status_lock);
if (!change_status_if_valid(IO_RECORD_INIT))
goto out;
ret = true;
out:
mutex_unlock(&status_lock);
return ret;
}
bool forced_init_record(void)
{
bool ret = false;
int loopnum = 0;
if (!record_buf)
goto out;
retry:
loopnum++;
ret = init_record();
if (!ret)
goto retry;
if (loopnum > 1)
pr_err("%s,%d: loopnum %d\n", __func__, __LINE__, loopnum);
ret = true;
out:
return ret;
}
void record_io_info(struct file *file, pgoff_t offset,
unsigned long req_size)
{
struct io_info *info;
int cnt;
/* check without lock */
if ((int)task_tgid_nr(current) != record_target)
return;
if (offset >= INT_MAX || req_size >= INT_MAX)
return;
cnt = atomic_read(&record_buf_cursor);
if (cnt < 0 || cnt >= NUM_IO_INFO_IN_BUF || !file || req_size == 0)
return;
if (!read_trylock(&record_rwlock))
return;
if (!__get_record_status())
goto out;
/* strict check */
if ((int)task_tgid_nr(current) != record_target)
goto out;
cnt = atomic_inc_return(&record_buf_cursor) - 1;
/* buffer is full */
if (cnt >= NUM_IO_INFO_IN_BUF) {
atomic_dec(&record_buf_cursor);
goto out;
}
info = record_buf + cnt;
get_file(file); /* will be put in release_records */
info->file = file;
info->inode = file_inode(file);
info->offset = (int)offset;
info->nr_pages = (int)req_size;
out:
read_unlock(&record_rwlock);
}
void release_records(void)
{
int i;
struct io_info *info;
for (i = 0; i < atomic_read(&record_buf_cursor); i++) {
info = record_buf + i;
fput(info->file);
}
}
static int __init io_record_init(void)
{
record_buf = vzalloc(sizeof(struct io_info) * NUM_IO_INFO_IN_BUF);
if (!record_buf)
goto record_buf_fail;
result_buf = vzalloc(RESULT_BUF_SIZE_IN_BYTES);
if (!result_buf)
goto result_buf_fail;
mutex_lock(&status_lock);
if (!change_status_if_valid(IO_RECORD_INIT))
BUG_ON(1); /* should success at boot time */
mutex_unlock(&status_lock);
return 0;
result_buf_fail:
vfree(record_buf);
record_buf_fail:
return -1;
}
module_init(io_record_init);