public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [RFC] powerpc: optimizing random() and POSIX question
@ 2020-02-11 14:04 Raphael M Zinsly
  2020-02-11 14:43 ` Florian Weimer
  0 siblings, 1 reply; 6+ messages in thread
From: Raphael M Zinsly @ 2020-02-11 14:04 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho, Paul Clarke

Hi,

I’m investigating ways to optimize random() for powerpc and got some 
questions regarding POSIX.
The current code on stdlib/random.c does:

long int
__random (void)
{
   int32_t retval;

   __libc_lock_lock (lock);

   (void) __random_r (&unsafe_state, &retval);

   __libc_lock_unlock (lock);

   return retval;
}

If I remove the lock in order to use lqarx/stqcx to access fptr and rptr 
(that are adjacents), then compute the result as is done in random_r, 
will this still be compliant with POSIX?
As the data is accessed atomically, only one thread access the critical 
section at a time. Am I missing something?


Thanks,
-- 
Raphael Moreira Zinsly
IBM
Linux on Power Toolchain

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] powerpc: optimizing random() and POSIX question
  2020-02-11 14:04 [RFC] powerpc: optimizing random() and POSIX question Raphael M Zinsly
@ 2020-02-11 14:43 ` Florian Weimer
  2020-02-12 13:22   ` Raphael M Zinsly
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Weimer @ 2020-02-11 14:43 UTC (permalink / raw)
  To: Raphael M Zinsly
  Cc: libc-alpha, Tulio Magno Quites Machado Filho, Paul Clarke

* Raphael M. Zinsly:

> If I remove the lock in order to use lqarx/stqcx to access fptr and
> rptr (that are adjacents), then compute the result as is done in
> random_r, will this still be compliant with POSIX?

I think POSIX requires random to the thread-safe in the presence of
concurrent initstate calls.  Would your change preserve this property?

This means that random in other threads would either produce values from
the new sequence, or from the old sequence.  But not values that are
part of neither sequence.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] powerpc: optimizing random() and POSIX question
  2020-02-11 14:43 ` Florian Weimer
@ 2020-02-12 13:22   ` Raphael M Zinsly
  2020-02-12 13:23     ` Florian Weimer
  0 siblings, 1 reply; 6+ messages in thread
From: Raphael M Zinsly @ 2020-02-12 13:22 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 11/02/2020 11:42, Florian Weimer wrote:
> I think POSIX requires random to the thread-safe in the presence of
> concurrent initstate calls.  Would your change preserve this property?
> 

Yes, concurrent calls to initstate, setstate or srandom will still be 
mutual exclusive.

Thanks,
-- 
Raphael Moreira Zinsly
IBM
Linux on Power Toolchain

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] powerpc: optimizing random() and POSIX question
  2020-02-12 13:22   ` Raphael M Zinsly
@ 2020-02-12 13:23     ` Florian Weimer
  2020-02-12 14:20       ` Adhemerval Zanella
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Weimer @ 2020-02-12 13:23 UTC (permalink / raw)
  To: Raphael M Zinsly; +Cc: libc-alpha

* Raphael M. Zinsly:

> On 11/02/2020 11:42, Florian Weimer wrote:
>> I think POSIX requires random to the thread-safe in the presence of
>> concurrent initstate calls.  Would your change preserve this property?
>>
>
> Yes, concurrent calls to initstate, setstate or srandom will still be
> mutual exclusive.

Then I don't see any immediate problems with this change.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] powerpc: optimizing random() and POSIX question
  2020-02-12 13:23     ` Florian Weimer
@ 2020-02-12 14:20       ` Adhemerval Zanella
  0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-02-12 14:20 UTC (permalink / raw)
  To: libc-alpha, Raphael M Zinsly



On 12/02/2020 10:23, Florian Weimer wrote:
> * Raphael M. Zinsly:
> 
>> On 11/02/2020 11:42, Florian Weimer wrote:
>>> I think POSIX requires random to the thread-safe in the presence of
>>> concurrent initstate calls.  Would your change preserve this property?
>>>
>>
>> Yes, concurrent calls to initstate, setstate or srandom will still be
>> mutual exclusive.
> 
> Then I don't see any immediate problems with this change.

So if I understood correctly, you want to make a lock-free implementation
of 'random' by doing something like:

--
__int128_t unsafe_state;

long int
__random_r (void)
{
  union {
    __int128_t u128;
    struct {
      uint32_t *fptr, *rptr, *end_ptr;
    } p;
  } u_e, u_d;

  while (1)
    {
       u_e.u128 = atomic_load_explicit ((__int128*) &unsafe_state,
                                         memory_order_relaxed);
       u_d = u_e;

       // update u.d.*

       if (atomic_compare_exchange_weak ((__int128*) &unsafe_state,
                                         &u_e.u128, u_d.u128))
          break;
    }

  return 0;
}
--

Besides the issue that at line stdlib/random_r.c:375 __random_r not
only updates the pointer value, but also the pointer value (which
would make failed CAS to update the random state), I failing to see
the either the value of adding a powerpc specific implementation
to such bad interface or to maybe implement a generic lock-free
implementation since we have __random_r.

I would rather focus on provide a better interface with a better
implementation, such as the lock-free arc4random one Florian has
suggested sometime ago. 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] powerpc: optimizing random() and POSIX question
@ 2020-02-12 15:05 Wilco Dijkstra
  0 siblings, 0 replies; 6+ messages in thread
From: Wilco Dijkstra @ 2020-02-12 15:05 UTC (permalink / raw)
  To: 'GNU C Library', rzinsly; +Cc: tuliom, Paul Clarke

Hi,

Wouldn't the easiest and safest option to be just add the single-threaded check
so 99% of applications just bypass the locks?

Also I would strongly suggest to optimize the generic code first - target-specific
optimizations are almost always counter productive in the long term.

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-02-12 15:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-11 14:04 [RFC] powerpc: optimizing random() and POSIX question Raphael M Zinsly
2020-02-11 14:43 ` Florian Weimer
2020-02-12 13:22   ` Raphael M Zinsly
2020-02-12 13:23     ` Florian Weimer
2020-02-12 14:20       ` Adhemerval Zanella
2020-02-12 15:05 Wilco Dijkstra

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 read-only IMAP folder(s) and NNTP newsgroup(s).