[PATCH 1/2] lkdtm: use atomic_t to replace count_lock

From: Cong Wang
Date: Wed Feb 01 2012 - 01:58:52 EST


Andrew, this patch replaces
lkdtm-avoid-calling-lkdtm_do_action-with-spin-lock-held.patch
in your tree.

---------->

The spin lock count_lock only protects count, it can be removed by
using atomic_t. Suggested by Arnd.

Cc: Prarit Bhargava <prarit@xxxxxxxxxx>
Cc: Arnd Bergmann <arnd@xxxxxxxx>
Cc: Greg Kroah-Hartman <greg@xxxxxxxxx>
Cc: Dave Young <dyoung@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Cong Wang <xiyou.wangcong@xxxxxxxxx>

---
drivers/misc/lkdtm.c | 19 ++++---------------
1 files changed, 4 insertions(+), 15 deletions(-)

diff --git a/drivers/misc/lkdtm.c b/drivers/misc/lkdtm.c
index 150cd70..afdef2e 100644
--- a/drivers/misc/lkdtm.c
+++ b/drivers/misc/lkdtm.c
@@ -119,8 +119,7 @@ static int recur_count = REC_NUM_DEFAULT;

static enum cname cpoint = CN_INVALID;
static enum ctype cptype = CT_NONE;
-static int count = DEFAULT_COUNT;
-static DEFINE_SPINLOCK(count_lock);
+static atomic_t count = ATOMIC_INIT(DEFAULT_COUNT);

module_param(recur_count, int, 0644);
MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test, "\
@@ -231,14 +230,11 @@ static const char *cp_name_to_str(enum cname name)
static int lkdtm_parse_commandline(void)
{
int i;
- unsigned long flags;

if (cpoint_count < 1 || recur_count < 1)
return -EINVAL;

- spin_lock_irqsave(&count_lock, flags);
- count = cpoint_count;
- spin_unlock_irqrestore(&count_lock, flags);
+ atomic_set(&count, cpoint_count);

/* No special parameters */
if (!cpoint_type && !cpoint_name)
@@ -353,18 +349,11 @@ static void lkdtm_do_action(enum ctype which)

static void lkdtm_handler(void)
{
- unsigned long flags;
-
- spin_lock_irqsave(&count_lock, flags);
- count--;
printk(KERN_INFO "lkdtm: Crash point %s of type %s hit, trigger in %d rounds\n",
- cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
+ cp_name_to_str(cpoint), cp_type_to_str(cptype), atomic_dec_return(&count));

- if (count == 0) {
+ if (!atomic_cmpxchg(&count, 0, cpoint_count))
lkdtm_do_action(cptype);
- count = cpoint_count;
- }
- spin_unlock_irqrestore(&count_lock, flags);
}

static int lkdtm_register_cpoint(enum cname which)
--
1.7.7.6

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