All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v7 0/5] um: suspend/resume support
@ 2020-12-02 19:58 Johannes Berg
  2020-12-02 19:58 ` [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer Johannes Berg
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Johannes Berg @ 2020-12-02 19:58 UTC (permalink / raw)
  To: linux-um

Here's a respin, for one since I wanted to use pause() instead of
the strange select(0, NULL, NULL, NULL, NULL), but also fixing some
signal handling bugs in the suspend/resume code.

johannes




_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer
  2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
@ 2020-12-02 19:58 ` Johannes Berg
  2020-12-03 11:32   ` Anton Ivanov
  2020-12-02 19:58 ` [PATCH v7 2/5] um: time: fix read_persistent_clock64() in time-travel Johannes Berg
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Johannes Berg @ 2020-12-02 19:58 UTC (permalink / raw)
  To: linux-um; +Cc: Johannes Berg

From: Johannes Berg <johannes.berg@intel.com>

There really is no reason to pass the amount of time we should
sleep, especially since it's just hard-coded to one second.

Additionally, one second isn't really all that long, and as we
are expecting to be woken up by a signal, we can sleep longer
and avoid doing some work every second, so replace the current
clock_nanosleep() with just an empty select() that can _only_
be woken up by a signal.

We can also remove the deliver_alarm() since we don't need to
do that when we got e.g. SIGIO that woke us up, and if we got
SIGALRM the signal handler will actually (have) run, so it's
just unnecessary extra work.

Similarly, in time-travel mode, just program the wakeup event
from idle to be S64_MAX, which is basically the most you could
ever simulate to. Of course, you should already have an event
in the list that's earlier and will cause a wakeup, normally
that's the regular timer interrupt, though in suspend it may
(later) also be an RTC event. Since actually getting to this
point would be a bug and you can't ever get out again, panic()
on it in the time control code.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
v7:
 - use pause()
---
 arch/um/include/linux/time-internal.h |  4 ++--
 arch/um/include/shared/os.h           |  2 +-
 arch/um/kernel/process.c              | 11 ++++-------
 arch/um/kernel/time.c                 | 12 ++++++++++--
 arch/um/os-Linux/time.c               | 17 ++++-------------
 5 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/arch/um/include/linux/time-internal.h b/arch/um/include/linux/time-internal.h
index f3b03d39a854..68e45e950137 100644
--- a/arch/um/include/linux/time-internal.h
+++ b/arch/um/include/linux/time-internal.h
@@ -28,7 +28,7 @@ struct time_travel_event {
 
 extern enum time_travel_mode time_travel_mode;
 
-void time_travel_sleep(unsigned long long duration);
+void time_travel_sleep(void);
 
 static inline void
 time_travel_set_event_fn(struct time_travel_event *e,
@@ -60,7 +60,7 @@ struct time_travel_event {
 
 #define time_travel_mode TT_MODE_OFF
 
-static inline void time_travel_sleep(unsigned long long duration)
+static inline void time_travel_sleep(void)
 {
 }
 
diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h
index e2bb7e488d59..0f7fb8bad728 100644
--- a/arch/um/include/shared/os.h
+++ b/arch/um/include/shared/os.h
@@ -256,7 +256,7 @@ extern void os_warn(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 
 /* time.c */
-extern void os_idle_sleep(unsigned long long nsecs);
+extern void os_idle_sleep(void);
 extern int os_timer_create(void);
 extern int os_timer_set_interval(unsigned long long nsecs);
 extern int os_timer_one_shot(unsigned long long nsecs);
diff --git a/arch/um/kernel/process.c b/arch/um/kernel/process.c
index 3bed09538dd9..0686fabba576 100644
--- a/arch/um/kernel/process.c
+++ b/arch/um/kernel/process.c
@@ -204,13 +204,10 @@ void initial_thread_cb(void (*proc)(void *), void *arg)
 
 static void um_idle_sleep(void)
 {
-	unsigned long long duration = UM_NSEC_PER_SEC;
-
-	if (time_travel_mode != TT_MODE_OFF) {
-		time_travel_sleep(duration);
-	} else {
-		os_idle_sleep(duration);
-	}
+	if (time_travel_mode != TT_MODE_OFF)
+		time_travel_sleep();
+	else
+		os_idle_sleep();
 }
 
 void arch_cpu_idle(void)
diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
index 8dafc3f2add4..8e8eb8ba04a4 100644
--- a/arch/um/kernel/time.c
+++ b/arch/um/kernel/time.c
@@ -46,6 +46,9 @@ static void time_travel_set_time(unsigned long long ns)
 	if (unlikely(ns < time_travel_time))
 		panic("time-travel: time goes backwards %lld -> %lld\n",
 		      time_travel_time, ns);
+	else if (unlikely(ns >= S64_MAX))
+		panic("The system was going to sleep forever, aborting");
+
 	time_travel_time = ns;
 }
 
@@ -399,9 +402,14 @@ static void time_travel_oneshot_timer(struct time_travel_event *e)
 	deliver_alarm();
 }
 
-void time_travel_sleep(unsigned long long duration)
+void time_travel_sleep(void)
 {
-	unsigned long long next = time_travel_time + duration;
+	/*
+	 * Wait "forever" (using S64_MAX because there are some potential
+	 * wrapping issues, especially with the current TT_MODE_EXTERNAL
+	 * controller application.
+	 */
+	unsigned long long next = S64_MAX;
 
 	if (time_travel_mode == TT_MODE_BASIC)
 		os_timer_disable();
diff --git a/arch/um/os-Linux/time.c b/arch/um/os-Linux/time.c
index 90f6de224c70..a61cbf73a179 100644
--- a/arch/um/os-Linux/time.c
+++ b/arch/um/os-Linux/time.c
@@ -7,6 +7,7 @@
  */
 
 #include <stddef.h>
+#include <unistd.h>
 #include <errno.h>
 #include <signal.h>
 #include <time.h>
@@ -99,19 +100,9 @@ long long os_nsecs(void)
 }
 
 /**
- * os_idle_sleep() - sleep for a given time of nsecs
- * @nsecs: nanoseconds to sleep
+ * os_idle_sleep() - sleep until interrupted
  */
-void os_idle_sleep(unsigned long long nsecs)
+void os_idle_sleep(void)
 {
-	struct timespec ts = {
-		.tv_sec  = nsecs / UM_NSEC_PER_SEC,
-		.tv_nsec = nsecs % UM_NSEC_PER_SEC
-	};
-
-	/*
-	 * Relay the signal if clock_nanosleep is interrupted.
-	 */
-	if (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL))
-		deliver_alarm();
+	pause();
 }
-- 
2.26.2


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v7 2/5] um: time: fix read_persistent_clock64() in time-travel
  2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
  2020-12-02 19:58 ` [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer Johannes Berg
@ 2020-12-02 19:58 ` Johannes Berg
  2020-12-03 11:33   ` Anton Ivanov
  2020-12-02 19:58 ` [PATCH v7 3/5] um: allow PM with suspend-to-idle Johannes Berg
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Johannes Berg @ 2020-12-02 19:58 UTC (permalink / raw)
  To: linux-um; +Cc: Johannes Berg

From: Johannes Berg <johannes.berg@intel.com>

In time-travel mode, we've relied on read_persistent_clock64()
being called only once at system startup, but this is both the
right thing to call from the pseudo-RTC, and also gets called
by the timekeeping core during suspend/resume.

Thus, fix this to always fall make use of the time_travel_time
in any time-travel mode, initializing time_travel_start at boot
to the right value depending on the time-travel mode.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 arch/um/kernel/time.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
index 8e8eb8ba04a4..80d33735cfd2 100644
--- a/arch/um/kernel/time.c
+++ b/arch/um/kernel/time.c
@@ -676,10 +676,8 @@ void read_persistent_clock64(struct timespec64 *ts)
 {
 	long long nsecs;
 
-	if (time_travel_start_set)
+	if (time_travel_mode != TT_MODE_OFF)
 		nsecs = time_travel_start + time_travel_time;
-	else if (time_travel_mode == TT_MODE_EXTERNAL)
-		nsecs = time_travel_ext_req(UM_TIMETRAVEL_GET_TOD, -1);
 	else
 		nsecs = os_persistent_clock_emulation();
 
@@ -689,6 +687,25 @@ void read_persistent_clock64(struct timespec64 *ts)
 
 void __init time_init(void)
 {
+#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
+	switch (time_travel_mode) {
+	case TT_MODE_EXTERNAL:
+		time_travel_start = time_travel_ext_req(UM_TIMETRAVEL_GET_TOD, -1);
+		/* controller gave us the *current* time, so adjust by that */
+		time_travel_ext_get_time();
+		time_travel_start -= time_travel_time;
+		break;
+	case TT_MODE_INFCPU:
+	case TT_MODE_BASIC:
+		if (!time_travel_start_set)
+			time_travel_start = os_persistent_clock_emulation();
+		break;
+	case TT_MODE_OFF:
+		/* we just read the host clock with os_persistent_clock_emulation() */
+		break;
+	}
+#endif
+
 	timer_set_signal_handler();
 	late_time_init = um_timer_setup;
 }
-- 
2.26.2


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v7 3/5] um: allow PM with suspend-to-idle
  2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
  2020-12-02 19:58 ` [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer Johannes Berg
  2020-12-02 19:58 ` [PATCH v7 2/5] um: time: fix read_persistent_clock64() in time-travel Johannes Berg
@ 2020-12-02 19:58 ` Johannes Berg
  2020-12-03 11:34   ` Anton Ivanov
  2020-12-02 19:58 ` [PATCH v7 4/5] um: support suspend to RAM Johannes Berg
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Johannes Berg @ 2020-12-02 19:58 UTC (permalink / raw)
  To: linux-um; +Cc: Johannes Berg

From: Johannes Berg <johannes.berg@intel.com>

In order to be able to experiment with suspend in UML, add the
minimal work to be able to suspend (s2idle) an instance of UML,
and be able to wake it back up from that state with the USR1
signal sent to the main UML process.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 arch/um/Kconfig                    |  5 +++++
 arch/um/include/shared/kern_util.h |  2 ++
 arch/um/include/shared/os.h        |  1 +
 arch/um/kernel/um_arch.c           | 25 +++++++++++++++++++++++++
 arch/um/os-Linux/signal.c          | 14 +++++++++++++-
 5 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/arch/um/Kconfig b/arch/um/Kconfig
index 4b799fad8b48..1c57599b82fa 100644
--- a/arch/um/Kconfig
+++ b/arch/um/Kconfig
@@ -192,3 +192,8 @@ config UML_TIME_TRAVEL_SUPPORT
 endmenu
 
 source "arch/um/drivers/Kconfig"
+
+config ARCH_SUSPEND_POSSIBLE
+	def_bool y
+
+source "kernel/power/Kconfig"
diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/kern_util.h
index ccafb62e8cce..9c08e728a675 100644
--- a/arch/um/include/shared/kern_util.h
+++ b/arch/um/include/shared/kern_util.h
@@ -39,6 +39,8 @@ extern int is_syscall(unsigned long addr);
 
 extern void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs);
 
+extern void uml_pm_wake(void);
+
 extern int start_uml(void);
 extern void paging_init(void);
 
diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h
index 0f7fb8bad728..78250a05394a 100644
--- a/arch/um/include/shared/os.h
+++ b/arch/um/include/shared/os.h
@@ -241,6 +241,7 @@ extern int set_signals(int enable);
 extern int set_signals_trace(int enable);
 extern int os_is_signal_stack(void);
 extern void deliver_alarm(void);
+extern void register_pm_wake_signal(void);
 
 /* util.c */
 extern void stack_protections(unsigned long address);
diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c
index 76b37297b7d4..237a8d73a096 100644
--- a/arch/um/kernel/um_arch.c
+++ b/arch/um/kernel/um_arch.c
@@ -13,6 +13,7 @@
 #include <linux/sched.h>
 #include <linux/sched/task.h>
 #include <linux/kmsg_dump.h>
+#include <linux/suspend.h>
 
 #include <asm/processor.h>
 #include <asm/sections.h>
@@ -377,3 +378,27 @@ void *text_poke(void *addr, const void *opcode, size_t len)
 void text_poke_sync(void)
 {
 }
+
+#ifdef CONFIG_PM_SLEEP
+void uml_pm_wake(void)
+{
+	pm_system_wakeup();
+}
+
+static int init_pm_wake_signal(void)
+{
+	/*
+	 * In external time-travel mode we can't use signals to wake up
+	 * since that would mess with the scheduling. We'll have to do
+	 * some additional work to support wakeup on virtio devices or
+	 * similar, perhaps implementing a fake RTC controller that can
+	 * trigger wakeup (and request the appropriate scheduling from
+	 * the external scheduler when going to suspend.)
+	 */
+	if (time_travel_mode != TT_MODE_EXTERNAL)
+		register_pm_wake_signal();
+	return 0;
+}
+
+late_initcall(init_pm_wake_signal);
+#endif
diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c
index b58bc68cbe64..0a2ea84033b4 100644
--- a/arch/um/os-Linux/signal.c
+++ b/arch/um/os-Linux/signal.c
@@ -136,6 +136,16 @@ void set_sigstack(void *sig_stack, int size)
 		panic("enabling signal stack failed, errno = %d\n", errno);
 }
 
+static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
+{
+	uml_pm_wake();
+}
+
+void register_pm_wake_signal(void)
+{
+	set_handler(SIGUSR1);
+}
+
 static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
 	[SIGSEGV] = sig_handler,
 	[SIGBUS] = sig_handler,
@@ -145,7 +155,9 @@ static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
 
 	[SIGIO] = sig_handler,
 	[SIGWINCH] = sig_handler,
-	[SIGALRM] = timer_alarm_handler
+	[SIGALRM] = timer_alarm_handler,
+
+	[SIGUSR1] = sigusr1_handler,
 };
 
 static void hard_handler(int sig, siginfo_t *si, void *p)
-- 
2.26.2


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v7 4/5] um: support suspend to RAM
  2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
                   ` (2 preceding siblings ...)
  2020-12-02 19:58 ` [PATCH v7 3/5] um: allow PM with suspend-to-idle Johannes Berg
@ 2020-12-02 19:58 ` Johannes Berg
  2020-12-03 11:34   ` Anton Ivanov
  2020-12-02 19:58 ` [PATCH v7 5/5] um: add a pseudo RTC Johannes Berg
  2020-12-03 10:06 ` [PATCH v7 0/5] um: suspend/resume support Anton Ivanov
  5 siblings, 1 reply; 13+ messages in thread
From: Johannes Berg @ 2020-12-02 19:58 UTC (permalink / raw)
  To: linux-um; +Cc: Johannes Berg

From: Johannes Berg <johannes.berg@intel.com>

With all the previous bits in place, we can now also support
suspend to RAM, in the sense that everything is suspended,
not just most, including userspace, processes like in s2idle.

Since um_idle_sleep() now waits forever, we can simply call
that to "suspend" the system.

As before, you can wake it up using SIGUSR1 since we're just
in a pause() call that only needs to return.

In order to implement selective resume from certain devices,
and not have any arbitrary device interrupt wake up, suspend
interrupts by removing SIGIO notification (O_ASYNC) from all
the FDs that are not supposed to wake up the system. However,
swap out the handler so we don't actually handle the SIGIO as
an interrupt.

Since we're in pause(), the mere act of receiving SIGIO wakes
us up, and then after things have been restored enough, re-set
O_ASYNC for all previously suspended FDs, reinstall the proper
SIGIO handler, and send SIGIO to self to process anything that
might now be pending.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
v7:
 - clean up a bit
 - send SIGIO to self properly, not to the entire group
---
 arch/um/include/shared/kern_util.h |  1 +
 arch/um/include/shared/os.h        |  3 +
 arch/um/kernel/irq.c               | 88 +++++++++++++++++++++++++++++-
 arch/um/kernel/process.c           |  2 +-
 arch/um/kernel/um_arch.c           | 42 ++++++++++++++
 arch/um/os-Linux/signal.c          |  5 ++
 6 files changed, 139 insertions(+), 2 deletions(-)

diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/kern_util.h
index 9c08e728a675..2888ec812f6e 100644
--- a/arch/um/include/shared/kern_util.h
+++ b/arch/um/include/shared/kern_util.h
@@ -68,5 +68,6 @@ extern void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs);
 extern void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs);
 extern void fatal_sigsegv(void) __attribute__ ((noreturn));
 
+void um_idle_sleep(void);
 
 #endif
diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h
index 78250a05394a..cd750d4edfb5 100644
--- a/arch/um/include/shared/os.h
+++ b/arch/um/include/shared/os.h
@@ -233,6 +233,7 @@ extern void timer_set_signal_handler(void);
 extern void set_sigstack(void *sig_stack, int size);
 extern void remove_sigstack(void);
 extern void set_handler(int sig);
+extern void send_sigio_to_self(void);
 extern int change_sig(int signal, int on);
 extern void block_signals(void);
 extern void unblock_signals(void);
@@ -307,6 +308,8 @@ extern int os_mod_epoll_fd(int events, int fd, void *data);
 extern int os_del_epoll_fd(int fd);
 extern void os_set_ioignore(void);
 extern void os_close_epoll_fd(void);
+extern void um_irqs_suspend(void);
+extern void um_irqs_resume(void);
 
 /* sigio.c */
 extern int add_sigio_fd(int fd);
diff --git a/arch/um/kernel/irq.c b/arch/um/kernel/irq.c
index 482269580b79..ea43312cbfd3 100644
--- a/arch/um/kernel/irq.c
+++ b/arch/um/kernel/irq.c
@@ -20,6 +20,7 @@
 #include <os.h>
 #include <irq_user.h>
 #include <irq_kern.h>
+#include <as-layout.h>
 
 
 extern void free_irqs(void);
@@ -36,12 +37,14 @@ struct irq_reg {
 	int events;
 	bool active;
 	bool pending;
+	bool wakeup;
 };
 
 struct irq_entry {
 	struct list_head list;
 	int fd;
 	struct irq_reg reg[NUM_IRQ_TYPES];
+	bool suspended;
 };
 
 static DEFINE_SPINLOCK(irq_lock);
@@ -70,6 +73,11 @@ static void irq_io_loop(struct irq_reg *irq, struct uml_pt_regs *regs)
 	}
 }
 
+void sigio_handler_suspend(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
+{
+	/* nothing */
+}
+
 void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
 {
 	struct irq_entry *irq_entry;
@@ -365,9 +373,86 @@ int um_request_irq(int irq, int fd, enum um_irq_type type,
 	clear_bit(irq, irqs_allocated);
 	return err;
 }
-
 EXPORT_SYMBOL(um_request_irq);
 
+#ifdef CONFIG_PM_SLEEP
+void um_irqs_suspend(void)
+{
+	struct irq_entry *entry;
+	unsigned long flags;
+
+	sig_info[SIGIO] = sigio_handler_suspend;
+
+	spin_lock_irqsave(&irq_lock, flags);
+	list_for_each_entry(entry, &active_fds, list) {
+		enum um_irq_type t;
+		bool wake = false;
+
+		for (t = 0; t < NUM_IRQ_TYPES; t++) {
+			if (!entry->reg[t].events)
+				continue;
+
+			if (entry->reg[t].wakeup) {
+				wake = true;
+				break;
+			}
+		}
+
+		if (!wake) {
+			entry->suspended = true;
+			os_clear_fd_async(entry->fd);
+		}
+	}
+	spin_unlock_irqrestore(&irq_lock, flags);
+}
+
+void um_irqs_resume(void)
+{
+	struct irq_entry *entry;
+	unsigned long flags;
+
+	spin_lock_irqsave(&irq_lock, flags);
+	list_for_each_entry(entry, &active_fds, list) {
+		if (entry->suspended) {
+			int err = os_set_fd_async(entry->fd);
+
+			WARN(err < 0, "os_set_fd_async returned %d\n", err);
+			entry->suspended = false;
+		}
+	}
+	spin_unlock_irqrestore(&irq_lock, flags);
+
+	sig_info[SIGIO] = sigio_handler;
+	send_sigio_to_self();
+}
+
+static int normal_irq_set_wake(struct irq_data *d, unsigned int on)
+{
+	struct irq_entry *entry;
+	unsigned long flags;
+
+	spin_lock_irqsave(&irq_lock, flags);
+	list_for_each_entry(entry, &active_fds, list) {
+		enum um_irq_type t;
+
+		for (t = 0; t < NUM_IRQ_TYPES; t++) {
+			if (!entry->reg[t].events)
+				continue;
+
+			if (entry->reg[t].irq != d->irq)
+				continue;
+			entry->reg[t].wakeup = on;
+			goto unlock;
+		}
+	}
+unlock:
+	spin_unlock_irqrestore(&irq_lock, flags);
+	return 0;
+}
+#else
+#define normal_irq_set_wake NULL
+#endif
+
 /*
  * irq_chip must define at least enable/disable and ack when
  * the edge handler is used.
@@ -384,6 +469,7 @@ static struct irq_chip normal_irq_type = {
 	.irq_ack = dummy,
 	.irq_mask = dummy,
 	.irq_unmask = dummy,
+	.irq_set_wake = normal_irq_set_wake,
 };
 
 static struct irq_chip alarm_irq_type = {
diff --git a/arch/um/kernel/process.c b/arch/um/kernel/process.c
index 0686fabba576..f0f50eae2293 100644
--- a/arch/um/kernel/process.c
+++ b/arch/um/kernel/process.c
@@ -202,7 +202,7 @@ void initial_thread_cb(void (*proc)(void *), void *arg)
 	kmalloc_ok = save_kmalloc_ok;
 }
 
-static void um_idle_sleep(void)
+void um_idle_sleep(void)
 {
 	if (time_travel_mode != TT_MODE_OFF)
 		time_travel_sleep();
diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c
index 237a8d73a096..9c7e6d7ea1b3 100644
--- a/arch/um/kernel/um_arch.c
+++ b/arch/um/kernel/um_arch.c
@@ -385,6 +385,45 @@ void uml_pm_wake(void)
 	pm_system_wakeup();
 }
 
+static int um_suspend_valid(suspend_state_t state)
+{
+	return state == PM_SUSPEND_MEM;
+}
+
+static int um_suspend_prepare(void)
+{
+	um_irqs_suspend();
+	return 0;
+}
+
+static int um_suspend_enter(suspend_state_t state)
+{
+	if (WARN_ON(state != PM_SUSPEND_MEM))
+		return -EINVAL;
+
+	/*
+	 * This is identical to the idle sleep, but we've just
+	 * (during suspend) turned off all interrupt sources
+	 * except for the ones we want, so now we can only wake
+	 * up on something we actually want to wake up on. All
+	 * timing has also been suspended.
+	 */
+	um_idle_sleep();
+	return 0;
+}
+
+static void um_suspend_finish(void)
+{
+	um_irqs_resume();
+}
+
+const struct platform_suspend_ops um_suspend_ops = {
+	.valid = um_suspend_valid,
+	.prepare = um_suspend_prepare,
+	.enter = um_suspend_enter,
+	.finish = um_suspend_finish,
+};
+
 static int init_pm_wake_signal(void)
 {
 	/*
@@ -397,6 +436,9 @@ static int init_pm_wake_signal(void)
 	 */
 	if (time_travel_mode != TT_MODE_EXTERNAL)
 		register_pm_wake_signal();
+
+	suspend_set_ops(&um_suspend_ops);
+
 	return 0;
 }
 
diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c
index 0a2ea84033b4..510e956b4320 100644
--- a/arch/um/os-Linux/signal.c
+++ b/arch/um/os-Linux/signal.c
@@ -234,6 +234,11 @@ void set_handler(int sig)
 		panic("sigprocmask failed - errno = %d\n", errno);
 }
 
+void send_sigio_to_self(void)
+{
+	kill(os_getpid(), SIGIO);
+}
+
 int change_sig(int signal, int on)
 {
 	sigset_t sigset;
-- 
2.26.2


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v7 5/5] um: add a pseudo RTC
  2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
                   ` (3 preceding siblings ...)
  2020-12-02 19:58 ` [PATCH v7 4/5] um: support suspend to RAM Johannes Berg
@ 2020-12-02 19:58 ` Johannes Berg
  2020-12-03 11:34   ` Anton Ivanov
  2020-12-03 10:06 ` [PATCH v7 0/5] um: suspend/resume support Anton Ivanov
  5 siblings, 1 reply; 13+ messages in thread
From: Johannes Berg @ 2020-12-02 19:58 UTC (permalink / raw)
  To: linux-um; +Cc: Johannes Berg

From: Johannes Berg <johannes.berg@intel.com>

Add a pseudo RTC that simply is able to send an alarm signal
waking up the system at a given time in the future.

Since apparently timerfd_create() FDs don't (always?) support
SIGIO, we fork a background thread that does nothing but wait
for control input. If the alarm is enabled it wakes up at the
right time to send an interrupt back to the system.

This probably isn't _quite_ right since it doesn't specifically
use CLOCK_REALTIME, and thus if time changes on the host we'll
not wake up at the right time, but it will let us test suspend
and RTC-initiated resume.

For time-travel mode, OTOH, just add an event at the specified
time in the future, and that's already sufficient to wake up
the system at that point in time since suspend will just be in
an "endless wait".

For s2idle support also call pm_system_wakeup().

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 arch/um/drivers/Kconfig               |  11 ++
 arch/um/drivers/Makefile              |   2 +
 arch/um/drivers/rtc.h                 |  15 ++
 arch/um/drivers/rtc_kern.c            | 210 ++++++++++++++++++++++++++
 arch/um/drivers/rtc_user.c            | 204 +++++++++++++++++++++++++
 arch/um/include/linux/time-internal.h |  11 ++
 arch/um/kernel/time.c                 |  10 +-
 7 files changed, 462 insertions(+), 1 deletion(-)
 create mode 100644 arch/um/drivers/rtc.h
 create mode 100644 arch/um/drivers/rtc_kern.c
 create mode 100644 arch/um/drivers/rtc_user.c

diff --git a/arch/um/drivers/Kconfig b/arch/um/drivers/Kconfig
index 2e7b8e0e7194..5509e627bbd4 100644
--- a/arch/um/drivers/Kconfig
+++ b/arch/um/drivers/Kconfig
@@ -346,3 +346,14 @@ config VIRTIO_UML
 	help
 	  This driver provides support for virtio based paravirtual device
 	  drivers over vhost-user sockets.
+
+config UML_RTC
+	bool "UML RTC driver"
+	depends on RTC_CLASS
+	# there's no use in this if PM_SLEEP isn't enabled ...
+	depends on PM_SLEEP
+	help
+	  When PM_SLEEP is configured, it may be desirable to wake up using
+	  rtcwake, especially in time-travel mode. This driver enables that
+	  by providing a fake RTC clock that causes a wakeup at the right
+	  time.
diff --git a/arch/um/drivers/Makefile b/arch/um/drivers/Makefile
index 2a249f619467..dcc64a02f81f 100644
--- a/arch/um/drivers/Makefile
+++ b/arch/um/drivers/Makefile
@@ -17,6 +17,7 @@ hostaudio-objs := hostaudio_kern.o
 ubd-objs := ubd_kern.o ubd_user.o
 port-objs := port_kern.o port_user.o
 harddog-objs := harddog_kern.o harddog_user.o
+rtc-objs := rtc_kern.o rtc_user.o
 
 LDFLAGS_pcap.o = $(shell $(CC) $(KBUILD_CFLAGS) -print-file-name=libpcap.a)
 
@@ -62,6 +63,7 @@ obj-$(CONFIG_UML_WATCHDOG) += harddog.o
 obj-$(CONFIG_BLK_DEV_COW_COMMON) += cow_user.o
 obj-$(CONFIG_UML_RANDOM) += random.o
 obj-$(CONFIG_VIRTIO_UML) += virtio_uml.o
+obj-$(CONFIG_UML_RTC) += rtc.o
 
 # pcap_user.o must be added explicitly.
 USER_OBJS := fd.o null.o pty.o tty.o xterm.o slip_common.o pcap_user.o vde_user.o vector_user.o
diff --git a/arch/um/drivers/rtc.h b/arch/um/drivers/rtc.h
new file mode 100644
index 000000000000..95e41c7d35c4
--- /dev/null
+++ b/arch/um/drivers/rtc.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 Intel Corporation
+ * Author: Johannes Berg <johannes@sipsolutions.net>
+ */
+#ifndef __UM_RTC_H__
+#define __UM_RTC_H__
+
+int uml_rtc_start(bool timetravel);
+int uml_rtc_enable_alarm(unsigned long long delta_seconds);
+void uml_rtc_disable_alarm(void);
+void uml_rtc_stop(bool timetravel);
+void uml_rtc_send_timetravel_alarm(void);
+
+#endif /* __UM_RTC_H__ */
diff --git a/arch/um/drivers/rtc_kern.c b/arch/um/drivers/rtc_kern.c
new file mode 100644
index 000000000000..d4ae97ff7aea
--- /dev/null
+++ b/arch/um/drivers/rtc_kern.c
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Intel Corporation
+ * Author: Johannes Berg <johannes@sipsolutions.net>
+ */
+#include <linux/platform_device.h>
+#include <linux/time-internal.h>
+#include <linux/suspend.h>
+#include <linux/err.h>
+#include <linux/rtc.h>
+#include <kern_util.h>
+#include <irq_kern.h>
+#include <os.h>
+#include "rtc.h"
+
+static time64_t uml_rtc_alarm_time;
+static bool uml_rtc_alarm_enabled;
+static struct rtc_device *uml_rtc;
+static int uml_rtc_irq_fd, uml_rtc_irq;
+
+#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
+
+static void uml_rtc_time_travel_alarm(struct time_travel_event *ev)
+{
+	uml_rtc_send_timetravel_alarm();
+}
+
+static struct time_travel_event uml_rtc_alarm_event = {
+	.fn = uml_rtc_time_travel_alarm,
+};
+#endif
+
+static int uml_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct timespec64 ts;
+
+	/* Use this to get correct time in time-travel mode */
+	read_persistent_clock64(&ts);
+	rtc_time64_to_tm(timespec64_to_ktime(ts) / NSEC_PER_SEC, tm);
+
+	return 0;
+}
+
+static int uml_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	rtc_time64_to_tm(uml_rtc_alarm_time, &alrm->time);
+	alrm->enabled = uml_rtc_alarm_enabled;
+
+	return 0;
+}
+
+static int uml_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
+{
+	unsigned long long secs;
+
+	if (!enable && !uml_rtc_alarm_enabled)
+		return 0;
+
+	uml_rtc_alarm_enabled = enable;
+
+	secs = uml_rtc_alarm_time - ktime_get_real_seconds();
+
+	if (time_travel_mode == TT_MODE_OFF) {
+		if (!enable) {
+			uml_rtc_disable_alarm();
+			return 0;
+		}
+
+		/* enable or update */
+		return uml_rtc_enable_alarm(secs);
+	} else {
+		time_travel_del_event(&uml_rtc_alarm_event);
+
+		if (enable)
+			time_travel_add_event_rel(&uml_rtc_alarm_event,
+						  secs * NSEC_PER_SEC);
+	}
+
+	return 0;
+}
+
+static int uml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	uml_rtc_alarm_irq_enable(dev, 0);
+	uml_rtc_alarm_time = rtc_tm_to_time64(&alrm->time);
+	uml_rtc_alarm_irq_enable(dev, alrm->enabled);
+
+	return 0;
+}
+
+static const struct rtc_class_ops uml_rtc_ops = {
+	.read_time = uml_rtc_read_time,
+	.read_alarm = uml_rtc_read_alarm,
+	.alarm_irq_enable = uml_rtc_alarm_irq_enable,
+	.set_alarm = uml_rtc_set_alarm,
+};
+
+static irqreturn_t uml_rtc_interrupt(int irq, void *data)
+{
+	char c = -1;
+
+	/* alarm triggered, it's now off */
+	uml_rtc_alarm_enabled = false;
+
+	os_read_file(uml_rtc_irq_fd, &c, sizeof(c));
+	WARN_ON(c);
+
+	pm_system_wakeup();
+	rtc_update_irq(uml_rtc, 1, RTC_IRQF | RTC_AF);
+
+	return IRQ_HANDLED;
+}
+
+static int uml_rtc_setup(void)
+{
+	int err;
+
+	err = uml_rtc_start(time_travel_mode != TT_MODE_OFF);
+	if (err < 0)
+		return err;
+
+	uml_rtc_irq_fd = err;
+
+	err = um_request_irq(UM_IRQ_ALLOC, uml_rtc_irq_fd, IRQ_READ,
+			     uml_rtc_interrupt, 0, "rtc", NULL);
+	if (err < 0) {
+		uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
+		return err;
+	}
+
+	irq_set_irq_wake(err, 1);
+
+	uml_rtc_irq = err;
+	return 0;
+}
+
+static void uml_rtc_cleanup(void)
+{
+	um_free_irq(uml_rtc_irq, NULL);
+	uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
+}
+
+static int uml_rtc_probe(struct platform_device *pdev)
+{
+	int err;
+
+	err = uml_rtc_setup();
+	if (err)
+		return err;
+
+	uml_rtc = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(uml_rtc)) {
+		err = PTR_ERR(uml_rtc);
+		goto cleanup;
+	}
+
+	uml_rtc->ops = &uml_rtc_ops;
+
+	err = rtc_register_device(uml_rtc);
+	if (err)
+		goto cleanup;
+
+	device_init_wakeup(&pdev->dev, 1);
+	return 0;
+cleanup:
+	uml_rtc_cleanup();
+	return err;
+}
+
+static int uml_rtc_remove(struct platform_device *pdev)
+{
+	device_init_wakeup(&pdev->dev, 0);
+	uml_rtc_cleanup();
+	return 0;
+}
+
+static struct platform_driver uml_rtc_driver = {
+	.probe = uml_rtc_probe,
+	.remove = uml_rtc_remove,
+	.driver = {
+		.name = "uml-rtc",
+	},
+};
+
+static int __init uml_rtc_init(void)
+{
+	struct platform_device *pdev;
+	int err;
+
+	err = platform_driver_register(&uml_rtc_driver);
+	if (err)
+		return err;
+
+	pdev = platform_device_alloc("uml-rtc", 0);
+	if (!pdev) {
+		err = -ENOMEM;
+		goto unregister;
+	}
+
+	err = platform_device_add(pdev);
+	if (err)
+		goto unregister;
+	return 0;
+
+unregister:
+	platform_device_put(pdev);
+	platform_driver_unregister(&uml_rtc_driver);
+	return err;
+}
+device_initcall(uml_rtc_init);
diff --git a/arch/um/drivers/rtc_user.c b/arch/um/drivers/rtc_user.c
new file mode 100644
index 000000000000..e2a1a119d2b6
--- /dev/null
+++ b/arch/um/drivers/rtc_user.c
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Intel Corporation
+ * Author: Johannes Berg <johannes@sipsolutions.net>
+ */
+#include <os.h>
+#include <errno.h>
+#include <sched.h>
+#include <unistd.h>
+#include <kern_util.h>
+#include <sys/select.h>
+#include <stdio.h>
+#include "rtc.h"
+
+/*
+ * It seems we should just be able to use a timerfd here,
+ * but apparently they don't send SIGIO. So use our own
+ * helper thread instead.
+ */
+
+static unsigned long uml_rtc_stack;
+static int uml_rtc_comm_fds[2];
+static int uml_rtc_irq_fds[2];
+static int uml_rtc_thread_pid;
+
+struct uml_rtc_message {
+	unsigned long long secs;
+	bool enabled;
+};
+
+struct uml_rtc_fds {
+	int comm_fd, irq_fd;
+};
+
+static int uml_rtc_signal_wake(int fd)
+{
+	char c = 0;
+	int err;
+
+	CATCH_EINTR(err = write(fd, &c, sizeof(c)));
+	if (err != 1) {
+		printf("rtc helper: failed to write irq (errno=%d)\n",
+		       errno);
+		fflush(stdout);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+void uml_rtc_send_timetravel_alarm(void)
+{
+	uml_rtc_signal_wake(uml_rtc_irq_fds[1]);
+}
+
+static int uml_rtc_thread(void *_data)
+{
+	struct timeval timeout = {};
+	bool enabled = false;
+	struct uml_rtc_fds fds;
+
+	os_fix_helper_signals();
+
+	fds = *(struct uml_rtc_fds *)_data;
+
+	while (1) {
+		fd_set fdset;
+		int readable, err;
+
+		FD_ZERO(&fdset);
+		FD_SET(fds.comm_fd, &fdset);
+
+		readable = select(fds.comm_fd + 1, &fdset, NULL, NULL,
+				  enabled ? &timeout : NULL);
+		if (readable == 1) {
+			struct uml_rtc_message msg;
+			char c = 0;
+
+			CATCH_EINTR(err = read(fds.comm_fd, &msg, sizeof(msg)));
+			if (err != sizeof(msg)) {
+				printf("rtc helper: failed to read (%d)\n",
+				       err);
+				fflush(stdout);
+				return -EIO;
+			}
+
+			enabled = msg.enabled;
+			timeout.tv_usec = 0;
+			timeout.tv_sec = msg.secs;
+
+			CATCH_EINTR(err = write(fds.comm_fd, &c, sizeof(c)));
+			if (err != 1) {
+				printf("rtc helper: failed to write comm (errno=%d)\n",
+				       errno);
+				fflush(stdout);
+				return -EIO;
+			}
+		} else if (readable == 0) {
+			enabled = false;
+
+			err = uml_rtc_signal_wake(fds.irq_fd);
+			if (err)
+				return err;
+		}
+		/* else ... should we update the timeout if enabled? */
+	}
+
+	return 0;
+}
+
+int uml_rtc_send(struct uml_rtc_message *msg)
+{
+	char c;
+	int err;
+
+	CATCH_EINTR(err = write(uml_rtc_comm_fds[0], msg, sizeof(*msg)));
+	if (err != sizeof(*msg))
+		return err >= 0 ? -EPIPE : err;
+
+	CATCH_EINTR(err = read(uml_rtc_comm_fds[0], &c, sizeof(c)));
+	if (err != sizeof(c))
+		return err >= 0 ? -EPIPE : err;
+
+	return 0;
+}
+
+int uml_rtc_start(bool timetravel)
+{
+	struct uml_rtc_message msg = {
+		.enabled = false,
+	};
+	struct uml_rtc_fds data = {};
+	int err;
+
+	if (uml_rtc_stack)
+		return -EBUSY;
+
+	err = os_pipe(uml_rtc_irq_fds, 1, 1);
+	if (err)
+		return err;
+
+	if (!timetravel) {
+		err = os_pipe(uml_rtc_comm_fds, 1, 1);
+		if (err)
+			goto fail;
+
+		data.comm_fd = uml_rtc_comm_fds[1];
+		data.irq_fd = uml_rtc_irq_fds[1];
+
+		err = run_helper_thread(uml_rtc_thread, &data, CLONE_FILES,
+					&uml_rtc_stack);
+		if (err < 0)
+			goto fail;
+		uml_rtc_thread_pid = err;
+
+		err = uml_rtc_send(&msg);
+		if (err)
+			goto fail;
+	}
+
+	return uml_rtc_irq_fds[0];
+
+fail:
+	uml_rtc_stop(timetravel);
+	return err;
+}
+
+int uml_rtc_enable_alarm(unsigned long long delta_seconds)
+{
+	struct uml_rtc_message msg = {
+		.enabled = true,
+		.secs = delta_seconds,
+	};
+
+	return uml_rtc_send(&msg);
+}
+
+void uml_rtc_disable_alarm(void)
+{
+	struct uml_rtc_message msg = {
+		.enabled = false,
+	};
+
+	uml_rtc_send(&msg);
+}
+
+void uml_rtc_stop(bool timetravel)
+{
+	os_close_file(uml_rtc_irq_fds[1]);
+	os_close_file(uml_rtc_irq_fds[0]);
+
+	if (timetravel)
+		return;
+
+	os_close_file(uml_rtc_comm_fds[1]);
+	os_close_file(uml_rtc_comm_fds[0]);
+
+	if (uml_rtc_thread_pid)
+		os_kill_process(uml_rtc_thread_pid, 1);
+	if (uml_rtc_stack)
+		free_stack(uml_rtc_stack, 0);
+	uml_rtc_thread_pid = 0;
+	uml_rtc_stack = 0;
+}
diff --git a/arch/um/include/linux/time-internal.h b/arch/um/include/linux/time-internal.h
index 68e45e950137..088d6a4c0b9d 100644
--- a/arch/um/include/linux/time-internal.h
+++ b/arch/um/include/linux/time-internal.h
@@ -54,6 +54,9 @@ static inline void time_travel_wait_readable(int fd)
 }
 
 void time_travel_add_irq_event(struct time_travel_event *e);
+void time_travel_add_event_rel(struct time_travel_event *e,
+			       unsigned long long delay_ns);
+bool time_travel_del_event(struct time_travel_event *e);
 #else
 struct time_travel_event {
 };
@@ -74,6 +77,14 @@ static inline void time_travel_propagate_time(void)
 static inline void time_travel_wait_readable(int fd)
 {
 }
+
+/*
+ * not inlines so the data structure need not exist,
+ * cause linker failures
+ */
+extern void time_travel_not_configured(void);
+#define time_travel_add_event_rel(...) time_travel_not_configured()
+#define time_travel_del_event(...) time_travel_not_configured()
 #endif /* CONFIG_UML_TIME_TRAVEL_SUPPORT */
 
 /*
diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
index 80d33735cfd2..303565ce8c64 100644
--- a/arch/um/kernel/time.c
+++ b/arch/um/kernel/time.c
@@ -302,6 +302,12 @@ static void time_travel_add_event(struct time_travel_event *e,
 	__time_travel_add_event(e, time);
 }
 
+void time_travel_add_event_rel(struct time_travel_event *e,
+			       unsigned long long delay_ns)
+{
+	time_travel_add_event(e, time_travel_time + delay_ns);
+}
+
 void time_travel_periodic_timer(struct time_travel_event *e)
 {
 	time_travel_add_event(&time_travel_timer_event,
@@ -328,7 +334,7 @@ static void time_travel_deliver_event(struct time_travel_event *e)
 	}
 }
 
-static bool time_travel_del_event(struct time_travel_event *e)
+bool time_travel_del_event(struct time_travel_event *e)
 {
 	if (!e->pending)
 		return false;
@@ -504,6 +510,8 @@ extern u64 time_travel_ext_req(u32 op, u64 time);
 
 /* these are empty macros so the struct/fn need not exist */
 #define time_travel_add_event(e, time) do { } while (0)
+/* externally not usable - redefine here so we can */
+#undef time_travel_del_event
 #define time_travel_del_event(e) do { } while (0)
 #endif
 
-- 
2.26.2


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 0/5] um: suspend/resume support
  2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
                   ` (4 preceding siblings ...)
  2020-12-02 19:58 ` [PATCH v7 5/5] um: add a pseudo RTC Johannes Berg
@ 2020-12-03 10:06 ` Anton Ivanov
  2020-12-03 10:14   ` Johannes Berg
  5 siblings, 1 reply; 13+ messages in thread
From: Anton Ivanov @ 2020-12-03 10:06 UTC (permalink / raw)
  To: Johannes Berg, linux-um


On 02/12/2020 19:58, Johannes Berg wrote:
> Here's a respin, for one since I wanted to use pause() instead of
> the strange select(0, NULL, NULL, NULL, NULL), but also fixing some
> signal handling bugs in the suspend/resume code.
>
> johannes
>
>
>
>
> _______________________________________________
> linux-um mailing list
> linux-um@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-um

Works nicely and I will ACK them shortly.

I will see if I can add some WOL functionality to the vector drivers.

It is definitely possible for the ones which use a raw socket - you leave them as capable of generating a SIGIO, but load a "sleeptime" BPF filter which allows only the types of packets that are entitled to wake you up - WOL magic, ARP, unicast to your own MAC, etc. IIRC ethtool has a list of all the possible options. When you wake up, you swap back the old filter (if there was any).

Brgds,

-- 
Anton R. Ivanov
https://www.kot-begemot.co.uk/


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 0/5] um: suspend/resume support
  2020-12-03 10:06 ` [PATCH v7 0/5] um: suspend/resume support Anton Ivanov
@ 2020-12-03 10:14   ` Johannes Berg
  0 siblings, 0 replies; 13+ messages in thread
From: Johannes Berg @ 2020-12-03 10:14 UTC (permalink / raw)
  To: Anton Ivanov, linux-um

On Thu, 2020-12-03 at 10:06 +0000, Anton Ivanov wrote:
> 
> Works nicely and I will ACK them shortly.

:)

> I will see if I can add some WOL functionality to the vector drivers.

That might be nice.

> It is definitely possible for the ones which use a raw socket - you
> leave them as capable of generating a SIGIO, but load a "sleeptime"
> BPF filter which allows only the types of packets that are entitled to
> wake you up - WOL magic, ARP, unicast to your own MAC, etc. IIRC
> ethtool has a list of all the possible options. When you wake up, you
> swap back the old filter (if there was any).

Right, you can set the IRQ to allow wakeup using just the normal
irq_set_irq_wake() function, and then you get woken up on anything.

And with a BPF filter you can drop all the other packets, so you don't
get a full socket queue either (in the host) that would prevent
something from working.

Indeed there are more options than I thought there were:

           wol p|u|m|b|a|g|s|f|d...
                  Sets Wake-on-LAN options.  Not all devices support this.  The argument to this option is a string
                  of characters specifying which options to enable.

                  p   Wake on PHY activity
                  u   Wake on unicast messages
                  m   Wake on multicast messages
                  b   Wake on broadcast messages
                  a   Wake on ARP
                  g   Wake on MagicPacket™
                  s   Enable SecureOn™ password for MagicPacket™
                  f   Wake on filter(s)
                  d   Disable  (wake  on  nothing).  This option
                      clears all previous options.


p would be trivial, requiring only a call to irq_set_irq_wake(), u, m,
b, a, g and s would require BPF filters, and f (filter) is difficult,
you'd need to support RX-flow-spec stuff, apparently.


I'm trying to add support for virtio ... hmm. Actually p or u might be
very useful there for me, but it'd require changing the virtio-net spec
I guess ... Maybe I'll just do an out-of-band wake device for my system
:)

johannes


_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer
  2020-12-02 19:58 ` [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer Johannes Berg
@ 2020-12-03 11:32   ` Anton Ivanov
  0 siblings, 0 replies; 13+ messages in thread
From: Anton Ivanov @ 2020-12-03 11:32 UTC (permalink / raw)
  To: Johannes Berg, linux-um; +Cc: Johannes Berg



On 02/12/2020 19:58, Johannes Berg wrote:
> From: Johannes Berg <johannes.berg@intel.com>
> 
> There really is no reason to pass the amount of time we should
> sleep, especially since it's just hard-coded to one second.
> 
> Additionally, one second isn't really all that long, and as we
> are expecting to be woken up by a signal, we can sleep longer
> and avoid doing some work every second, so replace the current
> clock_nanosleep() with just an empty select() that can _only_
> be woken up by a signal.
> 
> We can also remove the deliver_alarm() since we don't need to
> do that when we got e.g. SIGIO that woke us up, and if we got
> SIGALRM the signal handler will actually (have) run, so it's
> just unnecessary extra work.
> 
> Similarly, in time-travel mode, just program the wakeup event
> from idle to be S64_MAX, which is basically the most you could
> ever simulate to. Of course, you should already have an event
> in the list that's earlier and will cause a wakeup, normally
> that's the regular timer interrupt, though in suspend it may
> (later) also be an RTC event. Since actually getting to this
> point would be a bug and you can't ever get out again, panic()
> on it in the time control code.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> ---
> v7:
>   - use pause()
> ---
>   arch/um/include/linux/time-internal.h |  4 ++--
>   arch/um/include/shared/os.h           |  2 +-
>   arch/um/kernel/process.c              | 11 ++++-------
>   arch/um/kernel/time.c                 | 12 ++++++++++--
>   arch/um/os-Linux/time.c               | 17 ++++-------------
>   5 files changed, 21 insertions(+), 25 deletions(-)
> 
> diff --git a/arch/um/include/linux/time-internal.h b/arch/um/include/linux/time-internal.h
> index f3b03d39a854..68e45e950137 100644
> --- a/arch/um/include/linux/time-internal.h
> +++ b/arch/um/include/linux/time-internal.h
> @@ -28,7 +28,7 @@ struct time_travel_event {
>   
>   extern enum time_travel_mode time_travel_mode;
>   
> -void time_travel_sleep(unsigned long long duration);
> +void time_travel_sleep(void);
>   
>   static inline void
>   time_travel_set_event_fn(struct time_travel_event *e,
> @@ -60,7 +60,7 @@ struct time_travel_event {
>   
>   #define time_travel_mode TT_MODE_OFF
>   
> -static inline void time_travel_sleep(unsigned long long duration)
> +static inline void time_travel_sleep(void)
>   {
>   }
>   
> diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h
> index e2bb7e488d59..0f7fb8bad728 100644
> --- a/arch/um/include/shared/os.h
> +++ b/arch/um/include/shared/os.h
> @@ -256,7 +256,7 @@ extern void os_warn(const char *fmt, ...)
>   	__attribute__ ((format (printf, 1, 2)));
>   
>   /* time.c */
> -extern void os_idle_sleep(unsigned long long nsecs);
> +extern void os_idle_sleep(void);
>   extern int os_timer_create(void);
>   extern int os_timer_set_interval(unsigned long long nsecs);
>   extern int os_timer_one_shot(unsigned long long nsecs);
> diff --git a/arch/um/kernel/process.c b/arch/um/kernel/process.c
> index 3bed09538dd9..0686fabba576 100644
> --- a/arch/um/kernel/process.c
> +++ b/arch/um/kernel/process.c
> @@ -204,13 +204,10 @@ void initial_thread_cb(void (*proc)(void *), void *arg)
>   
>   static void um_idle_sleep(void)
>   {
> -	unsigned long long duration = UM_NSEC_PER_SEC;
> -
> -	if (time_travel_mode != TT_MODE_OFF) {
> -		time_travel_sleep(duration);
> -	} else {
> -		os_idle_sleep(duration);
> -	}
> +	if (time_travel_mode != TT_MODE_OFF)
> +		time_travel_sleep();
> +	else
> +		os_idle_sleep();
>   }
>   
>   void arch_cpu_idle(void)
> diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
> index 8dafc3f2add4..8e8eb8ba04a4 100644
> --- a/arch/um/kernel/time.c
> +++ b/arch/um/kernel/time.c
> @@ -46,6 +46,9 @@ static void time_travel_set_time(unsigned long long ns)
>   	if (unlikely(ns < time_travel_time))
>   		panic("time-travel: time goes backwards %lld -> %lld\n",
>   		      time_travel_time, ns);
> +	else if (unlikely(ns >= S64_MAX))
> +		panic("The system was going to sleep forever, aborting");
> +
>   	time_travel_time = ns;
>   }
>   
> @@ -399,9 +402,14 @@ static void time_travel_oneshot_timer(struct time_travel_event *e)
>   	deliver_alarm();
>   }
>   
> -void time_travel_sleep(unsigned long long duration)
> +void time_travel_sleep(void)
>   {
> -	unsigned long long next = time_travel_time + duration;
> +	/*
> +	 * Wait "forever" (using S64_MAX because there are some potential
> +	 * wrapping issues, especially with the current TT_MODE_EXTERNAL
> +	 * controller application.
> +	 */
> +	unsigned long long next = S64_MAX;
>   
>   	if (time_travel_mode == TT_MODE_BASIC)
>   		os_timer_disable();
> diff --git a/arch/um/os-Linux/time.c b/arch/um/os-Linux/time.c
> index 90f6de224c70..a61cbf73a179 100644
> --- a/arch/um/os-Linux/time.c
> +++ b/arch/um/os-Linux/time.c
> @@ -7,6 +7,7 @@
>    */
>   
>   #include <stddef.h>
> +#include <unistd.h>
>   #include <errno.h>
>   #include <signal.h>
>   #include <time.h>
> @@ -99,19 +100,9 @@ long long os_nsecs(void)
>   }
>   
>   /**
> - * os_idle_sleep() - sleep for a given time of nsecs
> - * @nsecs: nanoseconds to sleep
> + * os_idle_sleep() - sleep until interrupted
>    */
> -void os_idle_sleep(unsigned long long nsecs)
> +void os_idle_sleep(void)
>   {
> -	struct timespec ts = {
> -		.tv_sec  = nsecs / UM_NSEC_PER_SEC,
> -		.tv_nsec = nsecs % UM_NSEC_PER_SEC
> -	};
> -
> -	/*
> -	 * Relay the signal if clock_nanosleep is interrupted.
> -	 */
> -	if (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL))
> -		deliver_alarm();
> +	pause();
>   }
> 

Acked-By: Anton Ivanov <anton.ivanov@cambridgegreys.com>

-- 
Anton R. Ivanov
https://www.kot-begemot.co.uk/

_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 2/5] um: time: fix read_persistent_clock64() in time-travel
  2020-12-02 19:58 ` [PATCH v7 2/5] um: time: fix read_persistent_clock64() in time-travel Johannes Berg
@ 2020-12-03 11:33   ` Anton Ivanov
  0 siblings, 0 replies; 13+ messages in thread
From: Anton Ivanov @ 2020-12-03 11:33 UTC (permalink / raw)
  To: Johannes Berg, linux-um; +Cc: Johannes Berg



On 02/12/2020 19:58, Johannes Berg wrote:
> From: Johannes Berg <johannes.berg@intel.com>
> 
> In time-travel mode, we've relied on read_persistent_clock64()
> being called only once at system startup, but this is both the
> right thing to call from the pseudo-RTC, and also gets called
> by the timekeeping core during suspend/resume.
> 
> Thus, fix this to always fall make use of the time_travel_time
> in any time-travel mode, initializing time_travel_start at boot
> to the right value depending on the time-travel mode.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> ---
>   arch/um/kernel/time.c | 23 ++++++++++++++++++++---
>   1 file changed, 20 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
> index 8e8eb8ba04a4..80d33735cfd2 100644
> --- a/arch/um/kernel/time.c
> +++ b/arch/um/kernel/time.c
> @@ -676,10 +676,8 @@ void read_persistent_clock64(struct timespec64 *ts)
>   {
>   	long long nsecs;
>   
> -	if (time_travel_start_set)
> +	if (time_travel_mode != TT_MODE_OFF)
>   		nsecs = time_travel_start + time_travel_time;
> -	else if (time_travel_mode == TT_MODE_EXTERNAL)
> -		nsecs = time_travel_ext_req(UM_TIMETRAVEL_GET_TOD, -1);
>   	else
>   		nsecs = os_persistent_clock_emulation();
>   
> @@ -689,6 +687,25 @@ void read_persistent_clock64(struct timespec64 *ts)
>   
>   void __init time_init(void)
>   {
> +#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
> +	switch (time_travel_mode) {
> +	case TT_MODE_EXTERNAL:
> +		time_travel_start = time_travel_ext_req(UM_TIMETRAVEL_GET_TOD, -1);
> +		/* controller gave us the *current* time, so adjust by that */
> +		time_travel_ext_get_time();
> +		time_travel_start -= time_travel_time;
> +		break;
> +	case TT_MODE_INFCPU:
> +	case TT_MODE_BASIC:
> +		if (!time_travel_start_set)
> +			time_travel_start = os_persistent_clock_emulation();
> +		break;
> +	case TT_MODE_OFF:
> +		/* we just read the host clock with os_persistent_clock_emulation() */
> +		break;
> +	}
> +#endif
> +
>   	timer_set_signal_handler();
>   	late_time_init = um_timer_setup;
>   }
> 

Acked-By: Anton Ivanov <anton.ivanov@cambridgegreys.com>

-- 
Anton R. Ivanov
https://www.kot-begemot.co.uk/

_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 3/5] um: allow PM with suspend-to-idle
  2020-12-02 19:58 ` [PATCH v7 3/5] um: allow PM with suspend-to-idle Johannes Berg
@ 2020-12-03 11:34   ` Anton Ivanov
  0 siblings, 0 replies; 13+ messages in thread
From: Anton Ivanov @ 2020-12-03 11:34 UTC (permalink / raw)
  To: Johannes Berg, linux-um; +Cc: Johannes Berg



On 02/12/2020 19:58, Johannes Berg wrote:
> From: Johannes Berg <johannes.berg@intel.com>
> 
> In order to be able to experiment with suspend in UML, add the
> minimal work to be able to suspend (s2idle) an instance of UML,
> and be able to wake it back up from that state with the USR1
> signal sent to the main UML process.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> ---
>   arch/um/Kconfig                    |  5 +++++
>   arch/um/include/shared/kern_util.h |  2 ++
>   arch/um/include/shared/os.h        |  1 +
>   arch/um/kernel/um_arch.c           | 25 +++++++++++++++++++++++++
>   arch/um/os-Linux/signal.c          | 14 +++++++++++++-
>   5 files changed, 46 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/um/Kconfig b/arch/um/Kconfig
> index 4b799fad8b48..1c57599b82fa 100644
> --- a/arch/um/Kconfig
> +++ b/arch/um/Kconfig
> @@ -192,3 +192,8 @@ config UML_TIME_TRAVEL_SUPPORT
>   endmenu
>   
>   source "arch/um/drivers/Kconfig"
> +
> +config ARCH_SUSPEND_POSSIBLE
> +	def_bool y
> +
> +source "kernel/power/Kconfig"
> diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/kern_util.h
> index ccafb62e8cce..9c08e728a675 100644
> --- a/arch/um/include/shared/kern_util.h
> +++ b/arch/um/include/shared/kern_util.h
> @@ -39,6 +39,8 @@ extern int is_syscall(unsigned long addr);
>   
>   extern void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs);
>   
> +extern void uml_pm_wake(void);
> +
>   extern int start_uml(void);
>   extern void paging_init(void);
>   
> diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h
> index 0f7fb8bad728..78250a05394a 100644
> --- a/arch/um/include/shared/os.h
> +++ b/arch/um/include/shared/os.h
> @@ -241,6 +241,7 @@ extern int set_signals(int enable);
>   extern int set_signals_trace(int enable);
>   extern int os_is_signal_stack(void);
>   extern void deliver_alarm(void);
> +extern void register_pm_wake_signal(void);
>   
>   /* util.c */
>   extern void stack_protections(unsigned long address);
> diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c
> index 76b37297b7d4..237a8d73a096 100644
> --- a/arch/um/kernel/um_arch.c
> +++ b/arch/um/kernel/um_arch.c
> @@ -13,6 +13,7 @@
>   #include <linux/sched.h>
>   #include <linux/sched/task.h>
>   #include <linux/kmsg_dump.h>
> +#include <linux/suspend.h>
>   
>   #include <asm/processor.h>
>   #include <asm/sections.h>
> @@ -377,3 +378,27 @@ void *text_poke(void *addr, const void *opcode, size_t len)
>   void text_poke_sync(void)
>   {
>   }
> +
> +#ifdef CONFIG_PM_SLEEP
> +void uml_pm_wake(void)
> +{
> +	pm_system_wakeup();
> +}
> +
> +static int init_pm_wake_signal(void)
> +{
> +	/*
> +	 * In external time-travel mode we can't use signals to wake up
> +	 * since that would mess with the scheduling. We'll have to do
> +	 * some additional work to support wakeup on virtio devices or
> +	 * similar, perhaps implementing a fake RTC controller that can
> +	 * trigger wakeup (and request the appropriate scheduling from
> +	 * the external scheduler when going to suspend.)
> +	 */
> +	if (time_travel_mode != TT_MODE_EXTERNAL)
> +		register_pm_wake_signal();
> +	return 0;
> +}
> +
> +late_initcall(init_pm_wake_signal);
> +#endif
> diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c
> index b58bc68cbe64..0a2ea84033b4 100644
> --- a/arch/um/os-Linux/signal.c
> +++ b/arch/um/os-Linux/signal.c
> @@ -136,6 +136,16 @@ void set_sigstack(void *sig_stack, int size)
>   		panic("enabling signal stack failed, errno = %d\n", errno);
>   }
>   
> +static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
> +{
> +	uml_pm_wake();
> +}
> +
> +void register_pm_wake_signal(void)
> +{
> +	set_handler(SIGUSR1);
> +}
> +
>   static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
>   	[SIGSEGV] = sig_handler,
>   	[SIGBUS] = sig_handler,
> @@ -145,7 +155,9 @@ static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
>   
>   	[SIGIO] = sig_handler,
>   	[SIGWINCH] = sig_handler,
> -	[SIGALRM] = timer_alarm_handler
> +	[SIGALRM] = timer_alarm_handler,
> +
> +	[SIGUSR1] = sigusr1_handler,
>   };
>   
>   static void hard_handler(int sig, siginfo_t *si, void *p)
> 

Acked-By: Anton Ivanov <anton.ivanov@cambridgegreys.com>

-- 
Anton R. Ivanov
https://www.kot-begemot.co.uk/

_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 4/5] um: support suspend to RAM
  2020-12-02 19:58 ` [PATCH v7 4/5] um: support suspend to RAM Johannes Berg
@ 2020-12-03 11:34   ` Anton Ivanov
  0 siblings, 0 replies; 13+ messages in thread
From: Anton Ivanov @ 2020-12-03 11:34 UTC (permalink / raw)
  To: Johannes Berg, linux-um; +Cc: Johannes Berg



On 02/12/2020 19:58, Johannes Berg wrote:
> From: Johannes Berg <johannes.berg@intel.com>
> 
> With all the previous bits in place, we can now also support
> suspend to RAM, in the sense that everything is suspended,
> not just most, including userspace, processes like in s2idle.
> 
> Since um_idle_sleep() now waits forever, we can simply call
> that to "suspend" the system.
> 
> As before, you can wake it up using SIGUSR1 since we're just
> in a pause() call that only needs to return.
> 
> In order to implement selective resume from certain devices,
> and not have any arbitrary device interrupt wake up, suspend
> interrupts by removing SIGIO notification (O_ASYNC) from all
> the FDs that are not supposed to wake up the system. However,
> swap out the handler so we don't actually handle the SIGIO as
> an interrupt.
> 
> Since we're in pause(), the mere act of receiving SIGIO wakes
> us up, and then after things have been restored enough, re-set
> O_ASYNC for all previously suspended FDs, reinstall the proper
> SIGIO handler, and send SIGIO to self to process anything that
> might now be pending.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> ---
> v7:
>   - clean up a bit
>   - send SIGIO to self properly, not to the entire group
> ---
>   arch/um/include/shared/kern_util.h |  1 +
>   arch/um/include/shared/os.h        |  3 +
>   arch/um/kernel/irq.c               | 88 +++++++++++++++++++++++++++++-
>   arch/um/kernel/process.c           |  2 +-
>   arch/um/kernel/um_arch.c           | 42 ++++++++++++++
>   arch/um/os-Linux/signal.c          |  5 ++
>   6 files changed, 139 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/kern_util.h
> index 9c08e728a675..2888ec812f6e 100644
> --- a/arch/um/include/shared/kern_util.h
> +++ b/arch/um/include/shared/kern_util.h
> @@ -68,5 +68,6 @@ extern void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs);
>   extern void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs);
>   extern void fatal_sigsegv(void) __attribute__ ((noreturn));
>   
> +void um_idle_sleep(void);
>   
>   #endif
> diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h
> index 78250a05394a..cd750d4edfb5 100644
> --- a/arch/um/include/shared/os.h
> +++ b/arch/um/include/shared/os.h
> @@ -233,6 +233,7 @@ extern void timer_set_signal_handler(void);
>   extern void set_sigstack(void *sig_stack, int size);
>   extern void remove_sigstack(void);
>   extern void set_handler(int sig);
> +extern void send_sigio_to_self(void);
>   extern int change_sig(int signal, int on);
>   extern void block_signals(void);
>   extern void unblock_signals(void);
> @@ -307,6 +308,8 @@ extern int os_mod_epoll_fd(int events, int fd, void *data);
>   extern int os_del_epoll_fd(int fd);
>   extern void os_set_ioignore(void);
>   extern void os_close_epoll_fd(void);
> +extern void um_irqs_suspend(void);
> +extern void um_irqs_resume(void);
>   
>   /* sigio.c */
>   extern int add_sigio_fd(int fd);
> diff --git a/arch/um/kernel/irq.c b/arch/um/kernel/irq.c
> index 482269580b79..ea43312cbfd3 100644
> --- a/arch/um/kernel/irq.c
> +++ b/arch/um/kernel/irq.c
> @@ -20,6 +20,7 @@
>   #include <os.h>
>   #include <irq_user.h>
>   #include <irq_kern.h>
> +#include <as-layout.h>
>   
>   
>   extern void free_irqs(void);
> @@ -36,12 +37,14 @@ struct irq_reg {
>   	int events;
>   	bool active;
>   	bool pending;
> +	bool wakeup;
>   };
>   
>   struct irq_entry {
>   	struct list_head list;
>   	int fd;
>   	struct irq_reg reg[NUM_IRQ_TYPES];
> +	bool suspended;
>   };
>   
>   static DEFINE_SPINLOCK(irq_lock);
> @@ -70,6 +73,11 @@ static void irq_io_loop(struct irq_reg *irq, struct uml_pt_regs *regs)
>   	}
>   }
>   
> +void sigio_handler_suspend(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
> +{
> +	/* nothing */
> +}
> +
>   void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
>   {
>   	struct irq_entry *irq_entry;
> @@ -365,9 +373,86 @@ int um_request_irq(int irq, int fd, enum um_irq_type type,
>   	clear_bit(irq, irqs_allocated);
>   	return err;
>   }
> -
>   EXPORT_SYMBOL(um_request_irq);
>   
> +#ifdef CONFIG_PM_SLEEP
> +void um_irqs_suspend(void)
> +{
> +	struct irq_entry *entry;
> +	unsigned long flags;
> +
> +	sig_info[SIGIO] = sigio_handler_suspend;
> +
> +	spin_lock_irqsave(&irq_lock, flags);
> +	list_for_each_entry(entry, &active_fds, list) {
> +		enum um_irq_type t;
> +		bool wake = false;
> +
> +		for (t = 0; t < NUM_IRQ_TYPES; t++) {
> +			if (!entry->reg[t].events)
> +				continue;
> +
> +			if (entry->reg[t].wakeup) {
> +				wake = true;
> +				break;
> +			}
> +		}
> +
> +		if (!wake) {
> +			entry->suspended = true;
> +			os_clear_fd_async(entry->fd);
> +		}
> +	}
> +	spin_unlock_irqrestore(&irq_lock, flags);
> +}
> +
> +void um_irqs_resume(void)
> +{
> +	struct irq_entry *entry;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&irq_lock, flags);
> +	list_for_each_entry(entry, &active_fds, list) {
> +		if (entry->suspended) {
> +			int err = os_set_fd_async(entry->fd);
> +
> +			WARN(err < 0, "os_set_fd_async returned %d\n", err);
> +			entry->suspended = false;
> +		}
> +	}
> +	spin_unlock_irqrestore(&irq_lock, flags);
> +
> +	sig_info[SIGIO] = sigio_handler;
> +	send_sigio_to_self();
> +}
> +
> +static int normal_irq_set_wake(struct irq_data *d, unsigned int on)
> +{
> +	struct irq_entry *entry;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&irq_lock, flags);
> +	list_for_each_entry(entry, &active_fds, list) {
> +		enum um_irq_type t;
> +
> +		for (t = 0; t < NUM_IRQ_TYPES; t++) {
> +			if (!entry->reg[t].events)
> +				continue;
> +
> +			if (entry->reg[t].irq != d->irq)
> +				continue;
> +			entry->reg[t].wakeup = on;
> +			goto unlock;
> +		}
> +	}
> +unlock:
> +	spin_unlock_irqrestore(&irq_lock, flags);
> +	return 0;
> +}
> +#else
> +#define normal_irq_set_wake NULL
> +#endif
> +
>   /*
>    * irq_chip must define at least enable/disable and ack when
>    * the edge handler is used.
> @@ -384,6 +469,7 @@ static struct irq_chip normal_irq_type = {
>   	.irq_ack = dummy,
>   	.irq_mask = dummy,
>   	.irq_unmask = dummy,
> +	.irq_set_wake = normal_irq_set_wake,
>   };
>   
>   static struct irq_chip alarm_irq_type = {
> diff --git a/arch/um/kernel/process.c b/arch/um/kernel/process.c
> index 0686fabba576..f0f50eae2293 100644
> --- a/arch/um/kernel/process.c
> +++ b/arch/um/kernel/process.c
> @@ -202,7 +202,7 @@ void initial_thread_cb(void (*proc)(void *), void *arg)
>   	kmalloc_ok = save_kmalloc_ok;
>   }
>   
> -static void um_idle_sleep(void)
> +void um_idle_sleep(void)
>   {
>   	if (time_travel_mode != TT_MODE_OFF)
>   		time_travel_sleep();
> diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c
> index 237a8d73a096..9c7e6d7ea1b3 100644
> --- a/arch/um/kernel/um_arch.c
> +++ b/arch/um/kernel/um_arch.c
> @@ -385,6 +385,45 @@ void uml_pm_wake(void)
>   	pm_system_wakeup();
>   }
>   
> +static int um_suspend_valid(suspend_state_t state)
> +{
> +	return state == PM_SUSPEND_MEM;
> +}
> +
> +static int um_suspend_prepare(void)
> +{
> +	um_irqs_suspend();
> +	return 0;
> +}
> +
> +static int um_suspend_enter(suspend_state_t state)
> +{
> +	if (WARN_ON(state != PM_SUSPEND_MEM))
> +		return -EINVAL;
> +
> +	/*
> +	 * This is identical to the idle sleep, but we've just
> +	 * (during suspend) turned off all interrupt sources
> +	 * except for the ones we want, so now we can only wake
> +	 * up on something we actually want to wake up on. All
> +	 * timing has also been suspended.
> +	 */
> +	um_idle_sleep();
> +	return 0;
> +}
> +
> +static void um_suspend_finish(void)
> +{
> +	um_irqs_resume();
> +}
> +
> +const struct platform_suspend_ops um_suspend_ops = {
> +	.valid = um_suspend_valid,
> +	.prepare = um_suspend_prepare,
> +	.enter = um_suspend_enter,
> +	.finish = um_suspend_finish,
> +};
> +
>   static int init_pm_wake_signal(void)
>   {
>   	/*
> @@ -397,6 +436,9 @@ static int init_pm_wake_signal(void)
>   	 */
>   	if (time_travel_mode != TT_MODE_EXTERNAL)
>   		register_pm_wake_signal();
> +
> +	suspend_set_ops(&um_suspend_ops);
> +
>   	return 0;
>   }
>   
> diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c
> index 0a2ea84033b4..510e956b4320 100644
> --- a/arch/um/os-Linux/signal.c
> +++ b/arch/um/os-Linux/signal.c
> @@ -234,6 +234,11 @@ void set_handler(int sig)
>   		panic("sigprocmask failed - errno = %d\n", errno);
>   }
>   
> +void send_sigio_to_self(void)
> +{
> +	kill(os_getpid(), SIGIO);
> +}
> +
>   int change_sig(int signal, int on)
>   {
>   	sigset_t sigset;
> 

Anton Ivanov <anton.ivanov@cambridgegreys.com>

-- 
Anton R. Ivanov
https://www.kot-begemot.co.uk/

_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v7 5/5] um: add a pseudo RTC
  2020-12-02 19:58 ` [PATCH v7 5/5] um: add a pseudo RTC Johannes Berg
@ 2020-12-03 11:34   ` Anton Ivanov
  0 siblings, 0 replies; 13+ messages in thread
From: Anton Ivanov @ 2020-12-03 11:34 UTC (permalink / raw)
  To: Johannes Berg, linux-um; +Cc: Johannes Berg



On 02/12/2020 19:58, Johannes Berg wrote:
> From: Johannes Berg <johannes.berg@intel.com>
> 
> Add a pseudo RTC that simply is able to send an alarm signal
> waking up the system at a given time in the future.
> 
> Since apparently timerfd_create() FDs don't (always?) support
> SIGIO, we fork a background thread that does nothing but wait
> for control input. If the alarm is enabled it wakes up at the
> right time to send an interrupt back to the system.
> 
> This probably isn't _quite_ right since it doesn't specifically
> use CLOCK_REALTIME, and thus if time changes on the host we'll
> not wake up at the right time, but it will let us test suspend
> and RTC-initiated resume.
> 
> For time-travel mode, OTOH, just add an event at the specified
> time in the future, and that's already sufficient to wake up
> the system at that point in time since suspend will just be in
> an "endless wait".
> 
> For s2idle support also call pm_system_wakeup().
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> ---
>   arch/um/drivers/Kconfig               |  11 ++
>   arch/um/drivers/Makefile              |   2 +
>   arch/um/drivers/rtc.h                 |  15 ++
>   arch/um/drivers/rtc_kern.c            | 210 ++++++++++++++++++++++++++
>   arch/um/drivers/rtc_user.c            | 204 +++++++++++++++++++++++++
>   arch/um/include/linux/time-internal.h |  11 ++
>   arch/um/kernel/time.c                 |  10 +-
>   7 files changed, 462 insertions(+), 1 deletion(-)
>   create mode 100644 arch/um/drivers/rtc.h
>   create mode 100644 arch/um/drivers/rtc_kern.c
>   create mode 100644 arch/um/drivers/rtc_user.c
> 
> diff --git a/arch/um/drivers/Kconfig b/arch/um/drivers/Kconfig
> index 2e7b8e0e7194..5509e627bbd4 100644
> --- a/arch/um/drivers/Kconfig
> +++ b/arch/um/drivers/Kconfig
> @@ -346,3 +346,14 @@ config VIRTIO_UML
>   	help
>   	  This driver provides support for virtio based paravirtual device
>   	  drivers over vhost-user sockets.
> +
> +config UML_RTC
> +	bool "UML RTC driver"
> +	depends on RTC_CLASS
> +	# there's no use in this if PM_SLEEP isn't enabled ...
> +	depends on PM_SLEEP
> +	help
> +	  When PM_SLEEP is configured, it may be desirable to wake up using
> +	  rtcwake, especially in time-travel mode. This driver enables that
> +	  by providing a fake RTC clock that causes a wakeup at the right
> +	  time.
> diff --git a/arch/um/drivers/Makefile b/arch/um/drivers/Makefile
> index 2a249f619467..dcc64a02f81f 100644
> --- a/arch/um/drivers/Makefile
> +++ b/arch/um/drivers/Makefile
> @@ -17,6 +17,7 @@ hostaudio-objs := hostaudio_kern.o
>   ubd-objs := ubd_kern.o ubd_user.o
>   port-objs := port_kern.o port_user.o
>   harddog-objs := harddog_kern.o harddog_user.o
> +rtc-objs := rtc_kern.o rtc_user.o
>   
>   LDFLAGS_pcap.o = $(shell $(CC) $(KBUILD_CFLAGS) -print-file-name=libpcap.a)
>   
> @@ -62,6 +63,7 @@ obj-$(CONFIG_UML_WATCHDOG) += harddog.o
>   obj-$(CONFIG_BLK_DEV_COW_COMMON) += cow_user.o
>   obj-$(CONFIG_UML_RANDOM) += random.o
>   obj-$(CONFIG_VIRTIO_UML) += virtio_uml.o
> +obj-$(CONFIG_UML_RTC) += rtc.o
>   
>   # pcap_user.o must be added explicitly.
>   USER_OBJS := fd.o null.o pty.o tty.o xterm.o slip_common.o pcap_user.o vde_user.o vector_user.o
> diff --git a/arch/um/drivers/rtc.h b/arch/um/drivers/rtc.h
> new file mode 100644
> index 000000000000..95e41c7d35c4
> --- /dev/null
> +++ b/arch/um/drivers/rtc.h
> @@ -0,0 +1,15 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Copyright (C) 2020 Intel Corporation
> + * Author: Johannes Berg <johannes@sipsolutions.net>
> + */
> +#ifndef __UM_RTC_H__
> +#define __UM_RTC_H__
> +
> +int uml_rtc_start(bool timetravel);
> +int uml_rtc_enable_alarm(unsigned long long delta_seconds);
> +void uml_rtc_disable_alarm(void);
> +void uml_rtc_stop(bool timetravel);
> +void uml_rtc_send_timetravel_alarm(void);
> +
> +#endif /* __UM_RTC_H__ */
> diff --git a/arch/um/drivers/rtc_kern.c b/arch/um/drivers/rtc_kern.c
> new file mode 100644
> index 000000000000..d4ae97ff7aea
> --- /dev/null
> +++ b/arch/um/drivers/rtc_kern.c
> @@ -0,0 +1,210 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2020 Intel Corporation
> + * Author: Johannes Berg <johannes@sipsolutions.net>
> + */
> +#include <linux/platform_device.h>
> +#include <linux/time-internal.h>
> +#include <linux/suspend.h>
> +#include <linux/err.h>
> +#include <linux/rtc.h>
> +#include <kern_util.h>
> +#include <irq_kern.h>
> +#include <os.h>
> +#include "rtc.h"
> +
> +static time64_t uml_rtc_alarm_time;
> +static bool uml_rtc_alarm_enabled;
> +static struct rtc_device *uml_rtc;
> +static int uml_rtc_irq_fd, uml_rtc_irq;
> +
> +#ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
> +
> +static void uml_rtc_time_travel_alarm(struct time_travel_event *ev)
> +{
> +	uml_rtc_send_timetravel_alarm();
> +}
> +
> +static struct time_travel_event uml_rtc_alarm_event = {
> +	.fn = uml_rtc_time_travel_alarm,
> +};
> +#endif
> +
> +static int uml_rtc_read_time(struct device *dev, struct rtc_time *tm)
> +{
> +	struct timespec64 ts;
> +
> +	/* Use this to get correct time in time-travel mode */
> +	read_persistent_clock64(&ts);
> +	rtc_time64_to_tm(timespec64_to_ktime(ts) / NSEC_PER_SEC, tm);
> +
> +	return 0;
> +}
> +
> +static int uml_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> +{
> +	rtc_time64_to_tm(uml_rtc_alarm_time, &alrm->time);
> +	alrm->enabled = uml_rtc_alarm_enabled;
> +
> +	return 0;
> +}
> +
> +static int uml_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
> +{
> +	unsigned long long secs;
> +
> +	if (!enable && !uml_rtc_alarm_enabled)
> +		return 0;
> +
> +	uml_rtc_alarm_enabled = enable;
> +
> +	secs = uml_rtc_alarm_time - ktime_get_real_seconds();
> +
> +	if (time_travel_mode == TT_MODE_OFF) {
> +		if (!enable) {
> +			uml_rtc_disable_alarm();
> +			return 0;
> +		}
> +
> +		/* enable or update */
> +		return uml_rtc_enable_alarm(secs);
> +	} else {
> +		time_travel_del_event(&uml_rtc_alarm_event);
> +
> +		if (enable)
> +			time_travel_add_event_rel(&uml_rtc_alarm_event,
> +						  secs * NSEC_PER_SEC);
> +	}
> +
> +	return 0;
> +}
> +
> +static int uml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> +{
> +	uml_rtc_alarm_irq_enable(dev, 0);
> +	uml_rtc_alarm_time = rtc_tm_to_time64(&alrm->time);
> +	uml_rtc_alarm_irq_enable(dev, alrm->enabled);
> +
> +	return 0;
> +}
> +
> +static const struct rtc_class_ops uml_rtc_ops = {
> +	.read_time = uml_rtc_read_time,
> +	.read_alarm = uml_rtc_read_alarm,
> +	.alarm_irq_enable = uml_rtc_alarm_irq_enable,
> +	.set_alarm = uml_rtc_set_alarm,
> +};
> +
> +static irqreturn_t uml_rtc_interrupt(int irq, void *data)
> +{
> +	char c = -1;
> +
> +	/* alarm triggered, it's now off */
> +	uml_rtc_alarm_enabled = false;
> +
> +	os_read_file(uml_rtc_irq_fd, &c, sizeof(c));
> +	WARN_ON(c);
> +
> +	pm_system_wakeup();
> +	rtc_update_irq(uml_rtc, 1, RTC_IRQF | RTC_AF);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int uml_rtc_setup(void)
> +{
> +	int err;
> +
> +	err = uml_rtc_start(time_travel_mode != TT_MODE_OFF);
> +	if (err < 0)
> +		return err;
> +
> +	uml_rtc_irq_fd = err;
> +
> +	err = um_request_irq(UM_IRQ_ALLOC, uml_rtc_irq_fd, IRQ_READ,
> +			     uml_rtc_interrupt, 0, "rtc", NULL);
> +	if (err < 0) {
> +		uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
> +		return err;
> +	}
> +
> +	irq_set_irq_wake(err, 1);
> +
> +	uml_rtc_irq = err;
> +	return 0;
> +}
> +
> +static void uml_rtc_cleanup(void)
> +{
> +	um_free_irq(uml_rtc_irq, NULL);
> +	uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
> +}
> +
> +static int uml_rtc_probe(struct platform_device *pdev)
> +{
> +	int err;
> +
> +	err = uml_rtc_setup();
> +	if (err)
> +		return err;
> +
> +	uml_rtc = devm_rtc_allocate_device(&pdev->dev);
> +	if (IS_ERR(uml_rtc)) {
> +		err = PTR_ERR(uml_rtc);
> +		goto cleanup;
> +	}
> +
> +	uml_rtc->ops = &uml_rtc_ops;
> +
> +	err = rtc_register_device(uml_rtc);
> +	if (err)
> +		goto cleanup;
> +
> +	device_init_wakeup(&pdev->dev, 1);
> +	return 0;
> +cleanup:
> +	uml_rtc_cleanup();
> +	return err;
> +}
> +
> +static int uml_rtc_remove(struct platform_device *pdev)
> +{
> +	device_init_wakeup(&pdev->dev, 0);
> +	uml_rtc_cleanup();
> +	return 0;
> +}
> +
> +static struct platform_driver uml_rtc_driver = {
> +	.probe = uml_rtc_probe,
> +	.remove = uml_rtc_remove,
> +	.driver = {
> +		.name = "uml-rtc",
> +	},
> +};
> +
> +static int __init uml_rtc_init(void)
> +{
> +	struct platform_device *pdev;
> +	int err;
> +
> +	err = platform_driver_register(&uml_rtc_driver);
> +	if (err)
> +		return err;
> +
> +	pdev = platform_device_alloc("uml-rtc", 0);
> +	if (!pdev) {
> +		err = -ENOMEM;
> +		goto unregister;
> +	}
> +
> +	err = platform_device_add(pdev);
> +	if (err)
> +		goto unregister;
> +	return 0;
> +
> +unregister:
> +	platform_device_put(pdev);
> +	platform_driver_unregister(&uml_rtc_driver);
> +	return err;
> +}
> +device_initcall(uml_rtc_init);
> diff --git a/arch/um/drivers/rtc_user.c b/arch/um/drivers/rtc_user.c
> new file mode 100644
> index 000000000000..e2a1a119d2b6
> --- /dev/null
> +++ b/arch/um/drivers/rtc_user.c
> @@ -0,0 +1,204 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2020 Intel Corporation
> + * Author: Johannes Berg <johannes@sipsolutions.net>
> + */
> +#include <os.h>
> +#include <errno.h>
> +#include <sched.h>
> +#include <unistd.h>
> +#include <kern_util.h>
> +#include <sys/select.h>
> +#include <stdio.h>
> +#include "rtc.h"
> +
> +/*
> + * It seems we should just be able to use a timerfd here,
> + * but apparently they don't send SIGIO. So use our own
> + * helper thread instead.
> + */
> +
> +static unsigned long uml_rtc_stack;
> +static int uml_rtc_comm_fds[2];
> +static int uml_rtc_irq_fds[2];
> +static int uml_rtc_thread_pid;
> +
> +struct uml_rtc_message {
> +	unsigned long long secs;
> +	bool enabled;
> +};
> +
> +struct uml_rtc_fds {
> +	int comm_fd, irq_fd;
> +};
> +
> +static int uml_rtc_signal_wake(int fd)
> +{
> +	char c = 0;
> +	int err;
> +
> +	CATCH_EINTR(err = write(fd, &c, sizeof(c)));
> +	if (err != 1) {
> +		printf("rtc helper: failed to write irq (errno=%d)\n",
> +		       errno);
> +		fflush(stdout);
> +		return -EIO;
> +	}
> +
> +	return 0;
> +}
> +
> +void uml_rtc_send_timetravel_alarm(void)
> +{
> +	uml_rtc_signal_wake(uml_rtc_irq_fds[1]);
> +}
> +
> +static int uml_rtc_thread(void *_data)
> +{
> +	struct timeval timeout = {};
> +	bool enabled = false;
> +	struct uml_rtc_fds fds;
> +
> +	os_fix_helper_signals();
> +
> +	fds = *(struct uml_rtc_fds *)_data;
> +
> +	while (1) {
> +		fd_set fdset;
> +		int readable, err;
> +
> +		FD_ZERO(&fdset);
> +		FD_SET(fds.comm_fd, &fdset);
> +
> +		readable = select(fds.comm_fd + 1, &fdset, NULL, NULL,
> +				  enabled ? &timeout : NULL);
> +		if (readable == 1) {
> +			struct uml_rtc_message msg;
> +			char c = 0;
> +
> +			CATCH_EINTR(err = read(fds.comm_fd, &msg, sizeof(msg)));
> +			if (err != sizeof(msg)) {
> +				printf("rtc helper: failed to read (%d)\n",
> +				       err);
> +				fflush(stdout);
> +				return -EIO;
> +			}
> +
> +			enabled = msg.enabled;
> +			timeout.tv_usec = 0;
> +			timeout.tv_sec = msg.secs;
> +
> +			CATCH_EINTR(err = write(fds.comm_fd, &c, sizeof(c)));
> +			if (err != 1) {
> +				printf("rtc helper: failed to write comm (errno=%d)\n",
> +				       errno);
> +				fflush(stdout);
> +				return -EIO;
> +			}
> +		} else if (readable == 0) {
> +			enabled = false;
> +
> +			err = uml_rtc_signal_wake(fds.irq_fd);
> +			if (err)
> +				return err;
> +		}
> +		/* else ... should we update the timeout if enabled? */
> +	}
> +
> +	return 0;
> +}
> +
> +int uml_rtc_send(struct uml_rtc_message *msg)
> +{
> +	char c;
> +	int err;
> +
> +	CATCH_EINTR(err = write(uml_rtc_comm_fds[0], msg, sizeof(*msg)));
> +	if (err != sizeof(*msg))
> +		return err >= 0 ? -EPIPE : err;
> +
> +	CATCH_EINTR(err = read(uml_rtc_comm_fds[0], &c, sizeof(c)));
> +	if (err != sizeof(c))
> +		return err >= 0 ? -EPIPE : err;
> +
> +	return 0;
> +}
> +
> +int uml_rtc_start(bool timetravel)
> +{
> +	struct uml_rtc_message msg = {
> +		.enabled = false,
> +	};
> +	struct uml_rtc_fds data = {};
> +	int err;
> +
> +	if (uml_rtc_stack)
> +		return -EBUSY;
> +
> +	err = os_pipe(uml_rtc_irq_fds, 1, 1);
> +	if (err)
> +		return err;
> +
> +	if (!timetravel) {
> +		err = os_pipe(uml_rtc_comm_fds, 1, 1);
> +		if (err)
> +			goto fail;
> +
> +		data.comm_fd = uml_rtc_comm_fds[1];
> +		data.irq_fd = uml_rtc_irq_fds[1];
> +
> +		err = run_helper_thread(uml_rtc_thread, &data, CLONE_FILES,
> +					&uml_rtc_stack);
> +		if (err < 0)
> +			goto fail;
> +		uml_rtc_thread_pid = err;
> +
> +		err = uml_rtc_send(&msg);
> +		if (err)
> +			goto fail;
> +	}
> +
> +	return uml_rtc_irq_fds[0];
> +
> +fail:
> +	uml_rtc_stop(timetravel);
> +	return err;
> +}
> +
> +int uml_rtc_enable_alarm(unsigned long long delta_seconds)
> +{
> +	struct uml_rtc_message msg = {
> +		.enabled = true,
> +		.secs = delta_seconds,
> +	};
> +
> +	return uml_rtc_send(&msg);
> +}
> +
> +void uml_rtc_disable_alarm(void)
> +{
> +	struct uml_rtc_message msg = {
> +		.enabled = false,
> +	};
> +
> +	uml_rtc_send(&msg);
> +}
> +
> +void uml_rtc_stop(bool timetravel)
> +{
> +	os_close_file(uml_rtc_irq_fds[1]);
> +	os_close_file(uml_rtc_irq_fds[0]);
> +
> +	if (timetravel)
> +		return;
> +
> +	os_close_file(uml_rtc_comm_fds[1]);
> +	os_close_file(uml_rtc_comm_fds[0]);
> +
> +	if (uml_rtc_thread_pid)
> +		os_kill_process(uml_rtc_thread_pid, 1);
> +	if (uml_rtc_stack)
> +		free_stack(uml_rtc_stack, 0);
> +	uml_rtc_thread_pid = 0;
> +	uml_rtc_stack = 0;
> +}
> diff --git a/arch/um/include/linux/time-internal.h b/arch/um/include/linux/time-internal.h
> index 68e45e950137..088d6a4c0b9d 100644
> --- a/arch/um/include/linux/time-internal.h
> +++ b/arch/um/include/linux/time-internal.h
> @@ -54,6 +54,9 @@ static inline void time_travel_wait_readable(int fd)
>   }
>   
>   void time_travel_add_irq_event(struct time_travel_event *e);
> +void time_travel_add_event_rel(struct time_travel_event *e,
> +			       unsigned long long delay_ns);
> +bool time_travel_del_event(struct time_travel_event *e);
>   #else
>   struct time_travel_event {
>   };
> @@ -74,6 +77,14 @@ static inline void time_travel_propagate_time(void)
>   static inline void time_travel_wait_readable(int fd)
>   {
>   }
> +
> +/*
> + * not inlines so the data structure need not exist,
> + * cause linker failures
> + */
> +extern void time_travel_not_configured(void);
> +#define time_travel_add_event_rel(...) time_travel_not_configured()
> +#define time_travel_del_event(...) time_travel_not_configured()
>   #endif /* CONFIG_UML_TIME_TRAVEL_SUPPORT */
>   
>   /*
> diff --git a/arch/um/kernel/time.c b/arch/um/kernel/time.c
> index 80d33735cfd2..303565ce8c64 100644
> --- a/arch/um/kernel/time.c
> +++ b/arch/um/kernel/time.c
> @@ -302,6 +302,12 @@ static void time_travel_add_event(struct time_travel_event *e,
>   	__time_travel_add_event(e, time);
>   }
>   
> +void time_travel_add_event_rel(struct time_travel_event *e,
> +			       unsigned long long delay_ns)
> +{
> +	time_travel_add_event(e, time_travel_time + delay_ns);
> +}
> +
>   void time_travel_periodic_timer(struct time_travel_event *e)
>   {
>   	time_travel_add_event(&time_travel_timer_event,
> @@ -328,7 +334,7 @@ static void time_travel_deliver_event(struct time_travel_event *e)
>   	}
>   }
>   
> -static bool time_travel_del_event(struct time_travel_event *e)
> +bool time_travel_del_event(struct time_travel_event *e)
>   {
>   	if (!e->pending)
>   		return false;
> @@ -504,6 +510,8 @@ extern u64 time_travel_ext_req(u32 op, u64 time);
>   
>   /* these are empty macros so the struct/fn need not exist */
>   #define time_travel_add_event(e, time) do { } while (0)
> +/* externally not usable - redefine here so we can */
> +#undef time_travel_del_event
>   #define time_travel_del_event(e) do { } while (0)
>   #endif
>   
> 

Anton Ivanov <anton.ivanov@cambridgegreys.com>

-- 
Anton R. Ivanov
https://www.kot-begemot.co.uk/

_______________________________________________
linux-um mailing list
linux-um@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-um


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-12-03 11:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-02 19:58 [PATCH v7 0/5] um: suspend/resume support Johannes Berg
2020-12-02 19:58 ` [PATCH v7 1/5] um: simplify os_idle_sleep() and sleep longer Johannes Berg
2020-12-03 11:32   ` Anton Ivanov
2020-12-02 19:58 ` [PATCH v7 2/5] um: time: fix read_persistent_clock64() in time-travel Johannes Berg
2020-12-03 11:33   ` Anton Ivanov
2020-12-02 19:58 ` [PATCH v7 3/5] um: allow PM with suspend-to-idle Johannes Berg
2020-12-03 11:34   ` Anton Ivanov
2020-12-02 19:58 ` [PATCH v7 4/5] um: support suspend to RAM Johannes Berg
2020-12-03 11:34   ` Anton Ivanov
2020-12-02 19:58 ` [PATCH v7 5/5] um: add a pseudo RTC Johannes Berg
2020-12-03 11:34   ` Anton Ivanov
2020-12-03 10:06 ` [PATCH v7 0/5] um: suspend/resume support Anton Ivanov
2020-12-03 10:14   ` Johannes Berg

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.