[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 02 Feb 2012 21:33:40 +0800
From: Cong Wang <xiyou.wangcong@...il.com>
To: Arnd Bergmann <arnd@...db.de>
CC: linux-kernel@...r.kernel.org,
Andrew Morton <akpm@...ux-foundation.org>,
Prarit Bhargava <prarit@...hat.com>,
Greg Kroah-Hartman <greg@...ah.com>,
Dave Young <dyoung@...hat.com>
Subject: Re: [PATCH 1/2] lkdtm: use atomic_t to replace count_lock
On 02/01/2012 11:27 PM, Arnd Bergmann wrote:
> On Wednesday 01 February 2012, Cong Wang wrote:
>> 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);
>> }
>
> This use is not atomic, you could have two threads doing atomic_dec_return
> at the same time, and after that the value will be -1 so the atomic_cmpxchg
> does not trigger.
Yeah, simply combining two atomic operations is not atomic. :-/
>
> In order to have an atomic here, you have to use a loop around
> atomic_cmpxchg, like
>
>
> int old, new;
> old = atomic_read(&count);
> do {
> new = old ? old - 1 : cpoint_count;
> old = cmpxchg(&count, old, new);
> } while (old != new);
>
> I suppose you could also just keep the spinlock and move lkdtm_do_action()
> outside of it?
If we still need spinlock, I think we don't need to bother atomic_t at all.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists