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

Re: [PATCH 3/9] xen/ppc: Implement atomic.h


  • To: Shawn Anastasio <sanastasio@xxxxxxxxxxxxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Wed, 9 Aug 2023 08:48:56 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bf48YHefA6bFkSCH3odqD4dx8zvG/JfvGMJBg1ei1Bc=; b=NFnh4pIWoWBm7r/tvtDoxTs7oacird47wQh0HvY2+s5+00T/EjGjnxFGFlfnXShXkXNObkDYej4oBaJakK4zSrpAAbv6tldJvqpaKkrV1jtjmd+uQnEfabwpXVO9IHO1lg+Zr6GlhDwxhw7+BAZq/eS6SflCFSCdAhvOViu8kIxpJjbh1KOkU/fGyhhCMxT0T54A7yolWkiS7FgpiVwTkn7iRb1gLJv/V8/210dLgrkt3J0FIZmGSd0zG4ALvmbQ53fo8dkyIP3j/z9m/l8U7W9TkEcZXvyTzK/d8cEMTKR6eDHYV92Xr7pl97vj3ZUY93AvSGDEieK2xS+ybGw0jA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S5f47C9Khtn+y3rDsY4uaiA9W6bnqaM67FLsLYuvTb4L24xYjWJiOYNiMSvDbHps9uCdEsJskUZnhuVa2O/vu2gvVFhPZ2CKEfX+5gWEDNiCkRMGEckOz/wW+ikqR0AiCkvSQawaV/bALmpRS2hE4r6NwWbPOkttsX+wlGfaKhF7qE/Bw3hQ3jQmn4YgsEhIuQW3o+1bjjPiMoETL75OXaenIwD/o+W+VGxWHefwwKwnUa6qIqH/bIMZ+5makNrmbxfgK9O5n/V2lpfZpB01Trv9aN8XwD8wMAxwel4dLLZSGMrxwn+g/rqzXRa1pvEzyRkxGCPcOJPPNDuEHX9itA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: Timothy Pearson <tpearson@xxxxxxxxxxxxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Wed, 09 Aug 2023 06:49:06 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 08.08.2023 21:19, Shawn Anastasio wrote:
> On 8/7/23 11:13 AM, Jan Beulich wrote:
>> On 03.08.2023 01:02, Shawn Anastasio wrote:
>>> Implement atomic.h for PPC, based off of the original Xen 3.2
>>> implementation.
>>
>> Since likely that originally came from Linux, did you cross check that
>> Linux hasn't gained any bug fixes in the meantime?
> 
> I did -- the atomic barrier instructions used by linux have changed
> since this code was originally written, so I've updated them to be
> inline with modern linux.

Please mention this in the description then.

>>> +#include <xen/atomic.h>
>>> +
>>> +#include <asm/memory.h>
>>> +#include <asm/system.h>
>>> +
>>> +static inline int atomic_read(const atomic_t *v)
>>> +{
>>> +    return *(volatile int *)&v->counter;
>>> +}
>>> +
>>> +static inline int _atomic_read(atomic_t v)
>>> +{
>>> +    return v.counter;
>>> +}
>>> +
>>> +static inline void atomic_set(atomic_t *v, int i)
>>> +{
>>> +    v->counter = i;
>>> +}
>>> +
>>> +static inline void _atomic_set(atomic_t *v, int i)
>>> +{
>>> +    v->counter = i;
>>> +}
>>> +
>>> +void __bad_atomic_read(const volatile void *p, void *res);
>>> +void __bad_atomic_size(void);
>>> +
>>> +#define build_atomic_read(name, insn, type)                                
>>>     \
>>> +    static inline type name(const volatile type *addr)                     
>>>     \
>>> +    {                                                                      
>>>     \
>>> +        type ret;                                                          
>>>     \
>>> +        asm volatile ( insn "%U1%X1 %0,%1" : "=r"(ret) : "m<>"(*addr) );   
>>>     \
>>
>> As I think I had mentioned before, asm() contraints want a blank between
>> closing quote and opend paren. I.e. like this
>>
>>         asm volatile ( insn "%U1%X1 %0,%1" : "=r" (ret) : "m<>" (*addr) );
>>
> 
> My mistake, I went through and hand-formatted all of this code to try to
> be inline with Xen's style but forgot about the constraints.
> 
> As an aside, I don't suppose there is an automatic formatter somewhere
> that I've missed? I found an old clang-format fork that claims to add
> support for Xen's formatting[1] but it seems to only handle a subset of
> Xen's rules so I haven't found it very useful.
> 
> [1] https://github.com/NastyaVicodin/llvm-project/commits/main

Work there was recently revived, but we're not quite there yet, sorry.

>>> +#define read_atomic(p)                                                     
>>>     \
>>> +    ({                                                                     
>>>     \
>>> +        union {                                                            
>>>     \
>>> +            typeof(*(p)) val;                                              
>>>     \
>>> +            char c[0];                                                     
>>>     \
>>> +        } x_;                                                              
>>>     \
>>> +        read_atomic_size(p, x_.c, sizeof(*(p)));                           
>>>     \
>>> +        x_.val;                                                            
>>>     \
>>> +    })
>>> +
>>> +#define write_atomic(p, x)                                                 
>>>     \
>>> +    do                                                                     
>>>     \
>>> +    {                                                                      
>>>     \
>>> +        typeof(*(p)) x_ = (x);                                             
>>>     \
>>> +        write_atomic_size(p, &x_, sizeof(*(p)));                           
>>>     \
>>> +    } while ( 0 )
>>
>> Up to here you use underscore-suffixed locals, but then ...
>>
>>> +#define add_sized(p, x)                                                    
>>>     \
>>> +    ({                                                                     
>>>     \
>>> +        typeof(*(p)) __x = (x);                                            
>>>     \
>>
>> ... you have even two prefixing underscores here.
>>
> 
> The definitions of these macros were directly copied from elsewhere in
> Xen (x86 and arm). I can change them all to use underscore-suffixed
> local naming here, though.

We're still far away from eliminating all pre-existing issues. So copying
existing code is always at risk of then getting such comments. Again -
sorry for that, but otherwise we'll grow the number of issues.

>>> +        switch ( sizeof(*(p)) )                                            
>>>     \
>>> +        {                                                                  
>>>     \
>>> +        case 1:                                                            
>>>     \
>>> +            add_u8_sized((uint8_t *) (p), __x);                            
>>>     \
>>> +            break;                                                         
>>>     \
>>> +        case 2:                                                            
>>>     \
>>> +            add_u16_sized((uint16_t *) (p), __x);                          
>>>     \
>>> +            break;                                                         
>>>     \
>>> +        case 4:                                                            
>>>     \
>>> +            add_u32_sized((uint32_t *) (p), __x);                          
>>>     \
>>> +            break;                                                         
>>>     \
>>> +        default:                                                           
>>>     \
>>> +            __bad_atomic_size();                                           
>>>     \
>>> +            break;                                                         
>>>     \
>>> +        }                                                                  
>>>     \
>>> +    })
>>> +
>>> +static inline void atomic_add(int a, atomic_t *v)
>>> +{
>>> +    int t;
>>> +
>>> +    asm volatile ( "1: lwarx %0,0,%3\n"
>>> +                   "add %0,%2,%0\n"
>>> +                   "stwcx. %0,0,%3\n"
>>> +                   "bne- 1b"
>>> +                   : "=&r"(t), "=m"(v->counter)
>>> +                   : "r"(a), "r"(&v->counter), "m"(v->counter) : "cc" );
>>
>> "+m" and then drop the last input?
>>
> 
> Yes, that makes sense. Not sure why it was originally written that way
> but I'll change it.

I think this was attributed to not sufficiently well written documentation
for older gcc.

>>> +static inline int __atomic_add_unless(atomic_t *v, int a, int u)
>>> +{
>>> +   int c, old;
>>> +
>>> +   c = atomic_read(v);
>>> +   while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
>>
>> Btw, no real need to parenthesize v in cases like this one. Otoh a needs
>> parenthesizing.
> 
> FWIW this was copied directly from arm64/atomic.h, but seeing as its a
> normal function and not a macro I'm not sure I see why `a` would need
> parenthesis.

Oh, right you are. The parenthesized v misled me.

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.