Skip to content

Commit 88ce642

Browse files
jmberg-intelrichardweinberger
authored andcommitted
um: Implement time-travel=ext
This implements synchronized time-travel mode which - using a special application on a unix socket - lets multiple machines take part in a time-travelling simulation together. The protocol for the unix domain socket is defined in the new file include/uapi/linux/um_timetravel.h. Signed-off-by: Johannes Berg <[email protected]> Signed-off-by: Richard Weinberger <[email protected]>
1 parent dd9ada5 commit 88ce642

File tree

7 files changed

+563
-17
lines changed

7 files changed

+563
-17
lines changed

arch/um/drivers/virtio_uml.c

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/virtio.h>
2727
#include <linux/virtio_config.h>
2828
#include <linux/virtio_ring.h>
29+
#include <linux/time-internal.h>
2930
#include <shared/as-layout.h>
3031
#include <irq_kern.h>
3132
#include <init.h>
@@ -64,6 +65,11 @@ struct virtio_uml_device {
6465
struct virtio_uml_vq_info {
6566
int kick_fd, call_fd;
6667
char name[32];
68+
#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
69+
struct virtqueue *vq;
70+
vq_callback_t *callback;
71+
struct time_travel_event defer;
72+
#endif
6773
};
6874

6975
extern unsigned long long physmem_size, highmem;
@@ -118,10 +124,27 @@ static int vhost_user_recv_header(int fd, struct vhost_user_msg *msg)
118124

119125
static int vhost_user_recv(struct virtio_uml_device *vu_dev,
120126
int fd, struct vhost_user_msg *msg,
121-
size_t max_payload_size)
127+
size_t max_payload_size, bool wait)
122128
{
123129
size_t size;
124-
int rc = vhost_user_recv_header(fd, msg);
130+
int rc;
131+
132+
/*
133+
* In virtio time-travel mode, we're handling all the vhost-user
134+
* FDs by polling them whenever appropriate. However, we may get
135+
* into a situation where we're sending out an interrupt message
136+
* to a device (e.g. a net device) and need to handle a simulation
137+
* time message while doing so, e.g. one that tells us to update
138+
* our idea of how long we can run without scheduling.
139+
*
140+
* Thus, we need to not just read() from the given fd, but need
141+
* to also handle messages for the simulation time - this function
142+
* does that for us while waiting for the given fd to be readable.
143+
*/
144+
if (wait)
145+
time_travel_wait_readable(fd);
146+
147+
rc = vhost_user_recv_header(fd, msg);
125148

126149
if (rc == -ECONNRESET && vu_dev->registered) {
127150
struct virtio_uml_platform_data *pdata;
@@ -143,7 +166,8 @@ static int vhost_user_recv_resp(struct virtio_uml_device *vu_dev,
143166
struct vhost_user_msg *msg,
144167
size_t max_payload_size)
145168
{
146-
int rc = vhost_user_recv(vu_dev, vu_dev->sock, msg, max_payload_size);
169+
int rc = vhost_user_recv(vu_dev, vu_dev->sock, msg,
170+
max_payload_size, true);
147171

148172
if (rc)
149173
return rc;
@@ -173,7 +197,8 @@ static int vhost_user_recv_req(struct virtio_uml_device *vu_dev,
173197
struct vhost_user_msg *msg,
174198
size_t max_payload_size)
175199
{
176-
int rc = vhost_user_recv(vu_dev, vu_dev->req_fd, msg, max_payload_size);
200+
int rc = vhost_user_recv(vu_dev, vu_dev->req_fd, msg,
201+
max_payload_size, false);
177202

178203
if (rc)
179204
return rc;
@@ -700,6 +725,8 @@ static bool vu_notify(struct virtqueue *vq)
700725
const uint64_t n = 1;
701726
int rc;
702727

728+
time_travel_propagate_time();
729+
703730
if (info->kick_fd < 0) {
704731
struct virtio_uml_device *vu_dev;
705732

@@ -847,6 +874,23 @@ static int vu_setup_vq_call_fd(struct virtio_uml_device *vu_dev,
847874
return rc;
848875
}
849876

877+
#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
878+
static void vu_defer_irq_handle(struct time_travel_event *d)
879+
{
880+
struct virtio_uml_vq_info *info;
881+
882+
info = container_of(d, struct virtio_uml_vq_info, defer);
883+
info->callback(info->vq);
884+
}
885+
886+
static void vu_defer_irq_callback(struct virtqueue *vq)
887+
{
888+
struct virtio_uml_vq_info *info = vq->priv;
889+
890+
time_travel_add_irq_event(&info->defer);
891+
}
892+
#endif
893+
850894
static struct virtqueue *vu_setup_vq(struct virtio_device *vdev,
851895
unsigned index, vq_callback_t *callback,
852896
const char *name, bool ctx)
@@ -866,6 +910,19 @@ static struct virtqueue *vu_setup_vq(struct virtio_device *vdev,
866910
snprintf(info->name, sizeof(info->name), "%s.%d-%s", pdev->name,
867911
pdev->id, name);
868912

913+
#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
914+
/*
915+
* When we get an interrupt, we must bounce it through the simulation
916+
* calendar (the simtime device), except for the simtime device itself
917+
* since that's part of the simulation control.
918+
*/
919+
if (time_travel_mode == TT_MODE_EXTERNAL && callback) {
920+
info->callback = callback;
921+
callback = vu_defer_irq_callback;
922+
time_travel_set_event_fn(&info->defer, vu_defer_irq_handle);
923+
}
924+
#endif
925+
869926
vq = vring_create_virtqueue(index, num, PAGE_SIZE, vdev, true, true,
870927
ctx, vu_notify, callback, info->name);
871928
if (!vq) {
@@ -874,6 +931,9 @@ static struct virtqueue *vu_setup_vq(struct virtio_device *vdev,
874931
}
875932
vq->priv = info;
876933
num = virtqueue_get_vring_size(vq);
934+
#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
935+
info->vq = vq;
936+
#endif
877937

878938
if (vu_dev->protocol_features &
879939
BIT_ULL(VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS)) {

arch/um/include/linux/time-internal.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum time_travel_mode {
1515
TT_MODE_OFF,
1616
TT_MODE_BASIC,
1717
TT_MODE_INFCPU,
18+
TT_MODE_EXTERNAL,
1819
};
1920

2021
#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
@@ -35,6 +36,24 @@ time_travel_set_event_fn(struct time_travel_event *e,
3536
{
3637
e->fn = fn;
3738
}
39+
40+
void __time_travel_propagate_time(void);
41+
42+
static inline void time_travel_propagate_time(void)
43+
{
44+
if (time_travel_mode == TT_MODE_EXTERNAL)
45+
__time_travel_propagate_time();
46+
}
47+
48+
void __time_travel_wait_readable(int fd);
49+
50+
static inline void time_travel_wait_readable(int fd)
51+
{
52+
if (time_travel_mode == TT_MODE_EXTERNAL)
53+
__time_travel_wait_readable(fd);
54+
}
55+
56+
void time_travel_add_irq_event(struct time_travel_event *e);
3857
#else
3958
struct time_travel_event {
4059
};
@@ -47,5 +66,13 @@ static inline void time_travel_sleep(unsigned long long duration)
4766

4867
/* this is a macro so the event/function need not exist */
4968
#define time_travel_set_event_fn(e, fn) do {} while (0)
69+
70+
static inline void time_travel_propagate_time(void)
71+
{
72+
}
73+
74+
static inline void time_travel_wait_readable(int fd)
75+
{
76+
}
5077
#endif /* CONFIG_UML_TIME_TRAVEL_SUPPORT */
5178
#endif /* __TIMER_INTERNAL_H__ */

arch/um/include/shared/os.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ extern int os_falloc_punch(int fd, unsigned long long offset, int count);
181181
extern int os_eventfd(unsigned int initval, int flags);
182182
extern int os_sendmsg_fds(int fd, const void *buf, unsigned int len,
183183
const int *fds, unsigned int fds_num);
184+
int os_poll(unsigned int n, const int *fds);
184185

185186
/* start_up.c */
186187
extern void os_early_checks(void);

arch/um/kernel/skas/syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ void handle_syscall(struct uml_pt_regs *r)
2424
* went to sleep, even if said userspace interacts with the kernel in
2525
* various ways.
2626
*/
27-
if (time_travel_mode == TT_MODE_INFCPU)
27+
if (time_travel_mode == TT_MODE_INFCPU ||
28+
time_travel_mode == TT_MODE_EXTERNAL)
2829
schedule();
2930

3031
/* Initialize the syscall number and default return value. */

0 commit comments

Comments
 (0)