Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp659316rdb; Thu, 19 Oct 2023 15:39:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFh3Hm9dvl6WTfgPueNAApwvxkqg0Wgl14kXJPPdrHlmBnBowluiebi5XMJyX/k7AtBWGuZ X-Received: by 2002:a05:6870:b682:b0:1d5:a980:9caa with SMTP id cy2-20020a056870b68200b001d5a9809caamr331956oab.29.1697755196106; Thu, 19 Oct 2023 15:39:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697755196; cv=none; d=google.com; s=arc-20160816; b=ZZz1+lt0SmQOMeq51htsnZglrH6dXsKDU14b9OPitlZNds4ZkIrkNQI9zBF+LqtJb1 yL5mI8nW4iOlCv1ZbhGk0AbztDbJQinAoO9tyWUNW5k/86juKhnjSY7Z8C8x8JDGEvYH 2d2fm7UVbUXA67i48Cb5JuNNmWHfcQo4WsYLS9wRO57coUBN4w3zxbdl21Mdvo7Zcqaf leloT7hc/yo2gJQdAQkciYydFtKIWq4HCAqTwIPANYCIsdVSG//LwU+A9e8KuS44GC+R WX3pJ2E/aLV7JmcypixlEnBUlrcJ8ypSSQzSg/saD5gmOe61DWis95bb0thQ4FZZjqIg qCbg== 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=Bf4UVfm9ejOuj/+1if2blVbFZkZx0qYyb9UZRpdYTtM=; fh=l1jjhj9N74C+OUMGFFZgEIqzo5fBEvmOeqFfpgIhBwI=; b=MZU3teEkNlO9WJ6hIN8t3JtTQDSar6gUhaXhk5J1s8+8cmzwqFhOJfu/q9BFVB1laI gNp0COrsvMD0+2ZhHqQ46ERZLnLeq1abACakpfLykZopg73Fl/aV5WeShNwiU7eHxAvh OKJ5U3DRF3EtiTb9IAORM9MRLfbvyBHr79RI5LQDJyJJ1mzk629SO9LgavwEJn/NL8+U 6JuL0lJ76yDfTiFSHnsmwT3n2LirSqbWE1MHgEO+4uXu+non45x2m0CNdrT8/Rgxupl0 G+9nO0gqn0QkPI1TxOoP/O7ETyW4mqD5W20SnvrBnVgg+R2pKXrkHGro4klxHjNu9/58 hFxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=eGyoMnQJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id h7-20020a655187000000b00565ec2a1e2asi477835pgq.760.2023.10.19.15.39.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 15:39:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=eGyoMnQJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 5EBE882CFA6D; Thu, 19 Oct 2023 15:39:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233286AbjJSWjh (ORCPT + 99 others); Thu, 19 Oct 2023 18:39:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235549AbjJSWjg (ORCPT ); Thu, 19 Oct 2023 18:39:36 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2ADB115 for ; Thu, 19 Oct 2023 15:39:33 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-5046bf37ec1so196633e87.1 for ; Thu, 19 Oct 2023 15:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1697755172; x=1698359972; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Bf4UVfm9ejOuj/+1if2blVbFZkZx0qYyb9UZRpdYTtM=; b=eGyoMnQJ+ZzjkezGDNdPHNuXnOo6/ZO6a/BEKlN1j8fEaE5sx8Fi9oRP7ViDovgY+W iVIZumuj/ZSoa2gdaYBEjKpmTEBnZsUZsVONXYlWvvudyly9BrQKC9gWIIF3oHndUJVM o9Z8N6l0GtkFEScaTwmbVc2MNNEUQBf74fHMg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697755172; x=1698359972; h=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=Bf4UVfm9ejOuj/+1if2blVbFZkZx0qYyb9UZRpdYTtM=; b=bJ29A1Z1BuJ12fJZJ4q1e8Ri+f5WaMxoMJgZet9ZwIIfyv/ZiCztrECmdwBegjsz6Z F/tLFBAW43hHgzMFy/xs6rNP7DONcFrkvPz+b6So3lNXG6XFrPPUpwOYZJ1sTWLFfQTQ xiJxKKaciNQOIPON9h40/ILxCjDqVWCOLxRu66QOOzdYZsgoHjudUnDN6JrPTyMnpxYi pdJOay459tSpa7VOycmurs1UEEpbdnCn+GJdftFcaGsLaXn0KY2LA5oKc1OB7RZphSIO a/hJaG8YyFbHfrXRpX/qs1sAFqyQCgFIvzpf5SteSIMrJgmxUzOMizyPRVLgbmmDDFsA m+Cw== X-Gm-Message-State: AOJu0YztH4nV6SoPBeA75DnfSunVqjNA0P8fl1KzThWs5lbp9mkwnGKj Y6AXnc2adFwByUKyZzsuO3eLe89dqJgGy5hmHqHx9kOG X-Received: by 2002:a19:e05e:0:b0:507:b15b:8b99 with SMTP id g30-20020a19e05e000000b00507b15b8b99mr45225lfj.60.1697755171740; Thu, 19 Oct 2023 15:39:31 -0700 (PDT) Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com. [209.85.208.51]) by smtp.gmail.com with ESMTPSA id t14-20020aa7d70e000000b0053e4783afbasm312747edq.63.2023.10.19.15.39.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Oct 2023 15:39:30 -0700 (PDT) Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-53de8fc1ad8so212413a12.0 for ; Thu, 19 Oct 2023 15:39:30 -0700 (PDT) X-Received: by 2002:a17:907:3d9f:b0:9b2:b152:b0f2 with SMTP id he31-20020a1709073d9f00b009b2b152b0f2mr38956ejc.10.1697755170439; Thu, 19 Oct 2023 15:39:30 -0700 (PDT) MIME-Version: 1.0 References: <20231010164234.140750-1-ubizjak@gmail.com> <0617BB2F-D08F-410F-A6EE-4135BB03863C@vmware.com> <7D77A452-E61E-4B8B-B49C-949E1C8E257C@vmware.com> <9F926586-20D9-4979-AB7A-71124BBAABD3@vmware.com> <3F9D776E-AD7E-4814-9E3C-508550AD9287@vmware.com> <28B9471C-4FB0-4AB0-81DD-4885C3645E95@vmware.com> In-Reply-To: From: Linus Torvalds Date: Thu, 19 Oct 2023 15:39:12 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr() To: Uros Bizjak Cc: peterz@infradead.org, Nadav Amit , "the arch/x86 maintainers" , Linux Kernel Mailing List , Andy Lutomirski , Brian Gerst , Denys Vlasenko , "H . Peter Anvin" , Thomas Gleixner , Josh Poimboeuf , Nick Desaulniers Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 19 Oct 2023 15:39:50 -0700 (PDT) Unrelated question to the gcc people (well, related in the way that this discussion made me *test* this). Lookie here: int test(void) { unsigned int sum = 0; for (int i = 0; i < 4; i++) { unsigned int val; #if ONE asm("magic1 %0":"=r" (val): :"memory"); #else asm volatile("magic2 %0":"=r" (val)); #endif sum += val; } return sum; } and now build this with gcc -O2 -S -DONE -funroll-all-loops t.c and I get a *completely* nonsensical end result. What gcc generates is literally insane. What I *expected* to happen was that the two cases (with "-DONE" and without) would generate the same code, since one has a "asm volatile", and the other has a memory clobber. IOW, neither really should be something that can be combined. But no. The '-DONE" version is completely crazy with my gcc-13.2.1 setup. First off, it does actually CSE all the asm's despite the memory clobber. Which I find quite debatable, but whatever. But not only does it CSE them, it then does *not* just multiply the result by four. No. It generates this insanity: magic1 %eax movl %eax, %edx addl %eax, %eax addl %edx, %eax addl %edx, %eax ret so it has apparently done the CSE _after_ the other optimizations. Very strange. Honestly, the CSE part looks like an obvious bug to me. The gcc documentation states: 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 example, accessing the memory pointed to by one of the input parameters). so CSE'ing any inline asm with a memory clobber sounds *very* dubious. The asm literally told the compiler that it has side effects in unrelated memory locations! I don't think we actually care in the kernel (and yes, I think it would always be safer to use "asm volatile" if there's some unrelated memory locations that change), but since I was testing this and was surprised, and since the obvious reading of the documented behavior of a memory clobber really does scream "you can't combine those asms", I thought I'd mention this. Also, *without* the memory clobber, gcc obviously still does CSE the asm, but also, gcc ends up doing just magic1 %eax sall $2, %eax ret so the memory clobber clearly does actually make a difference. Just not a _sane_ one. In testing, clang does *not* have this apparently buggy behavior (but clang annoyingly actually checks the instruction mnemonics, so I had to change "magic" into "strl" instead to make clang happy). Hmm? Linus