Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3494827pxb; Mon, 4 Apr 2022 18:48:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw979pj3kgd5yH8T2CTbcQZms5OLLix6uJ3qUKer1tyf8eFkiwCEYixDIpAPgaWJ+alS3z0 X-Received: by 2002:a63:ec46:0:b0:381:81c4:ebbd with SMTP id r6-20020a63ec46000000b0038181c4ebbdmr880874pgj.534.1649123307582; Mon, 04 Apr 2022 18:48:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649123307; cv=none; d=google.com; s=arc-20160816; b=vMo9/V1LvqIOSa7QEBXL0PR0SrIVwLPCEqQfwfYb+N/jAaz2t4dCBEAJJxGIteeIxq UGeVy36FYODha6Z+WM8NQbZWWCh/FLJZhueLgxuY1E9xSGtVfcjZ5reCG/bdO/orUkMO 7Gi9gX2laPb6wmbNnlku2+kFfvUHuTYgwIMmu50/ctuN5AlHjcY6uka3pqPgNzvz1Apg c+qcn8pKOjY9/Uy8UoBP2wrZgWBEureohwfGk4NORIcHMV28hzo7lh1DK8F4zfMe8Ubq /Q18C4aa4X50fdotGERFkSDF4r6Ph1UhqiiPpv8akUBWOC5fIE/lEMrWTaZxFY3w7wtN hzcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=IGvz+saGqGJ7n4uIJ25zKRG9x6+wAmAWhzfjK7OHNP8=; b=Wx8yuJXPrJt6suHkon9tt+mUs8fbqJX1e3ROrvrPDP0HAx+wzVktDjiVM6cYj+kxnv nBBfoTmhzDjBvSTVW647FYEWRUEHFDbiwohXXCdXctzr1v9dO5mHtAd6SaJR45IOW0Qt xN0Da5fTnyjv9nk3AttahCGNGzxa8T89SzAx7I5uyVk0KPI2+5T0NnV7zANDC52+xqMQ SKQqP4UE9iEiRHC8EFIz25qLcB+zMHLQMJmUEQS43Bav8RjLo5AB1t3ZGGOkJ3nCKovH qyl32NrioXKAIv5Vt/wH32CKBWxvaiub5Dspw0W+WiSbYA7Ct7kfK8kACsqfYCwpS1Ow ENeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=k30o2vFa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id np4-20020a17090b4c4400b001bd14e01f1asi749576pjb.8.2022.04.04.18.48.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 18:48:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=k30o2vFa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 580DC2A963A; Mon, 4 Apr 2022 17:49:29 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234372AbiDCHjD (ORCPT + 99 others); Sun, 3 Apr 2022 03:39:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233345AbiDCHjB (ORCPT ); Sun, 3 Apr 2022 03:39:01 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5CD23614F for ; Sun, 3 Apr 2022 00:37:07 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id p15so13967686ejc.7 for ; Sun, 03 Apr 2022 00:37:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IGvz+saGqGJ7n4uIJ25zKRG9x6+wAmAWhzfjK7OHNP8=; b=k30o2vFaWNsgROXTs/2LzyBi7ggbM3drImTq01cuYrYB5rBgld+5LH23t38QhCrc88 K/3PK3XDaItwLglQNZmGWhysvvhjstzP6Zj5CVN0kTK/jaID7j0KdGwLuOYZ9Ft4g12D 4T0ZTxi1GuAEt026qDezcadOHAXvjrdBpYNe/e+BZjPQvAGQ786dG2m5ofvUzku2csIp xemxZfXkLUAZx6cHjrZ/loERSWbqshsCKJYihnUC88qdk/iUBA6IGLDJoWjPOLBhlFCt rqkzVLUHyAZ8syuZd1Cp3apuD3fnIxNBo3PGV3iw4xKGdtog6iveGakNDV0OvHff/yEw 8gSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IGvz+saGqGJ7n4uIJ25zKRG9x6+wAmAWhzfjK7OHNP8=; b=EF/eLuXFzCXCCIGCTnYJmPhZnWpHmzt2cpe8mowK+LZjn8RYfC/vVK+zH7NKjFkjW0 RVhbNAHxrmsgPlqGcAypSY8QsD2DPHrrOJF+gjGhTD83PxboOmLe+9gIu/dK0GJaHqj3 h7EFk948mUZw1zQ9B0XhCPQb07ys3/2n6/6Dp6JfOiZeiswUtyPy0ezELeBQsLo03VJE 3s3WGtlKihnW7Zll30UVfOh/T0J5QsxRVHgmNFpQVcs3DlgVdx6MB8bSzbFyBTb+vJau Djtb7j7CYnIV3kPdXGoy4EG2tKoStPNZBJ6aiV1QKukx3IOJGwVPyspGuhvutTTShfS5 r5zg== X-Gm-Message-State: AOAM531LzFVSEkuy/pYw9j1bngxBvDPS5VWlPJ5/4xZNjwOh0J/qpr/d 1TpPSEl2L5MQP3D+WI0s4KXobuM7t3x3jSDF8tU= X-Received: by 2002:a17:906:7314:b0:6df:839f:af7 with SMTP id di20-20020a170906731400b006df839f0af7mr6271448ejc.65.1648971426309; Sun, 03 Apr 2022 00:37:06 -0700 (PDT) MIME-Version: 1.0 References: <20220401164406.61583-1-jeremy.linton@arm.com> In-Reply-To: From: Andrew Pinski Date: Sun, 3 Apr 2022 00:36:53 -0700 Message-ID: Subject: Re: [PATCH] arm64/io: Remind compiler that there is a memory side effect To: Mark Rutland Cc: Jeremy Linton , GCC Mailing List , f.fainelli@gmail.com, maz@kernel.org, marcan@marcan.st, LKML , opendmb@gmail.com, Catalin Marinas , will@kernel.org, linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Fri, Apr 1, 2022 at 10:24 AM Mark Rutland via Gcc wrote: > > Hi Jeremy, > > Thanks for raising this. > > On Fri, Apr 01, 2022 at 11:44:06AM -0500, Jeremy Linton wrote: > > The relaxed variants of read/write macros are only declared > > as `asm volatile()` which forces the compiler to generate the > > instruction in the code path as intended. The only problem > > is that it doesn't also tell the compiler that there may > > be memory side effects. Meaning that if a function is comprised > > entirely of relaxed io operations, the compiler may think that > > it only has register side effects and doesn't need to be called. > > As I mentioned on a private mail, I don't think that reasoning above is > correct, and I think this is a miscompilation (i.e. a compiler bug). > > The important thing is that any `asm volatile` may have a side effects > generally outside of memory or GPRs, and whether the assembly contains a memory > load/store is immaterial. We should not need to add a memory clobber in order > to retain the volatile semantic. > > See: > > https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile > > ... and consider the x86 example that reads rdtsc, or an arm64 sequence like: > > | void do_sysreg_thing(void) > | { > | unsigned long tmp; > | > | tmp = read_sysreg(some_reg); > | tmp |= SOME_BIT; > | write_sysreg(some_reg); > | } > > ... where there's no memory that we should need to hazard against. > > This patch might workaround the issue, but I don't believe it is a correct fix. It might not be the most restricted fix but it is a fix. The best fix is to tell that you are writing to that location of memory. volatile asm does not do what you think it does. You didn't read further down about memory clobbers: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers Specifically this part: The "memory" clobber tells the compiler that the assembly code performs memory reads or writes to items other than those listed in the input and output operands > > > For an example function look at bcmgenet_enable_dma(), before the > > relaxed variants were removed. When built with gcc12 the code > > contains the asm blocks as expected, but then the function is > > never called. > > So it sounds like this is a regression in GCC 12, which IIUC isn't released yet > per: It is NOT a bug in GCC 12. Just you depended on behavior which accidently worked in the cases you were looking at. GCC 12 did not change in this area at all even. Thanks, Andrew Pinski > > https://gcc.gnu.org/gcc-12/changes.html > > ... which says: > > | Note: GCC 12 has not been released yet > > Surely we can fix it prior to release? > > Thanks, > Mark. > > > > > Signed-off-by: Jeremy Linton > > --- > > arch/arm64/include/asm/io.h | 8 ++++---- > > 1 file changed, 4 insertions(+), 4 deletions(-) > > > > diff --git a/arch/arm64/include/asm/io.h b/arch/arm64/include/asm/io.h > > index 7fd836bea7eb..3cceda7948a0 100644 > > --- a/arch/arm64/include/asm/io.h > > +++ b/arch/arm64/include/asm/io.h > > @@ -24,25 +24,25 @@ > > #define __raw_writeb __raw_writeb > > static inline void __raw_writeb(u8 val, volatile void __iomem *addr) > > { > > - asm volatile("strb %w0, [%1]" : : "rZ" (val), "r" (addr)); > > + asm volatile("strb %w0, [%1]" : : "rZ" (val), "r" (addr) : "memory"); > > } > > > > #define __raw_writew __raw_writew > > static inline void __raw_writew(u16 val, volatile void __iomem *addr) > > { > > - asm volatile("strh %w0, [%1]" : : "rZ" (val), "r" (addr)); > > + asm volatile("strh %w0, [%1]" : : "rZ" (val), "r" (addr) : "memory"); > > } > > > > #define __raw_writel __raw_writel > > static __always_inline void __raw_writel(u32 val, volatile void __iomem *addr) > > { > > - asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr)); > > + asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr) : "memory"); > > } > > > > #define __raw_writeq __raw_writeq > > static inline void __raw_writeq(u64 val, volatile void __iomem *addr) > > { > > - asm volatile("str %x0, [%1]" : : "rZ" (val), "r" (addr)); > > + asm volatile("str %x0, [%1]" : : "rZ" (val), "r" (addr) : "memory"); > > } > > > > #define __raw_readb __raw_readb > > -- > > 2.35.1 > >