2014-12-18 21:07:48

by Rogelio M. Serrano Jr.

[permalink] [raw]
Subject: atomic_inc and spin_lock_irq

whats the difference between:

atomic_inc(&port->count);

and

spin_lock_irq(&port->lock);
++port->count;
spin_unlock_irq(&port->lock);


2014-12-18 22:04:20

by Mihai Donțu

[permalink] [raw]
Subject: Re: atomic_inc and spin_lock_irq

On Thu, 18 Dec 2014 21:07:46 +0000 Rogelio M. Serrano Jr. wrote:
> whats the difference between:
>
> atomic_inc(&port->count);

It is intended to atomically increment a variable of a standard size
(int/long). See [1] for more information.

> and
>
> spin_lock_irq(&port->lock);
> ++port->count;
> spin_unlock_irq(&port->lock);

These are intended to be used when larger changes are to be made in an
atomic fashion (not just increments). See [2] for details and [3] for
an example.

[1] https://en.wikipedia.org/wiki/Fetch-and-add
[2] http://lxr.free-electrons.com/source/include/linux/spinlock_api_smp.h#L104
[3] http://lxr.free-electrons.com/source/arch/x86/mm/mmio-mod.c#L265

--
Mihai Donțu

2014-12-23 02:54:00

by PaX Team

[permalink] [raw]
Subject: Re: atomic_inc and spin_lock_irq

On 18 Dec 2014 at 21:07, Rogelio M. Serrano Jr. wrote:

> whats the difference between:
>
> atomic_inc(&port->count);
>
> and
>
> spin_lock_irq(&port->lock);
> ++port->count;
> spin_unlock_irq(&port->lock);

in PaX this kind of change brings the ->count accesses under the coverage
of the REFCOUNT feature. if the kernel had a non-atomic refcount type and
corresponding accessors, they'd be used here instead.