From nobody Sun May 5 17:24:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1588435671; cv=none; d=zohomail.com; s=zohoarc; b=bvFHtrQgYkvFDz1hlu68hj8fl/XpxL9ARDWURTwjKLwIJgIKHVHjpFmkiTC8Mt+nDS16PCNOlJXvvSk10JWki/ndg4NExjfFYcW0XRF1dsPfTKWHkfStQ6SpIpHQZZMjSdyUciEmSRxcWBjmMg+450SZ/TWl2REcXkwNYcMUoSk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588435671; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=EquJ+P1MjKxYVanc5AWfgfmBQn5lz7a3I5D3bo//H6o=; b=Y5ec3MJ1C+0OwfAqXB9IW2vp80eI4yOhPjIMQYwpeyaXdACi4duoh7eL0aC8SH8B0MArOlS1z9gIYbS1rx3F4JCHtPcqG/qSlYaE/4BwfzUxwR4J/z6y2ZVbQNcRGmWIzZP8Vdb/3gCnHlLyT3uX4ThxM7YQPVb/I5WbpmCq3Mc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1588435671590622.2221423035675; Sat, 2 May 2020 09:07:51 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufX-00014w-Sf; Sat, 02 May 2020 16:07:11 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufX-00014h-98 for xen-devel@lists.xenproject.org; Sat, 02 May 2020 16:07:11 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f8426b2a-8c8e-11ea-b07b-bc764e2007e4; Sat, 02 May 2020 16:07:06 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufR-0000pm-Sl; Sat, 02 May 2020 16:07:05 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jUufR-00054b-He; Sat, 02 May 2020 16:07:05 +0000 X-Inumbo-ID: f8426b2a-8c8e-11ea-b07b-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=EquJ+P1MjKxYVanc5AWfgfmBQn5lz7a3I5D3bo//H6o=; b=KUDrWZVvEZWpLImbEeyABfPOQ0 lzI103l846PopeLBujNexEEOSDZloGpuAry1hWahRpExEJ7uIRDMk1+6O9LnGHAVFnAMScNsTos6z Zj6gK1fgZTknM4ls5CqaKS3LnI9V/Jk4WDx42uIh5OxXqD1M+nrRK3Eo7Q8UL5wp6A8Q=; From: Julien Grall To: xen-devel@lists.xenproject.org Subject: [PATCH for-4.14 1/3] xen/arm: atomic: Allow read_atomic() to be used in more cases Date: Sat, 2 May 2020 17:06:58 +0100 Message-Id: <20200502160700.19573-2-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200502160700.19573-1-julien@xen.org> References: <20200502160700.19573-1-julien@xen.org> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Volodymyr Babchuk , Julien Grall , Stefano Stabellini , julien@xen.org Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Julien Grall The current implementation of read_atomic() on Arm will not allow to: 1) Read a value from a pointer to const because the temporary variable will be const and therefore it is not possible to assign any value. This can be solved by using a union between the type and a char[0]. 2) Read a pointer value (e.g void *) because the switch contains cast from other type than the size of a pointer. This can be solved by by introducing a static inline for the switch and use void * for the pointer. Reported-by: Juergen Gross Signed-off-by: Julien Grall Reviewed-by: Stefano Stabellini --- xen/include/asm-arm/atomic.h | 37 +++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/xen/include/asm-arm/atomic.h b/xen/include/asm-arm/atomic.h index e81bf80e305c..3c3d6bb04ee8 100644 --- a/xen/include/asm-arm/atomic.h +++ b/xen/include/asm-arm/atomic.h @@ -71,18 +71,37 @@ build_add_sized(add_u32_sized, "", WORD, uint32_t) #undef build_atomic_write #undef build_add_sized =20 +void __bad_atomic_read(const volatile void *p, void *res); void __bad_atomic_size(void); =20 +static always_inline void read_atomic_size(const volatile void *p, + void *res, + unsigned int size) +{ + switch ( size ) + { + case 1: + *(uint8_t *)res =3D read_u8_atomic(p); + break; + case 2: + *(uint16_t *)res =3D read_u16_atomic(p); + break; + case 4: + *(uint32_t *)res =3D read_u32_atomic(p); + break; + case 8: + *(uint64_t *)res =3D read_u64_atomic(p); + break; + default: + __bad_atomic_read(p, res); + break; + } +} + #define read_atomic(p) ({ \ - typeof(*p) __x; \ - switch ( sizeof(*p) ) { \ - case 1: __x =3D (typeof(*p))read_u8_atomic((uint8_t *)p); break; \ - case 2: __x =3D (typeof(*p))read_u16_atomic((uint16_t *)p); break; \ - case 4: __x =3D (typeof(*p))read_u32_atomic((uint32_t *)p); break; \ - case 8: __x =3D (typeof(*p))read_u64_atomic((uint64_t *)p); break; \ - default: __x =3D 0; __bad_atomic_size(); break; \ - } \ - __x; \ + union { typeof(*p) val; char c[0]; } x_; \ + read_atomic_size(p, x_.c, sizeof(*p)); \ + x_.val; \ }) =20 #define write_atomic(p, x) ({ \ --=20 2.17.1 From nobody Sun May 5 17:24:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1588435660; cv=none; d=zohomail.com; s=zohoarc; b=I6qy3m+BuKQAlowmEyY3V8NfZnLq8JXuyBODJkp6M9ryhtOM32drdvpxiB/8OTr4+C/UpqmWy8DUTs4izc0Vi2MtrKBQaEH4DrOYr5qY7i8Tx1/42SgPXUrZR44yd/gQnOs3wbATPjYPc3LUTgXwKWhUKYmji0okGMMD0frCch0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588435660; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=0DZRtHN+DWgkT9wV9AdwEWaM8yO76gVkuFpYC4Ito8Y=; b=HjWS2Sx2Veaj3CbtPNEYZgfRI7ti/thgJtjyBzrLno7VK3lw4CBxV6dBb+QjzJcRLbQOsIgOOKksjIz6r5cmKvdzhLk+H5wpDHx4JUwhkjgs3t6ediHGfqMV26yhzbAZa5IXklbY3woo/zj1EjkM6bDO4ECdwCMayTYFfjEnQm8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1588435660752906.3243557896214; Sat, 2 May 2020 09:07:40 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufW-00014b-HS; Sat, 02 May 2020 16:07:10 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufV-00014W-G9 for xen-devel@lists.xenproject.org; Sat, 02 May 2020 16:07:09 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f8ba485c-8c8e-11ea-9bd3-12813bfff9fa; Sat, 02 May 2020 16:07:07 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufS-0000ps-Uj; Sat, 02 May 2020 16:07:06 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jUufS-00054b-It; Sat, 02 May 2020 16:07:06 +0000 X-Inumbo-ID: f8ba485c-8c8e-11ea-9bd3-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=0DZRtHN+DWgkT9wV9AdwEWaM8yO76gVkuFpYC4Ito8Y=; b=tYNu4Jnwx4FrjVSD3Qr29oadK7 zspxZr/aznHXD7brDyeV1U1bN7GibM9gR+VNOxezJHMgg+0riMC9QUdXDQVkBLOg56dcbEnzDu1FI 5M/Ta6wUw+zW0KzzueJuWEnaRqA4/T+pFJT2YbKhtoQcdn3yjuTyY9Q/+zYLvKLCqTtc=; From: Julien Grall To: xen-devel@lists.xenproject.org Subject: [PATCH for-4.14 2/3] xen/arm: atomic: Rewrite write_atomic() Date: Sat, 2 May 2020 17:06:59 +0100 Message-Id: <20200502160700.19573-3-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200502160700.19573-1-julien@xen.org> References: <20200502160700.19573-1-julien@xen.org> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Volodymyr Babchuk , Julien Grall , Stefano Stabellini , julien@xen.org Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Julien Grall The current implementation of write_atomic has two issues: 1) It cannot be used to write pointer value because the switch contains cast to other size than the size of the pointer. 2) It will happily allow to write to a pointer to const. Additionally, the Arm implementation is returning a value when the x86 implementation does not anymore. This was introduced in commit 2934148a0773 "x86: simplify a few macros / inline functions". There are no users of the return value, so it is fine to drop it. The switch is now moved in a static inline helper allowing the compiler to prevent use of const pointer and also allow to write pointer value. Signed-off-by: Julien Grall Reviewed-by: Stefano Stabellini --- xen/include/asm-arm/atomic.h | 40 ++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/xen/include/asm-arm/atomic.h b/xen/include/asm-arm/atomic.h index 3c3d6bb04ee8..ac2798d095eb 100644 --- a/xen/include/asm-arm/atomic.h +++ b/xen/include/asm-arm/atomic.h @@ -98,23 +98,41 @@ static always_inline void read_atomic_size(const volati= le void *p, } } =20 +static always_inline void write_atomic_size(volatile void *p, + void *val, + unsigned int size) +{ + switch ( size ) + { + case 1: + write_u8_atomic(p, *(uint8_t *)val); + break; + case 2: + write_u16_atomic(p, *(uint16_t *)val); + break; + case 4: + write_u32_atomic(p, *(uint32_t *)val); + break; + case 8: + write_u64_atomic(p, *(uint64_t *)val); + break; + default: + __bad_atomic_size(); + break; + } +} + #define read_atomic(p) ({ \ union { typeof(*p) val; char c[0]; } x_; \ read_atomic_size(p, x_.c, sizeof(*p)); \ x_.val; \ }) =20 -#define write_atomic(p, x) ({ \ - typeof(*p) __x =3D (x); \ - switch ( sizeof(*p) ) { \ - case 1: write_u8_atomic((uint8_t *)p, (uint8_t)__x); break; \ - case 2: write_u16_atomic((uint16_t *)p, (uint16_t)__x); break; \ - case 4: write_u32_atomic((uint32_t *)p, (uint32_t)__x); break; \ - case 8: write_u64_atomic((uint64_t *)p, (uint64_t)__x); break; \ - default: __bad_atomic_size(); break; \ - } \ - __x; \ -}) +#define write_atomic(p, x) \ + do { \ + typeof(*p) x_ =3D (x); \ + write_atomic_size(p, &x_, sizeof(*p)); \ + } while ( false ) =20 #define add_sized(p, x) ({ \ typeof(*(p)) __x =3D (x); \ --=20 2.17.1 From nobody Sun May 5 17:24:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1588435662; cv=none; d=zohomail.com; s=zohoarc; b=R4zOmr8+a753IF7TE8Pp3gayt1UlEMH9WWgJdr7R5EsGNxgNRzNL6/N2xWk2fsxi28kZEu7DdqY5rgKPrZ/gx2lZQaVWueAS/wy/v74SgNNUJdE0BmJqvUG6YYNiE/EGAhyWcyojB11g+vqcIqqTFnWdwc1yiY/Z6IGnMCtwcXI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588435662; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=wb54UEHsoeNGMmUecx4QgE7grxFabOL6fqWlr9nKSEQ=; b=XZlgdxZ9B/HWZ0lgTkkFydso++B3rV1bOG4+ZT/x812GIGMb5seqL6GyMbXHEL3dO24rUM/HmGkA1x/6fngMeWeWJwNXtVMHFeDuf2VCdbVs9istXzXLgFqBIv9YSJskzeJ4IByOVfZLXIJf0RnnS9p3tD+pVz9IJRSGz1M14S4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1588435662519842.4684172762901; Sat, 2 May 2020 09:07:42 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufd-00015b-5d; Sat, 02 May 2020 16:07:17 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufc-00015U-9s for xen-devel@lists.xenproject.org; Sat, 02 May 2020 16:07:16 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f98e5e1c-8c8e-11ea-9887-bc764e2007e4; Sat, 02 May 2020 16:07:08 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jUufU-0000q3-7W; Sat, 02 May 2020 16:07:08 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jUufT-00054b-RM; Sat, 02 May 2020 16:07:08 +0000 X-Inumbo-ID: f98e5e1c-8c8e-11ea-9887-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=wb54UEHsoeNGMmUecx4QgE7grxFabOL6fqWlr9nKSEQ=; b=U0+/ooRnDk2I96yc6AxomlNAcq ypC8j6cRb+EpsMAARN672EU5IsqoQ/lbfmhIdfsPtkYv+sXKZXdgJsdBVyr8HKZEMW/u/HaAJ5+El ADU9zLAg1VPcKOJiTeqmfHL8oAV7H2yiElJ8kKuul8GiPtaDMVOtmN3SSmq+s8oRy9UI=; From: Julien Grall To: xen-devel@lists.xenproject.org Subject: [PATCH for-4.14 3/3] xen/x86: atomic: Don't allow to write atomically in a pointer to const Date: Sat, 2 May 2020 17:07:00 +0100 Message-Id: <20200502160700.19573-4-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200502160700.19573-1-julien@xen.org> References: <20200502160700.19573-1-julien@xen.org> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: julien@xen.org, Wei Liu , Andrew Cooper , Julien Grall , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Julien Grall At the moment, write_atomic() will happily write to a pointer to const. While there are no use in Xen, it would be best to catch them at compilation time. Signed-off-by: Julien Grall Acked-by: Jan Beulich --- xen/include/asm-x86/atomic.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xen/include/asm-x86/atomic.h b/xen/include/asm-x86/atomic.h index 6b40f9c9f872..0a332b1fae18 100644 --- a/xen/include/asm-x86/atomic.h +++ b/xen/include/asm-x86/atomic.h @@ -63,6 +63,8 @@ void __bad_atomic_size(void); =20 #define write_atomic(p, x) ({ \ typeof(*(p)) __x =3D (x); \ + /* Check that the pointer is not const */ \ + void *__maybe_unused p_ =3D &__x; \ unsigned long x_ =3D (unsigned long)__x; \ switch ( sizeof(*(p)) ) { \ case 1: write_u8_atomic((uint8_t *)(p), x_); break; \ --=20 2.17.1