From patchwork Sat May 2 16:06:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 11523967 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1CB6D139A for ; Sat, 2 May 2020 16:09:41 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ED31324953 for ; Sat, 2 May 2020 16:09:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=xen.org header.i=@xen.org header.b="KUDrWZVv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED31324953 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org 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 X-Inumbo-ID: f8426b2a-8c8e-11ea-b07b-bc764e2007e4 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) 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=; 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 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" 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 +void __bad_atomic_read(const volatile void *p, void *res); void __bad_atomic_size(void); +static always_inline void read_atomic_size(const volatile void *p, + void *res, + unsigned int size) +{ + switch ( size ) + { + case 1: + *(uint8_t *)res = read_u8_atomic(p); + break; + case 2: + *(uint16_t *)res = read_u16_atomic(p); + break; + case 4: + *(uint32_t *)res = read_u32_atomic(p); + break; + case 8: + *(uint64_t *)res = 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 = (typeof(*p))read_u8_atomic((uint8_t *)p); break; \ - case 2: __x = (typeof(*p))read_u16_atomic((uint16_t *)p); break; \ - case 4: __x = (typeof(*p))read_u32_atomic((uint32_t *)p); break; \ - case 8: __x = (typeof(*p))read_u64_atomic((uint64_t *)p); break; \ - default: __x = 0; __bad_atomic_size(); break; \ - } \ - __x; \ + union { typeof(*p) val; char c[0]; } x_; \ + read_atomic_size(p, x_.c, sizeof(*p)); \ + x_.val; \ }) #define write_atomic(p, x) ({ \ From patchwork Sat May 2 16:06:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 11523961 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3076615E6 for ; Sat, 2 May 2020 16:07:45 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0A140206C0 for ; Sat, 2 May 2020 16:07:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=xen.org header.i=@xen.org header.b="tYNu4Jnw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0A140206C0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org 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 X-Inumbo-ID: f8ba485c-8c8e-11ea-9bd3-12813bfff9fa 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) 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=; 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 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" 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 volatile void *p, } } +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; \ }) -#define write_atomic(p, x) ({ \ - typeof(*p) __x = (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_ = (x); \ + write_atomic_size(p, &x_, sizeof(*p)); \ + } while ( false ) #define add_sized(p, x) ({ \ typeof(*(p)) __x = (x); \ From patchwork Sat May 2 16:07:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 11523963 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 145C2139A for ; Sat, 2 May 2020 16:09:15 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E42C824953 for ; Sat, 2 May 2020 16:09:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=xen.org header.i=@xen.org header.b="U0+/ooRn" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E42C824953 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org 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 X-Inumbo-ID: f98e5e1c-8c8e-11ea-9887-bc764e2007e4 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) 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=; 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 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_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" 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); #define write_atomic(p, x) ({ \ typeof(*(p)) __x = (x); \ + /* Check that the pointer is not const */ \ + void *__maybe_unused p_ = &__x; \ unsigned long x_ = (unsigned long)__x; \ switch ( sizeof(*(p)) ) { \ case 1: write_u8_atomic((uint8_t *)(p), x_); break; \