Skip to contentSkip to conversation

lib/alchemy: fix rt_mutex_create return values

PATCH In review Henning Schild 2 participants 6 messages Jul 11, 2017 – Jul 27, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lib/alchemy: fix rt_mutex_create return values

From: Henning Schild <[email protected]>

rt_mutex_create turned an -EPERM from the cobalt kernel syscall path into an -EEXIST. If you have a model where threads create their shadows on demand, when seeing the first -EPERM, that return value must not be changed.

---
 lib/alchemy/mutex.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/alchemy/mutex.c +9 -4
index 3aa55dd521..8c5e5f5968 100644
@@ -96,7 +96,8 @@ static struct registry_operations registry_ops;
  * registered mutex.
  *
  * - -EPERM is returned if this service was called from an
- * asynchronous context.
+ * asynchronous context. Or - in dual kernel - when the thread is not
+ * allowed to issue syscalls.
  *
  * @apitags{mode-unrestricted, switch-secondary}
  *
@@ -147,12 +148,16 @@ int rt_mutex_create(RT_MUTEX *mutex, const char *name)
 		ret = 0;
 	}
 
-	if (syncluster_addobj(&alchemy_mutex_table, mcb->name, &mcb->cobj)) {
+	if (ret = syncluster_addobj(&alchemy_mutex_table, mcb->name,
+				    &mcb->cobj))
+	{
 		registry_destroy_file(&mcb->fsobj);
 		xnfree(mcb);
-		ret = -EEXIST;
-	} else
+		if (ret != -EPERM)
+			ret = -EEXIST;
+	} else {
 		mutex->handle = mainheap_ref(mcb, uintptr_t);
+	}
 out:
 	CANCEL_RESTORE(svc);
 
Signature · 1 line
2.13.0
#1 lore raw

PATCH 2/2 lib/alchemy: change API context tags

From: Henning Schild <[email protected]>

changing the prio always changes the secondary-mode thread as well see:
http://xenomai.org/migrating-from-xenomai-2-x-to-3-x/#Scheduling

---
 lib/alchemy/task.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/alchemy/task.c +1 -1
index d647d5c0ee..12acafc495 100644
@@ -1243,7 +1243,7 @@ RT_TASK *rt_task_self(void)
  * - -EPERM is returned if @a task is NULL and this service was called
  * from an invalid context.
  *
- * @apitags{mode-unrestricted, switch-primary}
+ * @apitags{mode-unrestricted, switch-primary, switch-secondary}
  *
  * @note The caller must be an Alchemy task if @a task is NULL.
  *
Signature · 1 line
2.13.0
#2 lore raw
In reply to Henning Schild

I am not sure i like what is happening there. Maybe we should just allow rt_mutex_create to return whatever syncluster_addobj returns. I just looked at the code and the pattern where any error from syncluster_addobj is turned into an EEXIST is in several places.

We have an application that creates shadow threads whenever the first cobalt syscall returns -EPERM. You can argue whether that is a good idea, but turning an -EPERM into something else is also not a good idea.

Henning

Am Tue, 11 Jul 2017 13:32:12 +0200
schrieb Henning Schild <[email protected]>:

Quoted 47 lines

From: Henning Schild <[email protected]>

rt_mutex_create turned an -EPERM from the cobalt kernel syscall path into an -EEXIST. If you have a model where threads create their shadows on demand, when seeing the first -EPERM, that return value must not be changed.

---
 lib/alchemy/mutex.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/alchemy/mutex.c +9 -4
index 3aa55dd521..8c5e5f5968 100644
@@ -96,7 +96,8 @@ static struct registry_operations registry_ops;
  * registered mutex.
  *
  * - -EPERM is returned if this service was called from an
- * asynchronous context.
+ * asynchronous context. Or - in dual kernel - when the thread is not
+ * allowed to issue syscalls.
  *
  * @apitags{mode-unrestricted, switch-secondary}
  *
@@ -147,12 +148,16 @@ int rt_mutex_create(RT_MUTEX *mutex, const char
 *name) ret = 0;
 	}
 
-	if (syncluster_addobj(&alchemy_mutex_table, mcb->name,
 &mcb->cobj)) {
+	if (ret = syncluster_addobj(&alchemy_mutex_table, mcb->name,
+				    &mcb->cobj))
+	{
 		registry_destroy_file(&mcb->fsobj);
 		xnfree(mcb);
-		ret = -EEXIST;
-	} else
+		if (ret != -EPERM)
+			ret = -EEXIST;
+	} else {
 		mutex->handle = mainheap_ref(mcb, uintptr_t);
+	}
 out:
 	CANCEL_RESTORE(svc);
#3 lore raw
Quoted 5 lines · Henning Schild

I am not sure i like what is happening there. Maybe we should just allow rt_mutex_create to return whatever syncluster_addobj returns. I just looked at the code and the pattern where any error from syncluster_addobj is turned into an EEXIST is in several places.

I'm not sure I like it either, that does not make any sense actually - it even hides the ENOMEM error, which would be just insane. I pushed a patch fixing the issue globally for all callers of syncluster_addobj(), updating the inline documentation in the same move.

Quoted 3 lines

We have an application that creates shadow threads whenever the first cobalt syscall returns -EPERM. You can argue whether that is a good idea, but turning an -EPERM into something else is also not a good idea.

As I mentioned lately, I would recommend pthread_kill(pthread_self(), 0) as a more robust way for a thread to check this. Receiving ESRCH would mean that it has not been shadowed.

Signature · 1 line
Philippe.
#4 lore raw
Quoted 12 lines · Philippe Gerum
Quoted 5 lines · Henning Schild

I am not sure i like what is happening there. Maybe we should just allow rt_mutex_create to return whatever syncluster_addobj returns. I just looked at the code and the pattern where any error from syncluster_addobj is turned into an EEXIST is in several places.

I'm not sure I like it either, that does not make any sense actually - it even hides the ENOMEM error, which would be just insane. I pushed a patch fixing the issue globally for all callers of syncluster_addobj(), updating the inline documentation in the same move.

Quoted 8 lines
Quoted 3 lines

We have an application that creates shadow threads whenever the first cobalt syscall returns -EPERM. You can argue whether that is a good idea, but turning an -EPERM into something else is also not a good idea.

As I mentioned lately, I would recommend pthread_kill(pthread_self(), 0) as a more robust way for a thread to check this. Receiving ESRCH would mean that it has not been shadowed.

To clarify, I'm suggesting to use pthread_kill() in the slow/error path of your code to confirm that EPERM as received really means "not a shadow", and not the result of a consistency check performed by the core on the mutex object when the debug option is turned on.

Signature · 1 line
Philippe.
#5 lore raw

Am Wed, 26 Jul 2017 17:34:01 +0200
schrieb Philippe Gerum <[email protected]>:

Quoted 23 lines · Philippe Gerum
Quoted 17 lines · Henning Schild
Quoted 5 lines

I am not sure i like what is happening there. Maybe we should just allow rt_mutex_create to return whatever syncluster_addobj returns. I just looked at the code and the pattern where any error from syncluster_addobj is turned into an EEXIST is in several places.

I'm not sure I like it either, that does not make any sense actually - it even hides the ENOMEM error, which would be just insane. I pushed a patch fixing the issue globally for all callers of syncluster_addobj(), updating the inline documentation in the same move.

Quoted 4 lines

We have an application that creates shadow threads whenever the first cobalt syscall returns -EPERM. You can argue whether that is a good idea, but turning an -EPERM into something else is also not a good idea.

Quoted 4 lines

As I mentioned lately, I would recommend pthread_kill(pthread_self(), 0) as a more robust way for a thread to check this. Receiving ESRCH would mean that it has not been shadowed.

Quoted 4 lines

To clarify, I'm suggesting to use pthread_kill() in the slow/error path of your code to confirm that EPERM as received really means "not a shadow", and not the result of a consistency check performed by the core on the mutex object when the debug option is turned on.

Thanks for the suggestion, i will forward it. And thanks for coming up with a proper fix to the observed problem.

Henning