Skip to contentSkip to conversation

+ delay-accounting-taskstats-interface-send-tgid-once.patch added to -mm tree

PATCH In review Arjan van de Ven 3 participants 6 messages Jun 27, 2006 1 message not in the archive Taskstats statistics interface

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

also in response to
Subject:
Re: 2.6.17-mm3
Date:
Tue, 27 Jun 2006 16:12:42 +0200
with Message-ID:
<[email protected]>

Review

Quoted 10 lines · On Mon, 2006-06-26 at 12:06 -0700, [email protected] wrote:
(quoted diff) +10 -0
+static inline void taskstats_tgid_alloc(struct signal_struct *sig)
+{
+	struct taskstats *stats;
+
+	stats = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
+	if (!stats)
+		return;
+
+	spin_lock(&sig->stats_lock);
+	if (!sig->stats) {

+static inline void taskstats_tgid_free(struct signal_struct *sig) +{

+       struct taskstats *stats = NULL;
+       spin_lock(&sig->stats_lock);
+       if (sig->stats) {
+               stats = sig->stats;
+               sig->stats = NULL;
+       }
+       spin_unlock(&sig->stats_lock);
+       if (stats)

+ kmem_cache_free(taskstats_cache, stats);
+}

this is buggy and deadlock prone!
(found by lockdep)

stats_lock nests within tasklist_lock, which is taken in irq context. (and thus needs irq_save treatment). But because of this nesting, it is not allowed to take stats_lock without disabling irqs, or you can have the following scenario

CPU 0 CPU 1
(in irq) (in the code above)
stats_lock is taken
tasklist_lock is taken
stats_lock_is taken <spin>
<interrupt happens> tasklist_lock is taken

which now forms an AB-BA deadlock!

this happens at least in copy_process which can call taskstats_tgid_free without first disabling interrupts (via cleanup_signal). There may be many other cases, I've not checked deeper yet.

Solution should be to make these functions use irqsave variant... any comments from the authors of this patch ?

#2 lore raw
Quoted 3 lines

CPU 0 CPU 1
(in irq) (in the code above)

actually CPU 0 doesn't even have to be in irq context; any context will do

Quoted 22 lines

stats_lock is taken
tasklist_lock is taken
stats_lock_is taken <spin>
<interrupt happens> tasklist_lock is taken

which now forms an AB-BA deadlock!

this happens at least in copy_process which can call taskstats_tgid_free without first disabling interrupts (via cleanup_signal). There may be many other cases, I've not checked deeper yet.

Solution should be to make these functions use irqsave variant... any comments from the authors of this patch ?

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw
In reply to Arjan van de Ven
Quoted 54 lines · Arjan van de Ven

also in response to
Subject:
Re: 2.6.17-mm3
Date:
Tue, 27 Jun 2006 16:12:42 +0200
with Message-ID:
<[email protected]>

Quoted 12 lines · On Mon, 2006-06-26 at 12:06 -0700, [email protected] wrote:
(quoted diff) +10 -0
+static inline void taskstats_tgid_alloc(struct signal_struct *sig)
+{
+	struct taskstats *stats;
+
+	stats = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
+	if (!stats)
+		return;
+
+	spin_lock(&sig->stats_lock);
+	if (!sig->stats) {
(quoted diff) +12 -0
+static inline void taskstats_tgid_free(struct signal_struct *sig)
+{
+       struct taskstats *stats = NULL;
+       spin_lock(&sig->stats_lock);
+       if (sig->stats) {
+               stats = sig->stats;
+               sig->stats = NULL;
+       }
+       spin_unlock(&sig->stats_lock);
+       if (stats)
+               kmem_cache_free(taskstats_cache, stats);
+}

this is buggy and deadlock prone!
(found by lockdep)

stats_lock nests within tasklist_lock, which is taken in irq context. (and thus needs irq_save treatment). But because of this nesting, it is not allowed to take stats_lock without disabling irqs, or you can have the following scenario

CPU 0 CPU 1
(in irq) (in the code above)
stats_lock is taken
tasklist_lock is taken
stats_lock_is taken <spin>
<interrupt happens> tasklist_lock is taken

Quoted 6 lines

which now forms an AB-BA deadlock!

this happens at least in copy_process which can call taskstats_tgid_free without first disabling interrupts (via cleanup_signal).

Arjan,

Didn't get how stats_lock is nesting within tasklist_lock in copy_process ? The call to cleanup_signal (or any call to taskstats_tgid_alloc/free) seems to be happening outside of holding the tasklist lock ? Or maybe I'm missing something.

Changing to an irqsave variant isn't a problem of course...

--Shailabh

Quoted 9 lines

There may be
many other cases, I've not checked deeper yet.

Solution should be to make these functions use irqsave variant... any comments from the authors of this patch ?

#4 lore raw
Quoted 61 lines · Shailabh Nagar
Quoted 53 lines · Arjan van de Ven

also in response to

                          Subject: Re: 2.6.17-mm3
                             Date: Tue, 27 Jun 2006 16:12:42 +0200

with Message-ID:
<[email protected]>

Quoted 11 lines · On Mon, 2006-06-26 at 12:06 -0700, [email protected] wrote:
(quoted diff) +10 -0
+static inline void taskstats_tgid_alloc(struct signal_struct *sig)
+{
+    struct taskstats *stats;
+
+    stats = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
+    if (!stats)
+        return;
+
+    spin_lock(&sig->stats_lock);
+    if (!sig->stats) {
(quoted diff) +12 -0
+static inline void taskstats_tgid_free(struct signal_struct *sig)
+{
+       struct taskstats *stats = NULL;
+       spin_lock(&sig->stats_lock);
+       if (sig->stats) {
+               stats = sig->stats;
+               sig->stats = NULL;
+       }
+       spin_unlock(&sig->stats_lock);
+       if (stats)
+               kmem_cache_free(taskstats_cache, stats);
+}

this is buggy and deadlock prone!
(found by lockdep)

stats_lock nests within tasklist_lock, which is taken in irq context. (and thus needs irq_save treatment). But because of this nesting, it is not allowed to take stats_lock without disabling irqs, or you can have the following scenario

CPU 0 CPU 1
(in irq) (in the code above)
stats_lock is taken
tasklist_lock is taken stats_lock_is taken <spin>
<interrupt happens>
tasklist_lock is taken
which now forms an AB-BA deadlock!

Quoted 2 lines

this happens at least in copy_process which can call taskstats_tgid_free without first disabling interrupts (via cleanup_signal).

Arjan,

Quoted 3 lines

Didn't get how stats_lock is nesting within tasklist_lock in copy_process ? The call to cleanup_signal (or any call to taskstats_tgid_alloc/free) seems to be happening outside of holding the tasklist lock ? Or maybe I'm missing something.

Yup...indeed I was.

In release_task(), __cleanup_signal() is being called within tasklist_lock protection and that leads to stats_lock being held nested within tasklist_lock.

Quoted 11 lines

Changing to an irqsave variant isn't a problem of course...

--Shailabh

Quoted 5 lines

There may be
many other cases, I've not checked deeper yet.

Solution should be to make these functions use irqsave variant... any comments from the authors of this patch ?

Will submit a patch to switch to irqsave variants.

Thanks,
Shailabh

#5 lore raw

PATCH 1/1 delay accounting taskstats interface: send tgid once locking fix

Use irqsave variants of spin_lock for newly introduced tsk->signal->stats_lock The lock is nested within tasklist_lock as part of release_task() and hence there is possibility of a AB-BA deadlock if a timer interrupt occurs while stats_lock is held.

Thanks to Arjan van de Ven for pointing out the lock bug.
The patch conservatively converts all use of stats_lock to irqsave variant rather than only the call within taskstats_tgid_free which is the function called within tasklist_lock protection.

---

 include/linux/taskstats_kern.h |   11 +++++++----
 kernel/taskstats.c             |   16 ++++++++++------
 2 files changed, 17 insertions(+), 10 deletions(-)
kernel/taskstats.c +10 -6
Index: linux-2.6.17/kernel/taskstats.c
===================================================================
@@ -133,6 +133,7 @@ static int fill_tgid(pid_t tgid, struct
 		struct taskstats *stats)
 {
 	struct task_struct *tsk, *first;
+	unsigned long flags;
 
 	/*
 	 * Add additional stats from live tasks except zombie thread group
@@ -152,10 +153,10 @@ static int fill_tgid(pid_t tgid, struct
 		get_task_struct(first);
 
 	/* Start with stats from dead tasks */
-	spin_lock(&first->signal->stats_lock);
+	spin_lock_irqsave(&first->signal->stats_lock, flags);
 	if (first->signal->stats)
 		memcpy(stats, first->signal->stats, sizeof(*stats));
-	spin_unlock(&first->signal->stats_lock);
+	spin_unlock_irqrestore(&first->signal->stats_lock, flags);
 
 	tsk = first;
 	read_lock(&tasklist_lock);
@@ -185,7 +186,9 @@ static int fill_tgid(pid_t tgid, struct
 
 static void fill_tgid_exit(struct task_struct *tsk)
 {
-	spin_lock(&tsk->signal->stats_lock);
+	unsigned long flags;
+
+	spin_lock_irqsave(&tsk->signal->stats_lock, flags);
 	if (!tsk->signal->stats)
 		goto ret;
 
@@ -197,7 +200,7 @@ static void fill_tgid_exit(struct task_s
 	 */
 	delayacct_add_tsk(tsk->signal->stats, tsk);
 ret:
-	spin_unlock(&tsk->signal->stats_lock);
+	spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
 	return;
 }
 
@@ -268,13 +271,14 @@ void taskstats_exit_send(struct task_str
 	size_t size;
 	int is_thread_group;
 	struct nlattr *na;
+	unsigned long flags;
 
 	if (!family_registered || !tidstats)
 		return;
 
-	spin_lock(&tsk->signal->stats_lock);
+	spin_lock_irqsave(&tsk->signal->stats_lock, flags);
 	is_thread_group = tsk->signal->stats ? 1 : 0;
-	spin_unlock(&tsk->signal->stats_lock);
+	spin_unlock_irqrestore(&tsk->signal->stats_lock, flags);
 
 	rc = 0;
 	/*
include/linux/taskstats_kern.h +7 -4
Index: linux-2.6.17/include/linux/taskstats_kern.h
===================================================================
@@ -50,17 +50,18 @@ static inline void taskstats_tgid_init(s
 static inline void taskstats_tgid_alloc(struct signal_struct *sig)
 {
 	struct taskstats *stats;
+	unsigned long flags;
 
 	stats = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
 	if (!stats)
 		return;
 
-	spin_lock(&sig->stats_lock);
+	spin_lock_irqsave(&sig->stats_lock, flags);
 	if (!sig->stats) {
 		sig->stats = stats;
 		stats = NULL;
 	}
-	spin_unlock(&sig->stats_lock);
+	spin_unlock_irqrestore(&sig->stats_lock, flags);
 
 	if (stats)
 		kmem_cache_free(taskstats_cache, stats);
@@ -69,12 +70,14 @@ static inline void taskstats_tgid_alloc(
 static inline void taskstats_tgid_free(struct signal_struct *sig)
 {
 	struct taskstats *stats = NULL;
-	spin_lock(&sig->stats_lock);
+	unsigned long flags;
+
+	spin_lock_irqsave(&sig->stats_lock, flags);
 	if (sig->stats) {
 		stats = sig->stats;
 		sig->stats = NULL;
 	}
-	spin_unlock(&sig->stats_lock);
+	spin_unlock_irqrestore(&sig->stats_lock, flags);
 	if (stats)
 		kmem_cache_free(taskstats_cache, stats);
 }
#6 lore raw

Re: [PATCH] delay accounting taskstats interface: send tgid once locking fix

Hi,

Quoted 5 lines · Shailabh Nagar

Use irqsave variants of spin_lock for newly introduced tsk->signal->stats_lock The lock is nested within tasklist_lock as part of release_task() and hence there is possibility of a AB-BA deadlock if a timer interrupt occurs while stats_lock is held.

Quoted 7 lines

Thanks to Arjan van de Ven for pointing out the lock bug.
The patch conservatively converts all use of stats_lock to irqsave variant rather than only the call within taskstats_tgid_free which is the function called within tasklist_lock protection.

Problem solved, thanks.

Regards,
Michal

Signature · 3 lines
Michal K. K. Piotrowski
LTG - Linux Testers Group
(http://www.stardust.webpages.pl/ltg/wiki/)