Skip to contentSkip to conversation

ANNOUNCE: lock validator -V1

PATCH In review Ingo Molnar 50 participants 319 messages May 29, 2006 – Feb 19, 2007 1 message not in the archive Page 2 of 2

Subsystems (58)
lore
Share & tools
lore
#166 lore raw
In reply to Arjan van de Ven
Quoted 26 lines · Arjan van de Ven
Quoted 8 lines · Benoit Boissinot
Quoted 6 lines · Ingo Molnar

We are pleased to announce the first release of the "lock dependency correctness validator" kernel debugging feature, which can be downloaded from:

http://redhat.com/~mingo/lockdep-patches/
[snip]

Quoted 8 lines

I get this right after ipw2200 is loaded (it is quite verbose, I probably shoudln't post everything...)

ipw2200: Detected Intel PRO/Wireless 2200BG Network Connection
ipw2200: Detected geography ZZD (13 802.11bg channels, 0 802.11a channels)

<c0301efa> netlink_broadcast+0x7a/0x360

this isn't allow to be called from IRQ context, because it takes nl_table_lock for read, but that is taken as

        write_lock_bh(&nl_table_lock);

in
static void netlink_table_grab(void)
so without disabling interrupts; which would thus deadlock if this read_lock-from-irq would hit.

Quoted 8 lines
Quoted 4 lines

<c02fb6a4> wireless_send_event+0x304/0x340
<e1cf8e11> ipw_rx+0x1371/0x1bb0 [ipw2200]
<e1cfe6ac> ipw_irq_tasklet+0x13c/0x500 [ipw2200]
<c0121ea0> tasklet_action+0x40/0x90

but it's more complex than that, since we ARE in BH context. The complexity comes from us holding &priv->lock, which is used in hard irq context.

It is probably related, but I got this in my log too:

BUG: warning at kernel/softirq.c:86/local_bh_disable()
 <c010402d> show_trace+0xd/0x10  <c0104687> dump_stack+0x17/0x20
 <c0121fdc> local_bh_disable+0x5c/0x70  <c03520f1> _read_lock_bh+0x11/0x30
 <c02e8dce> sock_def_readable+0x1e/0x80  <c0302130> netlink_broadcast+0x2b0/0x360
 <c02fb6a4> wireless_send_event+0x304/0x340  <e1cf8e11> ipw_rx+0x1371/0x1bb0 [ipw2200]
 <e1cfe6ac> ipw_irq_tasklet+0x13c/0x500 [ipw2200] <c0121ea0> tasklet_action+0x40/0x90
 <c01223b4> __do_softirq+0x54/0xc0  <c01056bb> do_softirq+0x5b/0xf0
 =======================
 <c0122455> irq_exit+0x35/0x40  <c01057c7> do_IRQ+0x77/0xc0
 <c0103949> common_interrupt+0x25/0x2c 
Quoted 16 lines

so the deadlock is like this:

cpu 0: user context cpu1: softirq context
netlink_table_grab takes nl_table_lock as take priv->lock in ipw_irq_tasklet
write_lock_bh, but leaves irqs enabled

hardirq comes in and the isr tries to take in ipw_rx, call wireless_send_event which
priv->lock but has to wait on cpu 1 tries to take nl_table_lock for read
but has to wait for cpu0

and... kaboom kabang deadlock :)

Signature · 1 line
powered by bash/screen/(urxvt/fvwm|linux-console)/gentoo/gnu/linux OS
#167 lore raw
Quoted 3 lines · Benoit Boissinot

It is probably related, but I got this in my log too:

BUG: warning at kernel/softirq.c:86/local_bh_disable()

this one is harmless, you can ignore it. (already sent a patch to remove the WARN_ON)

Ingo

#168 lore raw

PATCH 1/1 lock validator: netlink.c netlink_table_grab fix

In reply to Benoit Boissinot
Quoted 11 lines · Benoit Boissinot
Quoted 6 lines · Ingo Molnar

We are pleased to announce the first release of the "lock dependency correctness validator" kernel debugging feature, which can be downloaded from:

http://redhat.com/~mingo/lockdep-patches/
[snip]

I get this right after ipw2200 is loaded (it is quite verbose, I probably shoudln't post everything...)

This got rid of the oops for me, is it the right fix?

netlink/af_netlink.c renamed from usr/src/linux/net/netlink/af_netlink.c +4 -4
@@ -157,7 +157,7 @@ static void netlink_sock_destruct(struct
 
 static void netlink_table_grab(void)
 {
-	write_lock_bh(&nl_table_lock);
+	write_lock_irq(&nl_table_lock);
 
 	if (atomic_read(&nl_table_users)) {
 		DECLARE_WAITQUEUE(wait, current);
@@ -167,9 +167,9 @@ static void netlink_table_grab(void)
 			set_current_state(TASK_UNINTERRUPTIBLE);
 			if (atomic_read(&nl_table_users) == 0)
 				break;
-			write_unlock_bh(&nl_table_lock);
+			write_unlock_irq(&nl_table_lock);
 			schedule();
-			write_lock_bh(&nl_table_lock);
+			write_lock_irq(&nl_table_lock);
 		}
 
 		__set_current_state(TASK_RUNNING);
@@ -179,7 +179,7 @@ static void netlink_table_grab(void)
 
 static __inline__ void netlink_table_ungrab(void)
 {
-	write_unlock_bh(&nl_table_lock);
+	write_unlock_irq(&nl_table_lock);
 	wake_up(&nl_table_wait);
 }
 
#169 lore raw

Re: [patch mm1-rc2] lock validator: netlink.c netlink_table_grab fix

Quoted 1 line · Frederik Deweerdt

This got rid of the oops for me, is it the right fix?

I don't think netlink will contend with hardirqs. Can you test with this fix for ipw2200 driver?

Thanks,
-yi

drivers/net/wireless/ipw2200.c +3 -10
diff -urp a/drivers/net/wireless/ipw2200.c b/drivers/net/wireless/ipw2200.c
@@ -11058,11 +11058,9 @@ static irqreturn_t ipw_isr(int irq, void
 	if (!priv)
 		return IRQ_NONE;
 
-	spin_lock(&priv->lock);
-
 	if (!(priv->status & STATUS_INT_ENABLED)) {
 		/* Shared IRQ */
-		goto none;
+		return IRQ_NONE;
 	}
 
 	inta = ipw_read32(priv, IPW_INTA_RW);
@@ -11071,12 +11069,12 @@ static irqreturn_t ipw_isr(int irq, void
 	if (inta == 0xFFFFFFFF) {
 		/* Hardware disappeared */
 		IPW_WARNING("IRQ INTA == 0xFFFFFFFF\n");
-		goto none;
+		return IRQ_NONE;
 	}
 
 	if (!(inta & (IPW_INTA_MASK_ALL & inta_mask))) {
 		/* Shared interrupt */
-		goto none;
+		return IRQ_NONE;
 	}
 
 	/* tell the device to stop sending interrupts */
@@ -11091,12 +11089,7 @@ static irqreturn_t ipw_isr(int irq, void
 
 	tasklet_schedule(&priv->irq_tasklet);
 
-	spin_unlock(&priv->lock);
-
 	return IRQ_HANDLED;
-      none:
-	spin_unlock(&priv->lock);
-	return IRQ_NONE;
 }
 
 static void ipw_rf_kill(void *adapter)
#170 lore raw

Re: [patch mm1-rc2] lock validator: netlink.c netlink_table_grab fix

Quoted 6 lines · Zhu Yi
Quoted 1 line · Frederik Deweerdt

This got rid of the oops for me, is it the right fix?

I don't think netlink will contend with hardirqs. Can you test with this fix for ipw2200 driver?

It does work, thanks. But doesn't this add a possibility of missing some interrupts?

	cpu0				cpu1
        ====				====

in isr in tasklet

				ipw_enable_interrupts
				|->priv->status |= STATUS_INT_ENABLED;

ipw_disable_interrupts
|->priv->status &= ~STATUS_INT_ENABLED; |->ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);

				|->ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
				/* This is possible due to priv->lock no longer being taken
				   in isr */

=>interrupt from ipw2200
in new isr
if (!(priv->status & STATUS_INT_ENABLED))

	return IRQ_NONE; /* we wrongfully return here because priv->status
                            does not reflect the register's value */

Not sure this is really important at all, just curious.

Thanks,
Frederik

#171 lore raw

Re: [patch mm1-rc2] lock validator: netlink.c netlink_table_grab fix

Quoted 7 lines · Frederik Deweerdt
Quoted 6 lines · Zhu Yi
Quoted 1 line · Frederik Deweerdt

This got rid of the oops for me, is it the right fix?

I don't think netlink will contend with hardirqs. Can you test with this fix for ipw2200 driver?

Quoted 8 lines

It does work, thanks. But doesn't this add a possibility of missing some interrupts?

	cpu0				cpu1
        ====				====

in isr in tasklet

				ipw_enable_interrupts
				|->priv->status |= STATUS_INT_ENABLED;

This is unlikely. cpu0 should not receive ipw2200 interrupt since the interrupt is disabled until HERE (see below).

Quoted 7 lines

ipw_disable_interrupts
|->priv->status &= ~STATUS_INT_ENABLED; |->ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);

				|->ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
				/* This is possible due to priv->lock no longer being taken
				   in isr */

HERE

Well, this is not 100% if when the card fires two consecutive interrupts. Though unlikely, it's better to protect early than seeing some "weird" bugs one day. I proposed attached patch. If you can help to test, that will be appreciated (I cannot see the lockdep warning on my box somehow).

Thanks,
-yi

drivers/net/wireless/ipw2200.c +28 -9
diff -urp a/drivers/net/wireless/ipw2200.c b/drivers/net/wireless/ipw2200.c
@@ -542,7 +542,7 @@ static inline void ipw_clear_bit(struct 
 	ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
 }
 
-static inline void ipw_enable_interrupts(struct ipw_priv *priv)
+static inline void __ipw_enable_interrupts(struct ipw_priv *priv)
 {
 	if (priv->status & STATUS_INT_ENABLED)
 		return;
@@ -550,7 +550,7 @@ static inline void ipw_enable_interrupts
 	ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
 }
 
-static inline void ipw_disable_interrupts(struct ipw_priv *priv)
+static inline void __ipw_disable_interrupts(struct ipw_priv *priv)
 {
 	if (!(priv->status & STATUS_INT_ENABLED))
 		return;
@@ -558,6 +558,20 @@ static inline void ipw_disable_interrupt
 	ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
 }
 
+static inline void ipw_enable_interrupts(struct ipw_priv *priv)
+{
+	spin_lock_irqsave(&priv->irq_lock, priv->lock_flags);
+	__ipw_enable_interrupts(priv);
+	spin_unlock_irqrestore(&priv->irq_lock, priv->lock_flags);
+}
+
+static inline void ipw_disable_interrupts(struct ipw_priv *priv)
+{
+	spin_lock_irqsave(&priv->irq_lock, priv->lock_flags);
+	__ipw_disable_interrupts(priv);
+	spin_unlock_irqrestore(&priv->irq_lock, priv->lock_flags);
+}
+
 #ifdef CONFIG_IPW2200_DEBUG
 static char *ipw_error_desc(u32 val)
 {
@@ -1959,7 +1973,7 @@ static void ipw_irq_tasklet(struct ipw_p
 	unsigned long flags;
 	int rc = 0;
 
-	spin_lock_irqsave(&priv->lock, flags);
+	spin_lock_irqsave(&priv->irq_lock, flags);
 
 	inta = ipw_read32(priv, IPW_INTA_RW);
 	inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
@@ -1968,6 +1982,10 @@ static void ipw_irq_tasklet(struct ipw_p
 	/* Add any cached INTA values that need to be handled */
 	inta |= priv->isr_inta;
 
+	spin_unlock_irqrestore(&priv->irq_lock, flags);
+
+	spin_lock_irqsave(&priv->lock, flags);
+
 	/* handle all the justifications for the interrupt */
 	if (inta & IPW_INTA_BIT_RX_TRANSFER) {
 		ipw_rx(priv);
@@ -2096,10 +2114,10 @@ static void ipw_irq_tasklet(struct ipw_p
 		IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
 	}
 
+	spin_unlock_irqrestore(&priv->lock, flags);
+
 	/* enable all interrupts */
 	ipw_enable_interrupts(priv);
-
-	spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 #define IPW_CMD(x) case IPW_CMD_ ## x : return #x
@@ -11058,7 +11076,7 @@ static irqreturn_t ipw_isr(int irq, void
 	if (!priv)
 		return IRQ_NONE;
 
-	spin_lock(&priv->lock);
+	spin_lock(&priv->irq_lock);
 
 	if (!(priv->status & STATUS_INT_ENABLED)) {
 		/* Shared IRQ */
@@ -11080,7 +11098,7 @@ static irqreturn_t ipw_isr(int irq, void
 	}
 
 	/* tell the device to stop sending interrupts */
-	ipw_disable_interrupts(priv);
+	__ipw_disable_interrupts(priv);
 
 	/* ack current interrupts */
 	inta &= (IPW_INTA_MASK_ALL & inta_mask);
@@ -11091,11 +11109,11 @@ static irqreturn_t ipw_isr(int irq, void
 
 	tasklet_schedule(&priv->irq_tasklet);
 
-	spin_unlock(&priv->lock);
+	spin_unlock(&priv->irq_lock);
 
 	return IRQ_HANDLED;
       none:
-	spin_unlock(&priv->lock);
+	spin_unlock(&priv->irq_lock);
 	return IRQ_NONE;
 }
 
@@ -12185,6 +12203,7 @@ static int ipw_pci_probe(struct pci_dev 
 #ifdef CONFIG_IPW2200_DEBUG
 	ipw_debug_level = debug;
 #endif
+	spin_lock_init(&priv->irq_lock);
 	spin_lock_init(&priv->lock);
 	for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++)
 		INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
drivers/net/wireless/ipw2200.h +2 -0
diff -urp a/drivers/net/wireless/ipw2200.h b/drivers/net/wireless/ipw2200.h
@@ -1181,6 +1181,8 @@ struct ipw_priv {
 	struct ieee80211_device *ieee;
 
 	spinlock_t lock;
+	spinlock_t irq_lock;
+	unsigned long lock_flags;
 	struct mutex mutex;
 
 	/* basic pci-network driver stuff */
#172 lore raw

PATCH 1/11 syslets: add async.h include file, kernel-side API definitions

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

add include/linux/async.h which contains the kernel-side API declarations.

it also provides NOP stubs for the !CONFIG_ASYNC_SUPPORT case.

---
 include/linux/async.h |   25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/async.h new file +25 -0
Index: linux/include/linux/async.h
===================================================================
@@ -0,0 +1,25 @@
+#ifndef _LINUX_ASYNC_H
+#define _LINUX_ASYNC_H
+/*
+ * The syslet subsystem - asynchronous syscall execution support.
+ *
+ * Generic kernel API definitions:
+ */
+
+#ifdef CONFIG_ASYNC_SUPPORT
+extern void async_init(struct task_struct *t);
+extern void async_exit(struct task_struct *t);
+extern void __async_schedule(struct task_struct *t);
+#else /* !CONFIG_ASYNC_SUPPORT */
+static inline void async_init(struct task_struct *t)
+{
+}
+static inline void async_exit(struct task_struct *t)
+{
+}
+static inline void __async_schedule(struct task_struct *t)
+{
+}
+#endif /* !CONFIG_ASYNC_SUPPORT */
+
+#endif
#173 lore raw

PATCH 2/11 syslets: add syslet.h include file, user API/ABI definitions

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

add include/linux/syslet.h which contains the user-space API/ABI declarations. Add the new header to include/linux/Kbuild as well.

---
 include/linux/Kbuild   |    1 
 include/linux/syslet.h |  136 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+)
include/linux/Kbuild +1 -0
Index: linux/include/linux/Kbuild
===================================================================
@@ -140,6 +140,7 @@ header-y += sockios.h
 header-y += som.h
 header-y += sound.h
 header-y += synclink.h
+header-y += syslet.h
 header-y += telephony.h
 header-y += termios.h
 header-y += ticable.h
include/linux/syslet.h new file +136 -0

136 lines not on this page · raw message

#174 lore raw

Re: [patch 02/11] syslets: add syslet.h include file, user API/ABI definitions

include/linux/syslet.h line 84

Quoted 10 lines · Ingo Molnar
include/linux/syslet.h new file +10 -0
+/*
+ * Execution control: conditions upon the return code
+ * of the previous syslet atom. 'Stop' means syslet
+ * execution is stopped and the atom is put into the
+ * completion ring:
+ */
+#define SYSLET_STOP_ON_NONZERO			0x00000008
+#define SYSLET_STOP_ON_ZERO			0x00000010
+#define SYSLET_STOP_ON_NEGATIVE			0x00000020
+#define SYSLET_STOP_ON_NON_POSITIVE		0x00000040

This is confusing. Why the return code of the previous syslet atom? Wouldn't it be more clear if the flag was for the current tasklet? Worse, what is the previous atom? Imagine some case with a loop:

A
|
B<--.
| |
C---'

What will be the previous atom of B here? It can be either A or C, but their return values can be different and incompatible, so what flag should B set?

include/linux/syslet.h line 101

Quoted 10 lines
include/linux/syslet.h new file +10 -0
+/*
+ * Special modifier to 'stop' handling: instead of stopping the
+ * execution of the syslet, the linearly next syslet is executed.
+ * (Normal execution flows along atom->next, and execution stops
+ *  if atom->next is NULL or a stop condition becomes true.)
+ *
+ * This is what allows true branches of execution within syslets.
+ */
+#define SYSLET_SKIP_TO_NEXT_ON_STOP		0x00000080
+

Might rename this to SYSLET_SKIP_NEXT_ON_STOP too then.

Greetings,

Indan

#175 lore raw

Re: [patch 02/11] syslets: add syslet.h include file, user API/ABI definitions

Quoted 5 lines · Indan Zupancic
Quoted 5 lines

+ * Execution control: conditions upon the return code
+ * of the previous syslet atom. 'Stop' means syslet
+ * execution is stopped and the atom is put into the
+ * completion ring:
+ */

Quoted 8 lines

include/linux/syslet.h line 84

Quoted 4 lines
include/linux/syslet.h new file +4 -0
+#define SYSLET_STOP_ON_NONZERO			0x00000008
+#define SYSLET_STOP_ON_ZERO			0x00000010
+#define SYSLET_STOP_ON_NEGATIVE			0x00000020
+#define SYSLET_STOP_ON_NON_POSITIVE		0x00000040

This is confusing. Why the return code of the previous syslet atom? Wouldn't it be more clear if the flag was for the current tasklet? Worse, what is the previous atom? [...]

the previously executed atom. (I have fixed up the comment in my tree to say that.)

Quoted 11 lines

[...] Imagine some case with a loop:

A
|
B<--.
| |
C---'

What will be the previous atom of B here? It can be either A or C, but their return values can be different and incompatible, so what flag should B set?

previous here is the previously executed atom, which is always a specific atom. Think of atoms as 'instructions', and these condition flags as the 'CPU flags' like 'zero' 'carry' 'sign', etc. Syslets can be thought of as streams of simplified instructions.

Quoted 4 lines
Quoted 4 lines

+/*
+ * Special modifier to 'stop' handling: instead of stopping the
+ * execution of the syslet, the linearly next syslet is executed.
+ * (Normal execution flows along atom->next, and execution stops

Quoted 8 lines

Review

Quoted 6 lines

+ * if atom->next is NULL or a stop condition becomes true.)
+ *
+ * This is what allows true branches of execution within syslets.
+ */

(quoted diff) +2 -0
+#define SYSLET_SKIP_TO_NEXT_ON_STOP		0x00000080
+

Might rename this to SYSLET_SKIP_NEXT_ON_STOP too then.

but that's not what it does. It really 'skips to the next one on a stop event'. I.e. if you have three consecutive atoms (consecutive in linear memory):

atom1 returns 0
atom2 has SYSLET_STOP_ON_ZERO|SYSLET_SKIP_NEXT_ON_STOP set atom3

then after atom1 returns 0, the SYSLET_STOP_ON_ZERO condition is recognized as a 'stop' event - but due to the SYSLET_SKIP_NEXT_ON_STOP flag execution does not stop (i.e. we do not return to user-space or complete the syslet), but we continue execution at atom3.

this flag basically avoids having to add an atom->else pointer and keeps the data structure more compressed. Two-way branches are sufficiently rare, so i wanted to avoid the atom->else pointer.

Ingo

#176 lore raw

Re: [patch 02/11] syslets: add syslet.h include file, user API/ABI definitions

Quoted 7 lines · Ingo Molnar
Quoted 6 lines · Indan Zupancic

A
|
B<--.
| |
C---'

Quoted 8 lines
Quoted 3 lines

What will be the previous atom of B here? It can be either A or C, but their return values can be different and incompatible, so what flag should B set?

previous here is the previously executed atom, which is always a specific atom. Think of atoms as 'instructions', and these condition flags as the 'CPU flags' like 'zero' 'carry' 'sign', etc. Syslets can be thought of as streams of simplified instructions.

In the diagram above the previously executed atom, when handling atom B, can be either atom A or atom C. So B doesn't know what kind of return value to expect, because it depends on the previous atom's kind of syscall, and not on B's return type. So I think you would want to move those return value flags one atom earlier, in this case to A and C. So each atom will have a flag telling what to to depending on its own return value.

Quoted 21 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * Special modifier to 'stop' handling: instead of stopping the
+ * execution of the syslet, the linearly next syslet is executed.
+ * (Normal execution flows along atom->next, and execution stops

Quoted 8 lines

Review

Quoted 6 lines

+ * if atom->next is NULL or a stop condition becomes true.)
+ *
+ * This is what allows true branches of execution within syslets.
+ */

(quoted diff) +2 -0
+#define SYSLET_SKIP_TO_NEXT_ON_STOP		0x00000080
+

Might rename this to SYSLET_SKIP_NEXT_ON_STOP too then.

but that's not what it does. It really 'skips to the next one on a stop event'. I.e. if you have three consecutive atoms (consecutive in linear memory):

atom1 returns 0
atom2 has SYSLET_STOP_ON_ZERO|SYSLET_SKIP_NEXT_ON_STOP set atom3

Quoted 8 lines

then after atom1 returns 0, the SYSLET_STOP_ON_ZERO condition is recognized as a 'stop' event - but due to the SYSLET_SKIP_NEXT_ON_STOP flag execution does not stop (i.e. we do not return to user-space or complete the syslet), but we continue execution at atom3.

this flag basically avoids having to add an atom->else pointer and keeps the data structure more compressed. Two-way branches are sufficiently rare, so i wanted to avoid the atom->else pointer.

The flags are smart, they're just at the wrong place I think.

In your example, if atom3 has a 'next' pointing to atom2, atom2 wouldn't know which return value it's checking: The one of atom1, or the one of atom3? You're spreading syscall specific knowledge over multiple atoms while that isn't necessary.

What I propose:

atom1 returns 0, has SYSLET_STOP_ON_ZERO|SYSLET_SKIP_NEXT_ON_STOP set atom2
atom3

(You've already used my SYSLET_SKIP_NEXT_ON_STOP instead of SYSLET_SKIP_TO_NEXT_ON_STOP. ;-)

Perhaps it's even more clear when splitting that SYSLET_STOP_* into a SYSLET_STOP flag, and specific SYSLET_IF_* flags. Either that, or go all the way and introduce seperate SYSLET_SKIP_NEXT_ON_*.

atom1 returns 0, has SYSLET_SKIP_NEXT|SYSLET_IF_ZERO set atom2
atom3

Greetings,

Indan

#177 lore raw

Re: [patch 02/11] syslets: add syslet.h include file, user API/ABI definitions

Quoted 8 lines · Indan Zupancic

What I propose:

atom1 returns 0, has SYSLET_STOP_ON_ZERO|SYSLET_SKIP_NEXT_ON_STOP set atom2
atom3

(You've already used my SYSLET_SKIP_NEXT_ON_STOP instead of SYSLET_SKIP_TO_NEXT_ON_STOP. ;-)

doh. Yes. I noticed and implemented this yesterday and it's in the submitted syslet code - but i guess i was too tired to remember my own code - so i added the wrong comments :-/ If you look at the sample user-space code:

        init_atom(req, &req->open_file, __NR_sys_open,
                  &req->filename_p, &O_RDONLY_var, NULL, NULL, NULL, NULL,
                  &req->fd, SYSLET_STOP_ON_NEGATIVE, &req->read_file);

the 'STOP_ON_NEGATIVE' acts on that particular atom.

this indeed cleaned up things quite a bit and made the user-space syslet code alot more straightforward. A return value can still be recovered and examined (with a different condition and a different jump target) arbitrary number of times via ret_ptr and via sys_umem_add().

Ingo

#178 lore raw

Re: [patch 02/11] syslets: add syslet.h include file, user API/ABI definitions

In reply to Ingo Molnar
Quoted 5 lines · Ingo Molnar

add include/linux/syslet.h which contains the user-space API/ABI declarations. Add the new header to include/linux/Kbuild as well.

include/linux/syslet.h new file +2 -0
+struct syslet_uatom {
+	unsigned long				flags;

include/linux/syslet.h line 46

Quoted 10 lines
include/linux/syslet.h new file +10 -0
+	unsigned long				nr;
+	long __user				*ret_ptr;
+	struct syslet_uatom	__user		*next;
+	unsigned long		__user		*arg_ptr[6];
+	/*
+	 * User-space can put anything in here, kernel will not
+	 * touch it:
+	 */
+	void __user				*private;
+};

This structure, with its unsigned longs and pointers, is going to create enormous headaches for 32-bit processes on 64-bit machines as far as I can see---and on ppc64 machines, almost all processes are 32-bit, since there is no inherent speed penalty for running in 32-bit mode, and some space savings.

Have you thought about how you will handle compatibility for 32-bit processes? The issue will arise for x86_64 and ia64 (among others) too, I would think.

Paul.

#179 lore raw

PATCH 3/11 syslets: generic kernel bits

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

add the kernel generic bits - these are present even if !CONFIG_ASYNC_SUPPORT.

---
 include/linux/sched.h |    7 ++++++-
 kernel/exit.c         |    3 +++
 kernel/fork.c         |    2 ++
 kernel/sched.c        |    9 +++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
include/linux/sched.h +6 -1
Index: linux/include/linux/sched.h
===================================================================
@@ -88,7 +88,8 @@ struct sched_param {
 
 struct exec_domain;
 struct futex_pi_state;
-
+struct async_thread;
+struct async_head;
 /*
  * List of flags we want to share for kernel threads,
  * if only because they are not used by them anyway.
@@ -997,6 +998,10 @@ struct task_struct {
 /* journalling filesystem info */
 	void *journal_info;
 
+/* async syscall support: */
+	struct async_thread *at, *async_ready;
+	struct async_head *ah;
+
 /* VM state */
 	struct reclaim_state *reclaim_state;
 
kernel/exit.c +3 -0
Index: linux/kernel/exit.c
===================================================================
@@ -26,6 +26,7 @@
 #include <linux/ptrace.h>
 #include <linux/profile.h>
 #include <linux/mount.h>
+#include <linux/async.h>
 #include <linux/proc_fs.h>
 #include <linux/mempolicy.h>
 #include <linux/taskstats_kern.h>
@@ -889,6 +890,8 @@ fastcall NORET_TYPE void do_exit(long co
 		schedule();
 	}
 
+	async_exit(tsk);
+
 	tsk->flags |= PF_EXITING;
 
 	if (unlikely(in_atomic()))
kernel/fork.c +2 -0
Index: linux/kernel/fork.c
===================================================================
@@ -22,6 +22,7 @@
 #include <linux/personality.h>
 #include <linux/mempolicy.h>
 #include <linux/sem.h>
+#include <linux/async.h>
 #include <linux/file.h>
 #include <linux/key.h>
 #include <linux/binfmts.h>
@@ -1054,6 +1055,7 @@ static struct task_struct *copy_process(
 
 	p->lock_depth = -1;		/* -1 = no lock */
 	do_posix_clock_monotonic_gettime(&p->start_time);
+	async_init(p);
 	p->security = NULL;
 	p->io_context = NULL;
 	p->io_wait = NULL;
kernel/sched.c +9 -0
Index: linux/kernel/sched.c
===================================================================
@@ -38,6 +38,7 @@
 #include <linux/vmalloc.h>
 #include <linux/blkdev.h>
 #include <linux/delay.h>
+#include <linux/async.h>
 #include <linux/smp.h>
 #include <linux/threads.h>
 #include <linux/timer.h>
@@ -3436,6 +3437,14 @@ asmlinkage void __sched schedule(void)
 	}
 	profile_hit(SCHED_PROFILING, __builtin_return_address(0));
 
+	prev = current;
+	if (unlikely(prev->async_ready)) {
+		if (prev->state && !(preempt_count() & PREEMPT_ACTIVE) &&
+			(!(prev->state & TASK_INTERRUPTIBLE) ||
+				!signal_pending(prev)))
+			__async_schedule(prev);
+	}
+
 need_resched:
 	preempt_disable();
 	prev = current;
#180 lore raw

PATCH 4/11 syslets: core, data structures

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

this adds the data structures used by the syslet / async system calls infrastructure.

This is used only if CONFIG_ASYNC_SUPPORT is enabled.

---
 kernel/async.h |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
kernel/async.h new file +58 -0
Index: linux/kernel/async.h
===================================================================
@@ -0,0 +1,58 @@
+/*
+ * The syslet subsystem - asynchronous syscall execution support.
+ *
+ * Syslet-subsystem internal definitions:
+ */
+
+/*
+ * The kernel-side copy of a syslet atom - with arguments expanded:
+ */
+struct syslet_atom {
+	unsigned long				flags;
+	unsigned long				nr;
+	long __user				*ret_ptr;
+	struct syslet_uatom	__user		*next;
+	unsigned long				args[6];
+};
+
+/*
+ * The 'async head' is the thread which has user-space context (ptregs)
+ * 'below it' - this is the one that can return to user-space:
+ */
+struct async_head {
+	spinlock_t				lock;
+	struct task_struct			*user_task;
+
+	struct list_head			ready_async_threads;
+	struct list_head			busy_async_threads;
+
+	unsigned long				events_left;
+	wait_queue_head_t			wait;
+
+	struct async_head_user	__user		*uah;
+	struct syslet_uatom	__user		**completion_ring;
+	unsigned long				curr_ring_idx;
+	unsigned long				max_ring_idx;
+	unsigned long				ring_size_bytes;
+
+	unsigned int				nr_threads;
+	unsigned int				max_nr_threads;
+
+	struct completion			start_done;
+	struct completion			exit_done;
+};
+
+/*
+ * The 'async thread' is either a newly created async thread or it is
+ * an 'ex-head' - it cannot return to user-space and only has kernel
+ * context.
+ */
+struct async_thread {
+	struct task_struct			*task;
+	struct syslet_uatom	__user		*work;
+	struct async_head			*ah;
+
+	struct list_head			entry;
+
+	unsigned int				exit;
+};
#181 lore raw

cover ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar

I'm pleased to announce the first release of the "Syslet" kernel feature and kernel subsystem, which provides generic asynchrous system call support:

http://redhat.com/~mingo/syslet-patches/

Syslets are small, simple, lightweight programs (consisting of system-calls, 'atoms') that the kernel can execute autonomously (and, not the least, asynchronously), without having to exit back into user-space. Syslets can be freely constructed and submitted by any unprivileged user-space context - and they have access to all the resources (and only those resources) that the original context has access to.

because the proof of the pudding is eating it, here are the performance results from async-test.c which does open()+read()+close() of 1000 small random files (smaller is better):

synchronous IO | Syslets:
--------------------------------------------

  uncached:       45.8 seconds        |  34.2 seconds   ( +33.9% )
  cached:         31.6 msecs          |  26.5 msecs     ( +19.2% )

("uncached" results were done via "echo 3 > /proc/sys/vm/drop_caches". The default IO scheduler was the deadline scheduler, the test was run on ext3, using a single PATA IDE disk.)

So syslets, in this particular workload, are a nice speedup /both/ in the uncached and in the cached case. (note that i used only a single disk, so the level of parallelism in the hardware is quite limited.)

the testcode can be found at:

     http://redhat.com/~mingo/syslet-patches/async-test-0.1.tar.gz

The boring details:

Syslets consist of 'syslet atoms', where each atom represents a single system-call. These atoms can be chained to each other: serially, in branches or in loops. The return value of an executed atom is checked against the condition flags. So an atom can specify 'exit on nonzero' or 'loop until non-negative' kind of constructs.

Syslet atoms fundamentally execute only system calls, thus to be able to manipulate user-space variables from syslets i've added a simple special system call: sys_umem_add(ptr, val). This can be used to increase or decrease the user-space variable (and to get the result), or to simply read out the variable (if 'val' is 0).

So a single syslet (submitted and executed via a single system call) can be arbitrarily complex. For example it can be like this:

       --------------------
       |     accept()     |-----> [ stop if returns negative ]
       --------------------
                |
                V
  -------------------------------
  |   setsockopt(TCP_NODELAY)   |-----> [ stop if returns negative ]
  -------------------------------
                |
                v
       --------------------
       |      read()      |<---------
       --------------------         | [ loop while positive ]
           |    |                   |
           |    ---------------------
           |
        -----------------------------------------
        | decrease and read user space variable |
        -----------------------------------------                    A
                    |                                                |
                    -------[ loop back to accept() if positive ]------

(you can find a VFS example and a hello.c example in the user-space testcode.)

A syslet is executed opportunistically: i.e. the syslet subsystem assumes that the syslet will not block, and it will switch to a cachemiss kernel thread from the scheduler. This means that even a single-atom syslet (i.e. a pure system call) is very close in performance to a pure system call. The syslet NULL-overhead in the cached case is roughly 10% of the SYSENTER NULL-syscall overhead. This means that two atoms are a win already, even in the cached case.

When a 'cachemiss' occurs, i.e. if we hit schedule() and are about to consider other threads, the syslet subsystem picks up a 'cachemiss thread' and switches the current task's user-space context over to the cachemiss thread, and makes the cachemiss thread available. The original thread (which now becomes a 'busy' cachemiss thread) continues to block. This means that user-space will still be executed without stopping - even if user-space is single-threaded.

if the submitting user-space context /knows/ that a system call will block, it can request immediate 'cachemiss' via the SYSLET_ASYNC flag. This would be used if for example an O_DIRECT file is read() or write()n.

likewise, if user-space knows (or expects) that a system call takes alot of CPU time even in the cached case, and it wants to offload it to another asynchronous context, it can request that via the SYSLET_ASYNC flag too.

completions of asynchronous syslets are done via a user-space ringbuffer that the kernel fills and user-space clears. Waiting is done via the sys_async_wait() system call. Completion can be supressed on a per-atom basis via the SYSLET_NO_COMPLETE flag, for atoms that include some implicit notification mechanism. (such as sys_kill(), etc.)

As it might be obvious to some of you, the syslet subsystem takes many ideas and experience from my Tux in-kernel webserver :) The syslet code originates from a heavy rewrite of the Tux-atom and the Tux-cachemiss infrastructure.

Open issues:

- the 'TID' of the 'head' thread currently varies depending on which thread is running the user-space context.

- signal support is not fully thought through - probably the head should be getting all of them - the cachemiss threads are not really interested in executing signal handlers.

- sys_fork() and sys_async_exec() should be filtered out from the syscalls that are allowed - first one only makes sense with ptregs, second one is a nice kernel recursion thing :) I didnt want to duplicate the sys_call_table though - maybe others have a better idea.

See more details in Documentation/syslet-design.txt. The patchset is against v2.6.20, but should apply to the -git head as well.

Thanks to Zach Brown for the idea to drive cachemisses via the scheduler. Thanks to Arjan van de Ven for early review feedback.

Comments, suggestions, reports are welcome!

Ingo

#182 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 3 lines

A syslet is executed opportunistically: i.e. the syslet subsystem assumes that the syslet will not block, and it will switch to a cachemiss kernel thread from the scheduler. This means that even a

How is scheduler fairness maintained ? and what is done for resource accounting here ?

Quoted 2 lines

that the kernel fills and user-space clears. Waiting is done via the sys_async_wait() system call. Completion can be supressed on a per-atom

They should be selectable as well iff possible.

Quoted 1 line

Open issues:

Let me add some more

sys_setuid/gid/etc need to be synchronous only and not occur
while other async syscalls are running in parallel to meet current kernel assumptions.

sys_exec and other security boundaries must be synchronous only
and not allow async "spill over" (consider setuid async binary patching)

Quoted 2 lines

- sys_fork() and sys_async_exec() should be filtered out from the syscalls that are allowed - first one only makes sense with ptregs,

clone and vfork. async_vfork is a real mindbender actually.

Quoted 3 lines

second one is a nice kernel recursion thing :) I didnt want to duplicate the sys_call_table though - maybe others have a better idea.

What are the semantics of async sys_async_wait and async sys_async ?

#183 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 3 lines · Alan
Quoted 1 line

Open issues:

Let me add some more

Also: FPU state (especially important with the FPU and SSE memory copy variants), segment register bases on x86-64, interaction with set_fs()... There is no easy way of getting around the full thread context switch and its associated overhead (mucking around in CR0 is one of the more expensive bits of the context switch code path, and at the very least, setting the FPU not present is mandatory). I have looked into exactly this approach, and it's only cheaper if the code is incomplete. Linux's native threads are pretty damned good.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#184 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 7 lines · Benjamin LaHaise
Quoted 3 lines · Alan
Quoted 1 line

Open issues:

Let me add some more

Also: FPU state (especially important with the FPU and SSE memory copy variants)

are these preserved over explicit system calls?

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#185 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Benjamin LaHaise
Quoted 3 lines · Benjamin LaHaise

not present is mandatory). I have looked into exactly this approach, and it's only cheaper if the code is incomplete. Linux's native threads are pretty damned good.

Cheaper in time or in memory? Iow, would you be able to queue up as many threads as syslets?

Bert

Signature · 2 lines
http://www.PowerDNS.com      Open source, database driven DNS Software 
http://netherlabs.nl              Open and Closed source services
#186 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Benjamin LaHaise
Quoted 7 lines · Benjamin LaHaise
Quoted 3 lines
Quoted 1 line

Open issues:

Let me add some more

Also: FPU state (especially important with the FPU and SSE memory copy variants), segment register bases on x86-64, interaction with set_fs()...

agreed - i'll fix this. But i can see no big conceptual issue here - these resources are all attached to the user context, and that doesnt change upon an 'async context-switch'. So it's "only" a matter of properly separating the user execution context from the kernel execution context. The hardest bit was getting the ptregs details right - the FPU/SSE state is pretty much async already (in the hardware too) and isnt even touched by any of these codepaths.

Ingo

#187 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 11 lines · Ingo Molnar
Quoted 7 lines · Benjamin LaHaise
Quoted 3 lines
Quoted 1 line

Open issues:

Let me add some more

Also: FPU state (especially important with the FPU and SSE memory copy variants), segment register bases on x86-64, interaction with set_fs()...

Quoted 7 lines

agreed - i'll fix this. But i can see no big conceptual issue here - these resources are all attached to the user context, and that doesnt change upon an 'async context-switch'. So it's "only" a matter of properly separating the user execution context from the kernel execution context. The hardest bit was getting the ptregs details right - the FPU/SSE state is pretty much async already (in the hardware too) and isnt even touched by any of these codepaths.

Good work, Ingo.

I have not received first mail with announcement yet, so I will place my thoughts here if you do not mind.

First one is per-thread data like TID. What about TLS related kernel data (is non-exec stack property stored in TLS block or in kernel)? Should it be copied with regs too (or better introduce new clone flag, which would force that info copy)?

Btw, does SSE?/MMX?/call-it-yourself really saved on context switch? As far as I can see no syscalls (and kernel at all) use that registers.

Another one is more global AIO question - while this approach IMHO outperforms micro-thread design (Zach and Linus created really good starting points, but they too have fundamental limiting factor), it still has a problem - syscall blocks and the same thread thus is not allowed to continue execution and fill the pipe - so what if system issues thousands of requests and there are only tens of working thread at most. What Tux did, as far as I recall, (and some other similar state machines do :) was to break blocking syscall issues and return to the next execution entity (next syslet or atom). Is it possible to extend exactly this state machine and interface to allow that (so that some other state machine implementations would not continue its life :)?

Quoted 1 line

Ingo

Signature · 1 line
	Evgeniy Polyakov
#188 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 2 lines

I have not received first mail with announcement yet, so I will place my thoughts here if you do not mind.

An issue with sys_async_wait():
is is possible that events_left will be setup too late so that all events are already ready and thus sys_async_wait() can wait forever (or until next $sys_async_wait are ready)?

Signature · 1 line
	Evgeniy Polyakov
#189 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 7 lines · Evgeniy Polyakov
Quoted 2 lines

I have not received first mail with announcement yet, so I will place my thoughts here if you do not mind.

An issue with sys_async_wait(): is is possible that events_left will be setup too late so that all events are already ready and thus sys_async_wait() can wait forever (or until next $sys_async_wait are ready)?

yeah. I have fixed this up and have uploaded a newer queue to:

http://redhat.com/~mingo/syslet-patches/

Ingo

#190 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Evgeniy Polyakov
Quoted 4 lines · Evgeniy Polyakov

[...] it still has a problem - syscall blocks and the same thread thus is not allowed to continue execution and fill the pipe - so what if system issues thousands of requests and there are only tens of working thread at most. [...]

the same thread is allowed to continue execution even if the system call
blocks: take a look at async_schedule(). The blocked system-call is 'put aside' (in a sleeping thread), the kernel switches the user-space context (registers) to a free kernel thread and switches to it - and returns to user-space as if nothing happened - allowing the user-space context to 'fill the pipe' as much as it can. Or did i misunderstand your point?

basically there's SYSLET_ASYNC for 'always async' and SYSLET_SYNC for 'always sync' - but the default syslet behavior is: 'try sync and switch transparently to async on demand'. The testcode i sent very much uses this. (and this mechanism is in essence Zach's fibril-switching thing, but done via kernel threads.)

Ingo

#191 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 8 lines · Ingo Molnar
Quoted 4 lines · Evgeniy Polyakov

[...] it still has a problem - syscall blocks and the same thread thus is not allowed to continue execution and fill the pipe - so what if system issues thousands of requests and there are only tens of working thread at most. [...]

Quoted 7 lines

the same thread is allowed to continue execution even if the system call
blocks: take a look at async_schedule(). The blocked system-call is 'put aside' (in a sleeping thread), the kernel switches the user-space context (registers) to a free kernel thread and switches to it - and returns to user-space as if nothing happened - allowing the user-space context to 'fill the pipe' as much as it can. Or did i misunderstand your point?

Let me clarify what I meant.
There is only limited number of threads, which are supposed to execute blocking context, so when all they are used, main one will block too - I asked about possibility to reuse the same thread to execute queue of requests attached to it, each request can block, but if blocking issue is removed, it would be possible to return.

What I'm asking for is how actually kevent IO state machine functions work
- each IO request is made not through usual mpage and bio allocations, but with special kevent ones, which do not wait until completion, but instead in destructor it is either rescheduled (if big file is transferred, then it is split into parts for transmission) or committed as ready (thus it becomes possible to read completion through kevent queue or ring), so there are only several threads, each one does small job on each request, but the same request can be rescheduled to it again and again (from bio destructor or ->end_io callback for example).

So I asked if it is possible to extend this state machine to work not only with blocked syscalls but with non-blocked functions with possibility to reschedule the same item again.

Signature · 1 line
	Evgeniy Polyakov
#192 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 6 lines · Evgeniy Polyakov

Let me clarify what I meant. There is only limited number of threads, which are supposed to execute blocking context, so when all they are used, main one will block too - I asked about possibility to reuse the same thread to execute queue of requests attached to it, each request can block, but if blocking issue is removed, it would be possible to return.

ah, ok, i understand your point. This is not quite possible: the cachemisses are driven from schedule(), which can be arbitraily deep inside arbitrary system calls. It can be in a mutex_lock() deep inside a driver. It can be due to a alloc_pages() call done by a kmalloc() call done from within ext3, which was called from the loopback block driver, which was called from XFS, which was called from a VFS syscall.

Even if it were possible to backtrack i'm quite sure we dont want to do this, for three main reasons:

Firstly, backtracking and retrying always has a cost. We construct state on the way in - and we destruct on the way out. The kernel stack we have built up has a (nontrivial) construction cost and thus a construction value - we should preserve that if possible.

Secondly, and this is equally important: i wanted the number of async kernel threads to be the natural throttling mechanism. If user-space wants to use less threads and overcommit the request queue then it can be done in user-space: by over-queueing requests into a separate list, and taking from that list upon completion and submitting it. User-space has precise knowledge of overqueueing scenarios: if the event ring is full then all async kernel threads are busy.

but note that there's a deeper reason as well for not wanting
over-queueing: the main cost of a 'pending request' is the kernel stack of the blocked thread itself! So do we want to allow 'requests' to stay 'pending' even if there are "no more threads available"? Nope: because letting them 'pend' would essentially (and implicitly) mean an increase of the thread pool.

In other words: with the syslet subsystem, a kernel thread /is/ the asynchronous request itself. So 'have more requests pending' means 'have more kernel threads'. And 'no kernel thread available' must thus mean 'no queueing of this request'.

Thirdly, there is a performance advantage of this queueing property as
well: by letting a cachemiss thread only do a single syslet all work is concentrated back to the 'head' task, and all queueing decisions are immediately known by user-space and can be acted upon.

So the work-queueing setup is not symmetric at all, there's a fundamental bias and tendency back towards the head task - this helps caching too. That's what Tux did too - it always tried to queue back to the 'head task' as soon as it could. Spreading out work dynamically and transparently is necessary and nice, but it's useless if the system has no automatic tendency to move back into single-threaded (fully cached) state if the workload becomes less parallel. Without this fundamental (and transparent) 'shrink parallelism' property syslets would only degrade into yet another threading construct.

Ingo

#193 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 7 lines · Ingo Molnar
Quoted 6 lines

Let me clarify what I meant. There is only limited number of threads, which are supposed to execute blocking context, so when all they are used, main one will block too - I asked about possibility to reuse the same thread to execute queue of requests attached to it, each request can block, but if blocking issue is removed, it would be possible to return.

Quoted 6 lines

ah, ok, i understand your point. This is not quite possible: the cachemisses are driven from schedule(), which can be arbitraily deep inside arbitrary system calls. It can be in a mutex_lock() deep inside a driver. It can be due to a alloc_pages() call done by a kmalloc() call done from within ext3, which was called from the loopback block driver, which was called from XFS, which was called from a VFS syscall.

That's only because of schedule() is a main point where 'rescheduling'/requeuing (task switch in other words) happens - but if it will be possible to bypass schedule()'s decision and not reschedule there, but 'on demand', will it be possible to reuse the same syslet?

Let me show an example:
consider aio_sendfile() on a big file, so it is not possible to fully get it into VFS, but having spinning on per-page basis (like right now) is no optial solution too. For kevent AIO I created new address space operation aio_getpages() which is essentially mpage_readpages() - it populates several pages into VFS in one BIO (if possible, otherwise in the smallest possible number of chunks) and then in bio destruction callback (actually in bio_endio callback, but for that case it can be considered as the same) I reschedule the same request to some other (not exactly the same as started) thread. When processed data is being sent and next chunk of the file is populated to the VFS using aio_getpages(), which in BIO callback will reschedule the same request again.

So it is possible with essentially one thread (or limited number of them) to fill the whole IO pipe.

With syslet approach it seems to be impossible due to the fact, that request is a whole sendfile. Even if one uses proper readahed (fadvise) advise, there is no possibility to split sendfile and form it as a set of essentially the same requests with different start/offset/whatever parameters (well, exactly for senfile() it is possible - just setup several calls in one syslet from different offsets and with different lengths and form a proper state machine of them, but for example TCP recv() will not match that scenario).

So my main question was about possibility to reuse syslet state machine in kevent AIO instead of own (althtough own one lacks only one good feature of syslets threads currently - its set of threads is global, but not per-task, which does not allow to scale good with number of different processes doing IO) so to not duplicate the code if kevent is ever be possible to get into.

Signature · 1 line
	Evgeniy Polyakov
#194 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 17 lines · Ingo Molnar
Quoted 6 lines · Evgeniy Polyakov

Let me clarify what I meant. There is only limited number of threads, which are supposed to execute blocking context, so when all they are used, main one will block too - I asked about possibility to reuse the same thread to execute queue of requests attached to it, each request can block, but if blocking issue is removed, it would be possible to return.

ah, ok, i understand your point. This is not quite possible: the cachemisses are driven from schedule(), which can be arbitraily deep inside arbitrary system calls. It can be in a mutex_lock() deep inside a driver. It can be due to a alloc_pages() call done by a kmalloc() call done from within ext3, which was called from the loopback block driver, which was called from XFS, which was called from a VFS syscall.

Quoted 2 lines

Even if it were possible to backtrack i'm quite sure we dont want to do this, for three main reasons:

IMO it'd be quite simple. We detect the service-thread full condition, *before* entering exec_atom and we queue the atom in an async_head request list. Yes, there is the chance that from the test time in sys_async_exec, to the time we'll end up entering exec_atom and down to schedule, one of the threads would become free, but IMO better that blocking sys_async_exec.

- Davide

#195 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Benjamin LaHaise
Quoted 1 line · Benjamin LaHaise

[...] interaction with set_fs()...

hm, this one should already work in the current version, because addr_limit is in thread_info and hence stays with the async context. Or can you see any hole in it?

Ingo

#196 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Alan

Alan <[email protected]> writes:

Funny, it sounds like batch() on stereoids @) Ok with an async context it becomes somewhat more interesting.

Quoted 6 lines

sys_setuid/gid/etc need to be synchronous only and not occur
while other async syscalls are running in parallel to meet current kernel assumptions.

sys_exec and other security boundaries must be synchronous only
and not allow async "spill over" (consider setuid async binary patching)

He probably would need some generalization of Andrea's seccomp work. Perhaps using bitmaps? For paranoia I would suggest to white list, not black list calls.

-Andi

#197 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 6 lines · Andi Kleen
Quoted 2 lines

sys_exec and other security boundaries must be synchronous only
and not allow async "spill over" (consider setuid async binary patching)

He probably would need some generalization of Andrea's seccomp work. Perhaps using bitmaps? For paranoia I would suggest to white list, not black list calls.

It's actually more likely a lot more efficient to let the system call itself do the sanity checking. That allows the common system calls (that *don't* need to even check) to just not do anything at all, instead of having some complex logic in the common system call execution trying to figure out for each system call whether it is ok or not.

Ie, we could just add to "do_fork()" (which is where all of the vfork/clone/fork cases end up) a simple case like

	err = wait_async_context();
	if (err)
		return err;

or

	if (in_async_context())
		return -EINVAL;

or similar. We need that "async_context()" function anyway for the other cases where we can't do other things concurrently, like changing the UID.

I would suggest that "wait_async_context()" would do:

- if weare *in* an async context, return an error. We cannot wait for ourselves!
- if we are the "real thread", wait for all async contexts to go away (and since we are the real thread, no new ones will be created, so this is not going to be an infinite wait)

The new thing would be that wait_async_context() would possibly return -ERESTARTSYS (signal while an async context was executing), so any system call that does this would possibly return EINTR. Which "fork()" hasn't historically done. But if you have async events active, some operations likely cannot be done (setuid() and execve() comes to mind), so you really do need something like this.

And obviously it would only affect any program that actually would _use_ any of the suggested new interfaces, so it's not like a new error return would break anything old.

Linus

#198 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 11 lines · Linus Torvalds

Ie, we could just add to "do_fork()" (which is where all of the vfork/clone/fork cases end up) a simple case like

	err = wait_async_context();
	if (err)
		return err;

or

	if (in_async_context())
		return -EINVAL;

ok, this is a much nicer solution. I've scrapped the sys_async_sys_call_table[] thing.

Ingo

#199 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Linus Torvalds
Quoted 5 lines · Linus Torvalds
	if (in_async_context())
		return -EINVAL;

or similar. We need that "async_context()" function anyway for the other cases where we can't do other things concurrently, like changing the UID.

Yes, that's definitely better. Let's have the policy about weather a syscall is or is not async-enabled, inside the syscall itself. Simplify things a lot.

- Davide

#200 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Andi Kleen
Quoted 7 lines · Andi Kleen
Quoted 3 lines

sys_exec and other security boundaries must be synchronous
only and not allow async "spill over" (consider setuid async binary patching)

He probably would need some generalization of Andrea's seccomp work. Perhaps using bitmaps? For paranoia I would suggest to white list, not black list calls.

what i've implemented in my tree is sys_async_call_table[] which is a copy of sys_call_table[] with certain entries modified (by architecture level code, not by kernel/async.c) to sys_ni_syscall(). It's up to the architecture to decide which syscalls are allowed.

but i could use a bitmap too - whatever linear construct. [ I'm not sure there's much connection to seccomp - seccomp uses a NULL terminated whitelist - while syslets would use most of the entries (and would not want to have the overhead of checking a blacklist). ]

Ingo

#201 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Alan
Quoted 26 lines · Alan
Quoted 3 lines

A syslet is executed opportunistically: i.e. the syslet subsystem assumes that the syslet will not block, and it will switch to a cachemiss kernel thread from the scheduler. This means that even a

How is scheduler fairness maintained ? and what is done for resource accounting here ?

Quoted 2 lines

that the kernel fills and user-space clears. Waiting is done via the sys_async_wait() system call. Completion can be supressed on a per-atom

They should be selectable as well iff possible.

Quoted 1 line

Open issues:

Let me add some more

sys_setuid/gid/etc need to be synchronous only and not occur
while other async syscalls are running in parallel to meet current kernel assumptions.

sys_exec and other security boundaries must be synchronous only
and not allow async "spill over" (consider setuid async binary patching)

Quoted 2 lines

- sys_fork() and sys_async_exec() should be filtered out from the syscalls that are allowed - first one only makes sense with ptregs,

Quoted 8 lines

clone and vfork. async_vfork is a real mindbender actually.

Quoted 3 lines

second one is a nice kernel recursion thing :) I didnt want to duplicate the sys_call_table though - maybe others have a better idea.

What are the semantics of async sys_async_wait and async sys_async ?

Ooooohh. OpenVMS lives forever ;) Me likeee ;)

Signature · 1 line
Dmitry
#202 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 3 lines · Dmitry Torokhov
Quoted 1 line

What are the semantics of async sys_async_wait and async sys_async ?

Ooooohh. OpenVMS lives forever ;) Me likeee ;)

hm, i dont know OpenVMS - but googled around a bit for 'VMS asynchronous' and it gave me this:

http://en.wikipedia.org/wiki/Asynchronous_system_trap

is AST what you mean? From a quick read AST seems to be a signal mechanism a bit like Unix signals, extended to kernel-space as well - while syslets are a different 'safe execution engine' kind of thing centered around the execution of system calls.

Ingo

#203 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Hi Ingo,

Quoted 10 lines · Ingo Molnar
Quoted 3 lines · Dmitry Torokhov
Quoted 1 line

What are the semantics of async sys_async_wait and async sys_async ?

Ooooohh. OpenVMS lives forever ;) Me likeee ;)

hm, i dont know OpenVMS - but googled around a bit for 'VMS asynchronous' and it gave me this:

Quoted 7 lines

http://en.wikipedia.org/wiki/Asynchronous_system_trap

is AST what you mean? From a quick read AST seems to be a signal mechanism a bit like Unix signals, extended to kernel-space as well - while syslets are a different 'safe execution engine' kind of thing centered around the execution of system calls.

That is only one of ways of notifying userspace of system call completion on OpenVMS. Pretty much every syscall there exists in 2 flavors - async and sync, for example $QIO and $QIOW or $ENQ/$ENQW (actually -W flavor is async call + $SYNCH to wait for completion). Once system service call is completed the OS would raise a so-called event flag and may also deliver an AST to the process. Application may either wait for an event flag/set of event flags (EFN) or rely on AST to get notification.

Signature · 1 line
Dmitry
#204 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 4 lines
Quoted 1 line

Ooooohh. OpenVMS lives forever ;) Me likeee ;)

hm, i dont know OpenVMS - but googled around a bit for 'VMS asynchronous' and it gave me this:

VMS had SYS$QIO which is asynchronous I/O queueing with completions of sorts. You had to specifically remember if you wanted to a synchronous I/O.

Nothing afaik quite like series of commands batched async, although VMS has a call for everything else so its possible there is one buried in the back of volume 347 of the grey wall ;)

Looking at the completion side I'm not 100% sure we need async_wait given the async batches can include futex operations...

Alan

#205 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Alan
Quoted 6 lines · Alan
Quoted 3 lines

A syslet is executed opportunistically: i.e. the syslet subsystem assumes that the syslet will not block, and it will switch to a cachemiss kernel thread from the scheduler. This means that even a

How is scheduler fairness maintained ? and what is done for resource accounting here ?

the async threads are as if the user created user-space threads - and it's accounted (and scheduled) accordingly.

Quoted 5 lines
Quoted 3 lines

that the kernel fills and user-space clears. Waiting is done via the sys_async_wait() system call. Completion can be supressed on a per-atom

They should be selectable as well iff possible.

basically arbitrary notification interfaces are supported. For example if you add a sys_kill() call as the last syslet atom then this will notify any waiter in sigwait().

or if you want to select(), just do it in the fds that you are interested in, and the write that the syslet does triggers select() completion.

but the fastest one will be by using syslets: to just check the notification ring pointer in user-space, and then call into sys_async_wait() if the ring is empty.

I just noticed a small bug here: sys_async_wait() should also take the ring index userspace checked as a second parameter, and fix up the number of events it waits for with the delta between the ring index the kernel maintains and the ring index user-space has. The patch below fixes this bug.

Quoted 7 lines
Quoted 1 line

Open issues:

Let me add some more

sys_setuid/gid/etc need to be synchronous only and not occur
while other async syscalls are running in parallel to meet current kernel assumptions.

these should probably be taken out of the 'async syscall table', along with fork and the async syscalls themselves.

Quoted 3 lines

sys_exec and other security boundaries must be synchronous
only and not allow async "spill over" (consider setuid async binary patching)

i've tested sys_exec() and it seems to work, but i might have missed some corner-cases. (And what you raise is not academic, it might even make sense to do it, in the vfork() way.)

Quoted 4 lines
Quoted 2 lines

- sys_fork() and sys_async_exec() should be filtered out from the syscalls that are allowed - first one only makes sense with ptregs,

clone and vfork. async_vfork is a real mindbender actually.

yeah. Also, create_module() perhaps. I'm starting to lean towards an async_syscall_table[]. At which point we could reduce the max syslet parameter count to 4, and do those few 5 and 6 parameter syscalls (of which only splice() and futex() truly matter i suspect) via wrappers. This would fit a syslet atom into 32 bytes on x86. Hm?

Quoted 5 lines
Quoted 3 lines

second one is a nice kernel recursion thing :) I didnt want to duplicate the sys_call_table though - maybe others have a better idea.

What are the semantics of async sys_async_wait and async sys_async ?

agreed, that should be forbidden too.

Ingo

---------------------->

---
 kernel/async.c |   12 +++++++++---
 kernel/async.h |    2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)
kernel/async.c +9 -3
Index: linux/kernel/async.c
===================================================================
@@ -721,7 +721,8 @@ static void refill_cachemiss_pool(struct
  * to finish or for all async processing to finish (whichever
  * comes first).
  */
-asmlinkage long sys_async_wait(unsigned long min_wait_events)
+asmlinkage long
+sys_async_wait(unsigned long min_wait_events, unsigned long user_curr_ring_idx)
 {
 	struct async_head *ah = current->ah;
 
@@ -730,12 +731,17 @@ asmlinkage long sys_async_wait(unsigned 
 
 	if (min_wait_events) {
 		spin_lock(&ah->lock);
-		ah->events_left = min_wait_events;
+		/*
+		 * Account any completions that happened since user-space
+		 * checked the ring:
+	 	 */
+		ah->events_left = min_wait_events -
+				(ah->curr_ring_idx - user_curr_ring_idx);
 		spin_unlock(&ah->lock);
 	}
 
 	return wait_event_interruptible(ah->wait,
-		list_empty(&ah->busy_async_threads) || !ah->events_left);
+		list_empty(&ah->busy_async_threads) || ah->events_left > 0);
 }
 
 /**
kernel/async.h +1 -1
Index: linux/kernel/async.h
===================================================================
@@ -26,7 +26,7 @@ struct async_head {
 	struct list_head			ready_async_threads;
 	struct list_head			busy_async_threads;
 
-	unsigned long				events_left;
+	long					events_left;
 	wait_queue_head_t			wait;
 
 	struct async_head_user	__user		*uah;
#206 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 14 lines · Ingo Molnar

As it might be obvious to some of you, the syslet subsystem takes many ideas and experience from my Tux in-kernel webserver :) The syslet code originates from a heavy rewrite of the Tux-atom and the Tux-cachemiss infrastructure.

Open issues:

- the 'TID' of the 'head' thread currently varies depending on which thread is running the user-space context.

- signal support is not fully thought through - probably the head should be getting all of them - the cachemiss threads are not really interested in executing signal handlers.

Quoted 5 lines

- sys_fork() and sys_async_exec() should be filtered out from the syscalls that are allowed - first one only makes sense with ptregs, second one is a nice kernel recursion thing :) I didnt want to duplicate the sys_call_table though - maybe others have a better idea.

If this is going to be a generic AIO subsystem:

- Cancellation of peding request

- Davide

#207 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 3 lines · Davide Libenzi

If this is going to be a generic AIO subsystem:

- Cancellation of peding request

What about the busy_async_threads list becoming a hash/rb_tree indexed by syslet_atom ptr. A cancel would lookup the thread and send a signal (of course, signal handling of the async threads should be set properly)?

- Davide

#208 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 8 lines · Davide Libenzi
Quoted 3 lines

If this is going to be a generic AIO subsystem:

- Cancellation of peding request

What about the busy_async_threads list becoming a hash/rb_tree indexed by syslet_atom ptr. A cancel would lookup the thread and send a signal (of course, signal handling of the async threads should be set properly)?

well, each async syslet has a separate TID at the moment, so if we want a submitted syslet to be cancellable then we could return the TID of the syslet handler (instead of the NULL) in sys_async_exec(). Then user-space could send a signal the old-fashioned way, via sys_tkill(), if it so wishes.

the TID could also be used in a sys_async_wait_on() API. I.e. it would be a natural, readily accessible 'cookie' for the pending work. TIDs can be looked up lockless via RCU, so it's reasonably fast as well.

( Note that there's already a way to 'signal' pending syslets: do_exit()
in the user context will signal all async contexts (which results in -EINTR of currently executing syscalls, wherever possible) and will tear them down. But that's too crude for aio_cancel() i guess. )

Ingo

#209 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Davide Libenzi
Quoted 3 lines

If this is going to be a generic AIO subsystem:

- Cancellation of peding request

What about the busy_async_threads list becoming a hash/rb_tree indexed by syslet_atom ptr. A cancel would lookup the thread and send a signal (of course, signal handling of the async threads should be set properly)?

Quoted 5 lines

well, each async syslet has a separate TID at the moment, so if we want a submitted syslet to be cancellable then we could return the TID of the syslet handler (instead of the NULL) in sys_async_exec(). Then user-space could send a signal the old-fashioned way, via sys_tkill(), if it so wishes.

That works too. I was thinking about identifying syslets with the userspace ptr, but the TID is fine too.

Quoted 8 lines

the TID could also be used in a sys_async_wait_on() API. I.e. it would be a natural, readily accessible 'cookie' for the pending work. TIDs can be looked up lockless via RCU, so it's reasonably fast as well.

( Note that there's already a way to 'signal' pending syslets: do_exit()
in the user context will signal all async contexts (which results in -EINTR of currently executing syscalls, wherever possible) and will tear them down. But that's too crude for aio_cancel() i guess. )

Yup.

- Davide

#210 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Davide Libenzi
Quoted 5 lines · Davide Libenzi
Quoted 1 line

Open issues:

If this is going to be a generic AIO subsystem:

- Cancellation of pending request

How about implementing aio_cancel() as a NOP. Can anyone prove that the kernel didnt actually attempt to cancel that IO? [but unfortunately failed at doing so, because the platters were being written already.]

really, what's the point behind aio_cancel()?

Ingo

#211 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 2 lines · Ingo Molnar

really, what's the point behind aio_cancel()?

The main use case is when you open a file requester on a network file system where the server is down and you get tired of waiting and press "Cancel" it should abort the hanging IO immediately.

At least I would appreciate such a feature sometimes.

e.g. the readdir loop could be a syslet (are they powerful enough to allocate memory for a arbitary sized directory? Probably not) and then the cancel button could async_cancel() it.

-Andi

#212 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 5 lines · Andi Kleen
Quoted 1 line

really, what's the point behind aio_cancel()?

The main use case is when you open a file requester on a network file system where the server is down and you get tired of waiting and press "Cancel" it should abort the hanging IO immediately.

ok, that should work fine already - exit in the user context gets propagated to all async syslet contexts immediately. So if the syscalls that the syslet uses are reasonably interruptible, it will work out fine.

Ingo

#213 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 10 lines · Ingo Molnar
Quoted 5 lines · Andi Kleen
Quoted 1 line

really, what's the point behind aio_cancel()?

The main use case is when you open a file requester on a network file system where the server is down and you get tired of waiting and press "Cancel" it should abort the hanging IO immediately.

ok, that should work fine already - exit in the user context gets

That would be a little heavy handed. I wouldn't expect my GUI program to quit itself on cancel. And requiring it to create a new thread just to exit on cancel would be also nasty.

And of course you cannot interrupt blocked IOs this way right now (currently it only works with signals in some cases on NFS)

-Andi

#214 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 8 lines · Andi Kleen
Quoted 1 line

ok, that should work fine already - exit in the user context gets

That would be a little heavy handed. I wouldn't expect my GUI program to quit itself on cancel. And requiring it to create a new thread just to exit on cancel would be also nasty.

And of course you cannot interrupt blocked IOs this way right now (currently it only works with signals in some cases on NFS)

ok. The TID+signal approach i mentioned in the other reply should work. If it's frequent enough we could make this an explicit sys_async_cancel(TID) API.

Ingo

#215 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 1 line

ok. The TID+signal approach i mentioned in the other reply should work.

Not sure if a signal is good for this. It might conflict with existing strange historical semantics.

Quoted 2 lines

If it's frequent enough we could make this an explicit sys_async_cancel(TID) API.

Ideally there should be a new function like signal_pending() that checks for this. Then the network fs could check those in their blocking loops and error out.

Then it would even work on non intr NFS mounts.

-Andi

#216 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Davide Libenzi
Quoted 1 line

Open issues:

If this is going to be a generic AIO subsystem:

Quoted 7 lines
Quoted 1 line

- Cancellation of pending request

How about implementing aio_cancel() as a NOP. Can anyone prove that the kernel didnt actually attempt to cancel that IO? [but unfortunately failed at doing so, because the platters were being written already.]

really, what's the point behind aio_cancel()?

Lemme give you a real-world scenario: Question Answering in a Dialog System. Your locked-in-memory index ranks documents in a several million files corpus depending of the chances they have to have what you're looking for. You have a tenth of a second to read as many of them as you can, and each seek is 5ms. So you aio-read them, requesting them in order of ranking up to 200 or so, and see what you have at the 0.1s deadline. If you're lucky, a combination of cache (especially if you stat() the whole dir tree on a regular basis to keep the metadata fresh in cache) and of good io reorganisation by the scheduler will allow you to get a good number of them and do the information extraction, scoring and clustering of answers, which is pure CPU at that point. You *have* to cancel the remaining i/o because you do not want the disk saturated when the next request comes, especially if it's 10ms later because the dialog manager found out it needed a complementary request.

Incidentally, that's something I'm currently implementing for work, making these aio discussions more interesting that usual :-)

OG.

#217 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 1 line · Ingo Molnar

really, what's the point behind aio_cancel()?

- sequence

     aio_write()
     aio_cancel()
     aio_write()

with both writes going to the same place must be predictably

- think beyond files. Writes to sockets, ttys, they can block and cancel must abort them. Even for files the same applies in some situations, e.g., for network filesystems.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#218 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 7 lines · Ingo Molnar
Quoted 4 lines · Davide Libenzi
Quoted 1 line

Open issues:

If this is going to be a generic AIO subsystem:

Quoted 7 lines
Quoted 1 line

- Cancellation of pending request

How about implementing aio_cancel() as a NOP. Can anyone prove that the kernel didnt actually attempt to cancel that IO? [but unfortunately failed at doing so, because the platters were being written already.]

really, what's the point behind aio_cancel()?

You need cancel. If you scheduled an async syscall, and the "session" linked with that chain is going away, you better have that canceled before cleaning up buffers to where the chain is going to read/write. If you keep and hash or a tree indexed by atom-ptr, than become a matter of a lookup and sending a signal.

- Davide

#219 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 4 lines · Ingo Molnar

I'm pleased to announce the first release of the "Syslet" kernel feature and kernel subsystem, which provides generic asynchrous system call support:
[...]

Ok, I had little to time review the code, but it has been a long working day, so bear with me if I missed something.
I don't see how sys_async_exec would not block, based on your patches. Let's try to follow:

- We enter sys_async_exec

- We may fill the pool, but that's nothing interesting ATM. A bunch of threads will be created, and they'll end up sleeping inside the cachemiss_loop

- We set the async_ready pointer and we fall inside exec_atom

- There we copy the atom (nothing interesting from a scheduling POV) and we fall inside __exec_atom

- In __exec_atom we do the actual syscall call. Note that we're still the task/thread that called sys_async_exec

- So we enter the syscall, and now we end up in schedule because we're just unlucky

- We notice that the async_ready pointer is not NULL, and we call __async_schedule

- Finally we're in pick_new_async_thread and we pick one of the ready threads sleeping in cachemiss_loop

- We copy the pt_regs to the newly picked-up thread, we set its async head pointer, we set the current task async_ready pointer to NULL, we re-initialize the async_thread structure (the old async_ready), and we put ourselves in the busy_list

- Then we roll back to the schedule that started everything, and being still "prev" for the scheduler, we go to sleep

So the sys_async_exec task is going to block. Now, am I being really tired, or the cachemiss fast return is simply not there? There's another problem AFAICS:

- We woke up one of the cachemiss_loop threads in pick_new_async_thread

- The threads wakes up, mark itself as busy, and look at the ->work pointer hoping to find something to work on

But we never set that pointer to a userspace atom AFAICS. Me blind? :)

- Davide

#220 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

On Tue, 13 Feb 2007, Davide Libenzi wrote:

[...]

Quoted 2 lines

So the sys_async_exec task is going to block. Now, am I being really tired, or the cachemiss fast return is simply not there?

The former 8)

pick_new_async_head()

	new_task->ah = ah;

cachemiss_loop()

	for (;;) {
		if (unlikely(t->ah || ...))
			break;
Quoted 8 lines

There's another problem AFAICS:

- We woke up one of the cachemiss_loop threads in pick_new_async_thread

- The threads wakes up, mark itself as busy, and look at the ->work pointer hoping to find something to work on

But we never set that pointer to a userspace atom AFAICS. Me blind? :)

I still don't see at->work ever set to a valid userspace atom though...

- Davide

#221 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 11 lines · Davide Libenzi
Quoted 8 lines

There's another problem AFAICS:

- We woke up one of the cachemiss_loop threads in pick_new_async_thread

- The threads wakes up, mark itself as busy, and look at the ->work pointer hoping to find something to work on

But we never set that pointer to a userspace atom AFAICS. Me blind? :)

I still don't see at->work ever set to a valid userspace atom though...

yeah - i havent added support for 'submit syslet from within a syslet' support yet :-)

note that current normal syslet operation (both async and sync alike) does not need at->work at all. When we cachemiss then the new head task just wants to return a NULL pointer to user-space, to signal that work is continuing in the background. A ready 'cachemiss' thread is really not there to do cachemisses, it is a 'new head task in the waiting'. The name comes from Tux and i guess it's time for a rename :)

but i do plan a SYSLET_ASYNC_CONTINUE flag, roughly along the patch i've attached below: this would skip to the linearly next syslet and would let the original syslet execute in the. I have not fully thought this through though (let alone tested it ;) - can you see any hole in this approach? This would in essence allow the following construct:

syslet1 &
syslet2 &
syslet3 &
syslet4 &

submitted in parallel, straight to cachemiss threads, from a syslet itself.

there's yet another work submission variant that makes sense to do, a true syslet vector submission: to do a loop over syslet atoms in sys_async_exec(). That would have the added advantage of enabling caching. If one vector component generates a cachemiss then the head would continue with the next vector. (this too needs at->work alike communication between ex-head and new-head)

maybe the latter would be the cleaner approach - SYSLET_ASYNC_CONTINUE has no effect in cachemiss context, so it only makes sense if the submitted syslet is a pure vector of parallel atoms. Alternatively, SYSLET_ASYNC_CONTINUE would have to be made work from cachemiss contexts too. (because that makes sense too, to start new async execution from another async context.)

another not yet clear area is when there's no cachemiss thread available. Right now SYSLET_ASYNC_CONTINUE will just fail - which makes it nondeterministic.

Ingo

---
 include/linux/async.h  |   13 +++++++++++--
 include/linux/sched.h  |    3 +--
 include/linux/syslet.h |   20 +++++++++++++-------
 kernel/async.c         |   43 +++++++++++++++++++++++++++++--------------
 kernel/sched.c         |    2 +-
 5 files changed, 56 insertions(+), 27 deletions(-)

# *DOCUMENTATION*

include/linux/async.h +11 -2
Index: linux/include/linux/async.h
===================================================================
@@ -1,15 +1,23 @@
 #ifndef _LINUX_ASYNC_H
 #define _LINUX_ASYNC_H
+
+#include <linux/compiler.h>
+
 /*
  * The syslet subsystem - asynchronous syscall execution support.
  *
  * Generic kernel API definitions:
  */
 
+struct syslet_uatom;
+struct async_thread;
+struct async_head;
+
 #ifdef CONFIG_ASYNC_SUPPORT
 extern void async_init(struct task_struct *t);
 extern void async_exit(struct task_struct *t);
-extern void __async_schedule(struct task_struct *t);
+extern void
+__async_schedule(struct task_struct *t, struct syslet_uatom __user *next_uatom);
 #else /* !CONFIG_ASYNC_SUPPORT */
 static inline void async_init(struct task_struct *t)
 {
@@ -17,7 +25,8 @@ static inline void async_init(struct tas
 static inline void async_exit(struct task_struct *t)
 {
 }
-static inline void __async_schedule(struct task_struct *t)
+static inline void
+__async_schedule(struct task_struct *t, struct syslet_uatom __user *next_uatom)
 {
 }
 #endif /* !CONFIG_ASYNC_SUPPORT */
include/linux/sched.h +1 -2
Index: linux/include/linux/sched.h
===================================================================
@@ -83,13 +83,12 @@ struct sched_param {
 #include <linux/timer.h>
 #include <linux/hrtimer.h>
 #include <linux/task_io_accounting.h>
+#include <linux/async.h>
 
 #include <asm/processor.h>
 
 struct exec_domain;
 struct futex_pi_state;
-struct async_thread;
-struct async_head;
 /*
  * List of flags we want to share for kernel threads,
  * if only because they are not used by them anyway.
include/linux/syslet.h +13 -7
Index: linux/include/linux/syslet.h
===================================================================
@@ -56,10 +56,16 @@ struct syslet_uatom {
 #define SYSLET_ASYNC				0x00000001
 
 /*
+ * Queue this syslet asynchronously and continue executing the
+ * next linear atom:
+ */
+#define SYSLET_ASYNC_CONTINUE			0x00000002
+
+/*
  * Never queue this syslet asynchronously - even if synchronous
  * execution causes a context-switching:
  */
-#define SYSLET_SYNC				0x00000002
+#define SYSLET_SYNC				0x00000004
 
 /*
  * Do not queue the syslet in the completion ring when done.
@@ -70,7 +76,7 @@ struct syslet_uatom {
  * Some syscalls generate implicit completion events of their
  * own.
  */
-#define SYSLET_NO_COMPLETE			0x00000004
+#define SYSLET_NO_COMPLETE			0x00000008
 
 /*
  * Execution control: conditions upon the return code
@@ -78,10 +84,10 @@ struct syslet_uatom {
  * execution is stopped and the atom is put into the
  * completion ring:
  */
-#define SYSLET_STOP_ON_NONZERO			0x00000008
-#define SYSLET_STOP_ON_ZERO			0x00000010
-#define SYSLET_STOP_ON_NEGATIVE			0x00000020
-#define SYSLET_STOP_ON_NON_POSITIVE		0x00000040
+#define SYSLET_STOP_ON_NONZERO			0x00000010
+#define SYSLET_STOP_ON_ZERO			0x00000020
+#define SYSLET_STOP_ON_NEGATIVE			0x00000040
+#define SYSLET_STOP_ON_NON_POSITIVE		0x00000080
 
 #define SYSLET_STOP_MASK				\
 	(	SYSLET_STOP_ON_NONZERO		|	\
@@ -97,7 +103,7 @@ struct syslet_uatom {
  *
  * This is what allows true branches of execution within syslets.
  */
-#define SYSLET_SKIP_TO_NEXT_ON_STOP		0x00000080
+#define SYSLET_SKIP_TO_NEXT_ON_STOP		0x00000100
 
 /*
  * This is the (per-user-context) descriptor of the async completion
kernel/async.c +29 -14

100 lines not on this page · raw message

kernel/sched.c +1 -1
Index: linux/kernel/sched.c
===================================================================
@@ -3442,7 +3442,7 @@ asmlinkage void __sched schedule(void)
 		if (prev->state && !(preempt_count() & PREEMPT_ACTIVE) &&
 			(!(prev->state & TASK_INTERRUPTIBLE) ||
 				!signal_pending(prev)))
-			__async_schedule(prev);
+			__async_schedule(prev, NULL);
 	}
 
 need_resched:
#222 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar

Hi Ingo !

Quoted 6 lines · Ingo Molnar

I'm pleased to announce the first release of the "Syslet" kernel feature and kernel subsystem, which provides generic asynchrous system call support:

http://redhat.com/~mingo/syslet-patches/

Quoted 7 lines

Syslets are small, simple, lightweight programs (consisting of system-calls, 'atoms') that the kernel can execute autonomously (and, not the least, asynchronously), without having to exit back into user-space. Syslets can be freely constructed and submitted by any unprivileged user-space context - and they have access to all the resources (and only those resources) that the original context has access to.

I like this a lot. I've always felt frustrated by the wasted time in setsockopt() calls after accept() or before connect(), or in multiple calls to epoll_ctl(). It might also be useful as an efficient readv() emulation using recv(), etc...

Nice work !
Willy

#223 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar

Hi!

Quoted 7 lines

The boring details:

Syslets consist of 'syslet atoms', where each atom represents a single system-call. These atoms can be chained to each other: serially, in branches or in loops. The return value of an executed atom is checked against the condition flags. So an atom can specify 'exit on nonzero' or 'loop until non-negative' kind of constructs.

Ouch, yet another interpretter in kernel :-(. Can we reuse acpi or something?

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#224 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 3 lines · Pavel Machek

Ouch, yet another interpretter in kernel :-(. Can we reuse acpi or something?

Hah. You make the joke! I get it!

Mwahahahaa!

Linus

#225 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar
Quoted 6 lines · Ingo Molnar

Syslets consist of 'syslet atoms', where each atom represents a single system-call. These atoms can be chained to each other: serially, in branches or in loops. The return value of an executed atom is checked against the condition flags. So an atom can specify 'exit on nonzero' or 'loop until non-negative' kind of constructs.

Quoted 6 lines

Syslet atoms fundamentally execute only system calls, thus to be able to manipulate user-space variables from syslets i've added a simple special system call: sys_umem_add(ptr, val). This can be used to increase or decrease the user-space variable (and to get the result), or to simply read out the variable (if 'val' is 0).

This looks very interesting. A couple of questions:

Are there any special semantics that result from running the syslet atoms in kernel mode? If I wanted to, could I write a syslet emulation in userspace that's functionally identical to a kernel-based implementation? (Obviously the performance characteristics will be different.)

I'm asking from the perspective of trying to work out the Valgrind binding for this if it goes into the kernel. Valgrind needs to see all the input and output values of each system call the client makes, including those done within the syslet mechanism. It seems to me that the easiest way to do this would be to intercept the syslet system calls, and just implement them in usermode, performing the same series of syscalls directly, and applying the Valgrind machinery to each one in turn.

Would this work?

Also, an unrelated question: is there enough control structure in place to allow multiple syslet streams to synchronize with each other with futexes?

Thanks,
J

#226 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 15 lines · Jeremy Fitzhardinge

Are there any special semantics that result from running the syslet atoms in kernel mode? If I wanted to, could I write a syslet emulation in userspace that's functionally identical to a kernel-based implementation? (Obviously the performance characteristics will be different.)

I'm asking from the perspective of trying to work out the Valgrind binding for this if it goes into the kernel. Valgrind needs to see all the input and output values of each system call the client makes, including those done within the syslet mechanism. It seems to me that the easiest way to do this would be to intercept the syslet system calls, and just implement them in usermode, performing the same series of syscalls directly, and applying the Valgrind machinery to each one in turn.

Quoted 1 line

Would this work?

Hopefully the API will simplify enough so that emulation will becomes easier.

Quoted 3 lines

Also, an unrelated question: is there enough control structure in place to allow multiple syslet streams to synchronize with each other with futexes?

I think the whole point of an async execution of a syscall or a syslet, is that the syscall/syslet itself includes a non interlocked operations with other syscalls/syslets. So that the main scheduler thread can run in a lockless/singletask fashion. There are no technical obstacles that prevents you to do it, bu if you start adding locks (and hence having long-living syslet-threads) at that point you'll end up with a fully multithreaded solution.

- Davide

#227 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 6 lines · Davide Libenzi
Quoted 2 lines

Would this work?

Hopefully the API will simplify enough so that emulation will becomes easier.

The big question in my mind is how all this stuff interacts with signals. Can a blocked syscall atom be interrupted by a signal? If so, what thread does it get delivered to? How does sigprocmask affect this (is it atomic with respect to blocking atoms)?

Quoted 5 lines
Quoted 4 lines

Also, an unrelated question: is there enough control structure in place to allow multiple syslet streams to synchronize with each other with futexes?

Quoted 8 lines

I think the whole point of an async execution of a syscall or a syslet, is that the syscall/syslet itself includes a non interlocked operations with other syscalls/syslets. So that the main scheduler thread can run in a lockless/singletask fashion. There are no technical obstacles that prevents you to do it, bu if you start adding locks (and hence having long-living syslet-threads) at that point you'll end up with a fully multithreaded solution.

I was thinking you'd use the futexes more like barriers than locks. That way you could have several streams going asynchronously, but use futexes to gang them together at appropriate times in the stream. A handwavy example would be to have separate async streams for audio and video, but use futexes to stop them from drifting too far from each other.

J

#228 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

Quoted 4 lines · Jeremy Fitzhardinge
Quoted 3 lines · Davide Libenzi
Quoted 2 lines

Would this work?

Quoted 8 lines
Quoted 3 lines

Hopefully the API will simplify enough so that emulation will becomes easier.

The big question in my mind is how all this stuff interacts with signals. Can a blocked syscall atom be interrupted by a signal? If so, what thread does it get delivered to? How does sigprocmask affect this (is it atomic with respect to blocking atoms)?

Signal context is another thing that we need to transfer to the return-to-userspace task, in case we switch. Async threads inherit that from the "main" task once they're created, but from there to the sys_async_exec syscall, userspace might have changed the signal context, and re-emerging with a different one is not an option ;)
We should setup service-threds signal context, so that we can cancel them, but the implementation should be hidden to userspace (that will use sys_async_cancel - or whatever name - to do that).

- Davide

#229 lore raw

Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support

In reply to Ingo Molnar

I'm finally back from my travel and conference hiatus.. you guys have been busy! :)

Quoted 5 lines · Ingo Molnar

I'm pleased to announce the first release of the "Syslet" kernel feature and kernel subsystem, which provides generic asynchrous system call support:

http://redhat.com/~mingo/syslet-patches/

In general, I really like the look of this.

I think I'm convinced that your strong preference to do this with full kernel threads (1:1 task_struct -> thread_info/stack relationship) is the right thing to do. The fibrils fell on the side of risking bugs by sharing task_structs amongst stacks executing kernel paths. This, correct me if I'm wrong, falls on the side of risking behavioural quirks stemming from task_struct references that we happen to have not enabled sharing of yet.

I have strong hopes that we won't actually *care* about the behavioural differences we get from having individual task structs (which share the important things!) between syscall handlers. The *only* seemingly significant case I've managed to find, the IO scheduler priority and context fields, is easy enough to fix up. Jens and I have been talking about that. It's been bugging him for other reasons.

So, thanks, nice work. I'm going to focus on finding out if its feasible for The Database to use this instead of the current iocb mechanics. I'm optimistic.

Quoted 2 lines

Syslets are small, simple, lightweight programs (consisting of system-calls, 'atoms')

I will admit, though, that I'm not at all convinced that we need this. Adding a system call for addition (just addition? how far do we go?!) sure feels like a warning sign to me that we're heading down a slippery slope. I would rather we started with an obviously minimal syscall which just takes an array of calls and args and executes them unconditionally.

But its existance doesn't stop the use case I care about. So it's hard to get *too* worked up about it.

Quoted 1 line

Comments, suggestions, reports are welcome!

For what it's worth, it looks like 'x86-optimized-copy_uatom.patch' got some hunks that should have been in 'x86-optimized-sys_umem_add.patch'.

- z

#230 lore raw

PATCH 5/11 syslets: core code

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

the core syslet / async system calls infrastructure code.

Is built only if CONFIG_ASYNC_SUPPORT is enabled.

---
 kernel/Makefile |    1 
 kernel/async.c  |  811 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 812 insertions(+)
kernel/Makefile +1 -0
Index: linux/kernel/Makefile
===================================================================
@@ -10,6 +10,7 @@ obj-y     = sched.o fork.o exec_domain.o
 	    kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
 	    hrtimer.o rwsem.o latency.o nsproxy.o srcu.o
 
+obj-$(CONFIG_ASYNC_SUPPORT) += async.o
 obj-$(CONFIG_STACKTRACE) += stacktrace.o
 obj-y += time/
 obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
kernel/async.c new file +811 -0

811 lines not on this page · raw message

#231 lore raw

Re: [patch 05/11] syslets: core code

kernel/async.c line 120

Quoted 3 lines · Ingo Molnar
kernel/async.c new file +3 -0
+
+static struct async_thread *
+pick_ready_cachemiss_thread(struct async_head *ah)

The cachemiss names are confusing. I assume that's just a left over from Tux?

kernel/async.c line 269

Quoted 8 lines
kernel/async.c new file +8 -0
+
+	memset(atom->args, 0, sizeof(atom->args));
+
+	ret |= __get_user(arg_ptr, &uatom->arg_ptr[0]);
+	if (!arg_ptr)
+		return ret;
+	if (!access_ok(VERIFY_WRITE, arg_ptr, sizeof(*arg_ptr)))
+		return -EFAULT;

It's a little unclear why you do that many individual access_ok()s. And why is the target constant sized anyways?

+ /*
+ * Lock down the ring. Note: user-space should not munlock() this,
+ * because if the ring pages get swapped out then the async
+ * completion code might return a -EFAULT instead of the expected
+ * completion. (the kernel safely handles that case too, so this
+ * isnt a security problem.)
+ *
+ * mlock() is better here because it gets resource-accounted
+ * properly, and even unprivileged userspace has a few pages
+ * of mlock-able memory available. (which is more than enough
+ * for the completion-pointers ringbuffer)
+ */

If it's only a few pages you don't need any resource accounting. If it's more then it's nasty to steal the users quota. I think plain gup() would be better.

-Andi

#232 lore raw

Re: [patch 05/11] syslets: core code

Quoted 8 lines · Andi Kleen

kernel/async.c line 120

Quoted 3 lines · Ingo Molnar
kernel/async.c new file +3 -0
+
+static struct async_thread *
+pick_ready_cachemiss_thread(struct async_head *ah)

The cachemiss names are confusing. I assume that's just a left over from Tux?

yeah. Although 'stuff goes async' is quite similar to a cachemiss. We didnt have some resource available right now so the syscall has to block == i.e. some cache was not available.

Quoted 11 lines

kernel/async.c line 269

Quoted 8 lines
kernel/async.c new file +8 -0
+
+	memset(atom->args, 0, sizeof(atom->args));
+
+	ret |= __get_user(arg_ptr, &uatom->arg_ptr[0]);
+	if (!arg_ptr)
+		return ret;
+	if (!access_ok(VERIFY_WRITE, arg_ptr, sizeof(*arg_ptr)))
+		return -EFAULT;

It's a little unclear why you do that many individual access_ok()s. And why is the target constant sized anyways?

each indirect pointer has to be checked separately, before dereferencing it. (Andrew pointed out that they should be VERIFY_READ, i fixed that in my tree)

it looks a bit scary in C but the assembly code is very fast and quite straightforward.

Quoted 13 lines
kernel/async.c new file +12 -0
+	/*
+	 * Lock down the ring. Note: user-space should not munlock() this,
+	 * because if the ring pages get swapped out then the async
+	 * completion code might return a -EFAULT instead of the expected
+	 * completion. (the kernel safely handles that case too, so this
+	 * isnt a security problem.)
+	 *
+	 * mlock() is better here because it gets resource-accounted
+	 * properly, and even unprivileged userspace has a few pages
+	 * of mlock-able memory available. (which is more than enough
+	 * for the completion-pointers ringbuffer)
+	 */
Quoted 3 lines

If it's only a few pages you don't need any resource accounting. If it's more then it's nasty to steal the users quota. I think plain gup() would be better.

get_user_pages() would have to be limited in some way - and i didnt want to add yet another wacky limit thing - so i just used the already existing mlock() infrastructure for this. If Oracle wants to set up a 10 MB ringbuffer, they can set the PAM resource limits to 11 MB and still have enough stuff left. And i dont really expect GPG to start using syslets - just yet ;-)

a single page is enough for 1024 completion pointers - that's more than enough for most purposes - and the default mlock limit is 40K.

Ingo

#233 lore raw

Re: [patch 05/11] syslets: core code

Quoted 11 lines · Ingo Molnar
Quoted 10 lines

kernel/async.c line 269

Quoted 7 lines
kernel/async.c new file +7 -0
+	memset(atom->args, 0, sizeof(atom->args));
+
+	ret |= __get_user(arg_ptr, &uatom->arg_ptr[0]);
+	if (!arg_ptr)
+		return ret;
+	if (!access_ok(VERIFY_WRITE, arg_ptr, sizeof(*arg_ptr)))
+		return -EFAULT;

It's a little unclear why you do that many individual access_ok()s. And why is the target constant sized anyways?

Quoted 3 lines

each indirect pointer has to be checked separately, before dereferencing it. (Andrew pointed out that they should be VERIFY_READ, i fixed that in my tree)

But why only constant sized? It could be a variable length object, couldn't it?

If it's an array it could be all checked together

(i must be missing something here)

Quoted 5 lines
Quoted 3 lines

If it's only a few pages you don't need any resource accounting. If it's more then it's nasty to steal the users quota. I think plain gup() would be better.

get_user_pages() would have to be limited in some way - and i didnt want

If you only use it for a small ring buffer it is naturally limited.

Also beancounter will fix that eventually.

Quoted 2 lines

a single page is enough for 1024 completion pointers - that's more than enough for most purposes - and the default mlock limit is 40K.

Then limit it to a single page and use gup

-Andi

#234 lore raw

Re: [patch 05/11] syslets: core code

Quoted 6 lines · Andi Kleen
Quoted 6 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!access_ok(VERIFY_WRITE, arg_ptr, sizeof(*arg_ptr)))
+		return -EFAULT;

It's a little unclear why you do that many individual access_ok()s. And why is the target constant sized anyways?

Quoted 6 lines
Quoted 3 lines

each indirect pointer has to be checked separately, before dereferencing it. (Andrew pointed out that they should be VERIFY_READ, i fixed that in my tree)

But why only constant sized? It could be a variable length object, couldn't it?

i think what you might be missing is that it's only the 6 syscall arguments that are fetched via indirect pointers - security checks are then done by the system calls themselves. It's a bit awkward to think about, but it is surprisingly clean in the assembly, and it simplified syslet programming too.

Quoted 4 lines
Quoted 2 lines

get_user_pages() would have to be limited in some way - and i didnt want

If you only use it for a small ring buffer it is naturally limited.

yeah, but 'small' is a dangerous word when it comes to adding IO interfaces ;-)

Quoted 4 lines
Quoted 2 lines

a single page is enough for 1024 completion pointers - that's more than enough for most purposes - and the default mlock limit is 40K.

Then limit it to a single page and use gup

1024 (512 on 64-bit) is alot but not ALOT. It is also certainly not ALOOOOT :-) Really, people will want to have more than 512 disks/spindles in the same box. I have used such a beast myself. For Tux workloads and benchmarks we had parallelism levels of millions of pending requests (!) on a single system - networking, socket limits, disk IO combined with thousands of clients do create such scenarios. I really think that such 'pinned pages' are a pretty natural fit for sys_mlock() and RLIMIT_MEMLOCK, and since the kernel side is careful to use the _inatomic() uaccess methods, it's safe (and fast) as well.

Ingo

#235 lore raw

Re: [patch 05/11] syslets: core code

Quoted 11 lines · Ingo Molnar
Quoted 1 line

Then limit it to a single page and use gup

1024 (512 on 64-bit) is alot but not ALOT. It is also certainly not ALOOOOT :-) Really, people will want to have more than 512 disks/spindles in the same box. I have used such a beast myself. For Tux workloads and benchmarks we had parallelism levels of millions of pending requests (!) on a single system - networking, socket limits, disk IO combined with thousands of clients do create such scenarios. I really think that such 'pinned pages' are a pretty natural fit for sys_mlock() and RLIMIT_MEMLOCK, and since the kernel side is careful to use the _inatomic() uaccess methods, it's safe (and fast) as well.

This will end up badly - I used the same approach in the early kevent days and was proven to have swapable memory for the ring. I think it would be much better to have userspace allocated ring and use copy_to_user() there.

Btw, as a bit of advertisement, the whole completion part can be done through kevent which already has ring buffer, queue operations and non-racy updates... :)

Quoted 1 line

Ingo

Signature · 1 line
	Evgeniy Polyakov
#236 lore raw

Re: [patch 05/11] syslets: core code

Quoted 4 lines · Evgeniy Polyakov

This will end up badly - I used the same approach in the early kevent days and was proven to have swapable memory for the ring. I think it would be much better to have userspace allocated ring and use copy_to_user() there.

it is a userspace allocated ring - but pinned down by the kernel.

Ingo

#237 lore raw

Re: [patch 05/11] syslets: core code

Quoted 9 lines · Ingo Molnar
Quoted 4 lines · Evgeniy Polyakov

This will end up badly - I used the same approach in the early kevent days and was proven to have swapable memory for the ring. I think it would be much better to have userspace allocated ring and use copy_to_user() there.

it is a userspace allocated ring - but pinned down by the kernel.

That's a problem - 1000/512 pages per 'usual' thread ends up with the whole memory locked by malicious/stupid application (at least on Debian and Mandrake there is no locked memory limit by default). And if such a limit exists, this will hurt big-iron applications, which want to used high-order rings legitimely.

Quoted 1 line

Ingo

Signature · 1 line
	Evgeniy Polyakov
#238 lore raw

Re: [patch 05/11] syslets: core code

Quoted 2 lines

(at least on Debian
and Mandrake there is no locked memory limit by default).

that sounds like 2 very large bugtraq-worthy bugs in these distros.. so bad a bug that I almost find it hard to believe...

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#239 lore raw

Re: [patch 05/11] syslets: core code

Quoted 5 lines · Arjan van de Ven
Quoted 2 lines

(at least on Debian
and Mandrake there is no locked memory limit by default).

that sounds like 2 very large bugtraq-worthy bugs in these distros.. so bad a bug that I almost find it hard to believe...

Well:

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
max nice                        (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) unlimited
max rt priority                 (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
$ cat /etc/debian_version
4.0
$ ulimit -a
core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 1024
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 7168
virtual memory        (kbytes, -v) unlimited
$ cat /etc/mandrake-release
Mandrake Linux release 10.0 (Community) for i586

Anyway, even if there is a limit like in fc5 - 32kb, so I doubt any unpriveledged userspace application will ever run there.

Signature · 1 line
	Evgeniy Polyakov
maintainer
#240 lore raw

Re: [patch 05/11] syslets: core code

In reply to Ingo Molnar
Quoted 12 lines · On Tue, 13 Feb 2007 23:24:43 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 3 lines

If it's only a few pages you don't need any resource accounting. If it's more then it's nasty to steal the users quota. I think plain gup() would be better.

get_user_pages() would have to be limited in some way - and i didnt want to add yet another wacky limit thing - so i just used the already existing mlock() infrastructure for this. If Oracle wants to set up a 10 MB ringbuffer, they can set the PAM resource limits to 11 MB and still have enough stuff left. And i dont really expect GPG to start using syslets - just yet ;-)

Quoted 2 lines

a single page is enough for 1024 completion pointers - that's more than enough for most purposes - and the default mlock limit is 40K.

So if I have an application which instantiates a single mlocked page for this purpose, I can only run ten of them at once, and any other mlock-using process which I'm using starts to mysteriously fail.

It seems like a problem to me..

#241 lore raw

Re: [patch 05/11] syslets: core code

In reply to Ingo Molnar

Review

Quoted 3 lines · Ingo Molnar
(quoted diff) +2 -0
+	if (unlikely(signal_pending(t) || need_resched()))
+		goto stop;

So, this is how you'll prevent me from running an infinite loop ;-) The attached patch adds a cond_resched() instead, to allow infinite loops without DoS. I dropped the unlikely() as it's already in the definition of signal_pending().

Quoted 2 lines

+asmlinkage long sys_async_wait(unsigned long min_wait_events)

Here I would expect:

sys_async_wait_for_all(struct syslet_atom *atoms, long nr_atoms)

and

sys_async_wait_for_any(struct syslet_atom *atoms, long nr_atoms).

This way syslets can be used by different parts of a program without having them waiting for each other.

Thanks.

Signature · 1 line
Guillaume
kernel/async.c +2 -1
@@ -433,9 +433,10 @@
 
 	last_uatom = uatom;
 	ret = __exec_atom(t, &atom);
-	if (unlikely(signal_pending(t) || need_resched()))
+	if (signal_pending(t))
 		goto stop;
 
+	cond_resched();
 	uatom = next_uatom(&atom, uatom, ret);
 	if (uatom)
 		goto run_next;
#243 lore raw

Re: [patch 05/11] syslets: core code

In reply to Ingo Molnar
Quoted 2 lines · Ingo Molnar

the core syslet / async system calls infrastructure code.

Ok, having now looked at it more, I can say:

- I hate it.

I dislike it intensely, because it's so _close_ to being usable. But the programming interface looks absolutely horrid for any "casual" use, and while the loops etc look like fun, I think they are likely to be less than useful in practice. Yeah, you can do the "setup and teardown" just once, but it ends up being "once per user", and it ends up being a lot of stuff to do for somebody who wants to just do some simple async stuff.

And the whole "lock things down in memory" approach is bad. It's doing expensive things like mlock(), making the overhead for _single_ system calls much more expensive. Since I don't actually believe that the non-single case is even all that interesting, I really don't like it.

I think it's clever and potentially useful to allow user mode to see the data structures (and even allow user mode to *modify* them) while the async thing is running, but it really seems to be a case of excessive cleverness.

For example, how would you use this to emulate the *current* aio_read() etc interfaces that don't have any user-level component except for the actual call? And if you can't do that, the whole exercise is pointless.

Or how would you do the trivial example loop that I explained was a good idea:

        struct one_entry *prev = NULL;
        struct dirent *de;

        while ((de = readdir(dir)) != NULL) {
                struct one_entry *entry = malloc(..);

                /* Add it to the list, fill in the name */
                entry->next = prev;
                prev = entry;
                strcpy(entry->name, de->d_name);

                /* Do the stat lookup async */
                async_stat(de->d_name, &entry->stat_buf);
        }
        wait_for_async();
        .. Ta-daa! All done ..

Notice? This also "chains system calls together", but it does it using a *much* more powerful entity called "user space". That's what user space is. And yeah, it's a pretty complex sequencer, but happily we have hardware support for accelerating it to the point that the kernel never even needs to care.

The above is a *realistic* schenario, where you actually have things like memory allocation etc going on. In contrast, just chaining system calls together isn't a realistic schenario at all.

So I think we have one _known_ usage schenario:

- replacing the _existing_ aio_read() etc system calls (with not just existing semantics, but actually binary-compatible)

- simple code use where people are willing to perhaps do something Linux-specific, but because it's so _simple_, they'll do it.

In neither case does the "chaining atoms together" seem to really solve the problem. It's clever, but it's not what people would actually do.

And yes, you can hide things like that behind an abstraction library, but once you start doing that, I've got three questions for you:

- what's the point?
- we're adding overhead, so how are we getting it back
- how do we handle independent libraries each doing their own thing and version skew between them?

In other words, the "let user space sort out the complexity" is not a good answer. It just means that the interface is badly designed.

Linus

#244 lore raw

Re: [patch 05/11] syslets: core code

Quoted 3 lines · Linus Torvalds

And the whole "lock things down in memory" approach is bad. It's doing expensive things like mlock(), making the overhead for _single_ system calls much more expensive. [...]

hm, there must be some misunderstanding here. That mlock is /only/ once per the lifetime of the whole 'head' - i.e. per sys_async_register(). (And you can even forget i ever did it - it's 5 lines of code to turn the completion ring into a swappable entity.)

never does any MMU trick ever enter the picture during the whole operation of this thing, and that's very much intentional.

Ingo

#245 lore raw

Re: [patch 05/11] syslets: core code

Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Linus Torvalds

And the whole "lock things down in memory" approach is bad. It's doing expensive things like mlock(), making the overhead for _single_ system calls much more expensive. [...]

Quoted 7 lines

hm, there must be some misunderstanding here. That mlock is /only/ once per the lifetime of the whole 'head' - i.e. per sys_async_register(). (And you can even forget i ever did it - it's 5 lines of code to turn the completion ring into a swappable entity.)

never does any MMU trick ever enter the picture during the whole operation of this thing, and that's very much intentional.

to stress it: never does any mlocking or other lockdown happen of any syslet atom - it is /only/ the completion ring of syslet pointers that i made mlocked - but even that can be made generic memory no problem.

It's all about asynchronous system calls, and if you want you can have a terabyte of syslets in user memory, half of it swapped out. They have absolutely zero kernel context attached to them in the 'cached case' (be that locked memory or some other kernel resource).

Ingo

#246 lore raw

Re: [patch 05/11] syslets: core code

In reply to Ingo Molnar
Quoted 5 lines · Ingo Molnar

hm, there must be some misunderstanding here. That mlock is /only/ once per the lifetime of the whole 'head' - i.e. per sys_async_register(). (And you can even forget i ever did it - it's 5 lines of code to turn the completion ring into a swappable entity.)

But the whole point is that the notion of a "register" is wrong in the first place. It's wrong because:

- it assumes we are going to make these complex state machines (which I don't believe for a second that a real program will do)

- it assumes that we're going to make many async system calls that go together (which breaks the whole notion of having different libraries using this for their own internal reasons - they may not even *know* about other libraries that _also_ do async IO for *their* reasons)

- it fundamentally is based on a broken notion that everything would use this "AIO atom" in the first place, WHICH WE KNOW IS INCORRECT, since current users use "aio_read()" that simply doesn't have that and doesn't build up any such data structures.

So please answer my questions. The problem wasn't the mlock(), even though that was just STUPID. The problem was much deeper. This is not a "prepare to do a lot of very boutique linked list operations" problem. This is a "people already use 'aio_read()' and want to extend on it" problem.

You didn't at all react to that fundamental issue: you have an overly complex and clever thing that doesn't actually *match* what people do.

Linus

#247 lore raw

Re: [patch 05/11] syslets: core code

Quoted 2 lines · Linus Torvalds

But the whole point is that the notion of a "register" is wrong in the first place. [...]

forget about it then. The thing we "register" is dead-simple:

 struct async_head_user {
         struct syslet_uatom __user              **completion_ring;
         unsigned long                           ring_size_bytes;
         unsigned long                           max_nr_threads;

};

this can be passed in to sys_async_exec() as a second pointer, and the kernel can put the expected-completion pointer (and the user ring idx pointer) into its struct atom. It's just a few instructions, and only in the cachemiss case.

that would make completions arbitrarily split-up-able. No registration whatsoever. A waiter could specify which ring's events it is interested in. A 'ring' could be a single-entry thing as well, for a single instance of pending IO.

Ingo

#248 lore raw

Re: [patch 05/11] syslets: core code

Quoted 16 lines
Quoted 2 lines

But the whole point is that the notion of a "register" is wrong in the first place. [...]

forget about it then. The thing we "register" is dead-simple:

 struct async_head_user {
         struct syslet_uatom __user              **completion_ring;
         unsigned long                           ring_size_bytes;
         unsigned long                           max_nr_threads;

};

this can be passed in to sys_async_exec() as a second pointer, and the kernel can put the expected-completion pointer (and the user ring idx pointer) into its struct atom. It's just a few instructions, and only in the cachemiss case.

Quoted 4 lines

that would make completions arbitrarily split-up-able. No registration whatsoever. A waiter could specify which ring's events it is interested in. A 'ring' could be a single-entry thing as well, for a single instance of pending IO.

I like this, too. (Not surprisingly, having outlined something like it in a mail in one of the previous threads :)).

I'll bring up the POSIX AIO "list" IO case. It wants to issue a group of IOs and sleep until they all return. Being able to cheaply instantiate a ring implicitly with the submission of the IO calls in the list will make implementing this almost too easy. It'd obviously just wait for that list's ring to drain.

I hope. There might be complications around the edges (waiting for multiple list IOs to drain?), but it seems like this would be on the right track.

I might be alone in caring about having a less ridiculous POSIX AIO interface in glibc, though, I'll admit. It seems like it'd be a pretty sad missed opportunity if we rolled a fantastic general AIO interface and left glibc to still screw around with it's own manual threading :/.

- z

#249 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 2 lines

- it assumes we are going to make these complex state machines (which I don't believe for a second that a real program will do)

They've not had the chance before and there are certain chains of them which make huge amounts of sense because you don't want to keep taking completion hits. Not so much looping ones but stuff like

cork write sendfile uncork close

are very natural sequences.

There seem to be a lot of typical sequences it doesn't represent however (consider the trivial copy case where you use the result one syscall into the next)

Quoted 4 lines

- it assumes that we're going to make many async system calls that go together (which breaks the whole notion of having different libraries using this for their own internal reasons - they may not even *know* about other libraries that _also_ do async IO for *their* reasons)

They can each register their own async objects. They need to do this anyway so that the libraries can use asynchronous I/O and hide it from applications.

Quoted 3 lines

this "AIO atom" in the first place, WHICH WE KNOW IS INCORRECT, since current users use "aio_read()" that simply doesn't have that and doesn't build up any such data structures.

Do current users do this because that is all they have, because it is hard, or because the current option is all that makes sense ?

The ability to avoid asynchronous completion waits and complete/wake/despatch cycles is a good thing of itself. I don't know if it justifies the rest but it has potential for excellent performance.

Alan

#250 lore raw

Re: [patch 05/11] syslets: core code

Quoted 9 lines · Alan
Quoted 3 lines

this "AIO atom" in the first place, WHICH WE KNOW IS INCORRECT, since current users use "aio_read()" that simply doesn't have that and doesn't build up any such data structures.

Do current users do this because that is all they have, because it is hard, or because the current option is all that makes sense ?

The ability to avoid asynchronous completion waits and complete/wake/despatch cycles is a good thing of itself. [...]

yeah, that's another key thing. I do plan to provide a sys_upcall() syscall as well which calls a 5-parameter user-space function with a special stack. (it's like a lightweight signal/event handler, without any of the signal handler legacies and overhead - it's like a reverse system call - a "user call". Obviously pure userspace would never use sys_upcall(), unless as an act of sheer masochism.)

[ that way say a full HTTP request could be done by an asynchronous
context, because the HTTP parser could be done as a sys_upcall(). ]

so if it's simpler/easier for a syslet to do a step in user-space - as long as it's an 'atom' of processing - it can be done.

or if processing is so heavily in user-space that most of the logic lives there then just use plain pthreads. There's just no point in moving complex user-space code to the syslet side if it's easier/faster to do it in user-space. Syslets are there for asynchronous /kernel/ execution, and is centered around how the kernel does stuff: system calls.

besides sys_upcall() i also plan two other extensions:

- a CLONE_ASYNC_WORKER for user-space to be able use its pthread as an optional worker thread in the async engine. A thread executing user-space code qualifies as a 'busy' thread - it has to call into sys_async_cachemiss_thread() to 'offer' itself as a ready thread that the 'head' could switch to anytime.

- support for multiple heads sharing the async context pool. All the locking logic is there already (because cachemiss threads can already access the queue), it only needs a refcount in struct async_head (only accessed during fork/exit), and an update to the teardown logic (that too is a slowpath).

Ingo

#251 lore raw

Re: [patch 05/11] syslets: core code

Quoted 6 lines · Ingo Molnar

yeah, that's another key thing. I do plan to provide a sys_upcall() syscall as well which calls a 5-parameter user-space function with a special stack. (it's like a lightweight signal/event handler, without any of the signal handler legacies and overhead - it's like a reverse system call - a "user call". Obviously pure userspace would never use sys_upcall(), unless as an act of sheer masochism.)

That is exactly what I described as clets. Instead of having complex jump and condition interpreters on the kernel (on top of new syscalls to modify/increment userspace variables), you just code it in C and you pass the clet pointer to the kernel.
The upcall will setup a frame, execute the clet (where jump/conditions and userspace variable changes happen in machine code - gcc is pretty good in taking care of that for us) on its return, come back through a sys_async_return, and go back to userspace.

- Davide

#252 lore raw

Re: [patch 05/11] syslets: core code

Quoted 9 lines · Davide Libenzi
Quoted 6 lines · Ingo Molnar

yeah, that's another key thing. I do plan to provide a sys_upcall() syscall as well which calls a 5-parameter user-space function with a special stack. (it's like a lightweight signal/event handler, without any of the signal handler legacies and overhead - it's like a reverse system call - a "user call". Obviously pure userspace would never use sys_upcall(), unless as an act of sheer masochism.)

Quoted 8 lines

That is exactly what I described as clets. Instead of having complex jump and condition interpreters on the kernel (on top of new syscalls to modify/increment userspace variables), you just code it in C and you pass the clet pointer to the kernel.
The upcall will setup a frame, execute the clet (where jump/conditions and userspace variable changes happen in machine code - gcc is pretty good in taking care of that for us) on its return, come back through a sys_async_return, and go back to userspace.

So, for example, this is the setup code for the current API (and that's a really simple one - immagine going wacko with loops and userspace varaible changes):

static struct req *alloc_req(void)

{
        /*
         * Constants can be picked up by syslets via static variables:
         */
        static long O_RDONLY_var = O_RDONLY;
        static long FILE_BUF_SIZE_var = FILE_BUF_SIZE;
                
        struct req *req;
                 
        if (freelist) {
                req = freelist;
                freelist = freelist->next_free;
                req->next_free = NULL;
                return req;
        }
                        
        req = calloc(1, sizeof(struct req));
 
        /*
         * This is the first atom in the syslet, it opens the file:
         *
         *  req->fd = open(req->filename, O_RDONLY);
         *
         * It is linked to the next read() atom.
         */
        req->filename_p = req->filename;
        init_atom(req, &req->open_file, __NR_sys_open,
                  &req->filename_p, &O_RDONLY_var, NULL, NULL, NULL, NULL,
                  &req->fd, SYSLET_STOP_ON_NEGATIVE, &req->read_file);
        
        /*
         * This second read() atom is linked back to itself, it skips to
         * the next one on stop:
         */
        req->file_buf_ptr = req->file_buf;
        init_atom(req, &req->read_file, __NR_sys_read,
                  &req->fd, &req->file_buf_ptr, &FILE_BUF_SIZE_var,
                  NULL, NULL, NULL, NULL,
                  SYSLET_STOP_ON_NON_POSITIVE | SYSLET_SKIP_TO_NEXT_ON_STOP,
                  &req->read_file);
                
        /*
         * This close() atom has NULL as next, this finishes the syslet:
         */
        init_atom(req, &req->close_file, __NR_sys_close,
                  &req->fd, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL);
                
        return req;
}

Here's how your clet would look like:

static long main_sync_loop(ctx *c)

{
        int fd;
        char file_buf[FILE_BUF_SIZE+1];
        
        if ((fd = open(c->filename, O_RDONLY)) == -1)
                return -1;
        while (read(fd, file_buf, FILE_BUF_SIZE) > 0)
                ;
        close(fd);
        return 0;
}

Kinda easier to code isn't it? And the cost of the upcall to schedule the clet is widely amortized by the multple syscalls you're going to do inside your clet.

- Davide

#253 lore raw

Re: [patch 05/11] syslets: core code

Hi!

Quoted 78 lines
Quoted 4 lines

The upcall will setup a frame, execute the clet (where jump/conditions and userspace variable changes happen in machine code - gcc is pretty good in taking care of that for us) on its return, come back through a sys_async_return, and go back to userspace.

So, for example, this is the setup code for the current API (and that's a really simple one - immagine going wacko with loops and userspace varaible changes):

static struct req *alloc_req(void)

{
        /*
         * Constants can be picked up by syslets via static variables:
         */
        static long O_RDONLY_var = O_RDONLY;
        static long FILE_BUF_SIZE_var = FILE_BUF_SIZE;
                
        struct req *req;
                 
        if (freelist) {
                req = freelist;
                freelist = freelist->next_free;
                req->next_free = NULL;
                return req;
        }
                        
        req = calloc(1, sizeof(struct req));
 
        /*
         * This is the first atom in the syslet, it opens the file:
         *
         *  req->fd = open(req->filename, O_RDONLY);
         *
         * It is linked to the next read() atom.
         */
        req->filename_p = req->filename;
        init_atom(req, &req->open_file, __NR_sys_open,
                  &req->filename_p, &O_RDONLY_var, NULL, NULL, NULL, NULL,
                  &req->fd, SYSLET_STOP_ON_NEGATIVE, &req->read_file);
        
        /*
         * This second read() atom is linked back to itself, it skips to
         * the next one on stop:
         */
        req->file_buf_ptr = req->file_buf;
        init_atom(req, &req->read_file, __NR_sys_read,
                  &req->fd, &req->file_buf_ptr, &FILE_BUF_SIZE_var,
                  NULL, NULL, NULL, NULL,
                  SYSLET_STOP_ON_NON_POSITIVE | SYSLET_SKIP_TO_NEXT_ON_STOP,
                  &req->read_file);
                
        /*
         * This close() atom has NULL as next, this finishes the syslet:
         */
        init_atom(req, &req->close_file, __NR_sys_close,
                  &req->fd, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL);
                
        return req;
}

Here's how your clet would look like:

static long main_sync_loop(ctx *c)

{
        int fd;
        char file_buf[FILE_BUF_SIZE+1];
        
        if ((fd = open(c->filename, O_RDONLY)) == -1)
                return -1;
        while (read(fd, file_buf, FILE_BUF_SIZE) > 0)
                ;
        close(fd);
        return 0;
}
Quoted 3 lines

Kinda easier to code isn't it? And the cost of the upcall to schedule the clet is widely amortized by the multple syscalls you're going to do inside your clet.

I do not get it. What if clet includes

int *a = 0; *a = 1; /* enjoy your oops, stupid kernel? */

I.e. how do you make sure kernel is protected from malicious clets?

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#254 lore raw

Re: [patch 05/11] syslets: core code

Quoted 82 lines · Pavel Machek
Quoted 78 lines
Quoted 4 lines

The upcall will setup a frame, execute the clet (where jump/conditions and userspace variable changes happen in machine code - gcc is pretty good in taking care of that for us) on its return, come back through a sys_async_return, and go back to userspace.

So, for example, this is the setup code for the current API (and that's a really simple one - immagine going wacko with loops and userspace varaible changes):

static struct req *alloc_req(void)

{
        /*
         * Constants can be picked up by syslets via static variables:
         */
        static long O_RDONLY_var = O_RDONLY;
        static long FILE_BUF_SIZE_var = FILE_BUF_SIZE;
                
        struct req *req;
                 
        if (freelist) {
                req = freelist;
                freelist = freelist->next_free;
                req->next_free = NULL;
                return req;
        }
                        
        req = calloc(1, sizeof(struct req));
 
        /*
         * This is the first atom in the syslet, it opens the file:
         *
         *  req->fd = open(req->filename, O_RDONLY);
         *
         * It is linked to the next read() atom.
         */
        req->filename_p = req->filename;
        init_atom(req, &req->open_file, __NR_sys_open,
                  &req->filename_p, &O_RDONLY_var, NULL, NULL, NULL, NULL,
                  &req->fd, SYSLET_STOP_ON_NEGATIVE, &req->read_file);
        
        /*
         * This second read() atom is linked back to itself, it skips to
         * the next one on stop:
         */
        req->file_buf_ptr = req->file_buf;
        init_atom(req, &req->read_file, __NR_sys_read,
                  &req->fd, &req->file_buf_ptr, &FILE_BUF_SIZE_var,
                  NULL, NULL, NULL, NULL,
                  SYSLET_STOP_ON_NON_POSITIVE | SYSLET_SKIP_TO_NEXT_ON_STOP,
                  &req->read_file);
                
        /*
         * This close() atom has NULL as next, this finishes the syslet:
         */
        init_atom(req, &req->close_file, __NR_sys_close,
                  &req->fd, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL);
                
        return req;
}

Here's how your clet would look like:

static long main_sync_loop(ctx *c)

{
        int fd;
        char file_buf[FILE_BUF_SIZE+1];
        
        if ((fd = open(c->filename, O_RDONLY)) == -1)
                return -1;
        while (read(fd, file_buf, FILE_BUF_SIZE) > 0)
                ;
        close(fd);
        return 0;
}
Quoted 3 lines

Kinda easier to code isn't it? And the cost of the upcall to schedule the clet is widely amortized by the multple syscalls you're going to do inside your clet.

Quoted 5 lines

I do not get it. What if clet includes

int *a = 0; *a = 1; /* enjoy your oops, stupid kernel? */

I.e. how do you make sure kernel is protected from malicious clets?

Clets would execute in userspace, like signal handlers, but under the special schedule() handler. In that way chains happens by the mean of natural C code, and access to userspace variables happen by the mean of natural C code too (not with special syscalls to manipulate userspace memory). I'm not a big fan of chains of syscalls for the reasons I already explained, but at least clets (or whatever name) has a way lower cost for the programmer (easier to code than atom chains), and for the kernel (no need of all that atom handling stuff, no need of limited cond/jump interpreters in the kernel, and no need of nightmare compat code).

- Davide

#255 lore raw

Re: [patch 05/11] syslets: core code

Quoted 1 line · Davide Libenzi

Clets would execute in userspace, like signal handlers,

or like "event handlers" in cooperative multitasking environments without the Unix baggage

Quoted 1 line

but under the special schedule() handler.

or, better yet, as the next tasklet in the chain after the softirq dispatcher, since I/Os almost always unblock as a result of something that happens in an ISR or softirq

Quoted 4 lines

In that way chains happens by the mean of
natural C code, and access to userspace variables happen by the mean of natural C code too (not with special syscalls to manipulate userspace memory).

yep. That way you can exploit this nice hardware block called an MMU.

Quoted 2 lines

I'm not a big fan of chains of syscalls for the reasons I already explained,

to a kernel programmer, all userspace programs are chains of syscalls. :-)

Quoted 2 lines

but at least clets (or whatever name) has a way lower cost for the programmer (easier to code than atom chains),

except you still have the 80% of the code that is half-assed exception handling using overloaded semantics on function return values and a thread-local errno, which is totally unsafe with fibrils, syslets, clets, and giblets, since none of them promise to run continuations in the same thread context as the submission. Of course you aren't going to use errno as such, but that means that async-ifying code isn't s/syscall/aio_syscall/, it's a complete rewrite. If you're going to design a new AIO interface, please model it after the only standard that has ever made deeply pipelined, massively parallel execution programmer-friendly -- IEEE 754.

Quoted 1 line

and for the kernel (no need of all that atom handling stuff,

you still need this, but it has to be centered on a data structure that makes request throttling, dynamic reprioritization, and bulk cancellation practical

Quoted 1 line

no need of limited cond/jump interpreters in the kernel,

you still need this, for efficient handling of speculative execution, pipeline stalls, and exception propagation, but it's invisible to the interface and you don't have to invent it up front

Quoted 1 line

and no need of nightmare compat code).

compat code, yes. nighmare, no. Just like kernel FP emulation on any processor other than an x86. Unimplemented instruction traps. x86 is so utterly the wrong architecture on which to prototype this it isn't even funny.

Cheers,
- Michael

#256 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 4 lines · Linus Torvalds

- it fundamentally is based on a broken notion that everything would use this "AIO atom" in the first place, WHICH WE KNOW IS INCORRECT, since current users use "aio_read()" that simply doesn't have that and doesn't build up any such data structures.

i'm not sure what you mean here either - aio_read()/write()/etc. could very much be implemented using syslets - and in fact one goal of syslets is to enable such use. struct aiocb is mostly shaped by glibc internals, and it currently has 32 bytes of free space. Enough to put a single atom there. (or a pointer to an atom)

Ingo

#257 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 22 lines · Linus Torvalds
Quoted 2 lines · Ingo Molnar

the core syslet / async system calls infrastructure code.

Ok, having now looked at it more, I can say:

- I hate it.

I dislike it intensely, because it's so _close_ to being usable. But the programming interface looks absolutely horrid for any "casual" use, and while the loops etc look like fun, I think they are likely to be less than useful in practice. Yeah, you can do the "setup and teardown" just once, but it ends up being "once per user", and it ends up being a lot of stuff to do for somebody who wants to just do some simple async stuff.

And the whole "lock things down in memory" approach is bad. It's doing expensive things like mlock(), making the overhead for _single_ system calls much more expensive. Since I don't actually believe that the non-single case is even all that interesting, I really don't like it.

Quoted 4 lines

I think it's clever and potentially useful to allow user mode to see the data structures (and even allow user mode to *modify* them) while the async thing is running, but it really seems to be a case of excessive cleverness.

Ok, that makes the wierdo-count up to two :) I agree with you that the chained API can be improved at least.

- Davide

#258 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 19 lines · Linus Torvalds

Or how would you do the trivial example loop that I explained was a good idea:

        struct one_entry *prev = NULL;
        struct dirent *de;

        while ((de = readdir(dir)) != NULL) {
                struct one_entry *entry = malloc(..);

                /* Add it to the list, fill in the name */
                entry->next = prev;
                prev = entry;
                strcpy(entry->name, de->d_name);

                /* Do the stat lookup async */
                async_stat(de->d_name, &entry->stat_buf);
        }
        wait_for_async();
        .. Ta-daa! All done ..

i think you are banging on open doors. That async_stat() call is very much what i'd like to see glibc to provide, not really the raw syslet interface. Nor do i want to see raw syscalls exposed to applications. Plus the single-atom thing is what i think will be used mostly initially, so all my optimizations went into that case.

while i agree with you that state machines are hard, it's all a function of where the concentration of processing is. If most of the application complexity happens in user-space, then the logic should live there. But for infrastructure things (like the async_stat() calls, or aio_read(), or other, future interfaces) i wouldnt mind at all if they were implemented using syslets. Likewise, if someone wants to implement the hottest accept loop in Apache or Samba via syslets, keeping them from wasting time on writing in-kernel webservers (oops, did i really say that?), it can be done. If a JVM wants to use syslets, sure - it's an abstraction machine anyway so application programmers are not exposed to it.

syslets are just a realization that /if/ the thing we want to do is mostly on the kernel side, then we might as well put the logic to the kernel side. It's more of a 'compound interface builder' than the place for real program logic. It makes our interfaces usable more flexibly, and it allows the kernel to provide 'atomic' APIs, instead of having to provide the most common compounded uses as well.

and note that if you actually try to do an async_stat() sanely, you do get quite close to the point of having syslets. You get basically up to a one-shot atom concept and 90% of what i have in kernel/async.c. The remaining 10% of further execution control is easy and still it opens up these new things that were not possible before: compounding, vectoring, simple program logic, etc.

The 'cost' of syslets is mostly the atom->next pointer in essence. The whole async infrastructure only takes up 20 nsecs more in the cached case. (but with some crazier hacks i got the one-shot atom overhead [compared to a simple synchronous null syscall] to below 10 nsecs, so there's room in there for further optimizations. Our current null syscall latency is around ~150 nsecs.)

Ingo

#259 lore raw

Re: [patch 05/11] syslets: core code

Quoted 4 lines · Ingo Molnar

i think you are banging on open doors. That async_stat() call is very much what i'd like to see glibc to provide, not really the raw syslet interface.

Right. Which is why I wrote (and you removed) the rest of my email.

If the "raw" interfaces aren't actually what you use, and you just expect glibc to translate things into them, WHY DO WE HAVE THEM AT ALL?

Quoted 1 line

The 'cost' of syslets is mostly the atom->next pointer in essence.

No. The cost is:

- indirect interfaces are harder to follow and debug. It's a LOT easier to debug things that go wrong when it just does what you ask it for, instead of writing to memory and doing something totally obscure.

I don't know about you, but I use "strace" a lot. That's the kind of cost we have.

- the cost is the extra and totally unnecessary setup for the indirection, that nobody reallyis likely to use.

Quoted 5 lines

The whole async infrastructure only takes up 20 nsecs more in the cached case. (but with some crazier hacks i got the one-shot atom overhead [compared to a simple synchronous null syscall] to below 10 nsecs, so there's room in there for further optimizations. Our current null syscall latency is around ~150 nsecs.)

You are not counting the whole setup cost there, then, because your setup cost is going to be at a minimum more expensive than the null system call.

And yes, for benchmarks, it's going to be done just once, and then the benchmark will loop a million times. But for other things like libraries, that don't know whether they get called once, or a million times, this is a big deal.

This is why I'd like a "async_stat()" to basically be the *same* cost as a "stat()". To within nanoseconds. WITH ALL THE SETUP! Because otherwise, a library may not be able to use it without thinking about it a lot, because it simply doesn't know whether the caller is going to call it once or many times.

THIS was why I wanted the "synchronous mode". Exactly because it removes all the questions about "is it worth it". If the cost overhead is basically zero, you know it's always worth it.

Now, if you make the "async_submit()" _incldue_ the setup itself (as you alluded to in one of your emails), and the cost of that is basically negligible, and it still allows people to do things simply and just submit a single system call without any real overhead, then hey, it's may be a complex interface, but at least you can _use_ it as a simple one.

At that point most of my arguments against it go away. It might still be over-engineered, but if the costs aren't visible, and it's obvious enough that the over-engineering doesn't result in subtle bugs, THEN (and only then) is a more complex and generic interface worth it even if nobody actually ends up using it.

Linus

#260 lore raw

Re: [patch 05/11] syslets: core code

Quoted 8 lines · Linus Torvalds
Quoted 4 lines

case. (but with some crazier hacks i got the one-shot atom overhead [compared to a simple synchronous null syscall] to below 10 nsecs, so there's room in there for further optimizations. Our current null syscall latency is around ~150 nsecs.)

You are not counting the whole setup cost there, then, because your setup cost is going to be at a minimum more expensive than the null system call.

hm, this one-time cost was never on my radar. [ It's really dwarved by other startup costs (a single fork() takes 100 usecs, an exec() takes 800 usecs.) ]

In any case, we can delay this cost into the first cachemiss, or can eliminate it by making it a globally pooled thing. It does not seem like a big issue.

Ingo

#261 lore raw

Re: [patch 05/11] syslets: core code

Quoted 7 lines · Ingo Molnar
Quoted 3 lines

You are not counting the whole setup cost there, then, because your setup cost is going to be at a minimum more expensive than the null system call.

hm, this one-time cost was never on my radar. [ It's really dwarved by other startup costs (a single fork() takes 100 usecs, an exec() takes 800 usecs.) ]

i really count this into the category of 'application startup', and thus it's is another type of 'cachemiss': the cost of having to bootstrap a new context. (Even though obviously we want this to go as fast as possible too.) Library startups, linking (even with prelink), etc., is quite expensive already - goes into the tens of milliseconds.

or if it's a new thread startup - where this cost would indeed be visible, if the thread exits straight after being startup up, and where this thread would want to do a single AIO, then shareable async heads (see my mail to Alan) ought to solve this. (But short-lifetime threads are not really a good idea in themselves.)

but the moment it's some fork()ed context, or even an exec()ed context, this cost is very small in comparisno. And who in their right mind starts up a whole new process just to do a single IO and then exit without doing any other processing? (so that the async setup cost would show up)

but, short-lived contexts, where this cost would be visible, are generally a really bad idea.

Ingo

#262 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 27 lines · Linus Torvalds

Or how would you do the trivial example loop that I explained was a good idea:

        struct one_entry *prev = NULL;
        struct dirent *de;

        while ((de = readdir(dir)) != NULL) {
                struct one_entry *entry = malloc(..);

                /* Add it to the list, fill in the name */
                entry->next = prev;
                prev = entry;
                strcpy(entry->name, de->d_name);

                /* Do the stat lookup async */
                async_stat(de->d_name, &entry->stat_buf);
        }
        wait_for_async();
        .. Ta-daa! All done ..

Notice? This also "chains system calls together", but it does it using a *much* more powerful entity called "user space". That's what user space is. And yeah, it's a pretty complex sequencer, but happily we have hardware support for accelerating it to the point that the kernel never even needs to care.

Quoted 3 lines

The above is a *realistic* schenario, where you actually have things like memory allocation etc going on. In contrast, just chaining system calls together isn't a realistic schenario at all.

One can still perfectly fine and easily use sys_async_exec(...stat()...) in above scenario. Although I do think that having a web server in kernel is overkill, having a proper state machine for good async processing is a must.
Not that I agree, that it should be done on top of syscalls as basic elements, but it is an initial state.

Quoted 10 lines

So I think we have one _known_ usage schenario:

- replacing the _existing_ aio_read() etc system calls (with not just existing semantics, but actually binary-compatible)

- simple code use where people are willing to perhaps do something Linux-specific, but because it's so _simple_, they'll do it.

In neither case does the "chaining atoms together" seem to really solve the problem. It's clever, but it's not what people would actually do.

It is an example of what can be done. If one do not like it - do not use it. State machine is implemented in sendfile() syscall - and although it is not a good idea to have async sendfile as is in micro-thread design (due to network blocking and small per-page reading), it is still a state machine, which can be used with syslet state machine (if it could be extended).

Quoted 10 lines

And yes, you can hide things like that behind an abstraction library, but once you start doing that, I've got three questions for you:

- what's the point?
- we're adding overhead, so how are we getting it back
- how do we handle independent libraries each doing their own thing and version skew between them?

In other words, the "let user space sort out the complexity" is not a good answer. It just means that the interface is badly designed.

Well, if we can setup iocb structure, why we can not setup syslet one?

Yes, with syscalls as a state machine elements 99% of users will not use it (I can only think about proper fadvice()+read()/sendfile() states), but there is no problem to setup a structure in userspace at all. And if there is possibility to use it for other things, it is definitely a win.

Actually complex structure setup argument is stupid - everyone forces to have timeval structure instead of number of microseconds.

So there is no point in 'complex setup and overhead', but there is a. limit of the AIO (although my point is not to have huge amount of
working threads - they were created by people who can not program state machines (c) Alan Cox)
b. possibility to implement a state machine (in current form likely will
not be used except maybe some optional hints for IO tasks like fadvice)
c. in all other ways it has all pros and cons of micro-thread design (it

	looks neat and simple, although is utterly broken in some usage
	cases).
Quoted 1 line

Linus

Signature · 1 line
	Evgeniy Polyakov
#263 lore raw

Re: [patch 05/11] syslets: core code

Quoted 5 lines · Evgeniy Polyakov
Quoted 3 lines

In other words, the "let user space sort out the complexity" is not a good answer. It just means that the interface is badly designed.

Well, if we can setup iocb structure, why we can not setup syslet one?

(I'm cutting wildly, to try to get to the part I wanted to answer)

I actually think aio_read/write() and friends are *horrible* interfaces.

Here's a quick question: how many people have actually ever seen them used in "normal code"?

Yeah. Nobody uses them. They're not all that portable (even within unixes they aren't always there, much less in other places), they are fairly obscure, and they are just not really easy to use.

Guess what? The same is going to be true *in*spades* for any Linux- specific async system call thing.

This is why I think simplicity of use along with transparency, is so important. I think "aio_read()" is already a nasty enough interface, and it's sure more portable than any Linux-specific extension will be (only until the revolution comes, of course - at that point, everybody who doesn't use Linux will be up against the wall, so we can solve the problem that way).

So a Linux-specific extension needs to be *easier* to use or at least understand, and have *more* obvious upsides than "aio_read()" has. Otherwise, it's pointless - nobody is really going to use it.

This is why I think the main goals should be:

- the *internal* kernel goal of trying to replace some very specific aio_read() etc code with somethign more maintainable.

This is really a maintainability argument, nothing more. Even if we never do anything *but* aio_read() and friends, if we can avoid having the VFS code have multiple code-paths and explicit checks for AIO, and instead handle it more "automatically", I think it is already worth it.

- add extensions that people *actually*can*use* in practice.

And this is where "simple interfaces" comes in.

Quoted 10 lines

So there is no point in 'complex setup and overhead', but there is a. limit of the AIO (although my point is not to have huge amount of
working threads - they were created by people who can not program state machines (c) Alan Cox)
b. possibility to implement a state machine (in current form likely will
not be used except maybe some optional hints for IO tasks like fadvice)
c. in all other ways it has all pros and cons of micro-thread design (it

	looks neat and simple, although is utterly broken in some usage
	cases).

I don't think the "atom" approach is bad per se. I think it could be fine to have some state information in user space. It's just that I think complex interfaces that people largely won't even use is a big mistake. We should concentrate on usability first, and some excessive cleverness really isn't a big advantage.

Being able to do a "open + stat" looks like a fine thing. But I doubt you'll see a lot of other combinations.

Linus

#264 lore raw

Re: [patch 05/11] syslets: core code

Quoted 11 lines · Linus Torvalds
Quoted 4 lines
Quoted 2 lines

In other words, the "let user space sort out the complexity" is not a good answer. It just means that the interface is badly designed.

Well, if we can setup iocb structure, why we can not setup syslet one?

(I'm cutting wildly, to try to get to the part I wanted to answer)

I actually think aio_read/write() and friends are *horrible* interfaces.

Here's a quick question: how many people have actually ever seen them used in "normal code"?

Agree, existing AIO interface is far from ideal IMO, but it is used. No matter if it is normal or not, AIO itself is not normal interface - there are no books about POSIX AIO, so no one knows about AIO at all.

Quoted 14 lines

Yeah. Nobody uses them. They're not all that portable (even within unixes they aren't always there, much less in other places), they are fairly obscure, and they are just not really easy to use.

Guess what? The same is going to be true *in*spades* for any Linux- specific async system call thing.

This is why I think simplicity of use along with transparency, is so important. I think "aio_read()" is already a nasty enough interface, and it's sure more portable than any Linux-specific extension will be (only until the revolution comes, of course - at that point, everybody who doesn't use Linux will be up against the wall, so we can solve the problem that way).

Quoted 3 lines

So a Linux-specific extension needs to be *easier* to use or at least understand, and have *more* obvious upsides than "aio_read()" has. Otherwise, it's pointless - nobody is really going to use it.

Userspace_API_is_the_ever_possible_last_thing_to_ever_think_about. Period . // <- wrapped one

If system is designed that with API changes it breaks - that system sucks wildly and should be thrown away. Syslets do not suffer from that.

We can have tons of interfaces any alien would be happy with (imho it is not even kernel's task at all) - new table of syscalls the way usual ones are used for example.
And we will have async_stat() exactly the same way people would use that.

It is not even a thing to discus. There are other technical issues with syslets yet to resolve. If people do happy with design of the system, it is time to think about how it will look from user's point of view.

syslet(__NR_stat) -> async_stat() - say it and Ingo and other developers will think about how to implement that or start to discuss that it is bad interface, and instead something else should be invented.

If interface sucks and _interface_ must be changed/extended/replaced. If overall design sucks and it must be changed.

Solve problems one-by-one, not throwing something just because it uses wild interface which can be changed in a minute.

Quoted 5 lines

This is why I think the main goals should be:

- the *internal* kernel goal of trying to replace some very specific aio_read() etc code with somethign more maintainable.

Quoted 8 lines

This is really a maintainability argument, nothing more. Even if we never do anything *but* aio_read() and friends, if we can avoid having the VFS code have multiple code-paths and explicit checks for AIO, and instead handle it more "automatically", I think it is already worth it.

- add extensions that people *actually*can*use* in practice.

And this is where "simple interfaces" comes in.

There is absolutely _NO_ problem in having any interface people will use. What one will you?
async_stat() instead of syslet(complex_struct_blah_sync)?
No problem - it is _really_ trivial to implement.
Ingo mentioned that it should be done, and it is really simple task for glibc just like it is done for usual syscalls, which has completely nothing with overall system design at all.

Quoted 11 lines
Quoted 10 lines

So there is no point in 'complex setup and overhead', but there is a. limit of the AIO (although my point is not to have huge amount of
working threads - they were created by people who can not program state machines (c) Alan Cox)
b. possibility to implement a state machine (in current form likely will
not be used except maybe some optional hints for IO tasks like fadvice)
c. in all other ways it has all pros and cons of micro-thread design (it

	looks neat and simple, although is utterly broken in some usage
	cases).
Quoted 8 lines

I don't think the "atom" approach is bad per se. I think it could be fine to have some state information in user space. It's just that I think complex interfaces that people largely won't even use is a big mistake. We should concentrate on usability first, and some excessive cleverness really isn't a big advantage.

Being able to do a "open + stat" looks like a fine thing. But I doubt you'll see a lot of other combinations.

Then no problem.

Interface does suck, especially since it does not allow to forbid some syscalls from async execution, but it is just a brick which allows to build really good system of it.

I personally vote for table of async syscalls transferred into human-readable aliases like async_stat() and the like.

Quoted 1 line

Linus

Signature · 1 line
	Evgeniy Polyakov
#265 lore raw

Re: [patch 05/11] syslets: core code

Quoted 3 lines · Evgeniy Polyakov

Userspace_API_is_the_ever_possible_last_thing_to_ever_think_about. Period . // <- wrapped one

No, I really think you're wrong.

In many ways, the interfaces and especially data structures are *more* important than the code.

The code we can fix. The interfaces, on the other hand, we'll have to live with forever.

So complex interfaces that expose lots of implementation detail are not a good thing, and it's _not_ the last thing you want to think about. Complex interfaces with a lot of semantic knowledge seriously limit how you can fix things up later.

In contrast, simple interfaces that have clear and unambiguous semantics and that can be explained at a conceptual level are things that you can often implement in many different ways. So the interface isn't the bottle
neck: you may have to have a "backwards compatibility layer" for it

Quoted 2 lines

If system is designed that with API changes it breaks - that system sucks wildly and should be thrown away. Syslets do not suffer from that.

The syslet code itself looks fine. It's the user-visible part I'm not convinced about.

I'm just saying: how would use use this for existing programs?

For something this machine-specific, you're not going to have any big project written around the "async atom" code. So realistically, the kinds of usage we'd see is likely some compile-time configuration option, where people replace some specific sequence of code with another one. THAT is what we should aim to make easy and flexible, I think. And that is where interfaces really are as important as code.

We know one interface: the current aio_read() one. Nobody really _likes_ it (even database people would apparently like to extend it), but it has the huge advantage of "being there", and having real programs that really care that use it today.

Others? We don't know yet. And exposing complex interfaces that may not be the right ones is much *worse* than exposing simple interfaces (that _also_ may not be the right ones, of course - but simple and straightforward interfaces with obvious and not-very-complex semantics are a lot easier to write compatibility layers for if the internal code changes radically)

Linus

#266 lore raw

Re: [patch 05/11] syslets: core code

Quoted 20 lines · Linus Torvalds
Quoted 3 lines · Evgeniy Polyakov

Userspace_API_is_the_ever_possible_last_thing_to_ever_think_about. Period . // <- wrapped one

No, I really think you're wrong.

In many ways, the interfaces and especially data structures are *more* important than the code.

The code we can fix. The interfaces, on the other hand, we'll have to live with forever.

So complex interfaces that expose lots of implementation detail are not a good thing, and it's _not_ the last thing you want to think about. Complex interfaces with a lot of semantic knowledge seriously limit how you can fix things up later.

Quoted 4 lines

In contrast, simple interfaces that have clear and unambiguous semantics and that can be explained at a conceptual level are things that you can often implement in many different ways. So the interface isn't the bottle
neck: you may have to have a "backwards compatibility layer" for it

That's exaclt the way we should discuss it - you do ont like that interface, but Ingo proposed a way to change that via table of async syscalls - people asks, people answers - so eventually interface and (if any) other problems got resolved.

Quoted 20 lines
Quoted 2 lines

If system is designed that with API changes it breaks - that system sucks wildly and should be thrown away. Syslets do not suffer from that.

The syslet code itself looks fine. It's the user-visible part I'm not convinced about.

I'm just saying: how would use use this for existing programs?

For something this machine-specific, you're not going to have any big project written around the "async atom" code. So realistically, the kinds of usage we'd see is likely some compile-time configuration option, where people replace some specific sequence of code with another one. THAT is what we should aim to make easy and flexible, I think. And that is where interfaces really are as important as code.

We know one interface: the current aio_read() one. Nobody really _likes_ it (even database people would apparently like to extend it), but it has the huge advantage of "being there", and having real programs that really care that use it today.

Quoted 6 lines

Others? We don't know yet. And exposing complex interfaces that may not be the right ones is much *worse* than exposing simple interfaces (that _also_ may not be the right ones, of course - but simple and straightforward interfaces with obvious and not-very-complex semantics are a lot easier to write compatibility layers for if the internal code changes radically)

So we just need to describe the way we want to see new interface - that's it.

Here is a stub for async_stat() - probably broken example, but that does not matter - this interface is really easy to change.

static void syslet_setup(struct syslet *s, int nr, void *arg1...)

{
	s->flags = ...
	s->arg[1] = arg1;
	....
}

long glibc_async_stat(const char *path, struct stat *buf)

{
	/* What about making syslet and/or set of atoms per thread and preallocate 
	 * them when working threads are allocated? */
	struct syslet s;
	syslet_setup(&s, __NR_stat, path, buf, NULL, NULL, NULL, NULL);
	return async_submit(&s);
}
Quoted 1 line

Linus

Signature · 1 line
	Evgeniy Polyakov
#267 lore raw

Re: [patch 05/11] syslets: core code

Quoted 3 lines · Evgeniy Polyakov

So we just need to describe the way we want to see new interface - that's it.

Agreed. Absolutely.

But please keep the kernel interface as part of that. Not just a strange and complex kernel interface and then _usable_ library interfaces that use the strange and complex one internally. Because if the complex one has no validity on its own, it's just (a) a bitch to debug and (b) if we ever change any details inside the kernel we'll end up with a lot of subtle code where user land creates complex data, and the kernel just reads it, and both just (unnecessarily) work around the fact that the other doesn't do the straightforward thing.

Quoted 10 lines

Here is a stub for async_stat() - probably broken example, but that does not matter - this interface is really easy to change.

static void syslet_setup(struct syslet *s, int nr, void *arg1...)

{
	s->flags = ...
	s->arg[1] = arg1;
	....
}
Quoted 8 lines

long glibc_async_stat(const char *path, struct stat *buf)

{
	/* What about making syslet and/or set of atoms per thread and preallocate 
	 * them when working threads are allocated? */
	struct syslet s;
	syslet_setup(&s, __NR_stat, path, buf, NULL, NULL, NULL, NULL);
	return async_submit(&s);
}

And this is a classic example of potentially totally buggy code.

Why? You're releasing the automatic variable on the stack before it's necessarily all used!

So now you need to do a _longterm_ allocation, and that in turn means that you need to do a long-term de-allocation!

Ok, so do we make the rule be that all atoms *have* to be read fully before we start the async submission (so that the caller doesn't need to do a long-term allocation)?

Or do we make the rule be that just the *first* atom is copied by the kernel before the async_sumbit() returns, and thus it's ok to do the above *IFF* you only have a single system call?

See? The example you tried to use to show how "simple" the interface iswas actually EXACTLY THE REVERSE. It shows how subtle bugs can creep in!

Linus

#268 lore raw

Re: [patch 05/11] syslets: core code

Quoted 28 lines · Linus Torvalds
Quoted 7 lines

static void syslet_setup(struct syslet *s, int nr, void *arg1...)

{
	s->flags = ...
	s->arg[1] = arg1;
	....
}
Quoted 8 lines

long glibc_async_stat(const char *path, struct stat *buf)

{
	/* What about making syslet and/or set of atoms per thread and preallocate 
	 * them when working threads are allocated? */
	struct syslet s;
	syslet_setup(&s, __NR_stat, path, buf, NULL, NULL, NULL, NULL);
	return async_submit(&s);
}

And this is a classic example of potentially totally buggy code.

Why? You're releasing the automatic variable on the stack before it's necessarily all used!

So now you need to do a _longterm_ allocation, and that in turn means that you need to do a long-term de-allocation!

Ok, so do we make the rule be that all atoms *have* to be read fully before we start the async submission (so that the caller doesn't need to do a long-term allocation)?

Quoted 6 lines

Or do we make the rule be that just the *first* atom is copied by the kernel before the async_sumbit() returns, and thus it's ok to do the above *IFF* you only have a single system call?

See? The example you tried to use to show how "simple" the interface iswas actually EXACTLY THE REVERSE. It shows how subtle bugs can creep in!

So describe what are the requirements (constraints)?

Above example has exactly one syscall in the chain, so it is ok, but generally it is not correct.

So instead there will be
s = atom_create_and_add(__NR_stat, path, stat, NULL, NULL, NULL, NULL); atom then can be freed in the glibc_async_wait() wrapper just before returning data to userspace.

There are millions of possible ways to do that, but what exactly one should be used from your point of view? Describe _your_ vision of that path.

Currently generic example is following:
allocate mem
setup complex structure
submit syscall
wait syscall
free mem

the first two can be hidden in glibc setup/startup code, the last one - in waiting or cleanup entry.

Or it can be this one (just an idea):

glibc_async_stat(path, &stat);

int glibc_async_stat(char *path, struct stat *stat)

{
	struct pthread *p;

	asm ("movl %%gs:0, %0", "=r"(unsigned long)(p));

	atom = allocate_new_atom_and_setup_initial_values();
	setup_atom(atom, __NR_stat, path, stat, ...);
	add_atom_into_private_tree(p, atom);
	return async_submit(atom);
}

glibc_async_wait()

{
	struct pthread *p;

	asm ("movl %%gs:0, %0", "=r"(unsigned long)(p));
	
	cookie = sys_async_wait();
	atom = search_for_cookie_and_remove(p);
	free_atom(atom);
}

Although that cruft might need to be extended...

So, describe how exactly _you_ think it should be implemented with its pros and cons, so that systemn could be adopted without trying to mind-read of what is simple and good or complex and really bad.

Quoted 1 line

Linus

Signature · 1 line
	Evgeniy Polyakov
#269 lore raw

Re: [patch 05/11] syslets: core code

Quoted 8 lines · Evgeniy Polyakov
Quoted 3 lines

See? The example you tried to use to show how "simple" the interface iswas actually EXACTLY THE REVERSE. It shows how subtle bugs can creep in!

So describe what are the requirements (constraints)?

Above example has exactly one syscall in the chain, so it is ok, but generally it is not correct.

Well, it *could* be correct. It depends on the semantics of the atom fetching. If we make the semantics be that the first atom is fetched entirely synchronously, then we could make the rule be that single-syscall async things can do their job with a temporary allocation.

So that wasn't my point. My point was that a complicated interface that uses indirection actually has subtle issues. You *thought* you were doing something simple, and you didn't even realize the subtle assumptions you made.

THAT was the point. Interfaces are really really subtle and important. It's absolutely not a case of "we can just write wrappers to fix up any library issues".

Quoted 4 lines

So instead there will be
s = atom_create_and_add(__NR_stat, path, stat, NULL, NULL, NULL, NULL); atom then can be freed in the glibc_async_wait() wrapper just before returning data to userspace.

So now you add some kind of allocation/dealloction thing. In user space or in the kernel?

Quoted 2 lines

There are millions of possible ways to do that, but what exactly one should be used from your point of view? Describe _your_ vision of that path.

My vision is that we should be able to do the simple things *easily* and without any extra overhead.

And doing wrappers in user space is almost entirely unacceptable, becasue a lot of the wrapping needs to be done at release time (for example:
de-allocating memory), and that means that you no longer can do simple system calls that don't even need release notification AT ALL.

Quoted 6 lines

Currently generic example is following:
allocate mem
setup complex structure
submit syscall
wait syscall
free mem

And that "allocate mem" and "free mem" is a problem. It's not just a performance problem, it is a _complexity_ problem. It means that people have to track things that they are NOT AT ALL INTERESTED IN!

Quoted 3 lines

So, describe how exactly _you_ think it should be implemented with its pros and cons, so that systemn could be adopted without trying to mind-read of what is simple and good or complex and really bad.

So I think that a good implementation just does everything up-front, and doesn't _need_ a user buffer that is live over longer periods, except for the actual results. Exactly because the whole alloc/teardown is nasty.

And I think a good implementation doesn't need wrapping in user space to be useful - at *least* not wrapping at completion time, which is the really difficult one (since, by definition, in an async world completion is separated from the initial submit() event, and with kernel-only threads you actually want to *avoid* having to do user code after the operation completed).

I suspect Ingo's thing can do that. But I also suspect (nay, _know_, from this discussion), that you didn't even think of the problems.

Linus

#270 lore raw

Re: [patch 05/11] syslets: core code

Quoted 4 lines · Linus Torvalds

So I think that a good implementation just does everything up-front, and doesn't _need_ a user buffer that is live over longer periods, except for the actual results. Exactly because the whole alloc/teardown is nasty.

Btw, this doesn't necessarily mean "not supporting multiple atoms at all".

I think the batching of async things is potentially a great idea. I think it's quite workable for "open+fstat" kind of things, and I agree that it can solve other things too (the "socket+bind+connect+sendmsg+rcv" kind of complex setup things).

But I suspect that if we just said:
- we limit these atom sequences to just linear sequences of max "n" ops
- we read them all in in a single go at startup

we actually avoid several nasty issues. Not just the memory allocation issue in user space (now it's perfectly ok to build up a sequence of ops in temporary memory and throw it away once it's been submitted), but also issues like the 32-bit vs 64-bit compatibility stuff (the compat handlers would just convert it when they do the initial copying, and then the actual run-time wouldn't care about user-level pointers having different sizes etc).

Would it make the interface less cool? Yeah. Would it limit it to just a few linked system calls (to avoid memory allocation issues in the kernel)? Yes again. But it would simplify a lot of the interface issues.

It would _also_ allow the "sys_aio_read()" function to build up its *own* set of atoms in kernel space to actually do the read, and there would be no impact of the actual run-time wanting to read stuff from user space. Again - it's actually the same issue as with the compat system
call: by making the interfaces do things up-front rather than dynamically, it becomes more static, but also easier to do interface translations. You can translate into any arbitrary internal format _once_, and be done with it.

I dunno.

Linus

#271 lore raw

Re: [patch 05/11] syslets: core code

Quoted 8 lines · Linus Torvalds
Quoted 4 lines · Linus Torvalds

So I think that a good implementation just does everything up-front, and doesn't _need_ a user buffer that is live over longer periods, except for the actual results. Exactly because the whole alloc/teardown is nasty.

Quoted 6 lines

Btw, this doesn't necessarily mean "not supporting multiple atoms at all".

I think the batching of async things is potentially a great idea. I think it's quite workable for "open+fstat" kind of things, and I agree that it can solve other things too (the "socket+bind+connect+sendmsg+rcv" kind of complex setup things).

If you *really* want to allow chains (note that the above could be prolly be hosted on a real thread, once chains becomes that long), then try to build that chain with the current API, and compare it with:

long my_clet(ctx *c) {
	int fd, error = -1;

	if ((fd = socket(...)) == -1 ||
	    bind(fd, &c->laddr, sizeof(c->laddr)) ||
	    connect(fd, &c->saddr, sizeof(c->saddr)) ||
	    sendmsg(fd, ...) == -1 ||
	    recv(fd, ...) <= 0)
		goto
	error = 0;
erxit:
	close(fd);
	return error;
}

Points:

- Keep the submission API to submit one or an array of parallel async syscalls/clets

- Keep arguments of the syscall being longs (no need for extra pointer indirection compat code, and special copy_atoms functions)

- No need for the "next" atom pointer chaining (nice for compat too)

- No need to create special conditions/jump interpreters into the kernel (nice for compat and emulators). C->machine-code that that for us

- Easier to code. Try to build a chain like that with the current API and you will see what I saying

- Did I say faster? Machine code is faster than sudo-VM interpretation of jumps/conditions done inside the kernel

- Davide

#272 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

Would it make the interface less cool? Yeah. Would it limit it to just a few linked system calls (to avoid memory allocation issues in the kernel)? Yes again. But it would simplify a lot of the interface issues.

Only in toy applications. Real userspace code that lives between networks+disks and impatient humans is 80% exception handling, logging, and diagnostics. If you can't do any of that between stages of an async syscall chain, you're fscked when it comes to performance analysis (the "which 10% of the traffic do we not abort under pressure" kind, not the "cut overhead by 50%" kind). Not that real userspace code could get any simpler by using this facility anyway, since you can't jump the queue, cancel in bulk, or add cleanup hooks.

Efficiently interleaved execution of high-latency I/O chains would be nice. Low overhead for cache hits would be nicer. But least for the workloads that interest me, neither is anywhere near as important as the ability to say, "This 10% (or 90%) of my requests are going to take forever? Nevermind -- but don't cancel the 1% I can't do without."

This is not a scheduling problem, it is a caching problem. Caches are data structures, not thread pools. Assume that you need to design for dynamic reprioritization, speculative fetch, and opportunistic flush, even if you don't implement them at first. Above all, stay out of the way when a synchronous request misses cache -- and when application code decides that a bunch of its outstanding requests are no longer interesting, take the hint!

Oh, and while you're at it: I'd like to program AIO facilities using a C compiler with an explicitly parallel construct -- something along the lines of:

try (my_aio_batch, initial_priority, ...) {
} catch {
} finally {
}

Naturally the compiler will know how to convert synchronous syscalls to their asynchronous equivalent, will use an analogue of IEEE NaNs to minimize the hits to the exception path, and won't let you call functions that aren't annotated as safe in IO completion context. I would also like five acres in town and a pony.

Cheers,
- Michael

#273 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 41 lines · Linus Torvalds
Quoted 5 lines · Linus Torvalds

So I think that a good implementation just does everything up-front, and doesn't _need_ a user buffer that is live over longer periods, except for the actual results. Exactly because the whole alloc/teardown is nasty.

Btw, this doesn't necessarily mean "not supporting multiple atoms at all".

I think the batching of async things is potentially a great idea. I think it's quite workable for "open+fstat" kind of things, and I agree that it can solve other things too (the "socket+bind+connect+sendmsg+rcv" kind of complex setup things).

But I suspect that if we just said:
- we limit these atom sequences to just linear sequences of max "n" ops
- we read them all in in a single go at startup

we actually avoid several nasty issues. Not just the memory allocation issue in user space (now it's perfectly ok to build up a sequence of ops in temporary memory and throw it away once it's been submitted), but also issues like the 32-bit vs 64-bit compatibility stuff (the compat handlers would just convert it when they do the initial copying, and then the actual run-time wouldn't care about user-level pointers having different sizes etc).

Would it make the interface less cool? Yeah. Would it limit it to just a few linked system calls (to avoid memory allocation issues in the kernel)? Yes again. But it would simplify a lot of the interface issues.

It would _also_ allow the "sys_aio_read()" function to build up its *own* set of atoms in kernel space to actually do the read, and there would be no impact of the actual run-time wanting to read stuff from user space. Again - it's actually the same issue as with the compat system call: by making the interfaces do things up-front rather than dynamically, it becomes more static, but also easier to do interface translations. You can translate into any arbitrary internal format _once_, and be done with it.

Quoted 1 line

I dunno.

[ hm. I again wrote a pretty long email for you to read. Darn! ]

regarding the API - i share most of your concerns, and it's all a function of how widely we want to push this into user-space.

My initial thought was for syslets to be used by glibc as small, secure kernel-side 'syscall plugins' mainly - so that it can do things like 'POSIX AIO signal notifications' (which are madness in terms of performance, but which applications rely on) /without/ having to burden the kernel-side AIO with such requirements: glibc just adds an enclosing sys_kill() to the syslet and it will do the proper signal notification, asynchronously. (and of course syslets can be used for the Tux type of performance sillinesses as well ;-)

So a sane user API (all used at the glibc level, not at application level) would use simple syslets, while more broken ones would have to use longer ones - but nobody would have the burden of having to synchronize back to the issuer context. Natural selection will gravitate application use towards the APIs with the shorter syslets. (at least so i hope)

In this model syslets arent really user-programmable entities but rather small plugins available to glibc to build up more complex, more innovative (or just more broken) APIs than what the kernel wants to provide - without putting any true new ABI dependency on the kernel, other than the already existing syscall ABIs.

But if we'd like glibc to provide this to applications in some sort of standardized /programmable/ manner, with a wide range of atom selections (not directly coded syscall numbers, but rather as function pointers to actual glibc functions, which glibc could translate to syscall numbers, argument encodings, etc.), then i agree that doing the compat things and making it 32/64-bit agnostic (and much more) is pretty much a must. If 90% of this current job is finished then sorting those out will at least be another 90% of the work ;-)

and actually this latter model scares me, and i think that model scared the hell out of you as well.

But i really have no strong opinion about which one we want yet, without having walked the path. Somewhere inside me i'd of course like syslets to become a widely available interface - but my fear is that it might just not be 'human' enough to make sense - and we'd just not want to tie us down with an ABI that's not used. I dont want this to become another sys_sendfile - much talked about and _almost_ useful but in practice seldom used due to its programmability and utility limitations.

OTOH, the syslet concept right now already looks very ubiquitous, and the main problem with AIO use in applications wasnt just even its broken API or its broken performance, but the fundamental lack of all Linux IO disciplines supporting AIO, and the lack of significantly parallel hardware. We have kaio that is centered around block drivers - then we have epoll that works best with networking, and inotify that deals with some (but not all) VFS events - but neither supports every IO and event disciple well, at once. My feeling is that /this/ is the main fundamental problem with AIO in general, not just its programmability limitations.

Right now i'm concentrating on trying to build up something on the scheduling side that shows the issues in practice, shows the limitations and shows the possibilities. For example the easy ability to turn a cachemiss thread back into a user thread (and then back into a cachemiss thread) was a true surprise to me which increased utility quite a bit. I couldnt have designed it into the concept because it just didnt occur to me in the early stages. The notification ring related limitations you noticed is another important thing to fix - and these issues go to the core scheduling model of the concept and affect everything.

Thirdly, while Tux does not matter much to us, at least to me it is pretty clear what it takes to get performance up to the levels of Tux - and i dont see any big fundamental compromise possible on that front. Syslets are partly Tux repackaged into something generic - they are probably a bit slower than straight kernel code Tux, but not by much and it's also not behaving fundamentally differently. And if we dont offer at least something close to those possibilities then people will re-start trying to add those special-purpose state machine APIs again, and the whole "we need /true/ async IO" game starts again.

So if we accept "make parallelism easier to program" and "get somewhat close to Tux's performance and scalability" as a premise (which you might not agree with in that form), then i dont think there's much choice we have: either we use kernel threads, synchronous system calls and the scheduler intelligently (and the scheduling/threading bits of syslets are pretty much the most intelligent kernel thread based approach i can imagine at the moment =B-) or we use a special-purpose KAIO state machine subsystem, avoiding most of the existing synchronous infrastructure, painfully coding it into every IO discipline - and this will certainly haunt us until the end of times.

So that's why i'm not /that/ much worried about the final form of the API at the moment - even though i agree that it is /the/ most important decision factor in the end: i see various unavoidable externalities forcing us very much, and in the end we either like the result and make it available to programmers, or we dont, and limit it to system-glue glibc use - or we throw it away altogether. I'm curious about the end result even if it gets limited or gets thrown away (joining 4:4 on the way to the bit bucket ;) and while i'm cautiously optimistic that something useful can come out of this, i cannot know it for sure at the moment.

Ingo

#274 lore raw

Re: [patch 05/11] syslets: core code

Quoted 10 lines · Ingo Molnar

OTOH, the syslet concept right now already looks very ubiquitous, and the main problem with AIO use in applications wasnt just even its broken API or its broken performance, but the fundamental lack of all Linux IO disciplines supporting AIO, and the lack of significantly parallel hardware. We have kaio that is centered around block drivers - then we have epoll that works best with networking, and inotify that deals with some (but not all) VFS events - but neither supports every IO and event disciple well, at once. My feeling is that /this/ is the main fundamental problem with AIO in general, not just its programmability limitations.

That is quite dissapointing to hear when weekely released kevent can solve that problem already more than year ago - it was designed specially to support every possible notification types and does support file descriptor ones, VFS (dropped in current releases to reduce size) and tons of other including POSIX times, signals, own high-performance AIO (which was created as a a bit complex state machine over internals of page population code) and essentially everything one can ever imagine with quite a bit of code needed for new type.

I was requested to add waiting for futex through kevent queue - that is quite simple task, but having complete lack of feedback and ignorance of the project even from people who asked about its features, it looks like there is no need for that at all.

Signature · 1 line
	Evgeniy Polyakov
#275 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

THAT was the point. Interfaces are really really subtle and important. It's absolutely not a case of "we can just write wrappers to fix up any library issues".

Interfaces can be created and destroyed - they do not affect overall system design in anyway (well, if they do, something is broken). So let's solve problems in order of their appearence - if interfaces are more important for you than overall design - that is a problem I think.

Quoted 7 lines
Quoted 4 lines

So instead there will be
s = atom_create_and_add(__NR_stat, path, stat, NULL, NULL, NULL, NULL); atom then can be freed in the glibc_async_wait() wrapper just before returning data to userspace.

So now you add some kind of allocation/dealloction thing. In user space or in the kernel?

In userspace.
It was not added by me - it is just a wrapper.

Quoted 10 lines
Quoted 2 lines

There are millions of possible ways to do that, but what exactly one should be used from your point of view? Describe _your_ vision of that path.

My vision is that we should be able to do the simple things *easily* and without any extra overhead.

And doing wrappers in user space is almost entirely unacceptable, becasue a lot of the wrapping needs to be done at release time (for example:
de-allocating memory), and that means that you no longer can do simple system calls that don't even need release notification AT ALL.

syslets do work that way - they require some user memory - likely long-standing (100% sure for multi atom setup, maybe it can be optimized though) - if you do not want to allocate it explicitly - it is possible to have a wrapper.

Quoted 10 lines
Quoted 6 lines

Currently generic example is following:
allocate mem
setup complex structure
submit syscall
wait syscall
free mem

And that "allocate mem" and "free mem" is a problem. It's not just a performance problem, it is a _complexity_ problem. It means that people have to track things that they are NOT AT ALL INTERESTED IN!

I proposed a way to hide allocation - it is simple, but you've cut it. I can create another one without special per-thread thing - handle = async_init();
async_stat(handle, path, stat);
async_cleanup(); // not needed, since will be freed on exit automatically

Another one is to preallocate set of atoms in __attribute((contructor)) function.

There are really a lot of possible ways - _I_ can use the first one with explicit operations, others likely can not - so I _ask_ about how should it look like.

Quoted 8 lines
Quoted 3 lines

So, describe how exactly _you_ think it should be implemented with its pros and cons, so that systemn could be adopted without trying to mind-read of what is simple and good or complex and really bad.

So I think that a good implementation just does everything up-front, and doesn't _need_ a user buffer that is live over longer periods, except for the actual results. Exactly because the whole alloc/teardown is nasty.

Quoted 6 lines

And I think a good implementation doesn't need wrapping in user space to be useful - at *least* not wrapping at completion time, which is the really difficult one (since, by definition, in an async world completion is separated from the initial submit() event, and with kernel-only threads you actually want to *avoid* having to do user code after the operation completed).

So where is a problem?
I proposed already three ways to do the thing - user will not even know about something happend. You did not comment on anyone, instead you handwaving with talks about how in theory something should be similar to. What exactly do _you_ expect from interface?

Quoted 2 lines

I suspect Ingo's thing can do that. But I also suspect (nay, _know_, from this discussion), that you didn't even think of the problems.

That is another problem - you think you know something, but you fail to prove that.

I can work with explicit structure allocation/deallocation/setup - you do not want that - so I ask you about your opinion, and instead of getting an answer I receive theoretical word-fall about how perfect interface should look like.

You only need to have one function call without ever thinking bout freeing? I proposed _two_ ways to that.
You can live with explicit init/cleanup (opt) code? There is another one.

So please decribe your vision of interface with details, so that it could be think about or/and implemented.

Quoted 1 line

Linus

Signature · 1 line
	Evgeniy Polyakov
#276 lore raw

Re: [patch 05/11] syslets: core code

Quoted 3 lines · Evgeniy Polyakov

Interfaces can be created and destroyed - they do not affect overall system design in anyway (well, if they do, something is broken).

I'm sorry, but you've obviously never maintained any piece of software that actually has users.

As long as you think that interfaces can change, this discussion is pointless.

So go away, ponder things.

Linus

#277 lore raw

Re: [patch 05/11] syslets: core code

Quoted 5 lines · Linus Torvalds
Quoted 2 lines

Interfaces can be created and destroyed - they do not affect overall system design in anyway (well, if they do, something is broken).

I'm sorry, but you've obviously never maintained any piece of software that actually has users.

Strong. But saying for others usualy tends to show own problems.

Quoted 2 lines

As long as you think that interfaces can change, this discussion is pointless.

That is too cool phrase to be heared - if you will make me a favour and reread what was written you will (hopefully) detect that there were no words about interfaces being changed after put into the wild - talk was only about time when system is designed and implemented, and there is time for discussion about its rough edges - if its design is good, then interface can be changed in a moment without any problem - that is what we see with syslets right now - they are designed and implemented (the formed was done several years ago), and it is time to shape its edges - like change userspace API - it is easy, but you do not (want/like to) see that.

Quoted 1 line

So go away, ponder things.

But my above words are too lame for self-hearing Olympus liver. Definitely.

Quoted 1 line

Linus

Signature · 1 line
	Evgeniy Polyakov
#278 lore raw

Re: [patch 05/11] syslets: core code

Quoted 2 lines · Evgeniy Polyakov

if its design is good, then
interface can be changed in a moment without any problem

This isn't always the case. Sometimes the interface puts requirements (contract-like) upon the implementation. Case in point in the kernel, dnotify versus inotify. dnotify is a steaming pile of worthlessness, because it's userspace interface is so bad (meaning inefficient) as to be nearly unusable.

inotify has a different interface, one that supplies details about events rather that mere notice that an event occurred, and therefore has different requirements in implementation. dnotify probably was a good design, but for a worthless interface.

The interface isn't always important, but it's certainly something that has to be understood before putting the finishing touches on the behind-the-scenes implementation.

Ray

#279 lore raw

Re: [patch 05/11] syslets: core code

Quoted 10 lines · Ray Lee
Quoted 2 lines · Evgeniy Polyakov

if its design is good, then
interface can be changed in a moment without any problem

This isn't always the case. Sometimes the interface puts requirements (contract-like) upon the implementation. Case in point in the kernel, dnotify versus inotify. dnotify is a steaming pile of worthlessness, because it's userspace interface is so bad (meaning inefficient) as to be nearly unusable.

Quoted 8 lines

inotify has a different interface, one that supplies details about events rather that mere notice that an event occurred, and therefore has different requirements in implementation. dnotify probably was a good design, but for a worthless interface.

The interface isn't always important, but it's certainly something that has to be understood before putting the finishing touches on the behind-the-scenes implementation.

Absolutely.
And if overall system design is good, there is no problem to change (well, for those who fail to read to the end and understand my english replace 'to change' with 'to create and commit') interface to the state where it will satisfy all (majority of) users.

Situations when system is designed from interface down to system ends up with one thread per IO and huge limitations on how system is going to be used at all.

Quoted 1 line

Ray

Signature · 1 line
	Evgeniy Polyakov
#280 lore raw

Re: [patch 05/11] syslets: core code

On Fri, Feb 16, 2007 at 07:58:54PM +0300, Evgeniy Polyakov wrote:

| Absolutely.
| And if overall system design is good, there is no problem to change
| (well, for those who fail to read to the end and understand my english
| replace 'to change' with 'to create and commit') interface to the state
| where it will satisfy all (majority of) users.
| 
| Situations when system is designed from interface down to system ends up
| with one thread per IO and huge limitations on how system is going to be
| used at all.
| 
| -- 
| 	Evgeniy Polyakov

I'm sorry for meddling in conversation but I think Linus misunderstood you. If I'm right you propose to "create and commit" _new_ interfaces only? I mean _changing_ of interfaces exported to user space is very painfull... for further support. Don't swear at me if I wrote something stupid ;)

Signature · 1 line
		Cyrill
#281 lore raw

Re: [patch 05/11] syslets: core code

Quoted 14 lines · Cyrill V. Gorcunov

On Fri, Feb 16, 2007 at 07:58:54PM +0300, Evgeniy Polyakov wrote:

| Absolutely.
| And if overall system design is good, there is no problem to change
| (well, for those who fail to read to the end and understand my english
| replace 'to change' with 'to create and commit') interface to the state
| where it will satisfy all (majority of) users.
| 
| Situations when system is designed from interface down to system ends up
| with one thread per IO and huge limitations on how system is going to be
| used at all.
| 
| -- 
| 	Evgeniy Polyakov
Quoted 5 lines

I'm sorry for meddling in conversation but I think Linus misunderstood you. If I'm right you propose to "create and commit" _new_ interfaces only? I mean _changing_ of interfaces exported to user space is very painfull... for further support. Don't swear at me if I wrote something stupid ;)

Yes, I only proposed to change what Ingo has right now - although it is usable, but it does suck, but since overall syslet design is indeed good it does not suffer from possible interface changes - so I said that it can be trivially changed in that regard that until it is committed anything can be done to extend it.

Quoted 3 lines
Signature · 1 line
		Cyrill
Signature · 1 line
	Evgeniy Polyakov
#282 lore raw

Re: [patch 05/11] syslets: core code

On Sat, Feb 17, 2007 at 01:02:00PM +0300, Evgeniy Polyakov wrote:
[... snipped ...]

| Yes, I only proposed to change what Ingo has right now - although it is
| usable, but it does suck, but since overall syslet design is indeed good
| it does not suffer from possible interface changes - so I said that it
| can be trivially changed in that regard that until it is committed
| anything can be done to extend it.
| 
| -- 
| 	Evgeniy Polyakov
| 

I think Evgeniy - you are right! For times of research _changing_ a lot of things is almost a low. syslets are in test area and why should we bound ourself in survey of best. If something in syslets is sucks so lets change it as early as possible. Of course I mean no more interface changing after some _commit_ point (and that should be Linus decision).

Signature · 1 line
		Cyrill
#283 lore raw

Re: [patch 05/11] syslets: core code

In reply to Evgeniy Polyakov
Quoted 15 lines · Evgeniy Polyakov
Quoted 14 lines · Ray Lee
Quoted 2 lines · Evgeniy Polyakov

if its design is good, then
interface can be changed in a moment without any problem

This isn't always the case. Sometimes the interface puts requirements (contract-like) upon the implementation. Case in point in the kernel, dnotify versus inotify. dnotify is a steaming pile of worthlessness, because it's userspace interface is so bad (meaning inefficient) as to be nearly unusable.

inotify has a different interface, one that supplies details about events rather that mere notice that an event occurred, and therefore has different requirements in implementation. dnotify probably was a good design, but for a worthless interface.

Quoted 6 lines
Quoted 3 lines

The interface isn't always important, but it's certainly something that has to be understood before putting the finishing touches on the behind-the-scenes implementation.

Absolutely.
And if overall system design is good,

dnotify was a good system design for a stupid (or misunderstood) problem.

Quoted 4 lines

there is no problem to change
(well, for those who fail to read to the end and understand my english replace 'to change' with 'to create and commit') interface to the state where it will satisfy all (majority of) users.

You might be right, but the point I (and others) are trying to make is that there are some cases where you *really* need to understand the users of the interface first. You might have everything else right (userspace wants to know when filesystem changes occur, great), but if you don't know what form those notifications have to look like, you'll end up doing a lot of wasted work on a worthless piece of code that no one will ever use.

Sometimes the interface really is the most important thing. Just like a contract between people.

(This is probably why, by the way, most people are staying silent on your excellent kevent work. The kernel side is, in some ways, the easy part. It's getting an interface that will handle all users [ users == producers and consumers of kevents ], that is the hard bit.)

Or, let me put it yet another way: How do you prove to the rest of us that you, or Ingo, or whomever, are not building another dnotify? (Maybe you're smart enough in this problem space that you know you're not -- that's actually the most likely possibility. But you still have to prove it to the rest of us. Sucks, I know.)

Quoted 3 lines

Situations when system is designed from interface down to system ends up with one thread per IO and huge limitations on how system is going to be used at all.

The other side is you start from the goal in mind and get Ingo's state machines with loops and conditionals and marmalade in syslets which appear a bit baroque and overkill for the majority of us userspace folk.

(No offense intended to Ingo, he's obviously quite a bit more conversant on the needs of high speed interfaces than I am. However, I suspect I have a bit more clarity on what us normal folk would actually use, and kernel driven FSMs ain't it. Userspace often makes a lot of contextual decisions that I would absolutely *hate* to write and debug as a state machine that gets handed off to the kernel. I'll happily take a 10% hit in efficiency that Moore's law will get me back in a few months, instead of spending a bunch of time debugging difficult heisenbugs due to the syslet FSM reading a userspace variable at a slightly different time once in a blue moon. OTOH, I'm also not Oracle, so what do I know?)

The truth of this lies somewhere in the middle. It isn't kernel driven, or userspace interface driven, but a tradeoff between the two.

So:

Quoted 2 lines

Userspace_API_is_the_ever_possible_last_thing_to_ever_think_about. Period

Please listen to those of us who are saying that this might not be the case. Maybe we're idiots, but then again maybe we're not, okay? Sometimes the API really *DOES* change the underlying implementation.

Ray

#284 lore raw

Re: [patch 05/11] syslets: core code

Quoted 4 lines · Ray Lee

(This is probably why, by the way, most people are staying silent on your excellent kevent work. The kernel side is, in some ways, the easy part. It's getting an interface that will handle all users [ users == producers and consumers of kevents ], that is the hard bit.)

Kevent interface was completely changed 4 (!) times for the last year after kernel developers request without any damage to its kernel part.

Quoted 5 lines

Or, let me put it yet another way: How do you prove to the rest of us that you, or Ingo, or whomever, are not building another dnotify? (Maybe you're smart enough in this problem space that you know you're not -- that's actually the most likely possibility. But you still have to prove it to the rest of us. Sucks, I know.)

I only want to say that when system is designed correctly there is no problem to change interface (yes, I again said 'to change' just because I hope everyone understand that I'm talking about time when system is not yet committed to the tree).

Btw, dnotify had problems in its design highlighted at inotify statrt - mainly that watchers were not attached to inode.

It is right now the time to ask users what interface they expect from AIO - so I asked Linus and proposed three different ones, two of them were designed in a way that user would not even know that some allocation/freeing was done - and as a result I got 'you suck' response exactly the same as was returned on the first syslet release - just _anly_ fscking _just_ because it had ugly interface.

Quoted 7 lines
Quoted 3 lines

Situations when system is designed from interface down to system ends up with one thread per IO and huge limitations on how system is going to be used at all.

The other side is you start from the goal in mind and get Ingo's state machines with loops and conditionals and marmalade in syslets which appear a bit baroque and overkill for the majority of us userspace folk.

Well, I designed kevent AIO in the similar way, but it has even more complex one which is built on top of internal page population functions.

It is complex a bit, but it works fast. And it works with any type (if I would not be lazy and implement bindings) of AIO.

Interface of syslets is not perfect, but it can be changed (I said it again? I think we all understand what I mean by that already) trivially right now (before it is included) - it is not the way to throw thing just because it has bad interface which can be extended in a moment.

Quoted 14 lines

(No offense intended to Ingo, he's obviously quite a bit more conversant on the needs of high speed interfaces than I am. However, I suspect I have a bit more clarity on what us normal folk would actually use, and kernel driven FSMs ain't it. Userspace often makes a lot of contextual decisions that I would absolutely *hate* to write and debug as a state machine that gets handed off to the kernel. I'll happily take a 10% hit in efficiency that Moore's law will get me back in a few months, instead of spending a bunch of time debugging difficult heisenbugs due to the syslet FSM reading a userspace variable at a slightly different time once in a blue moon. OTOH, I'm also not Oracle, so what do I know?)

The truth of this lies somewhere in the middle. It isn't kernel driven, or userspace interface driven, but a tradeoff between the two.

Quoted 8 lines

So:

Quoted 2 lines

Userspace_API_is_the_ever_possible_last_thing_to_ever_think_about. Period

Please listen to those of us who are saying that this might not be the case. Maybe we're idiots, but then again maybe we're not, okay? Sometimes the API really *DOES* change the underlying implementation.

It is exactly the time to say what interface sould be good. System is almost ready - it is time to make it looks cool for users.

Quoted 1 line

Ray

Signature · 1 line
	Evgeniy Polyakov
#285 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 1 line · Linus Torvalds

We know one interface: the current aio_read() one. Nobody really _likes_

[...]

Quoted 5 lines

Others? We don't know yet. And exposing complex interfaces that may not be the right ones is much *worse* than exposing simple interfaces (that _also_ may not be the right ones, of course - but simple and

From humble userland, here's two things I'd hope to be able to do, although

I admit my needs are rather specialist.

1) batch, and wait for, with proper error reporting:

	socket();
	[ setsockopt(); ]
	bind();
	connect();
	gettimeofday();  // doesn't *always* happen
	send();
	recv();
	gettimeofday(); // doesn't *always* happen

	I go through this sequence for each outgoing powerdns UDP query
	because I need a new random source port for each query, and I
	connect because I care about errrors. Linux does not give me random
	source ports for UDP sockets.

	When async, I can probably just drop the setsockopt (for
	nonblocking). I already batch the gettimeofday to 'once per epoll
	return', but quite often this is once per packet.

2) On the client facing side (port 53), I'd very much hope for a way to
do 'recvv' on datagram sockets, so I can retrieve a whole bunch of UDP datagrams with only one kernel transition.

This would mean that I batch up either 10 calls to recv(), or one 'atom' of 10 recv's.

Both 1 and 2 are currently limiting factors when I enter the 100kqps domain of name serving. This doesn't mean the rest of my code is as tight as it could be, but I spend a significant portion of time in the kernel even at moderate (10kqps effective) loads, even though I already use epoll. A busy PowerDNS recursor typically spends 25% to 50% of its time on 'sy' load.

This might be due to my use of get/set/swap/makecontext though.

Bert

Signature · 2 lines
http://www.PowerDNS.com      Open source, database driven DNS Software 
http://netherlabs.nl              Open and Closed source services
#286 lore raw

Re: [patch 05/11] syslets: core code

Quoted 14 lines · bert hubert

1) batch, and wait for, with proper error reporting:

	socket();
	[ setsockopt(); ]
	bind();
	connect();
	gettimeofday();  // doesn't *always* happen
	send();
	recv();
	gettimeofday(); // doesn't *always* happen

	I go through this sequence for each outgoing powerdns UDP query
	because I need a new random source port for each query, and I
	connect because I care about errrors. Linux does not give me random
	source ports for UDP sockets.

What about a setsockopt or just random port selection patch? :)

Quoted 11 lines

When async, I can probably just drop the setsockopt (for nonblocking). I already batch the gettimeofday to 'once per epoll return', but quite often this is once per packet.

2) On the client facing side (port 53), I'd very much hope for a way to
do 'recvv' on datagram sockets, so I can retrieve a whole bunch of UDP datagrams with only one kernel transition.

This would mean that I batch up either 10 calls to recv(), or one 'atom' of 10 recv's.

Quoted 7 lines

Both 1 and 2 are currently limiting factors when I enter the 100kqps domain of name serving. This doesn't mean the rest of my code is as tight as it could be, but I spend a significant portion of time in the kernel even at moderate (10kqps effective) loads, even though I already use epoll. A busy PowerDNS recursor typically spends 25% to 50% of its time on 'sy' load.

This might be due to my use of get/set/swap/makecontext though.

It is only about one syscall in get and set/swap context, btw, so it should not be a main factor, doesn't it?

As an advertisement note, but if you have a lot of network events per epoll read try to use kevent - its socket notifications do not require additional traverse of the list of ready events as in poll usage.

Quoted 5 lines

Bert

Signature · 2 lines
http://www.PowerDNS.com      Open source, database driven DNS Software 
http://netherlabs.nl              Open and Closed source services
Signature · 1 line
	Evgeniy Polyakov
#287 lore raw

Re: [patch 05/11] syslets: core code

In reply to bert hubert
Quoted 3 lines

2) On the client facing side (port 53), I'd very much hope for a way to
do 'recvv' on datagram sockets, so I can retrieve a whole bunch of UDP datagrams with only one kernel transition.

I want to highlight this point that Bert is making.

Whenever we talk about AIO and kernel threads some folks are rightly concerned that we're talking about a thread *per IO* and fear that memory consumption will be fatal.

Take the case of userspace which implements what we'd think of as page cache writeback. (*coughs, points at email address*). It wants to issue thousands of IOs to disjoint regions of a file. "Thousands of kernel threads, oh crap!"

But it only issues each IO with a separate syscall (or io_submit() op) because it doesn't have an interface that lets it specify IOs that vector user memory addresses *and file position*.

If we had a seemingly obvious interface that let it kick off batched IOs to different parts of the file, the looming disaster of a thread per IO vanishes in that case.

struct off_vec {
	off_t pos;
	size_t len;
};

long sys_sgwrite(int fd, struct iovec *memvec, size_t mv_count,

	struct off_vec *ovec, size_t ov_count);

It doesn't take long to imagine other uses for this that are less exotic.

Take e2fsck and its iterating through indirect blocks or directory data blocks. It has a list of disjoint file regions (blocks) it wants to read, but it does them serially to keep the code from getting even more confusing. blktrace a clean e2fsck -f some time.. it's leaving *HALF* of the disk read bandwith on the table by performing serial block-sized reads. If it could specify batches of them the code would still be simple but it could tell the kernel and IO scheduler *exactly* what it wants, without having to mess around with sys_readahead() or AIO or any of that junk :).

Anyway, that's just something that's been on my mind. If there are obvious clean opportunities to get more done with single syscalls, it might not be such a bad thing.

- z

#288 lore raw

Re: [patch 05/11] syslets: core code

In reply to bert hubert
Quoted 5 lines · bert hubert

Both 1 and 2 are currently limiting factors when I enter the 100kqps domain of name serving. This doesn't mean the rest of my code is as tight as it could be, but I spend a significant portion of time in the kernel even at moderate (10kqps effective) loads, even though I already use epoll. A busy PowerDNS recursor typically spends 25% to 50% of its time on 'sy' load.

Well, I guess in your workload most of system overhead is because of sockets creation/destruction, UDP/IP stack work, nic driver, interrupts... I really doubt async_io could help you... Do you have some oprofile results to share with us ?

#289 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 8 lines · Linus Torvalds

I don't think the "atom" approach is bad per se. I think it could be fine to have some state information in user space. It's just that I think complex interfaces that people largely won't even use is a big mistake. We should concentrate on usability first, and some excessive cleverness really isn't a big advantage.

Being able to do a "open + stat" looks like a fine thing. But I doubt you'll see a lot of other combinations.

I actually think that building chains of syscalls bring you back to a multithreaded solution. Why? Because suddendly the service thread become from servicing a syscall (with possible cachehit optimization), to servicing a whole session. So the number of service threads needed (locked down by a chain) becomes big because requests goes from being short-lived syscalls to long-lived chains of them. Think about the trivial web server, and think about a chain that does open->fstat->sendhdrs->sendfile after an accept. What's the difference with a multithreaded solution that does accept->clone and execute the above code in the new thread? Nada, NIL. Actually, there is a difference. The standard multithreaded function is easier to code in C than with the complex atoms chains. The number of service thread becomes suddendly proportional to the number of active sessions.
The more I look at this, the more I think that async_submit should submit simple syscalls, or an array of them (unrelated/parallel).

- Davide

#290 lore raw

Re: [patch 05/11] syslets: core code

Quoted 11 lines · Davide Libenzi
Quoted 8 lines · Linus Torvalds

I don't think the "atom" approach is bad per se. I think it could be fine to have some state information in user space. It's just that I think complex interfaces that people largely won't even use is a big mistake. We should concentrate on usability first, and some excessive cleverness really isn't a big advantage.

Being able to do a "open + stat" looks like a fine thing. But I doubt you'll see a lot of other combinations.

Quoted 9 lines

I actually think that building chains of syscalls bring you back to a multithreaded solution. Why? Because suddendly the service thread become from servicing a syscall (with possible cachehit optimization), to servicing a whole session. So the number of service threads needed (locked down by a chain) becomes big because requests goes from being short-lived syscalls to long-lived chains of them. Think about the trivial web server, and think about a chain that does open->fstat->sendhdrs->sendfile after an accept. What's the difference with a multithreaded solution that does accept->clone and execute the above code in the new thread? Nada, NIL.

That is more ideological question about micro-thread design at all. If syslet will be able to perform only one syscall, one will have 4 threads for above case, not one, so it is even more broken.

So, if Linux moves that way of doing AIO (IMO incorrect, I think that the correct state machine made not of syscalls, but specially crafted entries - like populate pages into VFS, send chunk, recv chunk without blocking and continue on completion and the like), syslets with attached state machines are the (smallest evil) best choice.

Quoted 6 lines

Actually, there is a difference. The standard multithreaded function is easier to code in C than with the complex atoms chains. The number of service thread becomes suddendly proportional to the number of active sessions.
The more I look at this, the more I think that async_submit should submit simple syscalls, or an array of them (unrelated/parallel).

That is the case - atom items (I do hope that this subsystem would be able to perform not only syscalls, but any kernel interfaces suitable prototypes, v2 seems to move that direction) are called asynchronously from main userspace thread to achieve the maximum performance.

Quoted 2 lines

- Davide

Signature · 1 line
	Evgeniy Polyakov
#291 lore raw

Re: [patch 05/11] syslets: core code

Quoted 12 lines · Evgeniy Polyakov
Quoted 10 lines · Davide Libenzi

I actually think that building chains of syscalls bring you back to a multithreaded solution. Why? Because suddendly the service thread become from servicing a syscall (with possible cachehit optimization), to servicing a whole session. So the number of service threads needed (locked down by a chain) becomes big because requests goes from being short-lived syscalls to long-lived chains of them. Think about the trivial web server, and think about a chain that does open->fstat->sendhdrs->sendfile after an accept. What's the difference with a multithreaded solution that does accept->clone and execute the above code in the new thread? Nada, NIL.

Quoted 3 lines

That is more ideological question about micro-thread design at all. If syslet will be able to perform only one syscall, one will have 4 threads for above case, not one, so it is even more broken.

Nope, just one thread. Well, two, if you consider the "main" dispatch thread, and the syscall service thread.

Quoted 5 lines

So, if Linux moves that way of doing AIO (IMO incorrect, I think that the correct state machine made not of syscalls, but specially crafted entries - like populate pages into VFS, send chunk, recv chunk without blocking and continue on completion and the like), syslets with attached state machines are the (smallest evil) best choice.

But at that point you don't need to have complex atom interfaces, with chains, whips and leather pants :) Just code it in C and submit that to the async engine. The longer is the chain though, the closer you get to a fully multithreaded solution, in terms of service thread consuption. And what do you save WRT a multithreaded solution? Not thread creation/destroy, because that cost is fully amortized inside the chain execution cost (plus a pool would even save that).
IMO the plus of a generic async engine is mostly from a kernel code maintainance POV. You don't need anymore to have AIO-aware code paths, that automatically transalte to smaller and more maintainable code.

- Davide

#292 lore raw

Re: [patch 05/11] syslets: core code

Quoted 14 lines · Davide Libenzi
Quoted 12 lines · Evgeniy Polyakov
Quoted 10 lines · Davide Libenzi

I actually think that building chains of syscalls bring you back to a multithreaded solution. Why? Because suddendly the service thread become from servicing a syscall (with possible cachehit optimization), to servicing a whole session. So the number of service threads needed (locked down by a chain) becomes big because requests goes from being short-lived syscalls to long-lived chains of them. Think about the trivial web server, and think about a chain that does open->fstat->sendhdrs->sendfile after an accept. What's the difference with a multithreaded solution that does accept->clone and execute the above code in the new thread? Nada, NIL.

Quoted 6 lines
Quoted 3 lines

That is more ideological question about micro-thread design at all. If syslet will be able to perform only one syscall, one will have 4 threads for above case, not one, so it is even more broken.

Nope, just one thread. Well, two, if you consider the "main" dispatch thread, and the syscall service thread.

Argh, if they are supposed to run synchronously, for example stat can be done in parallel with sendfile in above example, but generally yes, one execution thread.

Quoted 6 lines
Quoted 5 lines

So, if Linux moves that way of doing AIO (IMO incorrect, I think that the correct state machine made not of syscalls, but specially crafted entries - like populate pages into VFS, send chunk, recv chunk without blocking and continue on completion and the like), syslets with attached state machines are the (smallest evil) best choice.

Quoted 10 lines

But at that point you don't need to have complex atom interfaces, with chains, whips and leather pants :) Just code it in C and submit that to the async engine. The longer is the chain though, the closer you get to a fully multithreaded solution, in terms of service thread consuption. And what do you save WRT a multithreaded solution? Not thread creation/destroy, because that cost is fully amortized inside the chain execution cost (plus a pool would even save that).
IMO the plus of a generic async engine is mostly from a kernel code maintainance POV. You don't need anymore to have AIO-aware code paths, that automatically transalte to smaller and more maintainable code.

It is completely possible to not wire up several syscalls and just use only one per async call, but _if_ such a requirement rises, the whole infrastructure is there.

Quoted 2 lines

- Davide

Signature · 1 line
	Evgeniy Polyakov
#293 lore raw

Re: [patch 05/11] syslets: core code

In reply to Linus Torvalds
Quoted 6 lines · Linus Torvalds

Here's a quick question: how many people have actually ever seen them used in "normal code"?

Yeah. Nobody uses them. They're not all that portable (even within unixes they aren't always there, much less in other places), they are fairly obscure, and they are just not really easy to use.

That's nonsense. They are widely used (just hear people scream if something changes or breaks) and they are available on all Unix implementations which are not geared towards embedded use. POSIX makes AIO in the next revision mandatory.

Just because you don't like it, don't discount it. Yes, the interface is not the best. But this is what you get if you cannot dictate interfaces to everybody. You have to make concessions.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#294 lore raw

PATCH 6/11 syslets: core, documentation

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

Add Documentation/syslet-design.txt with a high-level description of the syslet concepts.

---
 Documentation/syslet-design.txt |  137 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 137 insertions(+)
Documentation/syslet-design.txt new file +137 -0
Index: linux/Documentation/syslet-design.txt
===================================================================
@@ -0,0 +1,137 @@
+Syslets / asynchronous system calls
+===================================
+
+started by Ingo Molnar <[email protected]>
+
+Goal:
+-----
+
+The goal of the syslet subsystem is to allow user-space to execute
+arbitrary system calls asynchronously. It does so by allowing user-space
+to execute "syslets" which are small scriptlets that the kernel can execute
+both securely and asynchronously without having to exit to user-space.
+
+the core syslet concepts are:
+
+The Syslet Atom:
+----------------
+
+The syslet atom is a small, fixed-size (44 bytes on 32-bit) piece of
+user-space memory, which is the basic unit of execution within the syslet
+framework. A syslet represents a single system-call and its arguments.
+In addition it also has condition flags attached to it that allows the
+construction of larger programs (syslets) from these atoms.
+
+Arguments to the system call are implemented via pointers to arguments.
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).
+
+ [ Note: since the argument array is at the end of the atom, and the
+   kernel will not touch any argument beyond the final NULL one, atoms
+   might be packed more tightly. (the only special case exception to
+   this rule would be SKIP_TO_NEXT_ON_STOP atoms, where the kernel will
+   jump a full syslet_uatom number of bytes.) ]
+
+The Syslet:
+-----------
+
+A syslet is a program, represented by a graph of syslet atoms. The
+syslet atoms are chained to each other either via the atom->next pointer,
+or via the SYSLET_SKIP_TO_NEXT_ON_STOP flag.
+
+Running Syslets:
+----------------
+
+Syslets can be run via the sys_async_exec() system call, which takes
+the first atom of the syslet as an argument. The kernel does not need
+to be told about the other atoms - it will fetch them on the fly as
+execution goes forward.
+
+A syslet might either be executed 'cached', or it might generate a
+'cachemiss'.
+
+'Cached' syslet execution means that the whole syslet was executed
+without blocking. The system-call returns the submitted atom's address
+in this case.
+
+If a syslet blocks while the kernel executes a system-call embedded in
+one of its atoms, the kernel will keep working on that syscall in
+parallel, but it immediately returns to user-space with a NULL pointer,
+so the submitting task can submit other syslets.
+
+Completion of asynchronous syslets:
+-----------------------------------
+
+Completion of asynchronous syslets is done via the 'completion ring',
+which is a ringbuffer of syslet atom pointers user user-space memory,
+provided by user-space in the sys_async_register() syscall. The
+kernel fills in the ringbuffer starting at index 0, and user-space
+must clear out these pointers. Once the kernel reaches the end of
+the ring it wraps back to index 0. The kernel will not overwrite
+non-NULL pointers (but will return an error), user-space has to
+make sure it completes all events it asked for.
+
+Waiting for completions:
+------------------------
+
+Syslet completions can be waited for via the sys_async_wait()
+system call - which takes the number of events it should wait for as
+a parameter. This system call will also return if the number of
+pending events goes down to zero.
+
+Sample Hello World syslet code:
+
+--------------------------->
+/*
+ * Set up a syslet atom:
+ */
+static void
+init_atom(struct syslet_uatom *atom, int nr,
+	  void *arg_ptr0, void *arg_ptr1, void *arg_ptr2,
+	  void *arg_ptr3, void *arg_ptr4, void *arg_ptr5,
+	  void *ret_ptr, unsigned long flags, struct syslet_uatom *next)
+{
+	atom->nr = nr;
+	atom->arg_ptr[0] = arg_ptr0;
+	atom->arg_ptr[1] = arg_ptr1;
+	atom->arg_ptr[2] = arg_ptr2;
+	atom->arg_ptr[3] = arg_ptr3;
+	atom->arg_ptr[4] = arg_ptr4;
+	atom->arg_ptr[5] = arg_ptr5;
+	atom->ret_ptr = ret_ptr;
+	atom->flags = flags;
+	atom->next = next;
+}
+
+int main(int argc, char *argv[])
+{
+	unsigned long int fd_out = 1; /* standard output */
+	char *buf = "Hello Syslet World!\n";
+	unsigned long size = strlen(buf);
+	struct syslet_uatom atom, *done;
+
+	async_head_init();
+
+	/*
+	 * Simple syslet consisting of a single atom:
+	 */
+	init_atom(&atom, __NR_sys_write, &fd_out, &buf, &size,
+		  NULL, NULL, NULL, NULL, SYSLET_ASYNC, NULL);
+	done = sys_async_exec(&atom);
+	if (!done) {
+		sys_async_wait(1);
+		if (completion_ring[curr_ring_idx] == &atom) {
+			completion_ring[curr_ring_idx] = NULL;
+			printf("completed an async syslet atom!\n");
+		}
+	} else {
+		printf("completed an cached syslet atom!\n");
+	}
+
+	async_head_exit();
+
+	return 0;
+}
#295 lore raw

Re: [patch 06/11] syslets: core, documentation

Wow! You really helped Zach out ;)

Quoted 5 lines · Ingo Molnar
Documentation/syslet-design.txt new file +5 -0
+The Syslet Atom:
+----------------
+
+The syslet atom is a small, fixed-size (44 bytes on 32-bit) piece of
+user-space memory, which is the basic unit of execution within the syslet

Documentation/syslet-design.txt line 30

Quoted 10 lines
Documentation/syslet-design.txt new file +10 -0
+framework. A syslet represents a single system-call and its arguments.
+In addition it also has condition flags attached to it that allows the
+construction of larger programs (syslets) from these atoms.
+
+Arguments to the system call are implemented via pointers to arguments.
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).

Why do you need to have an extra memory indirection per parameter in copy_uatom()? It also forces you to have parameters pointed-to, to be "long" (or pointers), instead of their natural POSIX type (like fd being "int" for example). Also, you need to have array pointers (think about a "char buf[];" passed to an async read(2)) to be saved into a pointer variable, and pass the pointer of the latter to the async system. Same for all structures (ie. stat(2) "struct stat"). Let them be real argouments and add a nparams argoument to the structure:

struct syslet_atom {
       unsigned long                       flags;
       unsigned int                        nr;
       unsigned int                        nparams;
       long __user                         *ret_ptr;
       struct syslet_uatom     __user      *next;
       unsigned long                       args[6];
};

I can understand that chaining syscalls requires variable sharing, but the majority of the parameters passed to syscalls are just direct ones. Maybe a smart method that allows you to know if a parameter is a direct one or a pointer to one? An "unsigned int pmap" where bit N is 1 if param N is an indirection? Hmm?

Quoted 21 lines
Documentation/syslet-design.txt new file +21 -0
+Running Syslets:
+----------------
+
+Syslets can be run via the sys_async_exec() system call, which takes
+the first atom of the syslet as an argument. The kernel does not need
+to be told about the other atoms - it will fetch them on the fly as
+execution goes forward.
+
+A syslet might either be executed 'cached', or it might generate a
+'cachemiss'.
+
+'Cached' syslet execution means that the whole syslet was executed
+without blocking. The system-call returns the submitted atom's address
+in this case.
+
+If a syslet blocks while the kernel executes a system-call embedded in
+one of its atoms, the kernel will keep working on that syscall in
+parallel, but it immediately returns to user-space with a NULL pointer,
+so the submitting task can submit other syslets.
+
+Completion of asynchronous syslets:

Documentation/syslet-design.txt line 75

Quoted 10 lines
Documentation/syslet-design.txt new file +10 -0
+-----------------------------------
+
+Completion of asynchronous syslets is done via the 'completion ring',
+which is a ringbuffer of syslet atom pointers user user-space memory,
+provided by user-space in the sys_async_register() syscall. The
+kernel fills in the ringbuffer starting at index 0, and user-space
+must clear out these pointers. Once the kernel reaches the end of
+the ring it wraps back to index 0. The kernel will not overwrite
+non-NULL pointers (but will return an error), user-space has to
+make sure it completes all events it asked for.

Sigh, I really dislike shared userspace/kernel stuff, when we're transfering pointers to userspace. Did you actually bench it against a:

int async_wait(struct syslet_uatom **r, int n);

I can fully understand sharing userspace buffers with the kernel, if we're talking about KB transferd during a block or net I/O DMA operation, but for transfering a pointer? Behind each pointer transfer(4/8 bytes) there is a whole syscall execution, that makes the 4/8 bytes tranfers have a relative cost of 0.01% *maybe*. Different case is a O_DIRECT read of 16KB of data, where in that case the memory transfer has a relative cost compared to the syscall, that can be pretty high. The syscall saving argument is moot too, because syscall are cheap, and if there's a lot of async traffic, you'll be fetching lots of completions to keep you dispatch loop pretty busy for a while.
And the API is *certainly* cleaner.

- Davide

#296 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 10 lines · Davide Libenzi
Quoted 10 lines
Documentation/syslet-design.txt new file +10 -0
+The Syslet Atom:
+----------------
+
+The syslet atom is a small, fixed-size (44 bytes on 32-bit) piece of
+user-space memory, which is the basic unit of execution within the syslet
+framework. A syslet represents a single system-call and its arguments.
+In addition it also has condition flags attached to it that allows the
+construction of larger programs (syslets) from these atoms.
+
+Arguments to the system call are implemented via pointers to arguments.
Quoted 8 lines

Documentation/syslet-design.txt line 30

Quoted 5 lines
Documentation/syslet-design.txt new file +5 -0
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).

Why do you need to have an extra memory indirection per parameter in copy_uatom()? [...]

yes. Try to use them in real programs, and you'll see that most of the time the variable an atom wants to access should also be accessed by other atoms. For example a socket file descriptor - one atom opens it, another one reads from it, a third one closes it. By having the parameters in the atoms we'd have to copy the fd to two other places.

but i see your point: i actually had it like that in my earlier versions, only changed it to an indirect method later on, when writing more complex syslets. And, surprisingly, performance of atom handling /improved/ on both Intel and AMD CPUs when i added indirection, because the indirection enables the 'tail NULL' optimization. (which wasnt the goal of indirection, it was just a side-effect)

Quoted 3 lines

[...] It also forces you to have parameters pointed-to, to be "long" (or pointers), instead of their natural POSIX type (like fd being "int" for example). [...]

this wasnt a big problem while coding syslets. I'd also not expect application writers having to do these things on the syscall level - this is a system interface after all. But you do have a point.

Quoted 5 lines

I can understand that chaining syscalls requires variable sharing, but the majority of the parameters passed to syscalls are just direct ones. Maybe a smart method that allows you to know if a parameter is a direct one or a pointer to one? An "unsigned int pmap" where bit N is 1 if param N is an indirection? Hmm?

adding such things tends to slow down atom parsing.

there's another reason as well: i wanted syslets to be like 'instructions' - i.e. not self-modifying. If the fd parameter is embedded in the syslet then every syslet has to be replicated

note that chaining does not necessarily require variable sharing: a sys_umem_add() atom could be used to modify the next syslet's ->fd parameter. So for example

	sys_open() -> returns 'fd'
        sys_umem_add(&atom1->fd) <= atom1->fd is 0 initially
        sys_umem_add(&atom2->fd) <= the first umem_add returns the value
        atom1 [uses fd]
        atom2 [uses fd]

but i didnt like this approach: this means 1 more atom per indirect parameter, and quite some trickery to put the right information into the right place. Furthermore, this makes syslets very much tied to the 'register contents' - instead of them being 'pure instructions/code'.

Quoted 16 lines

Documentation/syslet-design.txt line 75

Quoted 11 lines
Documentation/syslet-design.txt new file +11 -0
+Completion of asynchronous syslets:
+-----------------------------------
+
+Completion of asynchronous syslets is done via the 'completion ring',
+which is a ringbuffer of syslet atom pointers user user-space memory,
+provided by user-space in the sys_async_register() syscall. The
+kernel fills in the ringbuffer starting at index 0, and user-space
+must clear out these pointers. Once the kernel reaches the end of
+the ring it wraps back to index 0. The kernel will not overwrite
+non-NULL pointers (but will return an error), user-space has to
+make sure it completes all events it asked for.

Sigh, I really dislike shared userspace/kernel stuff, when we're transfering pointers to userspace. Did you actually bench it against a:

Quoted 6 lines

int async_wait(struct syslet_uatom **r, int n);

I can fully understand sharing userspace buffers with the kernel, if we're talking about KB transferd during a block or net I/O DMA operation, but for transfering a pointer? Behind each pointer transfer(4/8 bytes) there is a whole syscall execution, [...]

there are three main reasons for this choice:

- firstly, by putting completion events into the user-space ringbuffer the asynchronous contexts are not held up at all, and the threads are available for further syslet use.

- secondly, it was the most obvious and simplest solution to me - it just fits well into the syslet model - which is an execution concept centered around pure user-space memory and system calls, not some kernel resource. Kernel fills in the ringbuffer, user-space clears it. If we had to worry about a handshake between user-space and kernel-space for the completion information to be passed along, that would either mean extra buffering or extra overhead. Extra buffering (in the kernel) would be for no good reason: why not buffer it in the place where the information is destined for in the first place. The ringbuffer of /pointers/ is what makes this really powerful. I never really liked the AIO/etc. method /event buffer/ rings. With syslets the 'cookie' is the pointer to the syslet atom itself. It doesnt get any more straightforward than that i believe.

- making 'is there more stuff for me to work on' a simple instruction in user-space makes it a no-brainer for user-space to promptly and without thinking complete events. It's also the right thing to do on
SMP: if one core is solely dedicated to the asynchronous workload, only running on kernel mode, and the other code is only running user-space, why ever switch between protection domains? [except if any of them is idle] The fastest completion signalling method is the /memory bus/, not an interrupt. User-space could in theory even use MWAIT (in user-space!) to wait for the other core to complete stuff. That makes for a hell of a fast wakeup.

Ingo

#297 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 22 lines · Ingo Molnar
Quoted 10 lines · Davide Libenzi
Quoted 10 lines
Documentation/syslet-design.txt new file +10 -0
+The Syslet Atom:
+----------------
+
+The syslet atom is a small, fixed-size (44 bytes on 32-bit) piece of
+user-space memory, which is the basic unit of execution within the syslet
+framework. A syslet represents a single system-call and its arguments.
+In addition it also has condition flags attached to it that allows the
+construction of larger programs (syslets) from these atoms.
+
+Arguments to the system call are implemented via pointers to arguments.
Quoted 8 lines

Documentation/syslet-design.txt line 30

Quoted 5 lines
Documentation/syslet-design.txt new file +5 -0
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).

Why do you need to have an extra memory indirection per parameter in copy_uatom()? [...]

Quoted 5 lines

yes. Try to use them in real programs, and you'll see that most of the time the variable an atom wants to access should also be accessed by other atoms. For example a socket file descriptor - one atom opens it, another one reads from it, a third one closes it. By having the parameters in the atoms we'd have to copy the fd to two other places.

Yes, of course we have to support the indirection, otherwise chaining won't work. But ...

Quoted 7 lines
Quoted 5 lines

I can understand that chaining syscalls requires variable sharing, but the majority of the parameters passed to syscalls are just direct ones. Maybe a smart method that allows you to know if a parameter is a direct one or a pointer to one? An "unsigned int pmap" where bit N is 1 if param N is an indirection? Hmm?

adding such things tends to slow down atom parsing.

I really think it simplifies it. You simply *copy* the parameter (I'd say that 70+% of cases falls inside here), and if the current "pmap" bit is set, then you do all the indirection copy-from-userspace stuff. It also simplify userspace a lot, since you can now pass arrays and structure pointers directly, w/out saving them in a temporary variable.

Quoted 31 lines
Quoted 10 lines

Sigh, I really dislike shared userspace/kernel stuff, when we're transfering pointers to userspace. Did you actually bench it against a:

int async_wait(struct syslet_uatom **r, int n);

I can fully understand sharing userspace buffers with the kernel, if we're talking about KB transferd during a block or net I/O DMA operation, but for transfering a pointer? Behind each pointer transfer(4/8 bytes) there is a whole syscall execution, [...]

there are three main reasons for this choice:

- firstly, by putting completion events into the user-space ringbuffer the asynchronous contexts are not held up at all, and the threads are available for further syslet use.

- secondly, it was the most obvious and simplest solution to me - it just fits well into the syslet model - which is an execution concept centered around pure user-space memory and system calls, not some kernel resource. Kernel fills in the ringbuffer, user-space clears it. If we had to worry about a handshake between user-space and kernel-space for the completion information to be passed along, that would either mean extra buffering or extra overhead. Extra buffering (in the kernel) would be for no good reason: why not buffer it in the place where the information is destined for in the first place. The ringbuffer of /pointers/ is what makes this really powerful. I never really liked the AIO/etc. method /event buffer/ rings. With syslets the 'cookie' is the pointer to the syslet atom itself. It doesnt get any more straightforward than that i believe.

Quoted 10 lines

- making 'is there more stuff for me to work on' a simple instruction in user-space makes it a no-brainer for user-space to promptly and without thinking complete events. It's also the right thing to do on
SMP: if one core is solely dedicated to the asynchronous workload, only running on kernel mode, and the other code is only running user-space, why ever switch between protection domains? [except if any of them is idle] The fastest completion signalling method is the /memory bus/, not an interrupt. User-space could in theory even use MWAIT (in user-space!) to wait for the other core to complete stuff. That makes for a hell of a fast wakeup.

That makes also for a hell ugly retrieval API IMO ;)
If it'd be backed up but considerable performance gains, then it might be OK. But I believe it won't be the case, and that leave us with an ugly API. OTOH, if noone else object this, it means that I'm the only wierdo :) and the API is just fine.

- Davide

#298 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 6 lines · Davide Libenzi
Quoted 6 lines
Quoted 5 lines

I can understand that chaining syscalls requires variable sharing, but the majority of the parameters passed to syscalls are just direct ones. Maybe a smart method that allows you to know if a parameter is a direct one or a pointer to one? An "unsigned int pmap" where bit N is 1 if param N is an indirection? Hmm?

Quoted 7 lines
Quoted 1 line

adding such things tends to slow down atom parsing.

I really think it simplifies it. You simply *copy* the parameter (I'd say that 70+% of cases falls inside here), and if the current "pmap" bit is set, then you do all the indirection copy-from-userspace stuff. It also simplify userspace a lot, since you can now pass arrays and structure pointers directly, w/out saving them in a temporary variable.

Very rough sketch below ...

---

struct syslet_uatom {
	unsigned long                           flags;
	unsigned int                            nr;
	unsigned short                          nparams;
	unsigned short                          pmap;
	long __user                             *ret_ptr;
	struct syslet_uatom     __user          *next;
	unsigned long           __user          args[6];
	void __user                             *private;
};

long copy_uatom(struct syslet_atom *atom, struct syslet_uatom __user *uatom)

{
	unsigned short i, pmap;
	unsigned long __user *arg_ptr;
	long ret = 0;

	if (!access_ok(VERIFY_WRITE, uatom, sizeof(*uatom)))
		return -EFAULT;

	ret = __get_user(atom->nr, &uatom->nr);
	ret |= __get_user(atom->nparams, &uatom->nparams);
	ret |= __get_user(pmap, &uatom->pmap);
	ret |= __get_user(atom->ret_ptr, &uatom->ret_ptr);
	ret |= __get_user(atom->flags, &uatom->flags);
	ret |= __get_user(atom->next, &uatom->next);
	if (unlikely(atom->nparams >= 6))
		return -EINVAL;
	for (i = 0; i < atom->nparams; i++, pmap >>= 1) {
		atom->args[i] = uatom->args[i];
		if (unlikely(pmap & 1)) {
			arg_ptr = (unsigned long __user *) atom->args[i];
			if (!access_ok(VERIFY_WRITE, arg_ptr, sizeof(*arg_ptr)))
				return -EFAULT;
			ret |= __get_user(atom->args[i], arg_ptr);
		}
	}

	return ret;
}

void init_utaom(struct syslet_uatom *ua, unsigned long flags, unsigned int nr,

		long *ret_ptr, struct syslet_uatom *next, void *private,
		int nparams, ...)
{
	int i, mode;
	va_list args;

	ua->flags = flags;
	ua->nr = nr;
	ua->ret_ptr = ret_ptr;
	ua->next = next;
	ua->private = private;
	ua->nparams = nparams;
	ua->pmap = 0;
	va_start(args, nparams);
	for (i = 0; i < nparams; i++) {
		mode = va_arg(args, int);
		ua->args[i] = va_arg(args, unsigned long);
		if (mode == UASYNC_INDIR)
			ua->pmap |= 1 << i;
	}
	va_end(args);
}

#define UASYNC_IMM 0
#define UASYNC_INDIR 1
#define UAPD(a) UASYNC_IMM, (unsigned long) (a)
#define UAPI(a) UASYNC_INDIR, (unsigned long) (a)

void foo(void)

{
	int fd;
	long res;
	struct stat stb;
	struct syslet_uatom ua;

	init_utaom(&ua, 0, __NR_fstat, &res, NULL, NULL, 2,
		   UAPI(&fd), UAPD(&stb));
	...
}

---

- Davide

#299 lore raw

Re: [patch 06/11] syslets: core, documentation

In reply to Ingo Molnar

Documentation/syslet-design.txt line 36

Quoted 12 lines · Ingo Molnar
Documentation/syslet-design.txt new file +12 -0
+Arguments to the system call are implemented via pointers to arguments.
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).
+
+ [ Note: since the argument array is at the end of the atom, and the
+   kernel will not touch any argument beyond the final NULL one, atoms
+   might be packed more tightly. (the only special case exception to
+   this rule would be SKIP_TO_NEXT_ON_STOP atoms, where the kernel will
+   jump a full syslet_uatom number of bytes.) ]

What if you need to increase the number of arguments passed to a system call later? That would be an API change since the size of syslet_uatom would change?

Also, what if you have an ABI such that:

sys_foo(int fd, long long a)

where:
arg[0] <= fd
arg[1] <= unused
arg[2] <= low 32-bits a
arg[3] <= high 32-bits a

it seems you need to point arg[1] to some valid but dummy variable.

How do you propose syslet users know about these kinds of ABI issues (including the endian-ness of 64-bit arguments) ?

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:
#300 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 17 lines · Russell King

Documentation/syslet-design.txt line 36

Quoted 12 lines · Ingo Molnar
Documentation/syslet-design.txt new file +12 -0
+Arguments to the system call are implemented via pointers to arguments.
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).
+
+ [ Note: since the argument array is at the end of the atom, and the
+   kernel will not touch any argument beyond the final NULL one, atoms
+   might be packed more tightly. (the only special case exception to
+   this rule would be SKIP_TO_NEXT_ON_STOP atoms, where the kernel will
+   jump a full syslet_uatom number of bytes.) ]

What if you need to increase the number of arguments passed to a system call later? That would be an API change since the size of syslet_uatom would change?

the syslet_uatom has a constant size right now, and space for a maximum of 6 arguments. /If/ the user knows that a specific atom (which for example does a sys_close()) takes only 1 argument, it could shrink the size of the atom down by 4 arguments.

[ i'd not actually recommend doing this, because it's generally a
volatile thing to play such tricks - i guess i shouldnt have written that side-note in the header file :-) ]

there should be no new ABI issues: the existing syscall ABI never changes, it's only extended. New syslets can rely on new properties of new system calls. This is quite parallel to how glibc handles system calls.

Quoted 2 lines

How do you propose syslet users know about these kinds of ABI issues (including the endian-ness of 64-bit arguments) ?

syslet users would preferably be libraries like glibc - not applications
- i'm not sure the raw syslet interface should be exposed to applications. Thus my current thinking is that syslets ought to be per-arch structures - no need to pad them out to 64 bits on 32-bit architectures - it's per-arch userspace that makes use of them anyway. system call encodings are fundamentally per-arch anyway - every arch does various fixups and has its own order of system calls.

but ... i'd not be against having a 'generic syscall layer' though, and syslets might be a good starting point for that. But that would necessiate a per-arch table of translating syscall numbers into this 'generic' numbering, at minimum - or a separate sys_async_call_table[].

Ingo

#301 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 24 lines · Ingo Molnar
Quoted 17 lines · Russell King

Documentation/syslet-design.txt line 36

Quoted 12 lines · Ingo Molnar
Documentation/syslet-design.txt new file +12 -0
+Arguments to the system call are implemented via pointers to arguments.
+This not only increases the flexibility of syslet atoms (multiple syslets
+can share the same variable for example), but is also an optimization:
+copy_uatom() will only fetch syscall parameters up until the point it
+meets the first NULL pointer. 50% of all syscalls have 2 or less
+parameters (and 90% of all syscalls have 4 or less parameters).
+
+ [ Note: since the argument array is at the end of the atom, and the
+   kernel will not touch any argument beyond the final NULL one, atoms
+   might be packed more tightly. (the only special case exception to
+   this rule would be SKIP_TO_NEXT_ON_STOP atoms, where the kernel will
+   jump a full syslet_uatom number of bytes.) ]

What if you need to increase the number of arguments passed to a system call later? That would be an API change since the size of syslet_uatom would change?

the syslet_uatom has a constant size right now, and space for a maximum of 6 arguments. /If/ the user knows that a specific atom (which for example does a sys_close()) takes only 1 argument, it could shrink the size of the atom down by 4 arguments.

Quoted 8 lines

[ i'd not actually recommend doing this, because it's generally a
volatile thing to play such tricks - i guess i shouldnt have written that side-note in the header file :-) ]

there should be no new ABI issues: the existing syscall ABI never changes, it's only extended. New syslets can rely on new properties of new system calls. This is quite parallel to how glibc handles system calls.

Let me spell it out, since you appear to have completely missed my point.

At the moment, SKIP_TO_NEXT_ON_STOP is specified to jump a "jump a full syslet_uatom number of bytes".

If we end up with a system call being added which requires more than the currently allowed number of arguments (and it _has_ happened before) then either those syscalls are not accessible to syslets, or you need to increase the arg_ptr array.

That makes syslet_uatom larger.

If syslet_uatom is larger, SKIP_TO_NEXT_ON_STOP increments the syslet_uatom pointer by a greater number of bytes.

If we're running a set of userspace syslets built for an older kernel on such a newer kernel, that is an incompatible change which will break.

Quoted 11 lines
Quoted 2 lines

How do you propose syslet users know about these kinds of ABI issues (including the endian-ness of 64-bit arguments) ?

syslet users would preferably be libraries like glibc - not applications
- i'm not sure the raw syslet interface should be exposed to applications. Thus my current thinking is that syslets ought to be per-arch structures - no need to pad them out to 64 bits on 32-bit architectures - it's per-arch userspace that makes use of them anyway. system call encodings are fundamentally per-arch anyway - every arch does various fixups and has its own order of system calls.

Quoted 4 lines

but ... i'd not be against having a 'generic syscall layer' though, and syslets might be a good starting point for that. But that would necessiate a per-arch table of translating syscall numbers into this 'generic' numbering, at minimum - or a separate sys_async_call_table[].

Okay - I guess the userspace library approach is fine, but it needs to be documented that applications which build syslets directly are going to be non-portable.

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:
#302 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 9 lines · Russell King

Let me spell it out, since you appear to have completely missed my point.

At the moment, SKIP_TO_NEXT_ON_STOP is specified to jump a "jump a full syslet_uatom number of bytes".

If we end up with a system call being added which requires more than the currently allowed number of arguments (and it _has_ happened before) then either those syscalls are not accessible to syslets, or you need to increase the arg_ptr array.

I was thinking about this yesterday, since I honestly thought that this whole chaining, and conditions, and parameter lists, and argoument passed by pointers, etc... was at the end a little clumsy IMO. Wouldn't a syslet look better like:

long syslet(void *ctx) {
	struct sctx *c = ctx;

	if (open(c->file, ...) == -1)
		return -1;
	read();
	send();
	blah();
	...
	return 0;
}

That'd be, instead of passing a chain of atoms, with the kernel interpreting conditions, and parameter lists, etc..., we let gcc do this stuff for us, and we pass the "clet" :) pointer to sys_async_exec, that exec the above under the same schedule-trapped environment, but in userspace. We setup a special userspace ad-hoc frame (ala signal), and we trap underneath task schedule attempt in the same way we do now. We setup the frame and when we return from sys_async_exec, we basically enter the "clet", that will return to a ret_from_async, that will return to userspace. Or, maybe we can support both. A simple single-syscall exec in the way we do now, and a clet way for the ones that requires chains and conditions. Hmmm?

- Davide

#303 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 11 lines · Davide Libenzi

That'd be, instead of passing a chain of atoms, with the kernel interpreting conditions, and parameter lists, etc..., we let gcc do this stuff for us, and we pass the "clet" :) pointer to sys_async_exec, that exec the above under the same schedule-trapped environment, but in userspace. We setup a special userspace ad-hoc frame (ala signal), and we trap underneath task schedule attempt in the same way we do now. We setup the frame and when we return from sys_async_exec, we basically enter the "clet", that will return to a ret_from_async, that will return to userspace. Or, maybe we can support both. A simple single-syscall exec in the way we do now, and a clet way for the ones that requires chains and conditions. Hmmm?

Which is just the same as using threads. My argument is that once you look at all the details involved, what you end up arriving at is the creation of threads. Threads are relatively cheap, it's just that the hardware currently has several performance bugs with them on x86 (and more on x86-64 with the MSR fiddling that hits the hot path). Architectures like powerpc are not going to benefit anywhere near as much from this exercise, as the state involved is processed much more sanely. IA64 as usual is simply doomed by way of having too many registers to switch.

If people really want to go down this path, please make an effort to compare threads on a properly tuned platform. This means that things like the kernel and userland stacks must take into account the cache alignment (we do some of this already, but there are some very definate L1 cache colour collisions between commonly hit data structures amongst threads). The existing AIO ringbuffer suffers from this, as important data is always on the beginning of the first page. Yes, these might be microoptimizations, but accumulated changes of this nature have been known to buy 100%+ improvements in performance.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#304 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 13 lines · Benjamin LaHaise
Quoted 11 lines · Davide Libenzi

That'd be, instead of passing a chain of atoms, with the kernel interpreting conditions, and parameter lists, etc..., we let gcc do this stuff for us, and we pass the "clet" :) pointer to sys_async_exec, that exec the above under the same schedule-trapped environment, but in userspace. We setup a special userspace ad-hoc frame (ala signal), and we trap underneath task schedule attempt in the same way we do now. We setup the frame and when we return from sys_async_exec, we basically enter the "clet", that will return to a ret_from_async, that will return to userspace. Or, maybe we can support both. A simple single-syscall exec in the way we do now, and a clet way for the ones that requires chains and conditions. Hmmm?

Quoted 8 lines

Which is just the same as using threads. My argument is that once you look at all the details involved, what you end up arriving at is the creation of threads. Threads are relatively cheap, it's just that the hardware currently has several performance bugs with them on x86 (and more on x86-64 with the MSR fiddling that hits the hot path). Architectures like powerpc are not going to benefit anywhere near as much from this exercise, as the state involved is processed much more sanely. IA64 as usual is simply doomed by way of having too many registers to switch.

Sort of, except that the whole thing can complete syncronously w/out context switches. The real point of the whole fibrils/syslets solution is that kind of optimization. The solution is as good as it is now, for single syscalls (modulo sys_async_cancel implementation), but for multiple chained submission it kinda stinks IMHO. Once you have to build chains, and conditions, and new syscalls to implement userspace variable increments, and so on..., at that point it's better to have the chain to be coded in C ala thread proc. Yes, it requires a frame setup and another entry to the kernel, but IMO that will be amortized in the cost of the multiple syscalls inside the "clet".

- Davide

#305 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 3 lines · Davide Libenzi

Sort of, except that the whole thing can complete syncronously w/out context switches. The real point of the whole fibrils/syslets solution is that kind of optimization. The solution is as good as it is now, for

Except that You Can't Do That (tm). Try to predict beforehand if the code path being followed will touch the FPU or SSE state, and you can't. There is no way to avoid the context switch overhead, as you have to preserve things so that whatever state is being returned to the user is as it was. Unless you plan on resetting the state beforehand, but then you have to call into arch specific code that ends up with a comparable overhead to the context switch.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#306 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 5 lines · Benjamin LaHaise
Quoted 3 lines · Davide Libenzi

Sort of, except that the whole thing can complete syncronously w/out context switches. The real point of the whole fibrils/syslets solution is that kind of optimization. The solution is as good as it is now, for

Quoted 7 lines

Except that You Can't Do That (tm). Try to predict beforehand if the code path being followed will touch the FPU or SSE state, and you can't. There is no way to avoid the context switch overhead, as you have to preserve things so that whatever state is being returned to the user is as it was. Unless you plan on resetting the state beforehand, but then you have to call into arch specific code that ends up with a comparable overhead to the context switch.

I think you may have mis-interpreted my words. *When* a schedule would block a synco execution try, then you do have a context switch. Noone argue that, and the code is clear. The sys_async_exec thread will block, and a newly woke up thread will re-emerge to sys_async_exec with a NULL returned to userspace. But in a "cachehit" case (no schedule happens during the syscall/*let execution), there is no context switch at all. That is the whole point of the optimization.

- Davide

#307 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 7 lines · Davide Libenzi

I think you may have mis-interpreted my words. *When* a schedule would block a synco execution try, then you do have a context switch. Noone argue that, and the code is clear. The sys_async_exec thread will block, and a newly woke up thread will re-emerge to sys_async_exec with a NULL returned to userspace. But in a "cachehit" case (no schedule happens during the syscall/*let execution), there is no context switch at all. That is the whole point of the optimization.

And I will repeat myself: that cannot be done. Tell me how the following what if scenario works: you're in an MMX optimized memory copy and you take a page fault. How does returning to the submittor of the async operation get the correct MMX state restored? It doesn't.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#308 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 9 lines · Benjamin LaHaise
Quoted 7 lines · Davide Libenzi

I think you may have mis-interpreted my words. *When* a schedule would block a synco execution try, then you do have a context switch. Noone argue that, and the code is clear. The sys_async_exec thread will block, and a newly woke up thread will re-emerge to sys_async_exec with a NULL returned to userspace. But in a "cachehit" case (no schedule happens during the syscall/*let execution), there is no context switch at all. That is the whole point of the optimization.

Quoted 4 lines

And I will repeat myself: that cannot be done. Tell me how the following what if scenario works: you're in an MMX optimized memory copy and you take a page fault. How does returning to the submittor of the async operation get the correct MMX state restored? It doesn't.

Bear with me Ben, and let's follow this up :) If you are in the middle of an MMX copy operation, inside the syscall, you are:

- Userspace, on task A, calls sys_async_exec

- Userspace in _not_ doing any MMX stuff before the call

- We execute the syscall

- Task A, executing the syscall and inside an MMX copy operation, gets a page fault

- We get a schedule

- Task A MMX state will *follow* task A, that will be put to sleep

- We wake task B that will return to userspace

So if the MMX work happens inside the syscall execution, we're fine because its context will follow the same task being put into sleep. Problem would be to preserve the *caller* (userspace) context. But than can be done in a lazy way (detecting if task A user the FPU) like we're currently doing it, once we detect a schedule-out condition. That wouldn't be the most common case for many userspace programs in any case.

- Davide

#309 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 6 lines · Davide Libenzi

Bear with me Ben, and let's follow this up :) If you are in the middle of an MMX copy operation, inside the syscall, you are:

- Userspace, on task A, calls sys_async_exec

- Userspace in _not_ doing any MMX stuff before the call

That's an incorrect assumption. Every task/thread in the system has FPU state associated with it, in part due to the fact that glibc has to change some of the rounding mode bits, making them different than the default from a freshly initialized state.

Quoted 1 line

- We wake task B that will return to userspace

At which point task B has to touch the FPU in userspace as part of the cleanup, which adds back in an expensive operation to the whole process.

The whole context switch mechanism is a zero sum game -- everything that occurs does so because it *must* be done. If you remove something at one point, then it has to occur somewhere else.

My opinion of this whole thread is that it implies that our thread creation and/or context switch is too slow. If that is the case, improve those elements first. At least some of those optimizations have to be done in hardware on x86, while on other platforms are probably unnecessary.

Fwiw, there are patches floating around that did AIO via kernel threads for file descriptors that didn't implement AIO (and remember: kernel thread context switches are cheaper than userland thread context switches). At least take a stab at measuring what the performance differences are and what optimizations are possible before prematurely introducing a new "fast" way of doing things that adds a bunch more to maintain.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#310 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 4 lines · Benjamin LaHaise
Quoted 3 lines · Davide Libenzi

Bear with me Ben, and let's follow this up :) If you are in the middle of an MMX copy operation, inside the syscall, you are:

Quoted 8 lines
Quoted 3 lines

- Userspace, on task A, calls sys_async_exec

- Userspace in _not_ doing any MMX stuff before the call

That's an incorrect assumption. Every task/thread in the system has FPU state associated with it, in part due to the fact that glibc has to change some of the rounding mode bits, making them different than the default from a freshly initialized state.

IMO I still belive this is not a huge problem. FPU state propagation/copy can be done in a clever way, once we detect the in-async condition.

- Davide

#311 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 7 lines · Davide Libenzi
Quoted 4 lines

That's an incorrect assumption. Every task/thread in the system has FPU state associated with it, in part due to the fact that glibc has to change some of the rounding mode bits, making them different than the default from a freshly initialized state.

IMO I still belive this is not a huge problem. FPU state propagation/copy can be done in a clever way, once we detect the in-async condition.

Show me. clts() and stts() are expensive hardware operations which there is no means of avoiding as control register writes impact the CPU in a not trivial manner. I've spent far too much time staring at profiles of what goes on in the context switch code in the process of looking for optimizations on this very issue to be ignored on this point.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#312 lore raw

Re: [patch 06/11] syslets: core, documentation

Quoted 6 lines · Benjamin LaHaise
Quoted 5 lines · Davide Libenzi
Quoted 4 lines

That's an incorrect assumption. Every task/thread in the system has FPU state associated with it, in part due to the fact that glibc has to change some of the rounding mode bits, making them different than the default from a freshly initialized state.

Quoted 8 lines
Quoted 2 lines

IMO I still belive this is not a huge problem. FPU state propagation/copy can be done in a clever way, once we detect the in-async condition.

Show me. clts() and stts() are expensive hardware operations which there is no means of avoiding as control register writes impact the CPU in a not trivial manner. I've spent far too much time staring at profiles of what goes on in the context switch code in the process of looking for optimizations on this very issue to be ignored on this point.

The trivial case is the cachehit case. Everything flows like usual since we don't swap threads.
If we're going to sleep, __async_schedule has to save/copy (depending if TS_USEDFPU is set) the current FPU state to the newly selected service thread (return-to-userspace thread).
When a fault eventually happen in the new userspace thread, context is restored.

- Davide

#313 lore raw

Re: [patch 06/11] syslets: core, documentation

In reply to Benjamin LaHaise
Quoted 4 lines · Benjamin LaHaise

My opinion of this whole thread is that it implies that our thread creation and/or context switch is too slow. If that is the case, improve those elements first. At least some of those optimizations have to be done in hardware on x86, while on other platforms are probably unnecessary.

Not necessarily too slow, but too opaque in terms of system-wide impact and global flow control. Here are the four practical use cases that I have seen come up in this discussion:

1) Databases that want to parallelize I/O storms, with an emphasis on getting results that are already cache-hot immediately (not least so they don't get evicted by other I/O results); there is also room to push some of the I/O clustering and sequencing logic down into the kernel.

2) Static-content-intensive network servers, with an emphasis on servicing those requests that can be serviced promptly (to avoid a ballooning connection backlog) and avoiding duplication of I/O effort when many clients suddenly want the same cold content; the real win may be in "smart prefetch" initiated from outside the network server proper.

3) Network information gathering GUIs, which want to harvest as much information as possible for immediate display and then switch to an event-based delivery mechanism for tardy responses; these need throttling of concurrent requests (ideally, in-kernel traffic shaping by request group and destination class) and efficient cancellation of no-longer-interesting requests.

4) Document search facilities, which need all of the above (big surprise there) as well as a rich diagnostic toolset, including a practical snooping and profiling facility to guide tuning for application responsiveness.

Even if threads were so cheap that you could just fire off one per I/O request, they're a poor solution to the host of flow control issues raised in these use cases. A sequential thread of execution per I/O request may be the friendliest mental model for the individual delayed I/Os, but the global traffic shaping and scheduling is a data structure problem.

The right question to be asking is, what are the operations that need to be supported on the system-wide pool of pending AIOs, and on what data structure can they be implemented efficiently? For instance, can we provide an RCU priority queue implementation (perhaps based on splay trees) so that userspace can scan a coherent read-only snapshot of the structure and select candidates for cancellation, etc., without interfering with kernel completions? Or is it more important to have a three-sided query operation (characteristic of priority search trees), or perhaps a lower amortized cost bulk delete?

Once you've thought through the data structure manipulation, you'll know what AIO submission / cancellation / reprioritization interfaces are practical. Then you can work on a programming model for application-level "I/O completions" that is library-friendly and allows a "fast path" optimization for the fraction of requests that can be served synchronously. Then and only then does it make sense to code-bum the asynchronous path. Not that it isn't interesting to think in advance about what stack space completions will run in and which bits of the task struct needn't be in a coherent condition; but that's probably not going to guide you to the design that meets the application needs.

I know I'm teaching my grandmother to suck eggs here. But there are application programmers high up the code stack whose code makes implicit use of asynchronous I/O continuations. In addition to the GUI example I blathered about a few days ago, I have in mind Narrative Javascript's "blocking operator" and Twisted Python's Deferred. Those folks would be well served by an async I/O interface to the kernel which mates well to their language's closure/continuation facilities. If it's usable from C, that's nice too. :-)

Cheers,
- Michael

#314 lore raw

PATCH 1/1 x86: split FPU state from task state

In reply to Benjamin LaHaise
Quoted 10 lines · Benjamin LaHaise
Quoted 8 lines · Davide Libenzi

I think you may have mis-interpreted my words. *When* a schedule would block a synco execution try, then you do have a context switch. Noone argue that, and the code is clear. The sys_async_exec thread will block, and a newly woke up thread will re-emerge to sys_async_exec with a NULL returned to userspace. But in a "cachehit" case (no schedule happens during the syscall/*let execution), there is no context switch at all. That is the whole point of the optimization.

Quoted 5 lines

And I will repeat myself: that cannot be done. Tell me how the following what if scenario works: you're in an MMX optimized memory copy and you take a page fault. How does returning to the submittor of the async operation get the correct MMX state restored? It doesn't.

this can very much be done, with a straightforward extension of how we handle FPU state. That makes sense independently of syslets/async as well, so find below the standalone patch from Arjan. It's in my current syslet queue and works great.

Ingo

------------------------>
Subject: [patch] x86: split FPU state from task state
From: Arjan van de Ven <[email protected]>

Split the FPU save area from the task struct. This allows easy migration of FPU context, and it's generally cleaner. It also allows the following two (future) optimizations:

1) allocate the right size for the actual cpu rather than 512 bytes always
2) only allocate when the application actually uses FPU, so in the first lazy FPU trap. This could save memory for non-fpu using apps.

---
 arch/i386/kernel/i387.c        |   96 ++++++++++++++++++++---------------------
 arch/i386/kernel/process.c     |   56 +++++++++++++++++++++++
 arch/i386/kernel/traps.c       |   10 ----
 include/asm-i386/i387.h        |    6 +-
 include/asm-i386/processor.h   |    6 ++
 include/asm-i386/thread_info.h |    6 ++
 kernel/fork.c                  |    7 ++
 7 files changed, 123 insertions(+), 64 deletions(-)
arch/i386/kernel/i387.c +48 -48
Index: linux/arch/i386/kernel/i387.c
===================================================================
@@ -31,9 +31,9 @@ void mxcsr_feature_mask_init(void)
 	unsigned long mask = 0;
 	clts();
 	if (cpu_has_fxsr) {
-		memset(&current->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
-		asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave)); 
-		mask = current->thread.i387.fxsave.mxcsr_mask;
+		memset(&current->thread.i387->fxsave, 0, sizeof(struct i387_fxsave_struct));
+		asm volatile("fxsave %0" : : "m" (current->thread.i387->fxsave));
+		mask = current->thread.i387->fxsave.mxcsr_mask;
 		if (mask == 0) mask = 0x0000ffbf;
 	} 
 	mxcsr_feature_mask &= mask;
@@ -49,16 +49,16 @@ void mxcsr_feature_mask_init(void)
 void init_fpu(struct task_struct *tsk)
 {
 	if (cpu_has_fxsr) {
-		memset(&tsk->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
-		tsk->thread.i387.fxsave.cwd = 0x37f;
+		memset(&tsk->thread.i387->fxsave, 0, sizeof(struct i387_fxsave_struct));
+		tsk->thread.i387->fxsave.cwd = 0x37f;
 		if (cpu_has_xmm)
-			tsk->thread.i387.fxsave.mxcsr = 0x1f80;
+			tsk->thread.i387->fxsave.mxcsr = 0x1f80;
 	} else {
-		memset(&tsk->thread.i387.fsave, 0, sizeof(struct i387_fsave_struct));
-		tsk->thread.i387.fsave.cwd = 0xffff037fu;
-		tsk->thread.i387.fsave.swd = 0xffff0000u;
-		tsk->thread.i387.fsave.twd = 0xffffffffu;
-		tsk->thread.i387.fsave.fos = 0xffff0000u;
+		memset(&tsk->thread.i387->fsave, 0, sizeof(struct i387_fsave_struct));
+		tsk->thread.i387->fsave.cwd = 0xffff037fu;
+		tsk->thread.i387->fsave.swd = 0xffff0000u;
+		tsk->thread.i387->fsave.twd = 0xffffffffu;
+		tsk->thread.i387->fsave.fos = 0xffff0000u;
 	}
 	/* only the device not available exception or ptrace can call init_fpu */
 	set_stopped_child_used_math(tsk);
@@ -152,18 +152,18 @@ static inline unsigned long twd_fxsr_to_
 unsigned short get_fpu_cwd( struct task_struct *tsk )
 {
 	if ( cpu_has_fxsr ) {
-		return tsk->thread.i387.fxsave.cwd;
+		return tsk->thread.i387->fxsave.cwd;
 	} else {
-		return (unsigned short)tsk->thread.i387.fsave.cwd;
+		return (unsigned short)tsk->thread.i387->fsave.cwd;
 	}
 }
 
 unsigned short get_fpu_swd( struct task_struct *tsk )
 {
 	if ( cpu_has_fxsr ) {
-		return tsk->thread.i387.fxsave.swd;
+		return tsk->thread.i387->fxsave.swd;
 	} else {
-		return (unsigned short)tsk->thread.i387.fsave.swd;
+		return (unsigned short)tsk->thread.i387->fsave.swd;
 	}
 }
 
@@ -171,9 +171,9 @@ unsigned short get_fpu_swd( struct task_
 unsigned short get_fpu_twd( struct task_struct *tsk )
 {
 	if ( cpu_has_fxsr ) {
-		return tsk->thread.i387.fxsave.twd;
+		return tsk->thread.i387->fxsave.twd;
 	} else {
-		return (unsigned short)tsk->thread.i387.fsave.twd;
+		return (unsigned short)tsk->thread.i387->fsave.twd;
 	}
 }
 #endif  /*  0  */
@@ -181,7 +181,7 @@ unsigned short get_fpu_twd( struct task_
 unsigned short get_fpu_mxcsr( struct task_struct *tsk )
 {
 	if ( cpu_has_xmm ) {
-		return tsk->thread.i387.fxsave.mxcsr;
+		return tsk->thread.i387->fxsave.mxcsr;
 	} else {
 		return 0x1f80;
 	}
@@ -192,27 +192,27 @@ unsigned short get_fpu_mxcsr( struct tas
 void set_fpu_cwd( struct task_struct *tsk, unsigned short cwd )
 {
 	if ( cpu_has_fxsr ) {
-		tsk->thread.i387.fxsave.cwd = cwd;
+		tsk->thread.i387->fxsave.cwd = cwd;
 	} else {
-		tsk->thread.i387.fsave.cwd = ((long)cwd | 0xffff0000u);
+		tsk->thread.i387->fsave.cwd = ((long)cwd | 0xffff0000u);
 	}
 }
 
 void set_fpu_swd( struct task_struct *tsk, unsigned short swd )
 {
 	if ( cpu_has_fxsr ) {
-		tsk->thread.i387.fxsave.swd = swd;
+		tsk->thread.i387->fxsave.swd = swd;
 	} else {
-		tsk->thread.i387.fsave.swd = ((long)swd | 0xffff0000u);
+		tsk->thread.i387->fsave.swd = ((long)swd | 0xffff0000u);
 	}
 }
 
 void set_fpu_twd( struct task_struct *tsk, unsigned short twd )
 {
 	if ( cpu_has_fxsr ) {
-		tsk->thread.i387.fxsave.twd = twd_i387_to_fxsr(twd);
+		tsk->thread.i387->fxsave.twd = twd_i387_to_fxsr(twd);
 	} else {
-		tsk->thread.i387.fsave.twd = ((long)twd | 0xffff0000u);
+		tsk->thread.i387->fsave.twd = ((long)twd | 0xffff0000u);
 	}
 }
 
@@ -298,8 +298,8 @@ static inline int save_i387_fsave( struc
 	struct task_struct *tsk = current;
 
 	unlazy_fpu( tsk );
-	tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
-	if ( __copy_to_user( buf, &tsk->thread.i387.fsave,
+	tsk->thread.i387->fsave.status = tsk->thread.i387->fsave.swd;
+	if ( __copy_to_user( buf, &tsk->thread.i387->fsave,
 			     sizeof(struct i387_fsave_struct) ) )
 		return -1;
 	return 1;
@@ -312,15 +312,15 @@ static int save_i387_fxsave( struct _fps
 
 	unlazy_fpu( tsk );
 
-	if ( convert_fxsr_to_user( buf, &tsk->thread.i387.fxsave ) )
+	if ( convert_fxsr_to_user( buf, &tsk->thread.i387->fxsave ) )
 		return -1;
 
-	err |= __put_user( tsk->thread.i387.fxsave.swd, &buf->status );
+	err |= __put_user( tsk->thread.i387->fxsave.swd, &buf->status );
 	err |= __put_user( X86_FXSR_MAGIC, &buf->magic );
 	if ( err )
 		return -1;
 
-	if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
+	if ( __copy_to_user( &buf->_fxsr_env[0], &tsk->thread.i387->fxsave,
 			     sizeof(struct i387_fxsave_struct) ) )
 		return -1;
 	return 1;
@@ -343,7 +343,7 @@ int save_i387( struct _fpstate __user *b
 			return save_i387_fsave( buf );
 		}
 	} else {
-		return save_i387_soft( &current->thread.i387.soft, buf );
+		return save_i387_soft( &current->thread.i387->soft, buf );
 	}
 }
 
@@ -351,7 +351,7 @@ static inline int restore_i387_fsave( st
 {
 	struct task_struct *tsk = current;
 	clear_fpu( tsk );
-	return __copy_from_user( &tsk->thread.i387.fsave, buf,
+	return __copy_from_user( &tsk->thread.i387->fsave, buf,
 				 sizeof(struct i387_fsave_struct) );
 }
 
@@ -360,11 +360,11 @@ static int restore_i387_fxsave( struct _
 	int err;
 	struct task_struct *tsk = current;
 	clear_fpu( tsk );
-	err = __copy_from_user( &tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
+	err = __copy_from_user( &tsk->thread.i387->fxsave, &buf->_fxsr_env[0],
 				sizeof(struct i387_fxsave_struct) );
 	/* mxcsr reserved bits must be masked to zero for security reasons */
-	tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
-	return err ? 1 : convert_fxsr_from_user( &tsk->thread.i387.fxsave, buf );
+	tsk->thread.i387->fxsave.mxcsr &= mxcsr_feature_mask;
+	return err ? 1 : convert_fxsr_from_user( &tsk->thread.i387->fxsave, buf );
 }
 
 int restore_i387( struct _fpstate __user *buf )
@@ -378,7 +378,7 @@ int restore_i387( struct _fpstate __user
 			err = restore_i387_fsave( buf );
 		}
 	} else {
-		err = restore_i387_soft( &current->thread.i387.soft, buf );
+		err = restore_i387_soft( &current->thread.i387->soft, buf );
 	}
 	set_used_math();
 	return err;
@@ -391,7 +391,7 @@ int restore_i387( struct _fpstate __user
 static inline int get_fpregs_fsave( struct user_i387_struct __user *buf,
 				    struct task_struct *tsk )
 {
-	return __copy_to_user( buf, &tsk->thread.i387.fsave,
+	return __copy_to_user( buf, &tsk->thread.i387->fsave,
 			       sizeof(struct user_i387_struct) );
 }
 
@@ -399,7 +399,7 @@ static inline int get_fpregs_fxsave( str
 				     struct task_struct *tsk )
 {
 	return convert_fxsr_to_user( (struct _fpstate __user *)buf,
-				     &tsk->thread.i387.fxsave );
+				     &tsk->thread.i387->fxsave );
 }
 
 int get_fpregs( struct user_i387_struct __user *buf, struct task_struct *tsk )
@@ -411,7 +411,7 @@ int get_fpregs( struct user_i387_struct 
 			return get_fpregs_fsave( buf, tsk );
 		}
 	} else {
-		return save_i387_soft( &tsk->thread.i387.soft,
+		return save_i387_soft( &tsk->thread.i387->soft,
 				       (struct _fpstate __user *)buf );
 	}
 }
@@ -419,14 +419,14 @@ int get_fpregs( struct user_i387_struct 
 static inline int set_fpregs_fsave( struct task_struct *tsk,
 				    struct user_i387_struct __user *buf )
 {
-	return __copy_from_user( &tsk->thread.i387.fsave, buf,
+	return __copy_from_user( &tsk->thread.i387->fsave, buf,
 				 sizeof(struct user_i387_struct) );
 }
 
 static inline int set_fpregs_fxsave( struct task_struct *tsk,
 				     struct user_i387_struct __user *buf )
 {
-	return convert_fxsr_from_user( &tsk->thread.i387.fxsave,
+	return convert_fxsr_from_user( &tsk->thread.i387->fxsave,
 				       (struct _fpstate __user *)buf );
 }
 
@@ -439,7 +439,7 @@ int set_fpregs( struct task_struct *tsk,
 			return set_fpregs_fsave( tsk, buf );
 		}
 	} else {
-		return restore_i387_soft( &tsk->thread.i387.soft,
+		return restore_i387_soft( &tsk->thread.i387->soft,
 					  (struct _fpstate __user *)buf );
 	}
 }
@@ -447,7 +447,7 @@ int set_fpregs( struct task_struct *tsk,
 int get_fpxregs( struct user_fxsr_struct __user *buf, struct task_struct *tsk )
 {
 	if ( cpu_has_fxsr ) {
-		if (__copy_to_user( buf, &tsk->thread.i387.fxsave,
+		if (__copy_to_user( buf, &tsk->thread.i387->fxsave,
 				    sizeof(struct user_fxsr_struct) ))
 			return -EFAULT;
 		return 0;
@@ -461,11 +461,11 @@ int set_fpxregs( struct task_struct *tsk
 	int ret = 0;
 
 	if ( cpu_has_fxsr ) {
-		if (__copy_from_user( &tsk->thread.i387.fxsave, buf,
+		if (__copy_from_user( &tsk->thread.i387->fxsave, buf,
 				  sizeof(struct user_fxsr_struct) ))
 			ret = -EFAULT;
 		/* mxcsr reserved bits must be masked to zero for security reasons */
-		tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
+		tsk->thread.i387->fxsave.mxcsr &= mxcsr_feature_mask;
 	} else {
 		ret = -EIO;
 	}
@@ -479,7 +479,7 @@ int set_fpxregs( struct task_struct *tsk
 static inline void copy_fpu_fsave( struct task_struct *tsk,
 				   struct user_i387_struct *fpu )
 {
-	memcpy( fpu, &tsk->thread.i387.fsave,
+	memcpy( fpu, &tsk->thread.i387->fsave,
 		sizeof(struct user_i387_struct) );
 }
 
@@ -490,10 +490,10 @@ static inline void copy_fpu_fxsave( stru
 	unsigned short *from;
 	int i;
 
-	memcpy( fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long) );
+	memcpy( fpu, &tsk->thread.i387->fxsave, 7 * sizeof(long) );
 
 	to = (unsigned short *)&fpu->st_space[0];
-	from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
+	from = (unsigned short *)&tsk->thread.i387->fxsave.st_space[0];
 	for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
 		memcpy( to, from, 5 * sizeof(unsigned short) );
 	}
@@ -540,7 +540,7 @@ int dump_task_extended_fpu(struct task_s
 	if (fpvalid) {
 		if (tsk == current)
 		       unlazy_fpu(tsk);
-		memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
+		memcpy(fpu, &tsk->thread.i387->fxsave, sizeof(*fpu));
 	}
 	return fpvalid;
 }
arch/i386/kernel/process.c +55 -1

65 lines not on this page · raw message

arch/i386/kernel/traps.c +0 -10
Index: linux/arch/i386/kernel/traps.c
===================================================================
@@ -1154,16 +1154,6 @@ void __init trap_init(void)
 	set_trap_gate(19,&simd_coprocessor_error);
 
 	if (cpu_has_fxsr) {
-		/*
-		 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
-		 * Generates a compile-time "error: zero width for bit-field" if
-		 * the alignment is wrong.
-		 */
-		struct fxsrAlignAssert {
-			int _:!(offsetof(struct task_struct,
-					thread.i387.fxsave) & 15);
-		};
-
 		printk(KERN_INFO "Enabling fast FPU save and restore... ");
 		set_in_cr4(X86_CR4_OSFXSR);
 		printk("done.\n");
include/asm-i386/i387.h +3 -3
Index: linux/include/asm-i386/i387.h
===================================================================
@@ -34,7 +34,7 @@ extern void init_fpu(struct task_struct 
 		"nop ; frstor %1",		\
 		"fxrstor %1",			\
 		X86_FEATURE_FXSR,		\
-		"m" ((tsk)->thread.i387.fxsave))
+		"m" ((tsk)->thread.i387->fxsave))
 
 extern void kernel_fpu_begin(void);
 #define kernel_fpu_end() do { stts(); preempt_enable(); } while(0)
@@ -60,8 +60,8 @@ static inline void __save_init_fpu( stru
 		"fxsave %[fx]\n"
 		"bt $7,%[fsw] ; jnc 1f ; fnclex\n1:",
 		X86_FEATURE_FXSR,
-		[fx] "m" (tsk->thread.i387.fxsave),
-		[fsw] "m" (tsk->thread.i387.fxsave.swd) : "memory");
+		[fx] "m" (tsk->thread.i387->fxsave),
+		[fsw] "m" (tsk->thread.i387->fxsave.swd) : "memory");
 	/* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
 	   is pending.  Clear the x87 state here by setting it to fixed
    	   values. safe_address is a random variable that should be in L1 */
include/asm-i386/processor.h +5 -1
Index: linux/include/asm-i386/processor.h
===================================================================
@@ -407,7 +407,7 @@ struct thread_struct {
 /* fault info */
 	unsigned long	cr2, trap_no, error_code;
 /* floating point info */
-	union i387_union	i387;
+	union i387_union	*i387;
 /* virtual 86 mode info */
 	struct vm86_struct __user * vm86_info;
 	unsigned long		screen_bitmap;
@@ -420,11 +420,15 @@ struct thread_struct {
 	unsigned long	io_bitmap_max;
 };
 
+
+extern union i387_union init_i387_context;
+
 #define INIT_THREAD  {							\
 	.vm86_info = NULL,						\
 	.sysenter_cs = __KERNEL_CS,					\
 	.io_bitmap_ptr = NULL,						\
 	.gs = __KERNEL_PDA,						\
+	.i387 = &init_i387_context,					\
 }
 
 /*
include/asm-i386/thread_info.h +6 -0
Index: linux/include/asm-i386/thread_info.h
===================================================================
@@ -102,6 +102,12 @@ static inline struct thread_info *curren
 
 #define free_thread_info(info)	kfree(info)
 
+#define __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
+extern struct task_struct * alloc_task_struct(void);
+extern void free_task_struct(struct task_struct *tsk);
+extern void memcpy_task_struct(struct task_struct *dst, struct task_struct *src);
+extern void task_struct_slab_init(void);
+
 #else /* !__ASSEMBLY__ */
 
 /* how to get the thread information struct from ASM */
kernel/fork.c +6 -1
Index: linux/kernel/fork.c
===================================================================
@@ -83,6 +83,8 @@ int nr_processes(void)
 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
 # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
 # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
+# define memcpy_task_struct(dst, src) *dst = *src;
+
 static struct kmem_cache *task_struct_cachep;
 #endif
 
@@ -137,6 +139,8 @@ void __init fork_init(unsigned long memp
 	task_struct_cachep =
 		kmem_cache_create("task_struct", sizeof(struct task_struct),
 			ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
+#else
+	task_struct_slab_init();
 #endif
 
 	/*
@@ -175,7 +179,8 @@ static struct task_struct *dup_task_stru
 		return NULL;
 	}
 
-	*tsk = *orig;
+	memcpy_task_struct(tsk, orig);
+
 	tsk->thread_info = ti;
 	setup_thread_stack(tsk, orig);
 
#315 lore raw

Re: [patch] x86: split FPU state from task state

Quoted 4 lines · Ingo Molnar

this can very much be done, with a straightforward extension of how we handle FPU state. That makes sense independently of syslets/async as well, so find below the standalone patch from Arjan. It's in my current syslet queue and works great.

That patch adds a bunch of memory dereferences and another allocation to the thread creation code path -- a tax that all users must pay. Granted, it's small, but at the very least should be configurable out for the 99.9% of users that will never use this functionality.

I'm willing to be convinced, it's just that I would like to see some numbers that scream out that this is a good thing.

-ben

#316 lore raw

Re: [patch] x86: split FPU state from task state

Quoted 7 lines · Benjamin LaHaise
Quoted 4 lines · Ingo Molnar

this can very much be done, with a straightforward extension of how we handle FPU state. That makes sense independently of syslets/async as well, so find below the standalone patch from Arjan. It's in my current syslet queue and works great.

That patch adds a bunch of memory dereferences

not really; you missed that most of the ->'s are actually just going to members of the union and aren't actually extra dereference.

Quoted 2 lines

and another allocation
to the thread creation code path -- a tax that all users must pay.

so the next step, as mentioned in the changelog, to allocate only on the first FPU fault, so that it becomes a GAIN for everyone, since only threads that use FPU will use the memory.

The second gain (although only on old cpus) is that you only need to allocate enough memory for your cpu, rather than 512 always.

Signature · 2 lines
if you want to mail me at work (you don't), use arjan (at) linux.intel.com
Test the interaction between Linux and your BIOS via http://www.linuxfirmwarekit.org
#317 lore raw

PATCH 7/11 syslets: x86, add create_async_thread() method

In reply to Ingo Molnar

From: Ingo Molnar <[email protected]>

add the create_async_thread() way of creating kernel threads:
these threads first execute a kernel function and when they return from it they execute user-space.

An architecture must implement this interface before it can turn CONFIG_ASYNC_SUPPORT on.

---
 arch/i386/kernel/entry.S     |   25 +++++++++++++++++++++++++
 arch/i386/kernel/process.c   |   31 +++++++++++++++++++++++++++++++
 include/asm-i386/processor.h |    5 +++++
 3 files changed, 61 insertions(+)
arch/i386/kernel/entry.S +25 -0
Index: linux/arch/i386/kernel/entry.S
===================================================================
@@ -996,6 +996,31 @@ ENTRY(kernel_thread_helper)
 	CFI_ENDPROC
 ENDPROC(kernel_thread_helper)
 
+ENTRY(async_thread_helper)
+	CFI_STARTPROC
+	/*
+	 * Allocate space on the stack for pt-regs.
+	 * sizeof(struct pt_regs) == 64, and we've got 8 bytes on the
+	 * kernel stack already:
+	 */
+	subl $64-8, %esp
+	CFI_ADJUST_CFA_OFFSET 64
+	movl %edx,%eax
+	push %edx
+	CFI_ADJUST_CFA_OFFSET 4
+	call *%ebx
+	addl $4, %esp
+	CFI_ADJUST_CFA_OFFSET -4
+
+	movl %eax, PT_EAX(%esp)
+
+	GET_THREAD_INFO(%ebp)
+
+	jmp syscall_exit
+	CFI_ENDPROC
+ENDPROC(async_thread_helper)
+
+
 .section .rodata,"a"
 #include "syscall_table.S"
 
arch/i386/kernel/process.c +31 -0
Index: linux/arch/i386/kernel/process.c
===================================================================
@@ -352,6 +352,37 @@ int kernel_thread(int (*fn)(void *), voi
 EXPORT_SYMBOL(kernel_thread);
 
 /*
+ * This gets run with %ebx containing the
+ * function to call, and %edx containing
+ * the "args".
+ */
+extern void async_thread_helper(void);
+
+/*
+ * Create an async thread
+ */
+int create_async_thread(int (*fn)(void *), void * arg, unsigned long flags)
+{
+	struct pt_regs regs;
+
+	memset(&regs, 0, sizeof(regs));
+
+	regs.ebx = (unsigned long) fn;
+	regs.edx = (unsigned long) arg;
+
+	regs.xds = __USER_DS;
+	regs.xes = __USER_DS;
+	regs.xgs = __KERNEL_PDA;
+	regs.orig_eax = -1;
+	regs.eip = (unsigned long) async_thread_helper;
+	regs.xcs = __KERNEL_CS | get_kernel_rpl();
+	regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
+
+	/* Ok, create the new task.. */
+	return do_fork(flags | CLONE_VM, 0, &regs, 0, NULL, NULL);
+}
+
+/*
  * Free current thread data structures etc..
  */
 void exit_thread(void)
include/asm-i386/processor.h +5 -0
Index: linux/include/asm-i386/processor.h
===================================================================
@@ -468,6 +468,11 @@ extern void prepare_to_copy(struct task_
  */
 extern int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
 
+/*
+ * create an async thread:
+ */
+extern int create_async_thread(int (*fn)(void *), void * arg, unsigned long flags);
+
 extern unsigned long thread_saved_pc(struct task_struct *tsk);
 void show_trace(struct task_struct *task, struct pt_regs *regs, unsigned long *stack);
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#319 lore raw

Re: [patch 19/31] clockevents: i386 drivers

Quoted 11 lines · On Wed, 13 Dec 2006 14:02:11 +0100 Ingo Molnar <[email protected]> wrote:

Add clockevent drivers for i386: lapic (local) and PIT (global). Update the timer IRQ to call into the PIT driver's event handler and the lapic-timer IRQ to call into the lapic clockevent driver. The assignement of timer functionality is delegated to the core framework code and replaces the compile and runtime evalution in do_timer_interrupt_hook()

Use the clockevents broadcast support and implement the lapic_broadcast function for ACPI.

No changes to existing functionality.

This patch breaks the NMI on my crufty old dual-PIII supermicro p6dbe machine:

Testing NMI watchdog ... CPU#0: NMI appears to be stuck (26->26)! CPU#1: NMI appears to be stuck (0->0)!

vmm:/home/akpm> cat /proc/interrupts
CPU0 CPU1

  0:         59          0   IO-APIC-edge      timer
  1:          2          0   IO-APIC-edge      i8042
  2:          0          0    XT-PIC-XT        cascade
  6:          3          0   IO-APIC-edge      floppy
  8:          1          0   IO-APIC-edge      rtc
 10:        192         61   IO-APIC-fasteoi   aic7xxx
 11:       1339         31   IO-APIC-fasteoi   eth0
 12:          3          1   IO-APIC-edge      i8042
 15:       3067          7   IO-APIC-edge      ide1
NMI:         26          0 
LOC:      58665      58663 
ERR:          0
MIS:          0

and it isn't changing.

See http://userweb.kernel.org/~akpm/nmi-prob/