xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock/mlock04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2010  Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This is a reproducer copied from one of LKML patch submission
10*49cdfc7eSAndroid Build Coastguard Worker  * which subject is
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * [PATCH] mlock: revert the optimization for dirtying pages and triggering writeback.
13*49cdfc7eSAndroid Build Coastguard Worker  * url see https://www.spinics.net/lists/kernel/msg1141090.html
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * "In 5ecfda0, we do some optimization in mlock, but it causes
16*49cdfc7eSAndroid Build Coastguard Worker  * a very basic test case(attached below) of mlock to fail. So
17*49cdfc7eSAndroid Build Coastguard Worker  * this patch revert it with some tiny modification so that it
18*49cdfc7eSAndroid Build Coastguard Worker  * apply successfully with the lastest 38-rc2 kernel."
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  * This bug was fixed by kernel
21*49cdfc7eSAndroid Build Coastguard Worker  * commit fdf4c587a7 ("mlock: operate on any regions with protection != PROT_NONE")
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * As this case does, mmaps a file with PROT_WRITE permissions but without
24*49cdfc7eSAndroid Build Coastguard Worker  * PROT_READ, so attempt to not unnecessarity break COW during mlock ended up
25*49cdfc7eSAndroid Build Coastguard Worker  * causing mlock to fail with a permission problem on unfixed kernel.
26*49cdfc7eSAndroid Build Coastguard Worker  */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1, file_len = 40960;
35*49cdfc7eSAndroid Build Coastguard Worker static char *testfile = "test_mlock";
36*49cdfc7eSAndroid Build Coastguard Worker 
verify_mlock(void)37*49cdfc7eSAndroid Build Coastguard Worker static void verify_mlock(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	char *buf;
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	buf = SAFE_MMAP(NULL, file_len, PROT_WRITE, MAP_SHARED, fd, 0);
42*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(mlock(buf, file_len), "mlock(%p, %d)", buf, file_len);
43*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNLOCK(buf, file_len);
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(buf, file_len);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(testfile, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FTRUNCATE(fd, file_len);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)53*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > -1)
56*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
60*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
61*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
62*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
63*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_mlock,
64*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
65*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "fdf4c587a793"},
66*49cdfc7eSAndroid Build Coastguard Worker 		{}
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker };
69