Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp3220340rwb; Mon, 7 Aug 2023 09:57:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDQWZZfkv3HN8LSoZgCmDWwAkvMJnr9O5Dd/6k+5OD+b8uO/evA1g1E/bH2/8fYcxUGqx9 X-Received: by 2002:a05:6358:2789:b0:139:f5e9:4463 with SMTP id l9-20020a056358278900b00139f5e94463mr6375633rwb.2.1691427425672; Mon, 07 Aug 2023 09:57:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691427425; cv=none; d=google.com; s=arc-20160816; b=AwBeIsa2siOOblSIWlJbSBDcZa5LYtnzCGNVuPDPneklj/K5yEuZ+5+daz9K1rhE8h q2K9Fy6CQs1jUfmqQKIYxs5OkKSclje/2mFCYhh9L/3o/wYfx8pBK6vzRU/4yS3jiIlf zcAZto6lrshDsDqDVpBHJy5vFcgsxJkBqiftsjaWwrbWK7gasGngSawRs//e8/hFv3Gl uGfIA2LrN3wiQZOGrrrCh5UZKo2b6lK2JwDEEsfFGCQDXG5I9/ADMvliNhZbugoEM06X HRzEnKh+hcjH47sPUL6+osAwN2N8qy5vgg+veEgP/v5y3SoqLNM4k+/7tRIX7nlgKcIE pZQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=f+TOwkD920NNblLgbScyx3AiTipyCC5JcEGUcFdBUPs=; fh=f40SdnOOu/5jCU5qoPBgK7WvMUETUPFE6penttNO3gg=; b=Ln03VNbxdyvr08Nb/ey/UeIFetgVvaRCDeYXGaivbVMZvSKzS878blffLUhc9Jpx+b +LhWP4R+DgDNI4nqYvG4CYGwSyXU9N3m+1YEYrfLN4aPC1ehjehZlr/0EdPXesgsUdcm uur8KQ97m6TbqBRG8TSCgm3mo+6NK8x6Lm3sk6LXLaeI0CuGLITQDlyMf29NCd20z4K8 DUmd1BDDoTdZjDSpkueqvFEJBJEFKuCboI1tOJVY8KV9k0EY9QsXgFDGJy+nRxthwu8x oaHzoN8944r8v9UO1pdYW8hi/MY2he0R4qURzMMEuLu1d/R6GQPLElBnOZwID3AkctUT YpLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="QP/v9hKM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h18-20020a63f912000000b005641ddd0303si4116285pgi.596.2023.08.07.09.56.52; Mon, 07 Aug 2023 09:57:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="QP/v9hKM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbjHGQSe (ORCPT + 99 others); Mon, 7 Aug 2023 12:18:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229824AbjHGQSd (ORCPT ); Mon, 7 Aug 2023 12:18:33 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BE2F10E3 for ; Mon, 7 Aug 2023 09:17:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691425061; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f+TOwkD920NNblLgbScyx3AiTipyCC5JcEGUcFdBUPs=; b=QP/v9hKMM9+R0Pp/4ZFCOLCxvcAPYs9SXpITRRb74ncS0vNXIkTz6JqxEEztNeS6+Mkiy9 Usg0G/NabyTVuovNdiG6UiFc7V0eEXOhezQ8C901c0o2cZi5DkffD2CIFPH6K1/m5U6Wld LBfnkzhIFO7XjhicQAMuuWlA4wPtbRI= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-83-rtAcrjWdNI2ShbD_mTbAbw-1; Mon, 07 Aug 2023 12:17:37 -0400 X-MC-Unique: rtAcrjWdNI2ShbD_mTbAbw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-40c10c73650so54861731cf.0 for ; Mon, 07 Aug 2023 09:17:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691425057; x=1692029857; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f+TOwkD920NNblLgbScyx3AiTipyCC5JcEGUcFdBUPs=; b=CGnLinj0zPJZnKlaQ43FAYHzfopTuPRqHErueh0Zl2dUX3OSMnADwunDNWrBvW/aCe 1hcjNUj3Ywfy83CpCUtnPEL0cEeTgM6no8ESJbuDCIBIo3BDDB46i689fXlz9zGyPQh0 /x9CWXaA4dTNZEURiJlZlsRuwL6ZD5WD4HPM2Y3O/jL1D+t29jXWq8sWsWJkMUEwoR2e QiPzMilupd9AANWo2tDl1orMTN+GVdrzcvs/WFwExYm3DHIQz/wme8twhcW9JtfI22z0 6HNM01hdrs0IoBW5Y8eMRa3drYnKyNuU7/ZTUR7itdRE6ZnhkxLGb/S0bRom8HEz6aVg tqNw== X-Gm-Message-State: AOJu0YxOtL0et5lUqONEpXlUEETlxEiUKTQniNLtrn1RbPu4tTeuO22b E+MFp8loGFd6CGjNuljaqpV0z31TiPQLFWLM1AuYqbdjpqecD/yeXpu/4bPDRr03nfj7PimkkeM ZWAMaEAVjAdZfzjesP/S/IwDkwO7zE3N95WO4le1y X-Received: by 2002:ac8:5a45:0:b0:403:72fa:62fc with SMTP id o5-20020ac85a45000000b0040372fa62fcmr12784376qta.68.1691425057283; Mon, 07 Aug 2023 09:17:37 -0700 (PDT) X-Received: by 2002:ac8:5a45:0:b0:403:72fa:62fc with SMTP id o5-20020ac85a45000000b0040372fa62fcmr12784353qta.68.1691425056956; Mon, 07 Aug 2023 09:17:36 -0700 (PDT) MIME-Version: 1.0 References: <20230804084900.1135660-2-leobras@redhat.com> <20230804084900.1135660-6-leobras@redhat.com> In-Reply-To: From: Leonardo Bras Soares Passos Date: Mon, 7 Aug 2023 13:17:22 -0300 Message-ID: Subject: Re: [RFC PATCH v3 4/5] riscv/cmpxchg: Implement cmpxchg for variables of size 1 and 2 To: Guo Ren Cc: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrea Parri , Geert Uytterhoeven , Andrzej Hajda , Palmer Dabbelt , linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Aug 5, 2023 at 1:24=E2=80=AFAM Guo Ren wrote: > > On Sat, Aug 5, 2023 at 11:14=E2=80=AFAM Leonardo Bras Soares Passos > wrote: > > > > Hello Guo Ren, thanks for the feedback! > > > > On Fri, Aug 4, 2023 at 2:45=E2=80=AFPM Guo Ren wrot= e: > > > > > > On Fri, Aug 4, 2023 at 4:49=E2=80=AFAM Leonardo Bras wrote: > > > > > > > > cmpxchg for variables of size 1-byte and 2-bytes is not yet availab= le for > > > > riscv, even though its present in other architectures such as arm64= and > > > > x86. This could lead to not being able to implement some locking me= chanisms > > > > or requiring some rework to make it work properly. > > > > > > > > Implement 1-byte and 2-bytes cmpxchg in order to achieve parity wit= h other > > > > architectures. > > > > > > > > Signed-off-by: Leonardo Bras > > > > --- > > > > arch/riscv/include/asm/cmpxchg.h | 35 ++++++++++++++++++++++++++++= ++++ > > > > 1 file changed, 35 insertions(+) > > > > > > > > diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/= asm/cmpxchg.h > > > > index 5a07646fae65..dfb433ac544f 100644 > > > > --- a/arch/riscv/include/asm/cmpxchg.h > > > > +++ b/arch/riscv/include/asm/cmpxchg.h > > > > @@ -72,6 +72,36 @@ > > > > * indicated by comparing RETURN with OLD. > > > > */ > > > > > > > > +#define __arch_cmpxchg_mask(sc_sfx, prepend, append, r, p, o, n) = \ > > > > +({ = \ > > > > + /* Depends on 2-byte variables being 2-byte aligned */ = \ > > > > + ulong __s =3D ((ulong)(p) & 0x3) * BITS_PER_BYTE; = \ > > > > + ulong __mask =3D GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1= , 0) \ > > > > + << __s; = \ > > > > + ulong __newx =3D (ulong)(n) << __s; = \ > > > > + ulong __oldx =3D (ulong)(o) << __s; = \ > > > > + ulong __retx; = \ > > > > + register unsigned int __rc; = \ > > > > + = \ > > > > + __asm__ __volatile__ ( = \ > > > > + prepend = \ > > > > + "0: lr.w %0, %2\n" = \ > > > > + " and %0, %0, %z5\n" = \ > > > > + " bne %0, %z3, 1f\n" = \ > > > > > bug: > > > - " and %0, %0, %z5\n" = \ > > > - " bne %0, %z3, 1f\n" = \ > > > + " and %1, %0, %z5\n" = \ > > > + " bne %1, %z3, 1f\n" = \ > > > Your code breaks the %0. > > > > What do you mean by breaks here? > > > > In the end of this macro, I intended to have __retx =3D (*p & __mask) > > which means the value is clean to be rotated at the end of the macro > > (no need to apply the mask again): r =3D __ret >> __s; > > > > Also, I assumed we are supposed to return the same variable type > > as the pointer, so this is valid: > > u8 a, *b, c; > > a =3D xchg(b, c); > > > > Is this correct? > I missed your removing "__ret & mask" at the end. So this may not the pro= blem. > > Your patch can't boot. After chewing your code for several hours, I > found a problem: > diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cm= pxchg.h > index 943f094375c7..67bcce63b267 100644 > --- a/arch/riscv/include/asm/cmpxchg.h > +++ b/arch/riscv/include/asm/cmpxchg.h > @@ -14,6 +14,7 @@ > #define __arch_xchg_mask(prepend, append, r, p, n) \ > ({ \ > /* Depends on 2-byte variables being 2-byte aligned */ \ > + volatile ulong *__p =3D (ulong *)((ulong)(p) & ~0x3); = \ Wow, I totally missed this one: I should not assume the processor behavior on unaligned lr/sc. Thanks for spotting this :) > ulong __s =3D ((ulong)(p) & 0x3) * BITS_PER_BYTE; = \ > ulong __mask =3D GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) = \ > << __s; \ > @@ -29,7 +30,7 @@ > " sc.w %1, %1, %2\n" \ > " bnez %1, 0b\n" \ > append \ > - : "=3D&r" (__retx), "=3D&r" (__rc), "+A" (*(p)) = \ > + : "=3D&r" (__retx), "=3D&r" (__rc), "+A" (*(__p)) = \ > : "rJ" (__newx), "rJ" (~__mask) \ > : "memory"); \ > \ > @@ -106,6 +107,7 @@ > #define __arch_cmpxchg_mask(sc_sfx, prepend, append, r, p, o, n) \ > ({ \ > /* Depends on 2-byte variables being 2-byte aligned */ \ > + volatile ulong *__p =3D (ulong *)((ulong)(p) & ~0x3); = \ > ulong __s =3D ((ulong)(p) & 0x3) * BITS_PER_BYTE; = \ > ulong __mask =3D GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) = \ > << __s; \ > @@ -125,7 +127,7 @@ > " bnez %1, 0b\n" \ > append \ > "1:\n" \ > - : "=3D&r" (__retx), "=3D&r" (__rc), "+A" (*(p)) = \ > + : "=3D&r" (__retx), "=3D&r" (__rc), "+A" (*(__p)) = \ > : "rJ" ((long)__oldx), "rJ" (__newx), \ > "rJ" (__mask), "rJ" (~__mask) \ > : "memory"); \ > > But the lkvm-static still can't boot with paravirt_spinlock .... Are > there any atomic tests in the Linux? I recall reading something about 'locktorture' or so, not sure if useful: https://docs.kernel.org/locking/locktorture.html > > I found you use some "register int variables". Would it cause the problem= ? Honestly, not sure. I will try inspecting the generated asm for any unexpected changes compared to your version. > > You can reference this file, and it has passed the lock torture test: > https://github.com/guoren83/linux/blob/sg2042-master-qspinlock-64ilp32_v4= /arch/riscv/include/asm/cmpxchg.h > > I also merged your patches with the qspinlock series: (Use the above > cmpxchg.h the lkvm would run normally.) > https://github.com/guoren83/linux/tree/qspinlock_v11 Thanks! I should reply as soon as I find anything. Best regards, Leo > > > > > > > > > + append = \ > > > > + "1:\n" = \ > > > > + : "=3D&r" (__retx), "=3D&r" (__rc), "+A" (*(p)) = \ > > > > + : "rJ" ((long)__oldx), "rJ" (__newx), = \ > > > > + "rJ" (__mask), "rJ" (~__mask) = \ > > > > + : "memory"); = \ > > > > + = \ > > > > + r =3D (__typeof__(*(p)))(__retx >> __s); = \ > > > > +}) > > > > + > > > > > > > > #define __arch_cmpxchg(lr_sfx, sc_sfx, prepend, append, r, p, co, = o, n) \ > > > > ({ = \ > > > > @@ -98,6 +128,11 @@ > > > > __typeof__(*(ptr)) __ret; = \ > > > > = \ > > > > switch (sizeof(*__ptr)) { = \ > > > > + case 1: = \ > > > > + case 2: = \ > > > > + __arch_cmpxchg_mask(sc_sfx, prepend, append, = \ > > > > + __ret, __ptr, __old, __new)= ; \ > > > > + break; = \ > > > > case 4: = \ > > > > __arch_cmpxchg(".w", ".w" sc_sfx, prepend, append, = \ > > > > __ret, __ptr, (long), __old, __new)= ; \ > > > > -- > > > > 2.41.0 > > > > > > > > > > > > > -- > > > Best Regards > > > Guo Ren > > > > > > > > -- > Best Regards > Guo Ren >