linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cong Wang <xiyou.wangcong@gmail.com>
To: Arnd Bergmann <arnd@arndb.de>
Cc: linux-kernel@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Prarit Bhargava <prarit@redhat.com>,
	Greg Kroah-Hartman <greg@kroah.com>,
	Dave Young <dyoung@redhat.com>
Subject: Re: [PATCH 1/2] lkdtm: use atomic_t to replace count_lock
Date: Thu, 02 Feb 2012 21:33:40 +0800	[thread overview]
Message-ID: <4F2A90B4.1050709@gmail.com> (raw)
In-Reply-To: <201202011527.35366.arnd@arndb.de>

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.

  reply	other threads:[~2012-02-02 13:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-01  6:58 [PATCH 1/2] lkdtm: use atomic_t to replace count_lock Cong Wang
2012-02-01  6:58 ` [PATCH 2/2] lkdtm: avoid calling sleeping functions in interrupt context Cong Wang
2012-02-01 15:27 ` [PATCH 1/2] lkdtm: use atomic_t to replace count_lock Arnd Bergmann
2012-02-02 13:33   ` Cong Wang [this message]
2012-02-02 13:44     ` Arnd Bergmann
2012-02-02 14:27       ` Cong Wang
2012-02-02 14:55         ` Arnd Bergmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F2A90B4.1050709@gmail.com \
    --to=xiyou.wangcong@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=dyoung@redhat.com \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=prarit@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).