From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 208624A84 for ; Thu, 17 Mar 2022 20:13:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647547995; 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=9qNtFrtkLvPTt+C4lJhyxR9PYkHaXFWorb7+v2VNB60=; b=JzySKq2FdrksAyB+x7CLUeROTxrdm8R5nxhX1CfsQzyLMzqwE/Ir0FQ2KHQ5bpqL3tneWz iOUW2LEtAPmH6kX5DwYT64dZQAED9XyZos/VtD8+16yfjSemoV+A7GD/Fd33WliyjWribu 2F8gXsGpIi2q2IxvlFr+bJcnioZ0vgE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-199-BvINtsxcOa6wZL1C7IXwhw-1; Thu, 17 Mar 2022 16:13:11 -0400 X-MC-Unique: BvINtsxcOa6wZL1C7IXwhw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E35EE296A613; Thu, 17 Mar 2022 20:13:10 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.88]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0662D401E75; Thu, 17 Mar 2022 20:13:06 +0000 (UTC) From: Florian Weimer To: Linus Torvalds Cc: Nick Desaulniers , "H. Peter Anvin" , Bill Wendling , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Nathan Chancellor , Juergen Gross , Peter Zijlstra , Andy Lutomirski , llvm@lists.linux.dev, LKML , linux-toolchains Subject: Re: [PATCH v5] x86: use builtins to read eflags References: <20220210223134.233757-1-morbo@google.com> <20220301201903.4113977-1-morbo@google.com> Date: Thu, 17 Mar 2022 21:13:05 +0100 In-Reply-To: (Linus Torvalds's message of "Thu, 17 Mar 2022 11:52:26 -0700") Message-ID: <878rt8gwxa.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 * Linus Torvalds: > You can actually operate on EFLAGS at multiple granularities. > > - normal pushf/popf. Don't do it unless you are doing system software. There's one exception: PUSHF/twiddle/POPF/PUSHF/compare is the recommended sequence to detect CPUID support on i386 (in userspace and elsewhere). > - you can use lahf/sahc to load/store only the arithmetic flags > into/from AH. Deprecated, and going away, but historically supported. And these instructions were missing from the original long mode, but they were added back. > Yes, yes, that complete mental breakdown with pushf/popf did get > fixed, but it really makes me very wary of thinking that we should > ever use a built-in that compiler writers really fundamentally got so > wrong before. > > What would make me think that you'd get it right now? In user space, > you'll basically never actually see the whole system flags issues, so > your test-cases would never work or be very contrieved. You'd have to > really work at it to see the problems. I think as the result of the nature of that kind of bug it does not matter whether you use a compiler builtin to access the flags (to put their combined value into a general-purpose register). GCC doesn't have barriers in the built-ins (if we are talking about __builtin_ia32_readeflags_u64 and __builtin_ia32_writeeflags_u64). I expect they are actually pretty useless, and were merely added for completeness of the intrinsics headers. It's not that you can write unsigned a, b, c; // =E2=80=A6 c =3D a + b; and examine __builtin_ia32_readeflags_u64() to see if there was an overflow. Neither GCC nor Clang model the EFLAGS register and arithmetic expression side effects to make this possible. Thanks, Florian