Re: [RFC PATCH 2/6] zsmalloc: make zspage lock preemptible
From: Sergey Senozhatsky
Date: Mon Jan 27 2025 - 19:30:00 EST
On (25/01/27 21:23), Uros Bizjak wrote:
> > +static void zspage_read_lock(struct zspage *zspage)
> > +{
> > + atomic_t *lock = &zspage->lock;
> > + int old;
> > +
> > + while (1) {
> > + old = atomic_read(lock);
> > + if (old == ZS_PAGE_WRLOCKED) {
> > + cpu_relax();
> > + continue;
> > + }
> > +
> > + if (atomic_cmpxchg(lock, old, old + 1) == old)
> > + return;
>
> You can use atomic_try_cmpxchg() here:
>
> if (atomic_try_cmpxchg(lock, &old, old + 1))
> return;
>
> > +
> > + cpu_relax();
> > + }
> > +}
> > +
> > +static void zspage_read_unlock(struct zspage *zspage)
> > +{
> > + atomic_dec(&zspage->lock);
> > +}
> > +
> > +static void zspage_write_lock(struct zspage *zspage)
> > +{
> > + atomic_t *lock = &zspage->lock;
> > + int old;
> > +
> > + while (1) {
> > + old = atomic_cmpxchg(lock, ZS_PAGE_UNLOCKED, ZS_PAGE_WRLOCKED);
> > + if (old == ZS_PAGE_UNLOCKED)
> > + return;
>
> Also, the above code can be rewritten as:
>
> while (1) {
> old = ZS_PAGE_UNLOCKED;
> if (atomic_try_cmpxchg (lock, &old, ZS_PAGE_WRLOCKED))
> return;
> > +
> > + cpu_relax();
> > + }
> > +}
>
> The above change will result in a slightly better generated asm.
Thanks, I'll take a look for the next version.