Skip to contentSkip to conversation

kernel:async function call:introduce async_run_inatomic(v2)

PATCH In review [email protected] 4 participants 6 messages May 13, 2009 – May 18, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel:async function call:introduce async_run_inatomic(v2)

From: Ming Lei <[email protected]>

The purpose of this function is to offer a simple way to schedule an asynchronous thread from an atomic context, because there are not any functions which can create and start a kernel thread in atomic contexts now. We use async_running_no_sync as running list to make callers that don't want to synchronize on cookies, so can avoid some side effects for async_synchronize_full().

Part of note for async_run_inatomic and some guideline is from Cornelia Huck.

---
 include/linux/async.h |    2 ++
 kernel/async.c        |   23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
include/linux/async.h +2 -0
index 18ce92b..9ff62af 100644
@@ -16,6 +16,8 @@
 typedef u64 async_cookie_t;
 typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 
+extern int async_run_inatomic(async_func_ptr *ptr, void *data);
+
 extern async_cookie_t async_schedule(async_func_ptr *ptr, void *data);
 extern async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
 					    struct list_head *list);
kernel/async.c +23 -0
index 8663f5b..cb527b3 100644
@@ -65,6 +65,7 @@ static async_cookie_t next_cookie = 1;
 
 static LIST_HEAD(async_pending);
 static LIST_HEAD(async_running);
+static LIST_HEAD(async_running_no_sync);
 static DEFINE_SPINLOCK(async_lock);
 
 static int async_enabled = 0;
@@ -222,6 +223,28 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,
 	return newcookie;
 }
 
+ /**
+ * async_run_inatomic - in atomic contexts schedule a function for
+ * 			asynchronous execution
+ *
+ * @ptr: function to execute asynchronously
+ * @data: data pointer to pass to the function
+ *
+ * Return zero if success, or else return others if failured
+ * Note:
+ * The purpose of this function is to offer a simple way to schedule an
+ * asynchronous thread from an atomic context, because there are not any
+ * functions which can create and start a kernel thread in atomic contexts
+ * now. We use async_running_no_sync as running list to make callers that
+ * don't want to synchronize on cookies, so can avoid some side effects for
+ * async_synchronize_full().
+ */
+int async_run_inatomic(async_func_ptr *ptr, void *data)
+{
+	return !__async_schedule(ptr, data, &async_running_no_sync, 1);
+}
+EXPORT_SYMBOL_GPL(async_run_inatomic);
+
 /**
  * async_schedule - schedule a function for asynchronous execution
  * @ptr: function to execute asynchronously
Signature · 1 line
1.6.0.GIT
#1 lore raw
Quoted 44 lines · On Wed, 13 May 2009 23:51:39 +0800, [email protected] wrote:

From: Ming Lei <[email protected]>

The purpose of this function is to offer a simple way to schedule an asynchronous thread from an atomic context, because there are not any functions which can create and start a kernel thread in atomic contexts now. We use async_running_no_sync as running list to make callers that don't want to synchronize on cookies, so can avoid some side effects for async_synchronize_full().

Part of note for async_run_inatomic and some guideline is from Cornelia Huck.

---
 include/linux/async.h |    2 ++
 kernel/async.c        |   23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
include/linux/async.h +2 -0
index 18ce92b..9ff62af 100644
@@ -16,6 +16,8 @@
 typedef u64 async_cookie_t;
 typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 
+extern int async_run_inatomic(async_func_ptr *ptr, void *data);
+
 extern async_cookie_t async_schedule(async_func_ptr *ptr, void *data);
 extern async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
 					    struct list_head *list);
kernel/async.c +1 -0
index 8663f5b..cb527b3 100644
@@ -65,6 +65,7 @@ static async_cookie_t next_cookie = 1;
 
 static LIST_HEAD(async_pending);
 static LIST_HEAD(async_running);
+static LIST_HEAD(async_running_no_sync);
 static DEFINE_SPINLOCK(async_lock);
 
 static int async_enabled = 0;

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 228

Quoted 6 lines
kernel/async.c +3 -0
@@ -222,6 +223,28 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,
 	return newcookie;
 }
 
+ /**
+ * async_run_inatomic - in atomic contexts schedule a function for
+ * 			asynchronous execution

I'm not 100% sure whether kerneldoc likes split lines for the description.

"async_run_inatomic - schedule a function for asynchronous execution from atomic context without checkpointing"
- but that's really a bit long...

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 233

Quoted 5 lines
kernel/async.c +5 -0
+ *
+ * @ptr: function to execute asynchronously
+ * @data: data pointer to pass to the function
+ *
+ * Return zero if success, or else return others if failured

Returns zero on success, !zero on failure

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 234

Quoted 1 line
kernel/async.c +1 -0
+ * Note:

I'd move the description of the function's purpose before Note:.

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,

Quoted 4 lines
kernel/async.c +4 -0
+ * The purpose of this function is to offer a simple way to schedule an
+ * asynchronous thread from an atomic context, because there are not any
+ * functions which can create and start a kernel thread in atomic contexts
+ * now. 

I don't think the second part of the sentence is needed, this should only be mentioned in the patch description. I'd rather add

"Since it does not return a cookie for checkpointing, it is for callers that don't need later synchronization."

Quoted 3 lines

We use async_running_no_sync as running list to make callers that
+ * don't want to synchronize on cookies, so can avoid some side effects for
+ * async_synchronize_full().

"async_run_inatomic() uses a distinct running list in order to avoid slowing down synchronization within the general domain."

(The cookie will still be checked when __lowest_in_progress() returns the cookie for the async_pending list, but I think that is negligible - and it would be a general issue for special domains.)

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 250

Quoted 10 lines

+ */

kernel/async.c +6 -0
+int async_run_inatomic(async_func_ptr *ptr, void *data)
+{
+	return !__async_schedule(ptr, data, &async_running_no_sync, 1);
+}
+EXPORT_SYMBOL_GPL(async_run_inatomic);
+
 /**
  * async_schedule - schedule a function for asynchronous execution
  * @ptr: function to execute asynchronously

Btw: Do you have any specific use cases for this function in mind?

#2 lore raw
Quoted 94 lines · Cornelia Huck
Quoted 44 lines · On Wed, 13 May 2009 23:51:39 +0800, [email protected] wrote:

From: Ming Lei <[email protected]>

The purpose of this function is to offer a simple way to schedule an asynchronous thread from an atomic context, because there are not any functions which can create and start a kernel thread in atomic contexts now. We use async_running_no_sync as running list to make callers that don't want to synchronize on cookies, so can avoid some side effects for async_synchronize_full().

Part of note for async_run_inatomic and some guideline is from Cornelia Huck.

---
 include/linux/async.h |    2 ++
 kernel/async.c        |   23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
include/linux/async.h +2 -0
index 18ce92b..9ff62af 100644
@@ -16,6 +16,8 @@
 typedef u64 async_cookie_t;
 typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 
+extern int async_run_inatomic(async_func_ptr *ptr, void *data);
+
 extern async_cookie_t async_schedule(async_func_ptr *ptr, void *data);
 extern async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
 					    struct list_head *list);
kernel/async.c +1 -0
index 8663f5b..cb527b3 100644
@@ -65,6 +65,7 @@ static async_cookie_t next_cookie = 1;
 
 static LIST_HEAD(async_pending);
 static LIST_HEAD(async_running);
+static LIST_HEAD(async_running_no_sync);
 static DEFINE_SPINLOCK(async_lock);
 
 static int async_enabled = 0;

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 228

Quoted 6 lines
kernel/async.c +3 -0
@@ -222,6 +223,28 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,
 	return newcookie;
 }
 
+ /**
+ * async_run_inatomic - in atomic contexts schedule a function for
+ * 			asynchronous execution

I'm not 100% sure whether kerneldoc likes split lines for the description.

"async_run_inatomic - schedule a function for asynchronous execution from atomic context without checkpointing"
- but that's really a bit long...

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 233

Quoted 5 lines
kernel/async.c +5 -0
+ *
+ * @ptr: function to execute asynchronously
+ * @data: data pointer to pass to the function
+ *
+ * Return zero if success, or else return others if failured

Returns zero on success, !zero on failure

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 234

Quoted 1 line
kernel/async.c +1 -0
+ * Note:

I'd move the description of the function's purpose before Note:.

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,

Quoted 4 lines
kernel/async.c +4 -0
+ * The purpose of this function is to offer a simple way to schedule an
+ * asynchronous thread from an atomic context, because there are not any
+ * functions which can create and start a kernel thread in atomic contexts
+ * now. 

I don't think the second part of the sentence is needed, this should only be mentioned in the patch description. I'd rather add

"Since it does not return a cookie for checkpointing, it is for callers that don't need later synchronization."

Quoted 3 lines

We use async_running_no_sync as running list to make callers that
+ * don't want to synchronize on cookies, so can avoid some side effects for
+ * async_synchronize_full().

"async_run_inatomic() uses a distinct running list in order to avoid slowing down synchronization within the general domain."

(The cookie will still be checked when __lowest_in_progress() returns the cookie for the async_pending list, but I think that is negligible - and it would be a general issue for special domains.)

Quoted 12 lines

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 250

Quoted 10 lines

+ */

kernel/async.c +6 -0
+int async_run_inatomic(async_func_ptr *ptr, void *data)
+{
+	return !__async_schedule(ptr, data, &async_running_no_sync, 1);
+}
+EXPORT_SYMBOL_GPL(async_run_inatomic);
+
 /**
  * async_schedule - schedule a function for asynchronous execution
  * @ptr: function to execute asynchronously

Btw: Do you have any specific use cases for this function in mind?

I have some usecases in mind concerning async_schedule_inatomic() because it still provides a way to synchronize against pending async jobs. Especially it could replace some lazy workqueues which receive rare events.

But I wonder if async_run_inatomic() is really matching any common pattern inside the kernel.
It seems to me rare to never need waiting for an async job completion.

Frederic.

#3 lore raw
Quoted 3 lines · On Thu, 14 May 2009 05:19:39 +0200 Frederic Weisbecker <[email protected]>…

But I wonder if async_run_inatomic() is really matching any common pattern inside the kernel.
It seems to me rare to never need waiting for an async job completion.

there are some very good reasons to have the option to wait for all; module unloading comes to mind for example.

I'd not like it to automatically punt all async "inatomic" work to a separate space; that should be an option for the caller.
If the work is not going to take long, it's actually GOOD that you can globally synchronize for it...

Signature · 3 lines
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org
#4 lore raw
Quoted 2 lines · Arjan van de Ven

there are some very good reasons to have the option to wait for all; module unloading comes to mind for example.

So we would need async_run_synchronize() as something that waits on the special domain for those cases...

Quoted 3 lines

I'd not like it to automatically punt all async "inatomic" work to a separate space; that should be an option for the caller.

I think the idea is only to push the "simple" cases to that domain; a normal "inatomic" function that allows a domain to be specified should be added as well.

author
#5 lore raw
In reply to Cornelia Huck

2009/5/14 Cornelia Huck <[email protected]>:

Quoted 87 lines
Quoted 44 lines · On Wed, 13 May 2009 23:51:39 +0800, [email protected] wrote:

From: Ming Lei <[email protected]>

The purpose of this function is to offer a simple way to schedule an asynchronous thread from an atomic context, because there are not any functions which can create and start a kernel thread in atomic contexts now. We use async_running_no_sync as running list to make callers that don't want to synchronize on cookies, so can avoid some side effects for async_synchronize_full().

Part of note for async_run_inatomic and some guideline is from Cornelia Huck.

---
 include/linux/async.h |    2 ++ kernel/async.c        |   23 +++++++++++++++++++++++

 2 files changed, 25 insertions(+), 0 deletions(-)
include/linux/async.h +2 -0
index 18ce92b..9ff62af 100644
@@ -16,6 +16,8 @@
  typedef u64 async_cookie_t;
  typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 
+extern int async_run_inatomic(async_func_ptr *ptr, void *data);
+
  extern async_cookie_t async_schedule(async_func_ptr *ptr, void *data);
  extern async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
                                           struct list_head *list);
kernel/async.c +1 -0
index 8663f5b..cb527b3 100644
@@ -65,6 +65,7 @@ static async_cookie_t next_cookie = 1;
 
  static LIST_HEAD(async_pending);
  static LIST_HEAD(async_running);
+static LIST_HEAD(async_running_no_sync);
  static DEFINE_SPINLOCK(async_lock);
 
  static int async_enabled = 0;

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 228

Quoted 6 lines
kernel/async.c +3 -0
@@ -222,6 +223,28 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,
       return newcookie;
  }
 
+ /**
+ * async_run_inatomic - in atomic contexts schedule a function for
+ *                   asynchronous execution

I'm not 100% sure whether kerneldoc likes split lines for the description.

"async_run_inatomic - schedule a function for asynchronous execution from atomic context without checkpointing"
- but that's really a bit long...

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 233

Quoted 5 lines
kernel/async.c +5 -0
+ *
+ * @ptr: function to execute asynchronously
+ * @data: data pointer to pass to the function
+ *
+ * Return zero if success, or else return others if failured

Returns zero on success, !zero on failure

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, · line 234

Quoted 1 line
kernel/async.c +1 -0
+ * Note:

I'd move the description of the function's purpose before Note:.

kernel/async.c static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data,

Quoted 4 lines
kernel/async.c +4 -0
+ * The purpose of this function is to offer a simple way to schedule an
+ * asynchronous thread from an atomic context, because there are not any
+ * functions which can create and start a kernel thread in atomic contexts
+ * now.

I don't think the second part of the sentence is needed, this should only be mentioned in the patch description. I'd rather add

"Since it does not return a cookie for checkpointing, it is for callers that don't need later synchronization."

Quoted 3 lines

We use async_running_no_sync as running list to make callers that
+ * don't want to synchronize on cookies, so can avoid some side effects for
+ * async_synchronize_full().

Quoted 6 lines

"async_run_inatomic() uses a distinct running list in order to avoid slowing down synchronization within the general domain."

(The cookie will still be checked when __lowest_in_progress() returns the cookie for the async_pending list, but I think that is negligible - and it would be a general issue for special domains.)

Yes, it still needs some synchronization before the non-sync function entry is moved
into async_running_no_sync, but it is negligible really.

Quoted 14 lines
Quoted 11 lines

+ */

kernel/async.c +6 -0
+int async_run_inatomic(async_func_ptr *ptr, void *data)
+{
+     return !__async_schedule(ptr, data, &async_running_no_sync, 1);
+}
+EXPORT_SYMBOL_GPL(async_run_inatomic);
+

 /**
  * async_schedule - schedule a function for asynchronous execution
  * @ptr: function to execute asynchronously

Btw: Do you have any specific use cases for this function in mind?

For example request_firmware_nowait(), this function declares it can be called in contexts where it is not possible to sleep, but really can not be called
in irq contexts, because the function calls kthread_run to do firmware loading.

Signature · 1 line
Lei Ming