我相信您对mlock 所做的事情的理解有些错误。它的预期用途是:
- 断言不会因为数据尚未从磁盘加载或换出而等待从内存中读取数据(出于性能原因,对实时应用程序至关重要)。
- 断言页面不会被换出(对于密码或明文私钥等私人数据至关重要)。
因此它断言页面已加载到 RAM 中并防止它们被换出。无法保证它会阻止从文件映射的脏页的写回(实际上并没有,请参见下面的实验)。
要提示内核您将很快从 fd 进行一些 读取,有 posix_fadvise(),所以
posix_fadvise(fd, offset, len, POSIX_FADV_RANDOM);
可能会将文件的请求部分加载到页面缓存中。
我不能肯定地说,但我想实际上没有办法像现在那样禁止写回特定文件的脏页。可能有一些方法可以提示它,但我也没有看到。
mmap/mlock 实验
alexander@goblin ~/tmp $ cat mmap.c
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[]) {
char *addr;
int fd;
struct stat sb;
size_t length;
if (argc != 2) {
fprintf(stderr, "%s file\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDWR);
if (fd == -1) {
handle_error("open");
}
if (fstat(fd, &sb) == -1) { /* To obtain file size */
handle_error("fstat");
}
length = sb.st_size;
addr = mmap(NULL, length , PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
handle_error("mmap");
}
if(mlock(addr, length)<0) {
handle_error("mlock");
}
strcpy(addr, "hello world!");
sleep(100);
munmap(addr, length);
close(fd);
exit(EXIT_SUCCESS);
}
alexander@goblin ~/tmp $ grep . /proc/sys/vm/dirty_{expire,writeback}_centisecs
/proc/sys/vm/dirty_expire_centisecs:1000
/proc/sys/vm/dirty_writeback_centisecs:500
alexander@goblin ~/tmp $ dd if=/dev/zero of=foo bs=4k count=1
1+0 records in
1+0 records out
4096 bytes (4.1 kB, 4.0 KiB) copied, 8.1296e-05 s, 50.4 MB/s
alexander@goblin ~/tmp $ fallocate -l 4096 foo
alexander@goblin ~/tmp $ sync foo
alexander@goblin ~/tmp $ sudo hdparm --fibmap foo
foo:
filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
byte_offset begin_LBA end_LBA sectors
0 279061632 279061639 8
alexander@goblin ~/tmp $ sudo dd if=/dev/mapper/vg_main-gentoo_home count=8 skip=279061632 iflag=nocache 2>/dev/null | hexdump -C
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000
alexander@goblin ~/tmp $ gcc mmap.c
alexander@goblin ~/tmp $ ./a.out foo &
[1] 26450
alexander@goblin ~/tmp $ sudo hdparm --fibmap foo
foo:
filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
byte_offset begin_LBA end_LBA sectors
0 279061632 279061639 8
alexander@goblin ~/tmp $ sudo dd if=/dev/mapper/vg_main-gentoo_home count=8 skip=279061632 iflag=nocache 2>/dev/null | hexdump -C
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000
alexander@goblin ~/tmp $ sleep 10
alexander@goblin ~/tmp $ sudo hdparm --fibmap foo
foo:
filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
byte_offset begin_LBA end_LBA sectors
0 279061632 279061639 8
alexander@goblin ~/tmp $ sudo dd if=/dev/mapper/vg_main-gentoo_home count=8 skip=279061632 iflag=nocache 2>/dev/null | hexdump -C
00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 00 00 00 00 |hello world!....|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000
alexander@goblin ~/tmp $ fg
./a.out foo
^C