@@ -121,13 +121,76 @@ static void test_hugepage(int pagemap_fd, int pagesize)
121
121
free (map );
122
122
}
123
123
124
+ static void test_mprotect (int pagemap_fd , int pagesize , bool anon )
125
+ {
126
+ const char * type [] = {"file" , "anon" };
127
+ const char * fname = "./soft-dirty-test-file" ;
128
+ int test_fd ;
129
+ char * map ;
130
+
131
+ if (anon ) {
132
+ map = mmap (NULL , pagesize , PROT_READ |PROT_WRITE ,
133
+ MAP_ANONYMOUS |MAP_PRIVATE , -1 , 0 );
134
+ if (!map )
135
+ ksft_exit_fail_msg ("anon mmap failed\n" );
136
+ } else {
137
+ test_fd = open (fname , O_RDWR | O_CREAT );
138
+ if (test_fd < 0 ) {
139
+ ksft_test_result_skip ("Test %s open() file failed\n" , __func__ );
140
+ return ;
141
+ }
142
+ unlink (fname );
143
+ ftruncate (test_fd , pagesize );
144
+ map = mmap (NULL , pagesize , PROT_READ |PROT_WRITE ,
145
+ MAP_SHARED , test_fd , 0 );
146
+ if (!map )
147
+ ksft_exit_fail_msg ("file mmap failed\n" );
148
+ }
149
+
150
+ * map = 1 ;
151
+ ksft_test_result (pagemap_is_softdirty (pagemap_fd , map ) == 1 ,
152
+ "Test %s-%s dirty bit of new written page\n" ,
153
+ __func__ , type [anon ]);
154
+ clear_softdirty ();
155
+ ksft_test_result (pagemap_is_softdirty (pagemap_fd , map ) == 0 ,
156
+ "Test %s-%s soft-dirty clear after clear_refs\n" ,
157
+ __func__ , type [anon ]);
158
+ mprotect (map , pagesize , PROT_READ );
159
+ ksft_test_result (pagemap_is_softdirty (pagemap_fd , map ) == 0 ,
160
+ "Test %s-%s soft-dirty clear after marking RO\n" ,
161
+ __func__ , type [anon ]);
162
+ mprotect (map , pagesize , PROT_READ |PROT_WRITE );
163
+ ksft_test_result (pagemap_is_softdirty (pagemap_fd , map ) == 0 ,
164
+ "Test %s-%s soft-dirty clear after marking RW\n" ,
165
+ __func__ , type [anon ]);
166
+ * map = 2 ;
167
+ ksft_test_result (pagemap_is_softdirty (pagemap_fd , map ) == 1 ,
168
+ "Test %s-%s soft-dirty after rewritten\n" ,
169
+ __func__ , type [anon ]);
170
+
171
+ munmap (map , pagesize );
172
+
173
+ if (!anon )
174
+ close (test_fd );
175
+ }
176
+
177
+ static void test_mprotect_anon (int pagemap_fd , int pagesize )
178
+ {
179
+ test_mprotect (pagemap_fd , pagesize , true);
180
+ }
181
+
182
+ static void test_mprotect_file (int pagemap_fd , int pagesize )
183
+ {
184
+ test_mprotect (pagemap_fd , pagesize , false);
185
+ }
186
+
124
187
int main (int argc , char * * argv )
125
188
{
126
189
int pagemap_fd ;
127
190
int pagesize ;
128
191
129
192
ksft_print_header ();
130
- ksft_set_plan (5 );
193
+ ksft_set_plan (15 );
131
194
132
195
pagemap_fd = open (PAGEMAP_FILE_PATH , O_RDONLY );
133
196
if (pagemap_fd < 0 )
@@ -138,6 +201,8 @@ int main(int argc, char **argv)
138
201
test_simple (pagemap_fd , pagesize );
139
202
test_vma_reuse (pagemap_fd , pagesize );
140
203
test_hugepage (pagemap_fd , pagesize );
204
+ test_mprotect_anon (pagemap_fd , pagesize );
205
+ test_mprotect_file (pagemap_fd , pagesize );
141
206
142
207
close (pagemap_fd );
143
208
0 commit comments