2019-06-04 10:11:32 +02:00
// SPDX-License-Identifier: GPL-2.0-only
2015-09-04 15:47:23 -07:00
/*
* Stress userfaultfd syscall .
*
* Copyright ( C ) 2015 Red Hat , Inc .
*
* This test allocates two virtual areas and bounces the physical
* memory across the two virtual areas ( from area_src to area_dst )
* using userfaultfd .
*
* There are three threads running per CPU :
*
* 1 ) one per - CPU thread takes a per - page pthread_mutex in a random
* page of the area_dst ( while the physical page may still be in
* area_src ) , and increments a per - page counter in the same page ,
* and checks its value against a verification region .
*
* 2 ) another per - CPU thread handles the userfaults generated by
* thread 1 above . userfaultfd blocking reads or poll ( ) modes are
* exercised interleaved .
*
* 3 ) one last per - CPU thread transfers the memory in the background
* at maximum bandwidth ( if not already transferred by thread
* 2 ) . Each cpu thread takes cares of transferring a portion of the
* area .
*
* When all threads of type 3 completed the transfer , one bounce is
* complete . area_src and area_dst are then swapped . All threads are
* respawned and so the bounce is immediately restarted in the
* opposite direction .
*
* per - CPU threads 1 by triggering userfaults inside
* pthread_mutex_lock will also verify the atomicity of the memory
* transfer ( UFFDIO_COPY ) .
*/
2023-04-12 12:42:41 -04:00
# include "uffd-common.h"
2018-06-13 21:31:43 -06:00
2015-09-22 14:58:58 -07:00
# ifdef __NR_userfaultfd
2015-09-04 15:47:23 -07:00
# define BOUNCE_RANDOM (1<<0)
# define BOUNCE_RACINGFAULTS (1<<1)
# define BOUNCE_VERIFY (1<<2)
# define BOUNCE_POLL (1<<3)
static int bounces ;
2017-09-06 16:23:46 -07:00
/* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
# define ALARM_INTERVAL_SECS 10
2015-09-04 15:47:23 -07:00
static char * zeropage ;
pthread_attr_t attr ;
2022-01-19 15:15:31 +05:00
# define swap(a, b) \
do { typeof ( a ) __tmp = ( a ) ; ( a ) = ( b ) ; ( b ) = __tmp ; } while ( 0 )
2018-10-26 15:09:09 -07:00
const char * examples =
2023-08-14 18:45:50 +08:00
" # Run anonymous memory test on 100MiB region with 99999 bounces: \n "
" ./uffd-stress anon 100 99999 \n \n "
" # Run share memory test on 1GiB region with 99 bounces: \n "
" ./uffd-stress shmem 1000 99 \n \n "
" # Run hugetlb memory test on 256MiB region with 50 bounces: \n "
" ./uffd-stress hugetlb 256 50 \n \n "
" # Run the same hugetlb test but using private file: \n "
" ./uffd-stress hugetlb-private 256 50 \n \n "
" # 10MiB-~6GiB 999 bounces anonymous test, "
" continue forever unless an error triggers \n "
" while ./uffd-stress anon $[RANDOM % 6000 + 10] 999; do true; done \n \n " ;
2018-10-26 15:09:09 -07:00
static void usage ( void )
{
2023-08-14 18:45:50 +08:00
fprintf ( stderr , " \n Usage: ./uffd-stress <test type> <MiB> <bounces> \n \n " ) ;
2018-10-26 15:09:09 -07:00
fprintf ( stderr , " Supported <test type>: anon, hugetlb, "
selftests/mm: add shmem-private test to uffd-stress
The userfaultfd stress test never tested private shmem, which I think was
overlooked long due. Add it so it matches with uffd unit test and it'll
cover all memory supported with the three memory types.
Meanwhile, rename the memory types a bit. Considering shared mem is the
major use case for both shmem / hugetlbfs, changing from:
anon, hugetlb, hugetlb_shared, shmem
To (with shmem-private added):
anon, hugetlb, hugetlb-private, shmem, shmem-private
Add the shmem-private to run_vmtests.sh too.
Link: https://lkml.kernel.org/r/20230412164546.329355-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Zach O'Keefe <zokeefe@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-04-12 12:45:46 -04:00
" hugetlb-private, shmem, shmem-private \n \n " ) ;
2018-10-26 15:09:09 -07:00
fprintf ( stderr , " Examples: \n \n " ) ;
2019-05-27 15:18:59 +00:00
fprintf ( stderr , " %s " , examples ) ;
2018-10-26 15:09:09 -07:00
exit ( 1 ) ;
}
2023-04-12 12:43:37 -04:00
static void uffd_stats_reset ( struct uffd_args * args , unsigned long n_cpus )
2020-04-06 20:06:32 -07:00
{
int i ;
for ( i = 0 ; i < n_cpus ; i + + ) {
2023-04-12 12:43:37 -04:00
args [ i ] . cpu = i ;
2023-04-12 12:43:41 -04:00
args [ i ] . apply_wp = test_uffdio_wp ;
2023-04-12 12:43:37 -04:00
args [ i ] . missing_faults = 0 ;
args [ i ] . wp_faults = 0 ;
args [ i ] . minor_faults = 0 ;
2020-04-06 20:06:32 -07:00
}
}
2015-09-04 15:47:23 -07:00
static void * locking_thread ( void * arg )
{
unsigned long cpu = ( unsigned long ) arg ;
2022-03-22 14:45:35 -07:00
unsigned long page_nr ;
2015-09-04 15:47:23 -07:00
unsigned long long count ;
2021-11-05 13:42:07 -07:00
if ( ! ( bounces & BOUNCE_RANDOM ) ) {
2015-09-04 15:47:23 -07:00
page_nr = - bounces ;
if ( ! ( bounces & BOUNCE_RACINGFAULTS ) )
page_nr + = cpu * nr_pages_per_cpu ;
}
while ( ! finished ) {
if ( bounces & BOUNCE_RANDOM ) {
2021-11-05 13:42:07 -07:00
if ( getrandom ( & page_nr , sizeof ( page_nr ) , 0 ) ! = sizeof ( page_nr ) )
err ( " getrandom failed " ) ;
2015-09-04 15:47:23 -07:00
} else
page_nr + = 1 ;
page_nr % = nr_pages ;
pthread_mutex_lock ( area_mutex ( area_dst , page_nr ) ) ;
count = * area_count ( area_dst , page_nr ) ;
2021-06-30 18:48:55 -07:00
if ( count ! = count_verify [ page_nr ] )
err ( " page_nr %lu memory corruption %llu %llu " ,
page_nr , count , count_verify [ page_nr ] ) ;
2015-09-04 15:47:23 -07:00
count + + ;
* area_count ( area_dst , page_nr ) = count_verify [ page_nr ] = count ;
pthread_mutex_unlock ( area_mutex ( area_dst , page_nr ) ) ;
}
return NULL ;
}
2017-10-13 15:57:54 -07:00
static int copy_page_retry ( int ufd , unsigned long offset )
{
2023-04-12 12:43:41 -04:00
return __copy_page ( ufd , offset , true , test_uffdio_wp ) ;
2017-10-13 15:57:54 -07:00
}
2015-09-04 15:47:23 -07:00
pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER ;
static void * uffd_read_thread ( void * arg )
{
2023-04-12 12:43:37 -04:00
struct uffd_args * args = ( struct uffd_args * ) arg ;
2015-09-04 15:47:23 -07:00
struct uffd_msg msg ;
pthread_mutex_unlock ( & uffd_read_mutex ) ;
/* from here cancellation is ok */
for ( ; ; ) {
2018-10-26 15:09:13 -07:00
if ( uffd_read_msg ( uffd , & msg ) )
continue ;
2023-04-12 12:43:37 -04:00
uffd_handle_page_fault ( & msg , args ) ;
2015-09-04 15:47:23 -07:00
}
2020-04-06 20:06:32 -07:00
return NULL ;
2015-09-04 15:47:23 -07:00
}
static void * background_thread ( void * arg )
{
unsigned long cpu = ( unsigned long ) arg ;
2020-04-06 20:06:36 -07:00
unsigned long page_nr , start_nr , mid_nr , end_nr ;
start_nr = cpu * nr_pages_per_cpu ;
end_nr = ( cpu + 1 ) * nr_pages_per_cpu ;
mid_nr = ( start_nr + end_nr ) / 2 ;
/* Copy the first half of the pages */
for ( page_nr = start_nr ; page_nr < mid_nr ; page_nr + + )
copy_page_retry ( uffd , page_nr * page_size ) ;
2015-09-04 15:47:23 -07:00
2020-04-06 20:06:36 -07:00
/*
* If we need to test uffd - wp , set it up now . Then we ' ll have
* at least the first half of the pages mapped already which
* can be write - protected for testing
*/
if ( test_uffdio_wp )
wp_range ( uffd , ( unsigned long ) area_dst + start_nr * page_size ,
nr_pages_per_cpu * page_size , true ) ;
/*
* Continue the 2 nd half of the page copying , handling write
* protection faults if any
*/
for ( page_nr = mid_nr ; page_nr < end_nr ; page_nr + + )
2017-10-13 15:57:54 -07:00
copy_page_retry ( uffd , page_nr * page_size ) ;
2015-09-04 15:47:23 -07:00
return NULL ;
}
2023-04-12 12:43:37 -04:00
static int stress ( struct uffd_args * args )
2015-09-04 15:47:23 -07:00
{
unsigned long cpu ;
pthread_t locking_threads [ nr_cpus ] ;
pthread_t uffd_threads [ nr_cpus ] ;
pthread_t background_threads [ nr_cpus ] ;
finished = 0 ;
for ( cpu = 0 ; cpu < nr_cpus ; cpu + + ) {
if ( pthread_create ( & locking_threads [ cpu ] , & attr ,
locking_thread , ( void * ) cpu ) )
return 1 ;
if ( bounces & BOUNCE_POLL ) {
2023-07-07 14:55:39 -07:00
if ( pthread_create ( & uffd_threads [ cpu ] , & attr , uffd_poll_thread , & args [ cpu ] ) )
err ( " uffd_poll_thread create " ) ;
2015-09-04 15:47:23 -07:00
} else {
if ( pthread_create ( & uffd_threads [ cpu ] , & attr ,
uffd_read_thread ,
2023-04-12 12:43:37 -04:00
( void * ) & args [ cpu ] ) )
2015-09-04 15:47:23 -07:00
return 1 ;
pthread_mutex_lock ( & uffd_read_mutex ) ;
}
if ( pthread_create ( & background_threads [ cpu ] , & attr ,
background_thread , ( void * ) cpu ) )
return 1 ;
}
for ( cpu = 0 ; cpu < nr_cpus ; cpu + + )
if ( pthread_join ( background_threads [ cpu ] , NULL ) )
return 1 ;
/*
* Be strict and immediately zap area_src , the whole area has
* been transferred already by the background treads . The
2022-06-10 15:12:44 +08:00
* area_src could then be faulted in a racy way by still
2015-09-04 15:47:23 -07:00
* running uffdio_threads reading zeropages after we zapped
* area_src ( but they ' re guaranteed to get - EEXIST from
* UFFDIO_COPY without writing zero pages into area_dst
* because the background threads already completed ) .
*/
2021-06-30 18:48:55 -07:00
uffd_test_ops - > release_pages ( area_src ) ;
2018-10-26 15:09:17 -07:00
finished = 1 ;
for ( cpu = 0 ; cpu < nr_cpus ; cpu + + )
if ( pthread_join ( locking_threads [ cpu ] , NULL ) )
return 1 ;
2015-09-04 15:47:23 -07:00
for ( cpu = 0 ; cpu < nr_cpus ; cpu + + ) {
char c ;
if ( bounces & BOUNCE_POLL ) {
2021-06-30 18:48:55 -07:00
if ( write ( pipefd [ cpu * 2 + 1 ] , & c , 1 ) ! = 1 )
err ( " pipefd write error " ) ;
2020-04-06 20:06:32 -07:00
if ( pthread_join ( uffd_threads [ cpu ] ,
2023-04-12 12:43:37 -04:00
( void * ) & args [ cpu ] ) )
2015-09-04 15:47:23 -07:00
return 1 ;
} else {
if ( pthread_cancel ( uffd_threads [ cpu ] ) )
return 1 ;
if ( pthread_join ( uffd_threads [ cpu ] , NULL ) )
return 1 ;
}
}
return 0 ;
}
2017-02-22 15:44:01 -08:00
static int userfaultfd_stress ( void )
{
void * area ;
unsigned long nr ;
2023-04-12 12:43:37 -04:00
struct uffd_args args [ nr_cpus ] ;
2023-04-12 12:42:47 -04:00
uint64_t mem_size = nr_pages * page_size ;
2017-02-22 15:44:01 -08:00
2023-07-07 14:55:39 -07:00
memset ( args , 0 , sizeof ( struct uffd_args ) * nr_cpus ) ;
2023-04-12 12:45:20 -04:00
if ( uffd_test_ctx_init ( UFFD_FEATURE_WP_UNPOPULATED , NULL ) )
err ( " context init failed " ) ;
2015-09-04 15:47:23 -07:00
2021-06-30 18:48:55 -07:00
if ( posix_memalign ( & area , page_size , page_size ) )
err ( " out of memory " ) ;
2015-09-04 15:47:23 -07:00
zeropage = area ;
bzero ( zeropage , page_size ) ;
pthread_mutex_lock ( & uffd_read_mutex ) ;
pthread_attr_init ( & attr ) ;
pthread_attr_setstacksize ( & attr , 16 * 1024 * 1024 ) ;
while ( bounces - - ) {
printf ( " bounces: %d, mode: " , bounces ) ;
if ( bounces & BOUNCE_RANDOM )
printf ( " rnd " ) ;
if ( bounces & BOUNCE_RACINGFAULTS )
printf ( " racing " ) ;
if ( bounces & BOUNCE_VERIFY )
printf ( " ver " ) ;
if ( bounces & BOUNCE_POLL )
printf ( " poll " ) ;
2020-12-14 19:14:02 -08:00
else
printf ( " read " ) ;
2015-09-04 15:47:23 -07:00
printf ( " , " ) ;
fflush ( stdout ) ;
if ( bounces & BOUNCE_POLL )
fcntl ( uffd , F_SETFL , uffd_flags | O_NONBLOCK ) ;
else
fcntl ( uffd , F_SETFL , uffd_flags & ~ O_NONBLOCK ) ;
/* register */
2023-04-12 12:42:47 -04:00
if ( uffd_register ( uffd , area_dst , mem_size ,
true , test_uffdio_wp , false ) )
2021-06-30 18:48:55 -07:00
err ( " register failure " ) ;
2015-09-04 15:47:23 -07:00
2017-09-06 16:23:46 -07:00
if ( area_dst_alias ) {
2023-04-12 12:42:47 -04:00
if ( uffd_register ( uffd , area_dst_alias , mem_size ,
true , test_uffdio_wp , false ) )
2021-06-30 18:48:55 -07:00
err ( " register failure alias " ) ;
2017-09-06 16:23:46 -07:00
}
2015-09-04 15:47:23 -07:00
/*
* The madvise done previously isn ' t enough : some
* uffd_thread could have read userfaults ( one of
* those already resolved by the background thread )
* and it may be in the process of calling
* UFFDIO_COPY . UFFDIO_COPY will read the zapped
* area_src and it would map a zero page in it ( of
* course such a UFFDIO_COPY is perfectly safe as it ' d
* return - EEXIST ) . The problem comes at the next
* bounce though : that racing UFFDIO_COPY would
* generate zeropages in the area_src , so invalidating
* the previous MADV_DONTNEED . Without this additional
* MADV_DONTNEED those zeropages leftovers in the
* area_src would lead to - EEXIST failure during the
* next bounce , effectively leaving a zeropage in the
* area_dst .
*
* Try to comment this out madvise to see the memory
* corruption being caught pretty quick .
*
* khugepaged is also inhibited to collapse THP after
* MADV_DONTNEED only after the UFFDIO_REGISTER , so it ' s
* required to MADV_DONTNEED here .
*/
2021-06-30 18:48:55 -07:00
uffd_test_ops - > release_pages ( area_dst ) ;
2015-09-04 15:47:23 -07:00
2023-04-12 12:43:37 -04:00
uffd_stats_reset ( args , nr_cpus ) ;
2020-04-06 20:06:32 -07:00
2015-09-04 15:47:23 -07:00
/* bounce pass */
2023-12-06 02:36:57 -08:00
if ( stress ( args ) ) {
uffd_test_ctx_clear ( ) ;
2015-09-04 15:47:23 -07:00
return 1 ;
2023-12-06 02:36:57 -08:00
}
2015-09-04 15:47:23 -07:00
2020-04-06 20:06:36 -07:00
/* Clear all the write protections if there is any */
if ( test_uffdio_wp )
wp_range ( uffd , ( unsigned long ) area_dst ,
nr_pages * page_size , false ) ;
2015-09-04 15:47:23 -07:00
/* unregister */
2023-04-12 12:42:47 -04:00
if ( uffd_unregister ( uffd , area_dst , mem_size ) )
2021-06-30 18:48:55 -07:00
err ( " unregister failure " ) ;
2017-09-06 16:23:46 -07:00
if ( area_dst_alias ) {
2023-04-12 12:42:47 -04:00
if ( uffd_unregister ( uffd , area_dst_alias , mem_size ) )
2021-06-30 18:48:55 -07:00
err ( " unregister failure alias " ) ;
2017-09-06 16:23:46 -07:00
}
2015-09-04 15:47:23 -07:00
/* verification */
2021-06-30 18:48:55 -07:00
if ( bounces & BOUNCE_VERIFY )
for ( nr = 0 ; nr < nr_pages ; nr + + )
if ( * area_count ( area_dst , nr ) ! = count_verify [ nr ] )
err ( " error area_count %llu %llu %lu \n " ,
* area_count ( area_src , nr ) ,
count_verify [ nr ] , nr ) ;
2015-09-04 15:47:23 -07:00
/* prepare next bounce */
2022-05-12 20:22:56 -07:00
swap ( area_src , area_dst ) ;
2015-09-04 15:47:23 -07:00
2022-05-12 20:22:56 -07:00
swap ( area_src_alias , area_dst_alias ) ;
2017-09-06 16:23:46 -07:00
2023-04-12 12:43:37 -04:00
uffd_stats_report ( args , nr_cpus ) ;
2015-09-04 15:47:23 -07:00
}
2023-12-06 02:36:57 -08:00
uffd_test_ctx_clear ( ) ;
2015-09-04 15:47:23 -07:00
2023-04-12 12:44:04 -04:00
return 0 ;
2015-09-04 15:47:23 -07:00
}
2017-05-03 14:54:54 -07:00
static void set_test_type ( const char * type )
2017-02-22 15:43:07 -08:00
{
2017-05-03 14:54:54 -07:00
if ( ! strcmp ( type , " anon " ) ) {
test_type = TEST_ANON ;
uffd_test_ops = & anon_uffd_test_ops ;
} else if ( ! strcmp ( type , " hugetlb " ) ) {
test_type = TEST_HUGETLB ;
uffd_test_ops = & hugetlb_uffd_test_ops ;
2017-09-06 16:23:46 -07:00
map_shared = true ;
selftests/mm: add shmem-private test to uffd-stress
The userfaultfd stress test never tested private shmem, which I think was
overlooked long due. Add it so it matches with uffd unit test and it'll
cover all memory supported with the three memory types.
Meanwhile, rename the memory types a bit. Considering shared mem is the
major use case for both shmem / hugetlbfs, changing from:
anon, hugetlb, hugetlb_shared, shmem
To (with shmem-private added):
anon, hugetlb, hugetlb-private, shmem, shmem-private
Add the shmem-private to run_vmtests.sh too.
Link: https://lkml.kernel.org/r/20230412164546.329355-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Zach O'Keefe <zokeefe@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-04-12 12:45:46 -04:00
} else if ( ! strcmp ( type , " hugetlb-private " ) ) {
2017-09-06 16:23:46 -07:00
test_type = TEST_HUGETLB ;
uffd_test_ops = & hugetlb_uffd_test_ops ;
2017-05-03 14:54:54 -07:00
} else if ( ! strcmp ( type , " shmem " ) ) {
2017-09-06 16:23:46 -07:00
map_shared = true ;
2017-05-03 14:54:54 -07:00
test_type = TEST_SHMEM ;
uffd_test_ops = & shmem_uffd_test_ops ;
selftests/mm: add shmem-private test to uffd-stress
The userfaultfd stress test never tested private shmem, which I think was
overlooked long due. Add it so it matches with uffd unit test and it'll
cover all memory supported with the three memory types.
Meanwhile, rename the memory types a bit. Considering shared mem is the
major use case for both shmem / hugetlbfs, changing from:
anon, hugetlb, hugetlb_shared, shmem
To (with shmem-private added):
anon, hugetlb, hugetlb-private, shmem, shmem-private
Add the shmem-private to run_vmtests.sh too.
Link: https://lkml.kernel.org/r/20230412164546.329355-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Zach O'Keefe <zokeefe@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-04-12 12:45:46 -04:00
} else if ( ! strcmp ( type , " shmem-private " ) ) {
test_type = TEST_SHMEM ;
uffd_test_ops = & shmem_uffd_test_ops ;
2017-05-03 14:54:54 -07:00
}
userfaultfd: selftests: modify selftest to use /dev/userfaultfd
We clearly want to ensure both userfaultfd(2) and /dev/userfaultfd keep
working into the future, so just run the test twice, using each interface.
Instead of always testing both userfaultfd(2) and /dev/userfaultfd, let
the user choose which to test.
As with other test features, change the behavior based on a new command
line flag. Introduce the idea of "test mods", which are generic (not
specific to a test type) modifications to the behavior of the test. This
is sort of borrowed from this RFC patch series [1], but simplified a bit.
The benefit is, in "typical" configurations this test is somewhat slow
(say, 30sec or something). Testing both clearly doubles it, so it may not
always be desirable, as users are likely to use one or the other, but
never both, in the "real world".
[1]: https://patchwork.kernel.org/project/linux-mm/patch/20201129004548.1619714-14-namit@vmware.com/
[axelrasmussen@google.com: modify selftest to exit with KSFT_SKIP *only* when features are unsupported, per Mike]
Link: https://lkml.kernel.org/r/20220819205201.658693-4-axelrasmussen@google.com
Link: https://lkml.kernel.org/r/20220808175614.3885028-4-axelrasmussen@google.com
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Acked-by: Peter Xu <peterx@redhat.com>
Acked-by: Mike Rapoport <rppt@linux.ibm.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Dmitry V. Levin <ldv@altlinux.org>
Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Zhang Yi <yi.zhang@huawei.com>
Cc: Mike Rapoport <rppt@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-08-08 10:56:12 -07:00
}
static void parse_test_type_arg ( const char * raw_type )
{
uint64_t features = UFFD_API_FEATURES ;
2023-04-12 12:45:25 -04:00
set_test_type ( raw_type ) ;
userfaultfd: selftests: modify selftest to use /dev/userfaultfd
We clearly want to ensure both userfaultfd(2) and /dev/userfaultfd keep
working into the future, so just run the test twice, using each interface.
Instead of always testing both userfaultfd(2) and /dev/userfaultfd, let
the user choose which to test.
As with other test features, change the behavior based on a new command
line flag. Introduce the idea of "test mods", which are generic (not
specific to a test type) modifications to the behavior of the test. This
is sort of borrowed from this RFC patch series [1], but simplified a bit.
The benefit is, in "typical" configurations this test is somewhat slow
(say, 30sec or something). Testing both clearly doubles it, so it may not
always be desirable, as users are likely to use one or the other, but
never both, in the "real world".
[1]: https://patchwork.kernel.org/project/linux-mm/patch/20201129004548.1619714-14-namit@vmware.com/
[axelrasmussen@google.com: modify selftest to exit with KSFT_SKIP *only* when features are unsupported, per Mike]
Link: https://lkml.kernel.org/r/20220819205201.658693-4-axelrasmussen@google.com
Link: https://lkml.kernel.org/r/20220808175614.3885028-4-axelrasmussen@google.com
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Acked-by: Peter Xu <peterx@redhat.com>
Acked-by: Mike Rapoport <rppt@linux.ibm.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Dmitry V. Levin <ldv@altlinux.org>
Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Zhang Yi <yi.zhang@huawei.com>
Cc: Mike Rapoport <rppt@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-08-08 10:56:12 -07:00
if ( ! test_type )
err ( " failed to parse test type argument: '%s' " , raw_type ) ;
2017-05-03 14:54:54 -07:00
if ( test_type = = TEST_HUGETLB )
2023-04-12 12:43:33 -04:00
page_size = default_huge_page_size ( ) ;
2017-05-03 14:54:54 -07:00
else
page_size = sysconf ( _SC_PAGE_SIZE ) ;
2021-06-30 18:48:55 -07:00
if ( ! page_size )
err ( " Unable to determine page size " ) ;
2017-02-22 15:43:07 -08:00
if ( ( unsigned long ) area_count ( NULL , 0 ) + sizeof ( unsigned long long ) * 2
2021-06-30 18:48:55 -07:00
> page_size )
err ( " Impossible to run this test " ) ;
userfaultfd/selftests: fix feature support detection
Before any tests are run, in set_test_type, we decide what feature(s) we
are going to be testing, based upon our command line arguments.
However, the supported features are not just a function of the memory
type being used, so this is broken.
For instance, consider writeprotect support. It is "normally" supported
for anonymous memory, but furthermore it requires that the kernel has
CONFIG_HAVE_ARCH_USERFAULTFD_WP. So, it is *not* supported at all on
aarch64, for example.
So, this fixes this by querying the kernel for the set of features it
supports in set_test_type, by opening a userfaultfd and issuing a
UFFDIO_API ioctl. Based upon the reported features, we toggle what
tests are enabled.
Link: https://lkml.kernel.org/r/20210930212309.4001967-3-axelrasmussen@google.com
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-11-05 13:42:10 -07:00
/*
* Whether we can test certain features depends not just on test type ,
* but also on whether or not this particular kernel supports the
* feature .
*/
2023-04-12 12:45:20 -04:00
if ( userfaultfd_open ( & features ) )
err ( " Userfaultfd open failed " ) ;
userfaultfd/selftests: fix feature support detection
Before any tests are run, in set_test_type, we decide what feature(s) we
are going to be testing, based upon our command line arguments.
However, the supported features are not just a function of the memory
type being used, so this is broken.
For instance, consider writeprotect support. It is "normally" supported
for anonymous memory, but furthermore it requires that the kernel has
CONFIG_HAVE_ARCH_USERFAULTFD_WP. So, it is *not* supported at all on
aarch64, for example.
So, this fixes this by querying the kernel for the set of features it
supports in set_test_type, by opening a userfaultfd and issuing a
UFFDIO_API ioctl. Based upon the reported features, we toggle what
tests are enabled.
Link: https://lkml.kernel.org/r/20210930212309.4001967-3-axelrasmussen@google.com
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-11-05 13:42:10 -07:00
test_uffdio_wp = test_uffdio_wp & &
( features & UFFD_FEATURE_PAGEFAULT_FLAG_WP ) ;
close ( uffd ) ;
uffd = - 1 ;
2017-05-03 14:54:54 -07:00
}
2017-09-06 16:23:46 -07:00
static void sigalrm ( int sig )
{
if ( sig ! = SIGALRM )
abort ( ) ;
test_uffdio_copy_eexist = true ;
alarm ( ALARM_INTERVAL_SECS ) ;
}
2017-05-03 14:54:54 -07:00
int main ( int argc , char * * argv )
{
2022-09-22 15:40:46 -07:00
size_t bytes ;
2017-05-03 14:54:54 -07:00
if ( argc < 4 )
2018-10-26 15:09:09 -07:00
usage ( ) ;
2017-05-03 14:54:54 -07:00
2021-06-30 18:48:55 -07:00
if ( signal ( SIGALRM , sigalrm ) = = SIG_ERR )
err ( " failed to arm SIGALRM " ) ;
2017-09-06 16:23:46 -07:00
alarm ( ALARM_INTERVAL_SECS ) ;
userfaultfd: selftests: modify selftest to use /dev/userfaultfd
We clearly want to ensure both userfaultfd(2) and /dev/userfaultfd keep
working into the future, so just run the test twice, using each interface.
Instead of always testing both userfaultfd(2) and /dev/userfaultfd, let
the user choose which to test.
As with other test features, change the behavior based on a new command
line flag. Introduce the idea of "test mods", which are generic (not
specific to a test type) modifications to the behavior of the test. This
is sort of borrowed from this RFC patch series [1], but simplified a bit.
The benefit is, in "typical" configurations this test is somewhat slow
(say, 30sec or something). Testing both clearly doubles it, so it may not
always be desirable, as users are likely to use one or the other, but
never both, in the "real world".
[1]: https://patchwork.kernel.org/project/linux-mm/patch/20201129004548.1619714-14-namit@vmware.com/
[axelrasmussen@google.com: modify selftest to exit with KSFT_SKIP *only* when features are unsupported, per Mike]
Link: https://lkml.kernel.org/r/20220819205201.658693-4-axelrasmussen@google.com
Link: https://lkml.kernel.org/r/20220808175614.3885028-4-axelrasmussen@google.com
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Acked-by: Peter Xu <peterx@redhat.com>
Acked-by: Mike Rapoport <rppt@linux.ibm.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Dmitry V. Levin <ldv@altlinux.org>
Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Zhang Yi <yi.zhang@huawei.com>
Cc: Mike Rapoport <rppt@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-08-08 10:56:12 -07:00
parse_test_type_arg ( argv [ 1 ] ) ;
2022-09-22 15:40:46 -07:00
bytes = atol ( argv [ 2 ] ) * 1024 * 1024 ;
2017-05-03 14:54:54 -07:00
nr_cpus = sysconf ( _SC_NPROCESSORS_ONLN ) ;
2022-09-22 15:40:46 -07:00
nr_pages_per_cpu = bytes / page_size / nr_cpus ;
2017-02-22 15:43:07 -08:00
if ( ! nr_pages_per_cpu ) {
2021-06-30 18:48:55 -07:00
_err ( " invalid MiB " ) ;
2018-10-26 15:09:09 -07:00
usage ( ) ;
2017-02-22 15:43:07 -08:00
}
2017-05-03 14:54:54 -07:00
bounces = atoi ( argv [ 3 ] ) ;
2017-02-22 15:43:07 -08:00
if ( bounces < = 0 ) {
2021-06-30 18:48:55 -07:00
_err ( " invalid bounces " ) ;
2018-10-26 15:09:09 -07:00
usage ( ) ;
2017-02-22 15:43:07 -08:00
}
nr_pages = nr_pages_per_cpu * nr_cpus ;
2017-05-03 14:54:54 -07:00
2017-02-22 15:43:07 -08:00
printf ( " nr_pages: %lu, nr_pages_per_cpu: %lu \n " ,
nr_pages , nr_pages_per_cpu ) ;
return userfaultfd_stress ( ) ;
}
2015-09-22 14:58:58 -07:00
# else /* __NR_userfaultfd */
# warning "missing __NR_userfaultfd definition"
int main ( void )
{
printf ( " skip: Skipping userfaultfd test (missing __NR_userfaultfd) \n " ) ;
2018-06-13 21:31:43 -06:00
return KSFT_SKIP ;
2015-09-22 14:58:58 -07:00
}
# endif /* __NR_userfaultfd */