Skip to contentSkip to conversation

lkdtm: avoid calling lkdtm_do_action() with spin lock held

PATCH In review Cong Wang 4 participants 8 messages Jan 28, 2012 – Feb 2, 2012 Char and misc drivers

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 lkdtm: avoid calling lkdtm_do_action() with spin lock held

lkdtm_do_action() may call sleeping functions like kmalloc(), so do not call it with spin lock held.

Cc Prarit Bhargava <[email protected]>

---

drivers/misc/lkdtm.c +5 -1
index 150cd70..28adefe 100644
@@ -354,6 +354,7 @@ static void lkdtm_do_action(enum ctype which)
 static void lkdtm_handler(void)
 {
 	unsigned long flags;
+	bool do_it = false;
 
 	spin_lock_irqsave(&count_lock, flags);
 	count--;
@@ -361,10 +362,13 @@ static void lkdtm_handler(void)
 			cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
 
 	if (count == 0) {
-		lkdtm_do_action(cptype);
+		do_it = true;
 		count = cpoint_count;
 	}
 	spin_unlock_irqrestore(&count_lock, flags);
+
+	if (do_it)
+		lkdtm_do_action(cptype);
 }
 
 static int lkdtm_register_cpoint(enum cname which)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Cong Wang

lkdtm_do_action() may call sleeping functions like kmalloc(), so do not call it with spin lock held.

Cc Prarit Bhargava <[email protected]>
Quoted 34 lines

---

drivers/misc/lkdtm.c +5 -2
index 150cd70..28adefe 100644
@@ -354,6 +354,7 @@ static void lkdtm_do_action(enum ctype which)
 static void lkdtm_handler(void)
 {
 	unsigned long flags;
+	bool do_it = false;
 
 	spin_lock_irqsave(&count_lock, flags);
 	count--;
@@ -361,10 +362,13 @@ static void lkdtm_handler(void)
 			cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
 
 	if (count == 0) {
-		lkdtm_do_action(cptype);
+		do_it = true;
 		count = cpoint_count;
 	}
 	spin_unlock_irqrestore(&count_lock, flags);
+
+	if (do_it)
+		lkdtm_do_action(cptype);
 }
 
 static int lkdtm_register_cpoint(enum cname which)
--

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/

Signature · 2 lines
Thanks
Dave
#2 lore raw
In reply to Cong Wang
Quoted 24 lines · On Sat, 28 Jan 2012 20:52:47 +0800 Cong Wang <[email protected]> wrote:

lkdtm_do_action() may call sleeping functions like kmalloc(), so do not call it with spin lock held.

Cc Prarit Bhargava <[email protected]>

---

drivers/misc/lkdtm.c +1 -1
index 150cd70..28adefe 100644
@@ -354,6 +354,7 @@ static void lkdtm_do_action(enum ctype which)
 static void lkdtm_handler(void)
 {
 	unsigned long flags;
+	bool do_it = false;
 
 	spin_lock_irqsave(&count_lock, flags);
 	count--;
@@ -361,10 +362,13 @@ static void lkdtm_handler(void)
 			cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
 
 	if (count == 0) {
-		lkdtm_do_action(cptype);

drivers/misc/lkdtm.c static void lkdtm_handler(void) · line 374

Quoted 10 lines
drivers/misc/lkdtm.c +4 -0
+		do_it = true;
 		count = cpoint_count;
 	}
 	spin_unlock_irqrestore(&count_lock, flags);
+
+	if (do_it)
+		lkdtm_do_action(cptype);
 }
 
 static int lkdtm_register_cpoint(enum cname which)

lkdtm_handler() can be called from module IRQ handlers, so perhaps the same problems can still happen. The patch does improve things though ;)

author
#3 lore raw
Quoted 4 lines · Andrew Morton

lkdtm_handler() can be called from module IRQ handlers, so perhaps the same problems can still happen. The patch does improve things though ;)

Yeah, what do you think about patch below (untested)?

---

drivers/misc/lkdtm.c +7 -3
index 28adefe..5cbd740 100644
@@ -315,7 +315,7 @@ static void lkdtm_do_action(enum ctype which)
 	}
 	case CT_OVERWRITE_ALLOCATION: {
 		size_t len = 1020;
-		u32 *data = kmalloc(len, GFP_KERNEL);
+		u32 *data = kmalloc(len, GFP_ATOMIC);
 
 		data[1024 / sizeof(u32)] = 0x12345678;
 		kfree(data);
@@ -323,14 +323,16 @@ static void lkdtm_do_action(enum ctype which)
 	}
 	case CT_WRITE_AFTER_FREE: {
 		size_t len = 1024;
-		u32 *data = kmalloc(len, GFP_KERNEL);
+		u32 *data = kmalloc(len, GFP_ATOMIC);
 
 		kfree(data);
-		schedule();
+		udelay(100);
 		memset(data, 0x78, len);
 		break;
 	}
 	case CT_SOFTLOCKUP:
+		if (in_interrupt())
+			break;
 		preempt_disable();
 		for (;;)
 			cpu_relax();
@@ -341,6 +343,8 @@ static void lkdtm_do_action(enum ctype which)
 			cpu_relax();
 		break;
 	case CT_HUNG_TASK:
+		if (in_interrupt())
+			break;
 		set_current_state(TASK_UNINTERRUPTIBLE);
 		schedule();
 		break;
maintainer
#4 lore raw

drivers/misc/lkdtm.c static void lkdtm_do_action(enum ctype which) · line 332

Quoted 13 lines · Cong Wang
drivers/misc/lkdtm.c +2 -2
@@ -323,14 +323,16 @@ static void lkdtm_do_action(enum ctype which)
        }
        case CT_WRITE_AFTER_FREE: {
                size_t len = 1024;
-               u32 *data = kmalloc(len, GFP_KERNEL);
+               u32 *data = kmalloc(len, GFP_ATOMIC);
 
                kfree(data);
-               schedule();
+               udelay(100);
                memset(data, 0x78, len);
                break;
        }

I can't think of why the udelay would have any positive effect here, if the idea of the schedule was to let some other process allocate and use the memory.

Can't you just get rid of the count_lock if you use an atomic_t for the count and use appropriate accesses on it?

Arnd

author
#5 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Cong Wang
drivers/misc/lkdtm.c +1 -1
@@ -323,14 +323,16 @@ static void lkdtm_do_action(enum ctype which)
         }
         case CT_WRITE_AFTER_FREE: {
                 size_t len = 1024;
-               u32 *data = kmalloc(len, GFP_KERNEL);
+               u32 *data = kmalloc(len, GFP_ATOMIC);
 
Quoted 10 lines

drivers/misc/lkdtm.c static void lkdtm_do_action(enum ctype which) · line 332

Quoted 6 lines
drivers/misc/lkdtm.c +1 -1
                 kfree(data);
-               schedule();
+               udelay(100);
                 memset(data, 0x78, len);
                 break;
         }

I can't think of why the udelay would have any positive effect here, if the idea of the schedule was to let some other process allocate and use the memory.

Hmm, on SMP udelay on this CPU will give a chance to other CPU's to use that memory, right?

Quoted 4 lines

Can't you just get rid of the count_lock if you use an atomic_t for the count and use appropriate accesses on it?

Good idea, will do.

maintainer
#6 lore raw
Quoted 21 lines · Cong Wang
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Cong Wang
drivers/misc/lkdtm.c +1 -1
@@ -323,14 +323,16 @@ static void lkdtm_do_action(enum ctype which)
         }
         case CT_WRITE_AFTER_FREE: {
                 size_t len = 1024;
-               u32 *data = kmalloc(len, GFP_KERNEL);
+               u32 *data = kmalloc(len, GFP_ATOMIC);
 
Quoted 10 lines

drivers/misc/lkdtm.c static void lkdtm_do_action(enum ctype which) · line 332

Quoted 6 lines
drivers/misc/lkdtm.c +1 -1
                 kfree(data);
-               schedule();
+               udelay(100);
                 memset(data, 0x78, len);
                 break;
         }

I can't think of why the udelay would have any positive effect here, if the idea of the schedule was to let some other process allocate and use the memory.

Quoted 3 lines

Hmm, on SMP udelay on this CPU will give a chance to other CPU's to use that memory, right?

There is a small chance for that, but it's much less likely than it would be using another process on the same CPU, plus it requires SMP.

Arnd

author
#7 lore raw
Quoted 22 lines · Arnd Bergmann
Quoted 21 lines · Cong Wang
Quoted 18 lines · Arnd Bergmann

drivers/misc/lkdtm.c static void lkdtm_do_action(enum ctype which) · line 332

Quoted 13 lines · Cong Wang
drivers/misc/lkdtm.c +2 -2
@@ -323,14 +323,16 @@ static void lkdtm_do_action(enum ctype which)
          }
          case CT_WRITE_AFTER_FREE: {
                  size_t len = 1024;
-               u32 *data = kmalloc(len, GFP_KERNEL);
+               u32 *data = kmalloc(len, GFP_ATOMIC);
 
                  kfree(data);
-               schedule();
+               udelay(100);
                  memset(data, 0x78, len);
                  break;
          }

I can't think of why the udelay would have any positive effect here, if the idea of the schedule was to let some other process allocate and use the memory.

Quoted 6 lines
Quoted 3 lines

Hmm, on SMP udelay on this CPU will give a chance to other CPU's to use that memory, right?

There is a small chance for that, but it's much less likely than it would be using another process on the same CPU, plus it requires SMP.

Yeah, I updated this in [PATCH 2/2] lkdtm: avoid calling sleeping functions in interrupt context.