Skip to contentSkip to conversation

kernel/async.c:introduce async_schedule*_atomic

PATCH In review [email protected] 5 participants 18 messages May 12, 2009 – May 18, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/async.c:introduce async_schedule*_atomic

From: Ming Lei <[email protected]>

The async_schedule* may not be called in atomic contexts if out of memory or if there's too much work pending already, because the async function to be called may sleep.

This patch fixes the comment of async_schedule*, and introduces async_schedules*_atomic to allow them called from atomic contexts safely.

---
 include/linux/async.h |    3 ++
 kernel/async.c        |   56 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 7 deletions(-)
include/linux/async.h +3 -0
index 68a9530..ede9849 100644
@@ -19,6 +19,9 @@ typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 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);
+extern async_cookie_t async_schedule_atomic(async_func_ptr *ptr, void *data);
+extern async_cookie_t async_schedule_domain_atomic(async_func_ptr *ptr, \
+					void *data, struct list_head *list);
 extern void async_synchronize_full(void);
 extern void async_synchronize_full_domain(struct list_head *list);
 extern void async_synchronize_cookie(async_cookie_t cookie);
kernel/async.c +49 -7

106 lines not on this page · raw message

Signature · 1 line
1.6.0.GIT
#1 lore raw
Quoted 9 lines · On Tue, May 12, 2009 at 11:13:42PM +0800, [email protected] wrote:

From: Ming Lei <[email protected]>

The async_schedule* may not be called in atomic contexts if out of memory or if there's too much work pending already, because the async function to be called may sleep.

This patch fixes the comment of async_schedule*, and introduces async_schedules*_atomic to allow them called from atomic contexts safely.

Aah, great. Such helper could easily replace some workqueues which receive (in atomic) rare jobs but still need to exist because they execute jobs which take too much time to be enqueued in kevents.

A good candidate: kpsmoused!

Quoted 11 lines
---
 include/linux/async.h |    3 ++
 kernel/async.c        |   56 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 7 deletions(-)
include/linux/async.h +0 -0
index 68a9530..ede9849 100644

include/linux/async.h typedef void (async_func_ptr) (void *data, async_cookie_t cookie); · line 23

Quoted 5 lines
include/linux/async.h +2 -0
@@ -19,6 +19,9 @@ typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 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);
+extern async_cookie_t async_schedule_atomic(async_func_ptr *ptr, void *data);
+extern async_cookie_t async_schedule_domain_atomic(async_func_ptr *ptr, \

trailing backslash?

Quoted 9 lines
include/linux/async.h +1 -0
+					void *data, struct list_head *list);
 extern void async_synchronize_full(void);
 extern void async_synchronize_full_domain(struct list_head *list);
 extern void async_synchronize_cookie(async_cookie_t cookie);
kernel/async.c +0 -0
index 968ef94..6bf565b 100644

kernel/async.c out: · line 175

Quoted 5 lines
kernel/async.c +1 -1
@@ -172,12 +172,13 @@ out:
 }
 
 
-static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running)
+static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, \

another one.

Quoted 22 lines
kernel/async.c +5 -2
+				struct list_head *running, int atomic)
 {
 	struct async_entry *entry;
 	unsigned long flags;
 	async_cookie_t newcookie;
-	
+	int  sync_run = 0;
 
 	/* allow irq-off callers */
 	entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
@@ -186,7 +187,9 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct l
 	 * If we're out of memory or if there's too much work
 	 * pending already, we execute synchronously.
 	 */
-	if (!async_enabled || !entry || atomic_read(&entry_count) > MAX_WORK) {
+	sync_run = !async_enabled || !entry || \
+			atomic_read(&entry_count) > MAX_WORK;
+	if (sync_run && !atomic) {
 		kfree(entry);
 		spin_lock_irqsave(&async_lock, flags);
 		newcookie = next_cookie++;

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

Quoted 5 lines
kernel/async.c +2 -0
@@ -195,7 +198,10 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct l
 		/* low on memory.. run synchronously */
 		ptr(data, newcookie);
 		return newcookie;
+	} else if (sync_run) {
+		return 0;

Then it's up to the caller to handle the error. I guess it's the best way to do.
You could put it in a separate list and retry later, but it wouldn't be a good idea because the work submitter couldn't be sure of the end result.

So I guess you did a right choice.

Quoted 19 lines
kernel/async.c +5 -2
 	}
+
 	entry->func = ptr;
 	entry->data = data;
 	entry->running = running;
@@ -215,15 +221,31 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct l
  * @data: data pointer to pass to the function
  *
  * Returns an async_cookie_t that may be used for checkpointing later.
- * Note: This function may be called from atomic or non-atomic contexts.
+ * Note:This function may be called from non-atomic contexts,and not
+ * 	called from atomic contexts with safety. Please use
+ * 	async_schedule_atomic in atomic contexts.
  */
 async_cookie_t async_schedule(async_func_ptr *ptr, void *data)
 {
-	return __async_schedule(ptr, data, &async_running);
+	return __async_schedule(ptr, data, &async_running, 0);
 }

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

Quoted 10 lines
kernel/async.c +7 -0
 EXPORT_SYMBOL_GPL(async_schedule);
 
 /**
+ * async_schedule_atomic - schedule a function for asynchronous execution
+ * @ptr: function to execute asynchronously
+ * @data: data pointer to pass to the function
+ *
+ * Returns an async_cookie_t that may be used for checkpointing later.
+ * Note: This function can be called from atomic contexts safely.
+ */

Please add a comment to tell that it might fail and return 0 in that case.

May be it would even be worth to detail the error. A cookie can't be negative, then you can use the common error pattern.

-ENOMEM while running out of memory
-ENOSPC when the threshold number of async work has been overlapped
....

Not sure such precision about the error path is needed though, it's just an idea.

Quoted 52 lines
kernel/async.c +29 -2
+async_cookie_t async_schedule_atomic(async_func_ptr *ptr, void *data)
+{
+	return __async_schedule(ptr, data, &async_running, 1);
+}
+EXPORT_SYMBOL_GPL(async_schedule_atomic);
+
+/**
  * async_schedule_domain - schedule a function for asynchronous execution within a certain domain
  * @ptr: function to execute asynchronously
  * @data: data pointer to pass to the function
@@ -233,16 +255,36 @@ EXPORT_SYMBOL_GPL(async_schedule);
  * @running may be used in the async_synchronize_*_domain() functions
  * to wait within a certain synchronization domain rather than globally.
  * A synchronization domain is specified via the running queue @running to use.
- * Note: This function may be called from atomic or non-atomic contexts.
+ * Note:This function may be called from non-atomic contexts,and not
+ * 	called from atomic contexts with safety. Please use
+ * 	async_schedule_domain_atomic in atomic contexts.
  */
 async_cookie_t async_schedule_domain(async_func_ptr *ptr, void *data,
 				     struct list_head *running)
 {
-	return __async_schedule(ptr, data, running);
+	return __async_schedule(ptr, data, running, 0);
 }
 EXPORT_SYMBOL_GPL(async_schedule_domain);
 
 /**
+ * async_schedule_domain_atomic - schedule a function for asynchronous execution within a certain domain
+ * @ptr: function to execute asynchronously
+ * @data: data pointer to pass to the function
+ * @running: running list for the domain
+ *
+ * Returns an async_cookie_t that may be used for checkpointing later.
+ * @running may be used in the async_synchronize_*_domain() functions
+ * to wait within a certain synchronization domain rather than globally.
+ * A synchronization domain is specified via the running queue @running to use.
+ * Note: This function can be called from atomic contexts safely.
+ */
+async_cookie_t async_schedule_domain_atomic(async_func_ptr *ptr, void *data,
+				     struct list_head *running)
+{
+	return __async_schedule(ptr, data, running, 1);
+}
+EXPORT_SYMBOL_GPL(async_schedule_domain_atomic);
+/**
  * async_synchronize_full - synchronize all asynchronous function calls
  *
  * This function waits until all asynchronous function calls have been done.
Signature · 1 line
1.6.0.GIT

Otherwise it looks good, and IMHO it is needed.

#2 lore raw
Quoted 69 lines · Frederic Weisbecker
Quoted 9 lines · On Tue, May 12, 2009 at 11:13:42PM +0800, [email protected] wrote:

From: Ming Lei <[email protected]>

The async_schedule* may not be called in atomic contexts if out of memory or if there's too much work pending already, because the async function to be called may sleep.

This patch fixes the comment of async_schedule*, and introduces async_schedules*_atomic to allow them called from atomic contexts safely.

Aah, great. Such helper could easily replace some workqueues which receive (in atomic) rare jobs but still need to exist because they execute jobs which take too much time to be enqueued in kevents.

A good candidate: kpsmoused!

Quoted 11 lines
---
 include/linux/async.h |    3 ++
 kernel/async.c        |   56 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 7 deletions(-)
include/linux/async.h +0 -0
index 68a9530..ede9849 100644

include/linux/async.h typedef void (async_func_ptr) (void *data, async_cookie_t cookie); · line 23

Quoted 5 lines
include/linux/async.h +2 -0
@@ -19,6 +19,9 @@ typedef void (async_func_ptr) (void *data, async_cookie_t cookie);
 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);
+extern async_cookie_t async_schedule_atomic(async_func_ptr *ptr, void *data);
+extern async_cookie_t async_schedule_domain_atomic(async_func_ptr *ptr, \

trailing backslash?

Quoted 9 lines
include/linux/async.h +1 -0
+					void *data, struct list_head *list);
 extern void async_synchronize_full(void);
 extern void async_synchronize_full_domain(struct list_head *list);
 extern void async_synchronize_cookie(async_cookie_t cookie);
kernel/async.c +0 -0
index 968ef94..6bf565b 100644

kernel/async.c out: · line 175

Quoted 5 lines
kernel/async.c +1 -1
@@ -172,12 +172,13 @@ out:
 }
 
 
-static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct list_head *running)
+static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, \

another one.

Quoted 8 lines
kernel/async.c +2 -1
+				struct list_head *running, int atomic)
 {
 	struct async_entry *entry;
 	unsigned long flags;
 	async_cookie_t newcookie;
-	
+	int  sync_run = 0;
 
Quoted 9 lines

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

Quoted 9 lines
kernel/async.c +2 -1
 	/* allow irq-off callers */
 	entry = kzalloc(sizeof(struct async_entry), GFP_ATOMIC);
@@ -186,7 +187,9 @@ static async_cookie_t __async_schedule(async_func_ptr *ptr, void *data, struct l
 	 * If we're out of memory or if there's too much work
 	 * pending already, we execute synchronously.
 	 */
-	if (!async_enabled || !entry || atomic_read(&entry_count) > MAX_WORK) {
+	sync_run = !async_enabled || !entry || \
+			atomic_read(&entry_count) > MAX_WORK;

And also this one...

Hey, Ming, none of these are macros... You don't need to add backslash to join two lines, C compilers can recognize.

Signature · 1 line
Live like a child, think like the god.
author
#3 lore raw

2009/5/12 Américo Wang <[email protected]>:

Quoted 9 lines · Frederic Weisbecker
Quoted 6 lines · On Tue, May 12, 2009 at 11:13:42PM +0800, [email protected] wrote:
Quoted 5 lines

From: Ming Lei <[email protected]>

-    if (!async_enabled || !entry || atomic_read(&entry_count) > MAX_WORK) {
+    sync_run = !async_enabled || !entry || \
+                    atomic_read(&entry_count) > MAX_WORK;

Quoted 4 lines

And also this one...

Hey, Ming, none of these are macros... You don't need to add backslash to join two lines, C compilers can recognize.

OK, I'll fix it in next version.
Thanks!

Quoted 5 lines

--
Live like a child, think like the god.

Signature · 1 line
Lei Ming
#4 lore raw
In reply to Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker
Quoted 9 lines · On Tue, May 12, 2009 at 11:13:42PM +0800, [email protected] wrote:

From: Ming Lei <[email protected]>

The async_schedule* may not be called in atomic contexts if out of memory or if there's too much work pending already, because the async function to be called may sleep.

This patch fixes the comment of async_schedule*, and introduces async_schedules*_atomic to allow them called from atomic contexts safely.

Note that async_schedule_atomic is a confusing name. At a first glance, it could mean that the scheduled job will be run atomically.

I would suggest async_schedule_inatomic() so that it follows the common naming pattern in use in the kernel, eg:

- copy_from_user_inatomic()
- futex_atomic_cmpxchg_inatomic()

and so on.

Quoted 5 lines
Quoted 5 lines

* Returns an async_cookie_t that may be used for checkpointing later.
- * Note: This function may be called from atomic or non-atomic contexts.
+ * Note:This function may be called from non-atomic contexts,and not
+ * called from atomic contexts with safety. Please use
+ * async_schedule_atomic in atomic contexts.

I suggest to add a comment which explains the reason for which it is unsafe to call it in atomic context: because the scheduled work might be synchronously executed.

One could believe this is because async_schedule() internally uses a function which might sleep whereas the actual problem may come from the scheduled function.

BTW, now that we have an atomic safe version, may be we could also adapt the kmalloc GFP flags subsequently?

Frederic.

#5 lore raw
Quoted 9 lines · Frederic Weisbecker
Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · On Tue, May 12, 2009 at 11:13:42PM +0800, [email protected] wrote:

* Returns an async_cookie_t that may be used for checkpointing later.
- * Note: This function may be called from atomic or non-atomic contexts.
+ * Note:This function may be called from non-atomic contexts,and not
+ * called from atomic contexts with safety. Please use
+ * async_schedule_atomic in atomic contexts.

Quoted 7 lines

I suggest to add a comment which explains the reason for which it is unsafe to call it in atomic context: because the scheduled work might be synchronously executed.

One could believe this is because async_schedule() internally uses a function which might sleep whereas the actual problem may come from the scheduled function.

I'm wondering whether this is not mixing two different things up:
- Making async_schedule_* safe from an atomic context.
- Disallowing calling the function synchronously if asynchronous scheduling failed.

Perhaps we want async_schedule_nosync() in addition?

Quoted 3 lines

BTW, now that we have an atomic safe version, may be we could also adapt the kmalloc GFP flags subsequently?

Yes, that would make sense.

#6 lore raw
Quoted 16 lines · Cornelia Huck
Quoted 13 lines · Frederic Weisbecker
Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · On Tue, May 12, 2009 at 11:13:42PM +0800, [email protected] wrote:

* Returns an async_cookie_t that may be used for checkpointing later.
- * Note: This function may be called from atomic or non-atomic contexts.
+ * Note:This function may be called from non-atomic contexts,and not
+ * called from atomic contexts with safety. Please use
+ * async_schedule_atomic in atomic contexts.

I suggest to add a comment which explains the reason for which it is unsafe to call it in atomic context: because the scheduled work might be synchronously executed.

Quoted 6 lines
Quoted 3 lines

One could believe this is because async_schedule() internally uses a function which might sleep whereas the actual problem may come from the scheduled function.

I'm wondering whether this is not mixing two different things up:
- Making async_schedule_* safe from an atomic context.

It was already safe from atomic context, async_schedule() internals does not make use of sleeping functions.
So indeed the inatomic suffix is not that much appropriate from an async internal point in view, it makes sense only from the caller/callee point of view.

But if we start worrying about the GFP flags used by __async_schedule(), then yes we are mixing up two things.

Quoted 4 lines

- Disallowing calling the function synchronously if asynchronous scheduling failed.

Perhaps we want async_schedule_nosync() in addition?

This division would make more sense indeed.

- async_schedule_inatomic() would be nosync() and would use GFP_ATOMIC. I guess the case where we want to run
a job synchronously from atomic in case of async failure is too rare (non-existent?).
- async_schedule_nosync() would be only nosync() and would use GFP_KERNEL

I'm not sure the second case will ever be used though.

Another alternative would be to define a single async_schedule_nosync() which also takes a gfp flag.

Quoted 5 lines
Quoted 3 lines

BTW, now that we have an atomic safe version, may be we could also adapt the kmalloc GFP flags subsequently?

Yes, that would make sense.

#7 lore raw
Quoted 6 lines · Frederic Weisbecker

This division would make more sense indeed.

- async_schedule_inatomic() would be nosync() and would use GFP_ATOMIC. I guess the case where we want to run
a job synchronously from atomic in case of async failure is too rare (non-existent?).

It would add complexity for those callers providing a function that is safe to be called in both contexts.

Quoted 4 lines

- async_schedule_nosync() would be only nosync() and would use GFP_KERNEL

I'm not sure the second case will ever be used though.

It might make sense for the "just fail if we cannot get memory" case.

Quoted 3 lines

Another alternative would be to define a single async_schedule_nosync() which also takes a gfp flag.

Wouldn't async_schedule() then need a gfp flag as well?

author
#8 lore raw

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

Quoted 18 lines
Quoted 6 lines · Frederic Weisbecker

This division would make more sense indeed.

- async_schedule_inatomic() would be nosync() and would use GFP_ATOMIC. I guess the case where we want to run
  a job synchronously from atomic in case of async failure is too rare (non-existent?).

It would add complexity for those callers providing a function that is safe to be called in both contexts.

Quoted 4 lines

- async_schedule_nosync() would be only nosync() and would use GFP_KERNEL

I'm not sure the second case will ever be used though.

Quoted 8 lines

It might make sense for the "just fail if we cannot get memory" case.

Quoted 3 lines

Another alternative would be to define a single async_schedule_nosync() which also takes a gfp flag.

Wouldn't async_schedule() then need a gfp flag as well?

IMHO, we should call async_schedule*() from non-atomic contexts and async_schedule_inatomic*() from atomic contexts explicitly, so async_schedule*()
use GFP_KERNEL and async_schedule_inatomic*() use GFP_ATOMIC always. This can simplify the problem much more.

Also we still allow async_schedule*() to run a job synchronously if out of memory
or other failure. This can keep consistency with before.

Any sugesstions or objections?

Signature · 1 line
Lei Ming
#9 lore raw
Quoted 25 lines · Ming Lei

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

Quoted 24 lines
Quoted 6 lines · Frederic Weisbecker

This division would make more sense indeed.

- async_schedule_inatomic() would be nosync() and would use GFP_ATOMIC. I guess the case where we want to run
  a job synchronously from atomic in case of async failure is too rare (non-existent?).

It would add complexity for those callers providing a function that is safe to be called in both contexts.

Quoted 4 lines

- async_schedule_nosync() would be only nosync() and would use GFP_KERNEL

I'm not sure the second case will ever be used though.

It might make sense for the "just fail if we cannot get memory" case.

Quoted 3 lines

Another alternative would be to define a single async_schedule_nosync() which also takes a gfp flag.

Quoted 8 lines
Quoted 2 lines

Wouldn't async_schedule() then need a gfp flag as well?

IMHO, we should call async_schedule*() from non-atomic contexts and async_schedule_inatomic*() from atomic contexts explicitly, so async_schedule*()
use GFP_KERNEL and async_schedule_inatomic*() use GFP_ATOMIC always. This can simplify the problem much more.

I think Cornelia is right about the complex case of a job launched from atomic context that could either be run synchronously. I have troubles to imagine such case though but I guess it's possible.

Quoted 3 lines

Also we still allow async_schedule*() to run a job synchronously if out of memory
or other failure. This can keep consistency with before.

Yes, but also most of the current users of async_schedule() could call it with GFP_KERNEL. For now it's not an issue because it is not widely used, but who knows how that will evolve...

Quoted 1 line

Any sugesstions or objections?

I have shared feelings. I don't know if the dual sense of this new helper deserves enough disambiguation and granularity to be split up in two parts:

- adding an async_schedule_nosync() helper
- add a new gfpflag_t parameter

Or should we just do:

- adding async_schedule_inatomic() which is a merge of nosync + GFP_ATOMIC
- use GFP_KERNEL in async_schedule()

It depends on the future users. Will someone ever accept to schedule a job that could end up running synchronously in the worst case?

Frederic.

#10 lore raw
Quoted 10 lines · Frederic Weisbecker
Quoted 3 lines · Ming Lei

Also we still allow async_schedule*() to run a job synchronously if out of memory
or other failure. This can keep consistency with before.

Yes, but also most of the current users of async_schedule() could call it with GFP_KERNEL. For now it's not an issue because it is not widely used, but who knows how that will evolve...

Well, if we want to change the interface, now would be a good time since there are still few callers.

Quoted 13 lines
Quoted 3 lines

Any sugesstions or objections?

I have shared feelings. I don't know if the dual sense of this new helper deserves enough disambiguation and granularity to be split up in two parts:

- adding an async_schedule_nosync() helper
- add a new gfpflag_t parameter

Quoted 8 lines

Or should we just do:

- adding async_schedule_inatomic() which is a merge of nosync + GFP_ATOMIC
- use GFP_KERNEL in async_schedule()

It depends on the future users. Will someone ever accept to schedule a job that could end up running synchronously in the worst case?

The current callers all look simple enough, it may be that all other cases besides inatomic+nosync would be overkill (and a too complex api may lead to confusion).

Do people have candidates for conversion to the async api in mind that would need one of the complex atomic/sync or non-atomic/non-sync versions? If no, maybe we should just use the second approach and make sure that the semantics are well documented.

#11 lore raw
Quoted 10 lines · On Wed, 13 May 2009 09:47:28 +0200 Cornelia Huck <[email protected]>…
Quoted 7 lines · Frederic Weisbecker
Quoted 3 lines · Ming Lei

Also we still allow async_schedule*() to run a job synchronously if out of memory
or other failure. This can keep consistency with before.

Quoted 6 lines
Quoted 3 lines

Yes, but also most of the current users of async_schedule() could call it with GFP_KERNEL. For now it's not an issue because it is not widely used, but who knows how that will evolve...

Well, if we want to change the interface, now would be a good time since there are still few callers.

I would prefer it that if we make a more complex interface, we keep the current simple interface as a wrapper, so that the simple case can remain simple.

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
#12 lore raw
Quoted 17 lines · Arjan van de Ven
Quoted 14 lines · On Wed, 13 May 2009 09:47:28 +0200 Cornelia Huck <[email protected]>…
Quoted 10 lines · Frederic Weisbecker
Quoted 3 lines · Ming Lei

Also we still allow async_schedule*() to run a job synchronously if out of memory
or other failure. This can keep consistency with before.

Yes, but also most of the current users of async_schedule() could call it with GFP_KERNEL. For now it's not an issue because it is not widely used, but who knows how that will evolve...

Quoted 6 lines
Quoted 2 lines

Well, if we want to change the interface, now would be a good time since there are still few callers.

I would prefer it that if we make a more complex interface, we keep the current simple interface as a wrapper, so that the simple case can remain simple.

Of course. I was just thinking about changing the semantics of async_schedule() to doing GFP_KERNEL allocation - now it's still easy to audit all callers.

author
#13 lore raw
In reply to Cornelia Huck

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

Quoted 5 lines
Quoted 2 lines · Frederic Weisbecker

This division would make more sense indeed.

Quoted 7 lines
Quoted 4 lines

- async_schedule_inatomic() would be nosync() and would use GFP_ATOMIC. I guess the case where we want to run
  a job synchronously from atomic in case of async failure is too rare (non-existent?).

It would add complexity for those callers providing a function that is safe to be called in both contexts.

So we introduce async_schedule*_inatomic(), the patch aims at making caller clear that async_schedule*_inatomic() should be used in atomic contexts instead of
async_schedule*().

Quoted 3 lines
Quoted 3 lines

- async_schedule_nosync() would be only nosync() and would use GFP_KERNEL

I wonder if there is such kind of requirement, can we not introduce it in the patch?
If someone does need it, we can introduce it later.

Quoted 11 lines
Quoted 2 lines

I'm not sure the second case will ever be used though.

It might make sense for the "just fail if we cannot get memory" case.

Quoted 3 lines

Another alternative would be to define a single async_schedule_nosync() which also takes a gfp flag.

Wouldn't async_schedule() then need a gfp flag as well?

IMHO, it is better that async_schedule() is always called in non-atomic contexts and
async_schedule*_inatomic() is always called in atomic contexts, so we can't need a gfp
flag, right?

Signature · 1 line
Lei Ming
author
#14 lore raw
In reply to Frederic Weisbecker

2009/5/13 Frederic Weisbecker <[email protected]>:

Quoted 6 lines · Frederic Weisbecker

Note that async_schedule_atomic is a confusing name. At a first glance, it could mean that the scheduled job will be run atomically.

Quoted 7 lines

I would suggest async_schedule_inatomic() so that it follows the common naming pattern in use in the kernel, eg:

- copy_from_user_inatomic()
- futex_atomic_cmpxchg_inatomic()

and so on.

Agree, I'll fix it in next version.

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

  * Returns an async_cookie_t that may be used for checkpointing later.
- * Note: This function may be called from atomic or non-atomic contexts.
+ * Note:This function may be called from non-atomic contexts,and not
+ *         called from atomic contexts with safety. Please use
+ *         async_schedule_atomic in atomic contexts.

Quoted 7 lines

I suggest to add a comment which explains the reason for which it is unsafe to call it in atomic context: because the scheduled work might be synchronously executed.

One could believe this is because async_schedule() internally uses a function which might sleep whereas the actual problem may come from the scheduled function.

Agree,add it in next version.

Quoted 3 lines

BTW, now that we have an atomic safe version, may be we could also adapt the kmalloc GFP flags subsequently?

Signature · 1 line
Lei Ming
#15 lore raw
In reply to [email protected]
Quoted 9 lines · On Tue, 12 May 2009 23:13:42 +0800 [email protected] wrote:

From: Ming Lei <[email protected]>

The async_schedule* may not be called in atomic contexts if out of memory or if there's too much work pending already, because the async function to be called may sleep.

This patch fixes the comment of async_schedule*, and introduces async_schedules*_atomic to allow them called from atomic contexts safely.

(sorry for the late response; have been away from most of my email for a few days)

I like the general idea; I was hoping to do it a little bit different though, API wise.
I don't mind the parameter for "don't do blocking things" (we should use that to also use GFP_KERNEL/GFP_NOFS or whatever for the allocation), it makes sense.

What I would like to see is the option to pass in memory that was externally kmalloc'd. So that you can do

foo = kmalloc(..)
spin_lock(bar)
...
async_schedule_atomic(...);

spin_unlock(bar);
if (not_used_foo)

    kfree(foo);

in cases where you don't want to fail while in the atomic portion, but can fail better earlier.

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
author
#16 lore raw

2009/5/18 Arjan van de Ven <[email protected]>:

Quoted 30 lines
Quoted 9 lines · On Tue, 12 May 2009 23:13:42 +0800 [email protected] wrote:

From: Ming Lei <[email protected]>

The async_schedule* may not be called in atomic contexts if out of memory or if there's too much work pending already, because the async function to be called may sleep.

This patch fixes the comment of async_schedule*, and introduces async_schedules*_atomic to allow them called from atomic contexts safely.

(sorry for the late response; have been away from most of my email for a few days)

I like the general idea; I was hoping to do it a little bit different though, API wise.
I don't mind the parameter for "don't do blocking things" (we should use that to also use GFP_KERNEL/GFP_NOFS or whatever for the allocation), it makes sense.

What I would like to see is the option to pass in memory that was externally kmalloc'd. So that you can do

foo = kmalloc(..)
spin_lock(bar)
...
async_schedule_atomic(...);

Quoted 6 lines

spin_unlock(bar);
if (not_used_foo)
   kfree(foo);

in cases where you don't want to fail while in the atomic portion, but can fail better earlier.

If we do this way, the type of async_schedule*() and async_schedule*_inatomic() is different, and callers will be messed with them, especially for _inatomic().

BTW: I have submited new version of this patches, would you mind giving a review?

Thanks.

Signature · 1 line
Lei Ming
#17 lore raw
Quoted 7 lines · On Mon, 18 May 2009 09:55:14 +0800 Ming Lei <[email protected]> wrote:
Quoted 3 lines

in cases where you don't want to fail while in the atomic portion, but can fail better earlier.

If we do this way, the type of async_schedule*() and async_schedule*_inatomic() is different, and callers will be messed with them, especially for _inatomic().

I'm not so much proposing the external "simple API" to have this, but the primitive should have it, so that we can provide a __async_schedule_inatomic() which does have the option..

Quoted 3 lines

BTW: I have submited new version of this patches, would you mind giving a review?

I gave comments on various threads from my inbox, but I cannot rule out having missed one... If you think I missed one, could you send me a private email with the latest patches you want reviewed (even if it's a forward from what you sent earlier)?

I'm happy to see these enhancements btw...

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