Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1334393

Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min operations

From Konstantin Khlebnikov <koct9i@gmail.com>
Newsgroups linux.kernel
Subject Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min operations
Date 2016-02-15 12:20 +0100
Message-ID <r2pma-aS-23@gated-at.bofh.it> (permalink)
References <r20QO-FA-15@gated-at.bofh.it> <r2p2O-8en-11@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Mon, Feb 15, 2016 at 1:50 PM, Will Deacon <will.deacon@arm.com> wrote:
> Adding Peter and Paul,
>
> On Sun, Feb 14, 2016 at 12:09:00PM +0300, Konstantin Khlebnikov wrote:
>> bool atomic_add_max(atomic_t *var, int add, int max);
>> bool atomic_sub_min(atomic_t *var, int sub, int min);
>
> What are the memory-ordering requirements for these? Do you also want
> relaxed/acquire/release versions for the use-cases you outline?
>
> One observation is that you provide no ordering guarantees if the
> comparison fails, which is fine if that's what you want, but we should
> probably write that down like we do for cmpxchg.

Ok. Good point.

>
>> bool this_cpu_add_max(var, add, max);
>> bool this_cpu_sub_min(var, sub, min);
>>
>> They add/subtract only if result will be not bigger than max/lower that min.
>> Returns true if operation was done and false otherwise.
>>
>> Inside they check that (add <= max - var) and (sub <= var - min). Signed
>> operations work if all possible values fits into range which length fits
>> into non-negative range of that type: 0..INT_MAX, INT_MIN+1..0, -1000..1000.
>> Unsigned operations work if value always in valid range: min <= var <= max.
>> Char and short automatically casts to int, they never overflows.
>>
>> Patch adds the same for atomic_long_t, atomic64_t, local_t, local64_t.
>> And unsigned variants: atomic_u32_add_max atomic_u32_sub_min for atomic_t,
>> atomic_u64_add_max atomic_u64_sub_min for atomic64_t.
>>
>> Patch comes with test which hopefully covers all possible cornercases,
>> see CONFIG_ATOMIC64_SELFTEST and CONFIG_PERCPU_TEST.
>>
>> All this allows to build any kind of counter in several lines:
>
> Do you have another patch converting people over to these new atomics?

Thanks for comments.
Sure, I'll try to use this as wide as possible.

For now this solution is still incomlete. For example there is no simple way for
handing cpu-hotplug: per-cpu batches must be updated when cpu disappears.
Ideally cpu hotplug handlers should be registered in the same way as init/exit
functions and placed into separate code segment. Memory hotplug could be
handled in the same way too because resource limit or batching often depents
on memory size.

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH RFC] Introduce atomic and per-cpu add-max and sub-min  operations Konstantin Khlebnikov <koct9i@gmail.com> - 2016-02-14 10:10 +0100
  Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min  operations Tejun Heo <tj@kernel.org> - 2016-02-14 18:00 +0100
    Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min operations Konstantin Khlebnikov <koct9i@gmail.com> - 2016-02-14 18:40 +0100
      Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min  operations Christoph Lameter <cl@linux.com> - 2016-02-16 17:20 +0100
  Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min  operations Will Deacon <will.deacon@arm.com> - 2016-02-15 12:00 +0100
    Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min operations Konstantin Khlebnikov <koct9i@gmail.com> - 2016-02-15 12:20 +0100
    Re: [PATCH RFC] Introduce atomic and per-cpu add-max and sub-min  operations Peter Zijlstra <peterz@infradead.org> - 2016-02-15 15:20 +0100

csiph-web