All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Cong Wang <xiyou.wangcong@gmail.com>
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: Wed, 1 Feb 2012 15:27:35 +0000	[thread overview]
Message-ID: <201202011527.35366.arnd@arndb.de> (raw)
In-Reply-To: <1328079501-24746-1-git-send-email-xiyou.wangcong@gmail.com>

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.

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?

	Arnd

  parent reply	other threads:[~2012-02-01 15:27 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 ` Arnd Bergmann [this message]
2012-02-02 13:33   ` [PATCH 1/2] lkdtm: use atomic_t to replace count_lock Cong Wang
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=201202011527.35366.arnd@arndb.de \
    --to=arnd@arndb.de \
    --cc=akpm@linux-foundation.org \
    --cc=dyoung@redhat.com \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=prarit@redhat.com \
    --cc=xiyou.wangcong@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.