Re: atomic test and set

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On Oct 17, 2017, at 5:40 PM, Tobin C. Harding <me@xxxxxxxx> wrote:

Hi,

Do we have an atomic test and set function in the kernel. I have tried

Yes and it is used very much inside the kernel.

const int KEY_FLAG_BIT = 1;

...

   static siphash_key_t ptr_secret __read_mostly;
   static unsigned long have_key = 0;

   if (test_and_set_bit(KEY_FLAG_BIT, &have_key))
       get_random_bytes(&ptr_secret, sizeof(ptr_secret));


But that doesn't work.

What doesn't work? What you expected and how is the result different?

I looked in include/linux/atomic.h and thought about using

   static atomic_t have_key = ATOMIC_INIT(0);

       if (atomic_xchg(&have_key, 1) == 0)
           get_random_bytes(&ptr_secret, sizeof(ptr_secret));


This works. My question is; does this code LOAD the value at have_key and STORE the argument on
every call? Or does it LOAD the value, check if it is the same as the argument, and STORE _only_ if
it is different?

Did you read this https://www.mjmwired.net/kernel/Documentation/atomic_ops.txt ?
(Is this whole discussion just premature optimization?)

This has nothing to do with optimization but rather the requirement. In kernel atomic variables are used to avoid race conditions generally.

I cannot grok the macros in atomic.h, they seem circular. Here is the macro definitions in call
chain order starting with atomic_xchg()

Implementation would differ based on the architecture. In some architectures it would be a single instruction and in others it may not be the case.


#define  atomic_xchg(...)                        \
   __atomic_op_fence(atomic_xchg, __VA_ARGS__)

#define __atomic_op_fence(op, args...)                    \
({                                    \
   typeof(op##_relaxed(args)) __ret;                \
   smp_mb__before_atomic();                    \
   __ret = op##_relaxed(args);                    \
   smp_mb__after_atomic();                        \
   __ret;                                \
})

#define  atomic_xchg_release(...)                    \
   __atomic_op_release(atomic_xchg, __VA_ARGS__)

#define __atomic_op_release(op, args...)                \
({                                    \
   smp_mb__before_atomic();                    \
   op##_relaxed(args);                        \
})

#define  atomic_xchg_relaxed        atomic_xchg

thanks,
Tobin.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux