syscalls/mmap14: Rewrite test using new LTP API
PATCH v3 1/1 syscalls/mmap14: Rewrite test using new LTP API
--- testcases/kernel/syscalls/mmap/mmap14.c | 135 ++++++++---------------- 1 file changed, 45 insertions(+), 90 deletions(-)
testcases/kernel/syscalls/mmap/mmap14.c +45 -90
@@ -1,124 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2013 FNST, DAN LI <[email protected]> -* -* 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, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) 2024 SUSE LLC Avinesh Kumar <[email protected]> */ -/* -* Test Description: -* Verify MAP_LOCKED works fine. -* "Lock the pages of the mapped region into memory in the manner of mlock(2)." +/*\ +* [Description] * -* Expected Result: -* mmap() should succeed returning the address of the mapped region, -* and this region should be locked into memory. +* Verify that, mmap() call with MAP_LOCKED flag successfully locks +* the mapped pages into memory. */ -#include <stdio.h> -#include <sys/mman.h> -#include "test.h" +#include <stdio.h> +#include "tst_test.h" -#define TEMPFILE "mmapfile" -#define MMAPSIZE (1UL<<20) +#define MMAPSIZE (1UL<<22)#define LINELEN 256 -char *TCID ="mmap14" ; -int TST_TOTAL =1 ; -static char *addr; -static void getvmlck (unsigned int *lock_sz); -static void setup (void ); -static void cleanup (void ); +static struct rlimit rlim; +static unsigned int sz_before; -int main (int argc,char *argv[]) +static void setup (void ) { -int lc; -unsigned int sz_before; -unsigned int sz_after; -unsigned int sz_ch; - -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; +SAFE_GETRLIMIT (RLIMIT_MEMLOCK , &rlim); -getvmlck (&sz_before); +getvmlck (&sz_before); - addr =mmap (NULL ,MMAPSIZE ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_LOCKED |MAP_ANONYMOUS , --1 ,0 ); - -if (addr ==MAP_FAILED ) { -tst_resm (TFAIL |TERRNO ,"mmap of %s failed" ,TEMPFILE ); -continue ; - } - -getvmlck (&sz_after); - - sz_ch = sz_after - sz_before; -if (sz_ch ==MMAPSIZE /1024 ) { -tst_resm (TPASS ,"Functionality of mmap() " -"successful" ); - }else { -tst_resm (TFAIL ,"Expected %luK locked, " -"get %uK locked" , -MMAPSIZE /1024 , sz_ch); - } - -if (munmap (addr,MMAPSIZE ) !=0 ) -tst_brkm (TFAIL |TERRNO ,NULL ,"munmap failed" ); - } - -cleanup (); -tst_exit (); +if (((sz_before *1024 ) +MMAPSIZE ) > rlim.rlim_cur) +tst_brk (TCONF ,"Trying to exceed RLIMIT_MEMLOCK limit" ); }void getvmlck (unsigned int *lock_sz) { -int ret;char line[LINELEN ]; -FILE *fstatus =NULL ; +FILE *fp =NULL ; - fstatus =fopen ("/proc/self/status" ,"r" ); -if (fstatus ==NULL ) -tst_brkm (TFAIL |TERRNO ,NULL ,"Open dev status failed" ); + fp =fopen ("/proc/self/status" ,"r" ); +if (fp ==NULL ) +tst_brk (TFAIL |TERRNO ,"could not open status file" ); -while (fgets (line,LINELEN , fstatus) !=NULL ) +while (fgets (line,LINELEN , fp) !=NULL ) {if (strstr (line,"VmLck" ) !=NULL )break ; + } - ret =sscanf (line,"%*[^0-9]%d%*[^0-9]" , lock_sz); -if (ret !=1 ) -tst_brkm (TFAIL |TERRNO ,NULL ,"Get lock size failed" ); +if (sscanf (line,"%*[^0-9]%d%*[^0-9]" , lock_sz) !=1 ) +tst_brk (TFAIL |TERRNO ,"Getting locked memory size failed" ); -fclose (fstatus); +fclose (fp); } -static void setup (void ) +static void run (void ) { -tst_require_root (); +unsigned int sz_after, sz_diff; -tst_sig (FORK ,DEF_HANDLER , cleanup); + addr =mmap (NULL ,MMAPSIZE ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_LOCKED |MAP_ANONYMOUS ,-1 ,0 ); -TEST_PAUSE ; -} +if (addr !=MAP_FAILED ) { +tst_res (TPASS ,"mmap() with MAP_LOCKED flag passed" ); + }else { +tst_res (TFAIL |TERRNO ,"mmap() failed" ); +return ; + } -static void cleanup (void ) -{ +getvmlck (&sz_after); + sz_diff = sz_after - sz_before; +TST_EXP_EQ_LU (MMAPSIZE /1024 , sz_diff); + +SAFE_MUNMAP (addr,MMAPSIZE ); } + +static struct tst_test test = { + .setup = setup, + .test_all = run +};
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Avinesh,
Quoted 1 line
testcases/kernel/syscalls/mmap/mmap14.c +0
-0
...
Quoted 13 lines
testcases/kernel/syscalls/mmap/mmap14.c +4
-7-/* -* Test Description: -* Verify MAP_LOCKED works fine. -* "Lock the pages of the mapped region into memory in the manner of mlock(2)." +/*\ +* [Description] * -* Expected Result: -* mmap() should succeed returning the address of the mapped region, -* and this region should be locked into memory. +* Verify that, mmap() call with MAP_LOCKED flag successfully locks +* the mapped pages into memory. */
testcases/kernel/syscalls/mmap/mmap14.c line 17
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap14.c +3
-5-#include <stdio.h> -#include <sys/mman.h> -#include "test.h" +#include <stdio.h> +#include "tst_test.h" -#define TEMPFILE "mmapfile" -#define MMAPSIZE (1UL<<20) +#define MMAPSIZE (1UL<<22)
I wonder why it was needed to increase the map size. Anyway, even with 1UL<<20 it's too high for old SLES 15-SP1.
ulimit -l prints 64 (kB => 65536) ...
Quoted 3 lines
testcases/kernel/syscalls/mmap/mmap14.c +2
-0+static void setup (void ) { +SAFE_GETRLIMIT (RLIMIT_MEMLOCK , &rlim);
...
testcases/kernel/syscalls/mmap/mmap14.c line 32
Quoted 2 lines
testcases/kernel/syscalls/mmap/mmap14.c +2
-0+if (((sz_before *1024 ) +MMAPSIZE ) > rlim.rlim_cur) +tst_brk (TCONF ,"Trying to exceed RLIMIT_MEMLOCK limit" );
... therefore it TCONF here in the setup function.
But disabling this check it runs OK. I would expect we would get EAGAIN (according to man "The file has been locked, or too much memory has been locked") or MAP_FAILED or ENOMEM (but man says: "This implementation will try to populate (prefault) the whole range but the mmap() call doesn't fail with ENOMEM if this fails.")
Just experimenting, to increase to 1UL<<40 I get ENOMEM, doing really too huge 1UL<<200 I get EINVAL.
Therefore I would say the check in this way is wrong.
Or, could we just run SAFE_SETRLIMIT(RLIMIT_MEMLOCK, ...) in case of low limit and increase it in cleanup?
testcases/kernel/syscalls/mmap/mmap14.c line 40
Quoted 13 lines
}
void
getvmlck(unsigned int *lock_sz)
{testcases/kernel/syscalls/mmap/mmap14.c +2
-5-int ret;char line[LINELEN ]; -FILE *fstatus =NULL ; +FILE *fp =NULL ; - fstatus =fopen ("/proc/self/status" ,"r" ); -if (fstatus ==NULL ) -tst_brkm (TFAIL |TERRNO ,NULL ,"Open dev status failed" ); + fp =fopen ("/proc/self/status" ,"r" );
SAFE_FOPEN() (requires #include "tst_safe_stdio.h") and 2 lines below aren't needed.
Quoted 4 lines
testcases/kernel/syscalls/mmap/mmap14.c +2
-1+if (fp ==NULL ) +tst_brk (TFAIL |TERRNO ,"could not open status file" ); -while (fgets (line,LINELEN , fstatus) !=NULL )
testcases/kernel/syscalls/mmap/mmap14.c line 50
Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap14.c +4
-3+while (fgets (line,LINELEN , fp) !=NULL ) {if (strstr (line,"VmLck" ) !=NULL )break ; + } - ret =sscanf (line,"%*[^0-9]%d%*[^0-9]" , lock_sz); -if (ret !=1 ) -tst_brkm (TFAIL |TERRNO ,NULL ,"Get lock size failed" ); +if (sscanf (line,"%*[^0-9]%d%*[^0-9]" , lock_sz) !=1 ) +tst_brk (TFAIL |TERRNO ,"Getting locked memory size failed" );
I would say this should be tst_brk(TFAIL | TERRNO) (it's not directly related to the testing, more to the preparation). Also I hope Andrea Manzini will add his first easyhack for SAFE_SSCANF() and he will use it :).
The rest LGTM.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp