time-travel broadcast and shared memory
Johannes Berg
maintainercover time-travel broadcast and shared memory
So I've been mentioning the shared memory part, but because it (mechanically, not semantically) depends on the broadcast messages we use to separate tests, I've included those patches here as well.
As for the shared memory part, the calendar/controller we have already implements that protocol, see
https://github.com/linux-test-project/usfstl/tree/main/control
johannes
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerPATCH 1/4 um: time-travel: support time-travel protocol broadcast messages
From: Mordechay Goodstein <[email protected]>
Add a message type to the time-travel protocol to broadcast a small (64-bit) value to all participants in a simulation. The main use case is to have an identical message come to all participants in a simulation, e.g. to separate out logs for different tests running in a single simulation.
Down in the guts of time_travel_handle_message() we can't use printk() and not even printk_deferred(), so just store the message and print it at the start of the userspace() function.
Unfortunately this means that other prints in the kernel can actually bypass the message, but in most cases where this is used, for example to separate test logs, userspace will be involved. Also, even if we could use printk_deferred(), we'd still need to flush it out in the userspace() function since otherwise userspace messages might cross it.
As a result, this is a reasonable compromise, there's no need to have any core changes and it solves the main use case we have for it.
--- arch/um/include/shared/timetravel.h | 9 +++++ arch/um/kernel/time.c | 59 +++++++++++++++++++++++++++++ arch/um/os-Linux/skas/process.c | 3 ++ include/uapi/linux/um_timetravel.h | 10 +++++ 4 files changed, 81 insertions(+)
arch/um/include/shared/timetravel.h +9 -0
@@ -15,8 +15,17 @@ enum time_travel_mode {#if defined(UML_CONFIG_UML_TIME_TRAVEL_SUPPORT ) || \ defined(CONFIG_UML_TIME_TRAVEL_SUPPORT )extern enum time_travel_mode time_travel_mode; +extern int time_travel_should_print_bc_msg;#else #define time_travel_mode TT_MODE_OFF +#define time_travel_should_print_bc_msg 0#endif /* (UML_)CONFIG_UML_TIME_TRAVEL_SUPPORT */ +void _time_travel_print_bc_msg (void ); +static inline void time_travel_print_bc_msg (void ) +{ +if (time_travel_should_print_bc_msg) +_time_travel_print_bc_msg (); +} +#endif /* _UM_TIME_TRAVEL_H_ */
arch/um/kernel/time.c +59 -0
75 lines not on this page · raw message
arch/um/os-Linux/skas/process.c +3 -0
@@ -23,6 +23,7 @@#include <skas.h> #include <sysdep/stub.h> #include <linux/threads.h> +#include <timetravel.h> int is_skas_winch (int pid,int fd,void *data) {
@@ -388,6 +389,8 @@ void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)interrupt_end ();while (1 ) { +time_travel_print_bc_msg (); +if (kill_userspace_mm[0 ])fatal_sigsegv ();
include/uapi/linux/um_timetravel.h +10 -0
@@ -123,6 +123,16 @@ enum um_timetravel_ops {* the simulation. */ UM_TIMETRAVEL_GET_TOD =8 , + +/** @UM_TIMETRAVEL_BROADCAST: Send/Receive a broadcast messge +* This message can be used to sync all components in the system +* with a single message, if the calender gets the message, the +* calender broadcast the message to all components, and if a +* component receives it it should act based on it e.g print a +* message to it's log system. +* (calendar <-> host) +*/ + UM_TIMETRAVEL_BROADCAST = 9, };#endif /* _UAPI_LINUX_UM_TIMETRAVEL_H */
Signature · 7 lines
2.41.0 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerPATCH 2/4 um: generalize os_rcv_fd
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
Change os_rcv_fd() to os_rcv_fd_msg() that can more generally receive any number of FDs in any kind of message.
--- arch/um/drivers/port_kern.c | 14 +++++---- arch/um/drivers/xterm.c | 2 +- arch/um/drivers/xterm_kern.c | 13 ++++++-- arch/um/include/shared/os.h | 3 +- arch/um/kernel/ksyms.c | 2 +- arch/um/os-Linux/file.c | 61 +++++++++++++++++++----------------- 6 files changed, 54 insertions(+), 41 deletions(-)
arch/um/drivers/port_kern.c +8 -6
@@ -45,15 +45,17 @@ struct connection {static irqreturn_t pipe_interrupt (int irq,void *data) {struct connection *conn = data; -int fd; +int n_fds =1 , fd =-1 ; +ssize_t ret; - fd =os_rcv_fd (conn->socket[0 ], &conn->helper_pid); -if (fd <0 ) { -if (fd == -EAGAIN ) + ret =os_rcv_fd_msg (conn->socket[0 ], &fd, n_fds, &conn->helper_pid, +sizeof (conn->helper_pid)); +if (ret !=sizeof (conn->helper_pid)) { +if (ret == -EAGAIN )return IRQ_NONE ; -printk (KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n" , - -fd); +printk (KERN_ERR "pipe_interrupt : os_rcv_fd_msg returned %d\n" , + ret);os_close_file (conn->fd); }
arch/um/drivers/xterm.c +1 -1
@@ -156,7 +156,7 @@ static int xterm_open(int input, int output, int primary, void *d, new =xterm_fd (fd, &data->helper_pid);if (new <0 ) { err = new; -printk (UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n" , +printk (UM_KERN_ERR "xterm_open : xterm_fd failed, err = %d\n" , -err); goto out_kill; }
arch/um/drivers/xterm_kern.c +10 -3
@@ -21,12 +21,19 @@ struct xterm_wait {static irqreturn_t xterm_interrupt (int irq,void *data) {struct xterm_wait *xterm = data; -int fd; +int fd =-1 , n_fds =1 ; +ssize_t ret; - fd =os_rcv_fd (xterm->fd, &xterm->pid); -if (fd == -EAGAIN ) + ret =os_rcv_fd_msg (xterm->fd, &fd, n_fds, + &xterm->pid,sizeof (xterm->pid)); +if (ret == -EAGAIN )return IRQ_NONE ; +if (ret <0 ) + fd = ret; +else if (ret !=sizeof (xterm->pid)) + fd = -EMSGSIZE ; + xterm->new_fd = fd;complete (&xterm->ready);
arch/um/include/shared/os.h +2 -1
@@ -164,7 +164,8 @@ extern int os_accept_connection(int fd);extern int os_create_unix_socket (const char *file,int len,int close_on_exec);extern int os_shutdown_socket (int fd,int r,int w);extern void os_close_file (int fd); -extern int os_rcv_fd (int fd,int *helper_pid_out); +ssize_t os_rcv_fd_msg (int fd,int *fds,unsigned int n_fds, +void *data,size_t data_len);extern int os_connect_socket (const char *name);extern int os_file_type (char *file);extern int os_file_mode (const char *file,struct openflags *mode_out);
arch/um/kernel/ksyms.c +1 -1
@@ -33,7 +33,7 @@ EXPORT_SYMBOL(os_shutdown_socket);EXPORT_SYMBOL (os_create_unix_socket);EXPORT_SYMBOL (os_connect_socket);EXPORT_SYMBOL (os_accept_connection); -EXPORT_SYMBOL (os_rcv_fd); +EXPORT_SYMBOL (os_rcv_fd_msg);EXPORT_SYMBOL (run_helper);EXPORT_SYMBOL (os_major);EXPORT_SYMBOL (os_minor);
arch/um/os-Linux/file.c +32 -29
76 lines not on this page · raw message
Signature · 7 lines
2.41.0 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerPATCH 3/4 um: add mmap/mremap/munmap OS calls
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
For the upcoming shared-memory time-travel external optimisations, we need to be able to mmap/mremap (and on error munmap). Add the necessary OS calls.
--- arch/um/include/shared/os.h | 3 +++ arch/um/os-Linux/file.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
arch/um/include/shared/os.h +3 -0
@@ -180,6 +180,9 @@ extern int os_eventfd(unsigned int initval, int flags);extern int os_sendmsg_fds (int fd,const void *buf,unsigned int len,const int *fds,unsigned int fds_num);int os_poll (unsigned int n,const int *fds); +void *os_mmap_rw_shared (int fd,size_t size); +void *os_mremap_rw_shared (void *old_addr,size_t old_size,size_t new_size); +void os_munmap (void *addr,size_t size);/* start_up.c */ extern void os_early_checks (void );
arch/um/os-Linux/file.c +28 -0
@@ -17,6 +17,7 @@#include <sys/stat.h> #include <sys/sysmacros.h> #include <sys/un.h> +#include <sys/mman.h> #include <sys/types.h> #include <sys/eventfd.h> #include <poll.h>
@@ -708,3 +709,30 @@ int os_poll(unsigned int n, const int *fds)return -EIO ; } + +void *os_mmap_rw_shared (int fd,size_t size) +{ +void *res =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); + +if (res ==MAP_FAILED ) +return NULL ; + +return res; +} + +void *os_mremap_rw_shared (void *old_addr,size_t old_size,size_t new_size) +{ +void *res; + + res =mremap (old_addr, old_size, new_size,MREMAP_MAYMOVE ,NULL ); + +if (res ==MAP_FAILED ) +return NULL ; + +return res; +} + +void os_munmap (void *addr,size_t size) +{ +munmap (addr, size); +}
Signature · 7 lines
2.41.0 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 3/4] um: add mmap/mremap/munmap OS calls
Quoted 59 lines · Johannes Berg
From: Johannes Berg <[email protected]>
For the upcoming shared-memory time-travel external optimisations, we need to be able to mmap/mremap (and on error munmap). Add the necessary OS calls.
Signed-off-by Johannes Berg <[email protected]>--- arch/um/include/shared/os.h | 3 +++ arch/um/os-Linux/file.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)arch/um/include/shared/os.h +3
-0@@ -180,6 +180,9 @@ extern int os_eventfd(unsigned int initval, int flags);extern int os_sendmsg_fds (int fd,const void *buf,unsigned int len,const int *fds,unsigned int fds_num);int os_poll (unsigned int n,const int *fds); +void *os_mmap_rw_shared (int fd,size_t size); +void *os_mremap_rw_shared (void *old_addr,size_t old_size,size_t new_size); +void os_munmap (void *addr,size_t size);/* start_up.c */ extern void os_early_checks (void );arch/um/os-Linux/file.c +18
-0@@ -17,6 +17,7 @@#include <sys/stat.h> #include <sys/sysmacros.h> #include <sys/un.h> +#include <sys/mman.h> #include <sys/types.h> #include <sys/eventfd.h> #include <poll.h> @@ -708,3 +709,30 @@ int os_poll(unsigned int n, const int *fds)return -EIO ; } + +void *os_mmap_rw_shared (int fd,size_t size) +{ +void *res =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); + +if (res ==MAP_FAILED ) +return NULL ; + +return res; +} + +void *os_mremap_rw_shared (void *old_addr,size_t old_size,size_t new_size) +{ +void *res; + + res =mremap (old_addr, old_size, new_size,MREMAP_MAYMOVE ,NULL ); +
arch/um/os-Linux/file.c int os_poll(unsigned int n, const int *fds) · line 738
Quoted 10 lines
arch/um/os-Linux/file.c +10
-0+if (res ==MAP_FAILED ) +return NULL ; + +return res; +} + +void os_munmap (void *addr,size_t size) +{ +munmap (addr, size); +}
os_munmap already exists as os_munmap_memory
The other two are OK.
Signature · 8 lines
Anton R. Ivanov https://www.kot-begemot.co.uk/ _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerRe: [PATCH 3/4] um: add mmap/mremap/munmap OS calls
Quoted 2 lines · Anton Ivanov
os_munmapalready exists asos_munmap_memory
Hah, oops. Thanks :)
johannes
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerPATCH 4/4 um: add shared memory optimisation for time-travel=ext
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
With external time travel, a LOT of message can end up being exchanged on the socket, taking a significant amount of time just to do that.
Add a new shared memory optimisation to that, where a number of changes are made:
- the controller sends a client ID and a shared memory FD (and a logging FD we don't use) in the ACK message to the initial START
- the shared memory holds the current time and the free_until value, so that there's no need to exchange messages for that
- if the client that's running has shared memory support, any client (the running one included) can request the next time it wants to run inside the shared memory, rather than sending a message, by also updating the free_until value
- when shared memory is enabled, RUN/WAIT messages no longer have an ACK, further cutting down on messages
Together, this can reduce the number of messages very significantly, and reduce overall test/simulation run time.
--- arch/um/kernel/time.c | 130 +++++++++++++++++++-- include/uapi/linux/um_timetravel.h | 182 ++++++++++++++++++++++++++--- 2 files changed, 285 insertions(+), 27 deletions(-)
arch/um/kernel/time.c +118 -12
@@ -30,6 +30,7 @@ EXPORT_SYMBOL_GPL(time_travel_mode);static bool time_travel_start_set;static unsigned long long time_travel_start;static unsigned long long time_travel_time; +static unsigned long long time_travel_shm_offset;static LIST_HEAD (time_travel_events);static LIST_HEAD (time_travel_irqs);static unsigned long long time_travel_timer_interval;
@@ -39,8 +40,11 @@ static int time_travel_ext_fd = -1;static unsigned int time_travel_ext_waiting;static bool time_travel_ext_prev_request_valid;static unsigned long long time_travel_ext_prev_request; -static bool time_travel_ext_free_until_valid; -static unsigned long long time_travel_ext_free_until; +static unsigned long long *time_travel_ext_free_until; +static unsigned long long _time_travel_ext_free_until; +static u16 time_travel_shm_id; +static struct um_timetravel_schedshm *time_travel_shm; +static union um_timetravel_schedshm_client *time_travel_shm_client;static void time_travel_set_time (unsigned long long ns) {
@@ -57,6 +61,7 @@ enum time_travel_message_handling {TTMH_IDLE ,TTMH_POLL ,TTMH_READ , + TTMH_READ_START_ACK, };static u64 bc_message;
@@ -68,6 +73,40 @@ void _time_travel_print_bc_msg(void)printk (KERN_INFO "time-travel: received broadcast 0x%llx\n" , bc_message); } +static void time_travel_setup_shm (int fd,u16 id) +{ +u32 len; + + time_travel_shm =os_mmap_rw_shared (fd,sizeof (*time_travel_shm)); + +if (!time_travel_shm) + goto out; + + len = time_travel_shm->len; + +if (time_travel_shm->version !=UM_TIMETRAVEL_SCHEDSHM_VERSION || + len <struct_size (time_travel_shm, clients, id +1 )) { +os_munmap (time_travel_shm,sizeof (*time_travel_shm)); + time_travel_shm =NULL ; + goto out; + } + + time_travel_shm =os_mremap_rw_shared (time_travel_shm, +sizeof (*time_travel_shm), + len); +if (!time_travel_shm) + goto out; + + time_travel_shm_offset = time_travel_shm->current_time; + time_travel_shm_client = &time_travel_shm->clients[id]; + time_travel_shm_client->capa |=UM_TIMETRAVEL_SCHEDSHM_CAP_TIME_SHARE ; + time_travel_shm_id = id; +/* always look at that free_until from now on */ + time_travel_ext_free_until = &time_travel_shm->free_until; +out: +os_close_file (fd); +} +static void time_travel_handle_message (struct um_timetravel_msg *msg,enum time_travel_message_handling mode) {
@@ -88,7 +127,20 @@ static void time_travel_handle_message(struct um_timetravel_msg *msg, } } - ret =os_read_file (time_travel_ext_fd, msg,sizeof (*msg)); +if (unlikely (mode ==TTMH_READ_START_ACK )) { +int fd[UM_TIMETRAVEL_SHARED_MAX_FDS ]; + + ret =os_rcv_fd_msg (time_travel_ext_fd, fd, +ARRAY_SIZE (fd), msg,sizeof (*msg)); +if (ret ==sizeof (*msg)) { +time_travel_setup_shm (fd[UM_TIMETRAVEL_SHARED_MEMFD ], + msg->time &UM_TIMETRAVEL_START_ACK_ID ); +/* we don't use the logging for now */ +os_close_file (fd[UM_TIMETRAVEL_SHARED_LOGFD ]); + } + }else { + ret =os_read_file (time_travel_ext_fd, msg,sizeof (*msg)); + }if (ret ==0 )panic ("time-travel external link is broken\n" );
@@ -104,10 +156,20 @@ static void time_travel_handle_message(struct um_timetravel_msg *msg,return ;case UM_TIMETRAVEL_RUN :time_travel_set_time (msg->time); +if (time_travel_shm) { +/* no request right now since we're running */ + time_travel_shm_client->flags &= + ~UM_TIMETRAVEL_SCHEDSHM_FLAGS_REQ_RUN ; +/* no ack for shared memory RUN */ +return ; + }break ;case UM_TIMETRAVEL_FREE_UNTIL : - time_travel_ext_free_until_valid = true; - time_travel_ext_free_until = msg->time; +/* not supposed to get this with shm, but ignore it */ +if (time_travel_shm) +break ; + time_travel_ext_free_until = &_time_travel_ext_free_until; + _time_travel_ext_free_until = msg->time;break ;case UM_TIMETRAVEL_BROADCAST : bc_message = msg->time;
@@ -148,8 +210,15 @@ static u64 time_travel_ext_req(u32 op, u64 time)block_signals_hard ();os_write_file (time_travel_ext_fd, &msg,sizeof (msg)); +/* no ACK expected for WAIT in shared memory mode */ +if (msg.op ==UM_TIMETRAVEL_WAIT && time_travel_shm) + goto done; +while (msg.op !=UM_TIMETRAVEL_ACK ) -time_travel_handle_message (&msg,TTMH_READ ); +time_travel_handle_message (&msg, + op ==UM_TIMETRAVEL_START ? +TTMH_READ_START_ACK : +TTMH_READ );if (msg.seq != mseq) panic("time-travel: ACK message has different seqno! op=%d, seq=%d != %d time=%lld\n",
@@ -157,6 +226,7 @@ static u64 time_travel_ext_req(u32 op, u64 time)if (op ==UM_TIMETRAVEL_GET )time_travel_set_time (msg.time); +done:unblock_signals_hard ();return msg.time;
@@ -192,13 +262,33 @@ static void time_travel_ext_update_request(unsigned long long time)/* * if we're running and are allowed to run past the request * then we don't need to update it either +* +* Note for shm we ignore FREE_UNTIL messages and leave the pointer +* to shared memory, and for non-shm the offset is 0. */ -if (!time_travel_ext_waiting && time_travel_ext_free_until_valid && - time < time_travel_ext_free_until) +if (!time_travel_ext_waiting && time_travel_ext_free_until && + time < (*time_travel_ext_free_until - time_travel_shm_offset))return ; time_travel_ext_prev_request = time; time_travel_ext_prev_request_valid = true; + +if (time_travel_shm) { +union um_timetravel_schedshm_client *running; + + running = &time_travel_shm->clients[time_travel_shm->running_id]; + +if (running->capa &UM_TIMETRAVEL_SCHEDSHM_CAP_TIME_SHARE ) { + time_travel_shm_client->flags |= +UM_TIMETRAVEL_SCHEDSHM_FLAGS_REQ_RUN ; + time += time_travel_shm_offset; + time_travel_shm_client->req_time = time; +if (time < time_travel_shm->free_until) + time_travel_shm->free_until = time; +return ; + } + } +time_travel_ext_req (UM_TIMETRAVEL_REQUEST , time); }
@@ -206,6 +296,14 @@ void __time_travel_propagate_time(void) {static unsigned long long last_propagated; +if (time_travel_shm) { +if (time_travel_shm->running_id != time_travel_shm_id) +panic ("time-travel: setting time while not running\n" ); + time_travel_shm->current_time = time_travel_time + + time_travel_shm_offset; +return ; + } +if (last_propagated == time_travel_time)return ;
@@ -221,9 +319,12 @@ static bool time_travel_ext_request(unsigned long long time)* If we received an external sync point ("free until") then we * don't have to request/wait for anything until then, unless * we're already waiting. +* +* Note for shm we ignore FREE_UNTIL messages and leave the pointer +* to shared memory, and for non-shm the offset is 0. */ -if (!time_travel_ext_waiting && time_travel_ext_free_until_valid && - time < time_travel_ext_free_until) +if (!time_travel_ext_waiting && time_travel_ext_free_until && + time < (*time_travel_ext_free_until - time_travel_shm_offset))return false;time_travel_ext_update_request (time);
@@ -237,7 +338,8 @@ static void time_travel_ext_wait(bool idle) }; time_travel_ext_prev_request_valid = false; - time_travel_ext_free_until_valid = false; +if (!time_travel_shm) + time_travel_ext_free_until =NULL ; time_travel_ext_waiting++;time_travel_ext_req (UM_TIMETRAVEL_WAIT ,-1 );
@@ -260,7 +362,11 @@ static void time_travel_ext_wait(bool idle)static void time_travel_ext_get_time (void ) { -time_travel_ext_req (UM_TIMETRAVEL_GET ,-1 ); +if (time_travel_shm) +time_travel_set_time (time_travel_shm->current_time - + time_travel_shm_offset); +else +time_travel_ext_req (UM_TIMETRAVEL_GET ,-1 ); }static void __time_travel_update_time (unsigned long long ns,bool idle)
include/uapi/linux/um_timetravel.h +167 -15
208 lines not on this page · raw message
Signature · 7 lines
2.41.0 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um