Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id B7416C001E0 for ; Mon, 23 Oct 2023 15:24:12 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.621547.968118 (Exim 4.92) (envelope-from ) id 1quwmV-0007MU-6u; Mon, 23 Oct 2023 15:23:51 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 621547.968118; Mon, 23 Oct 2023 15:23:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1quwmV-0007MN-2Q; Mon, 23 Oct 2023 15:23:51 +0000 Received: by outflank-mailman (input) for mailman id 621547; Mon, 23 Oct 2023 15:23:50 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1quwmU-0007MD-Cg for xen-devel@lists.xenproject.org; Mon, 23 Oct 2023 15:23:50 +0000 Received: from support.bugseng.com (mail.bugseng.com [162.55.131.47]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 28d5b1d3-71b8-11ee-9b0e-b553b5be7939; Mon, 23 Oct 2023 17:23:47 +0200 (CEST) Received: from [192.168.1.9] (net-188-218-250-245.cust.vodafonedsl.it [188.218.250.245]) by support.bugseng.com (Postfix) with ESMTPSA id 3825F4EE0747; Mon, 23 Oct 2023 17:23:46 +0200 (CEST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 28d5b1d3-71b8-11ee-9b0e-b553b5be7939 Message-ID: <0032186f-80c7-4dba-b46e-10d4a8e2a8cb@bugseng.com> Date: Mon, 23 Oct 2023 17:23:45 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [XEN PATCH 3/4] xen/include: add pure and const attributes Content-Language: en-US To: Jan Beulich Cc: consulting@bugseng.com, sstabellini@kernel.org, Andrew Cooper , George Dunlap , Julien Grall , Wei Liu , xen-devel@lists.xenproject.org References: <89778285-5cba-8fb5-70bc-710b6dd30a10@suse.com> From: Simone Ballarin Organization: BUGSENG In-Reply-To: <89778285-5cba-8fb5-70bc-710b6dd30a10@suse.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 23/10/23 15:34, Jan Beulich wrote: > On 18.10.2023 16:18, Simone Ballarin wrote: >> Add const and pure attributes to address reports >> of MISRA C:2012 Rule 13.1: Initializer lists shall >> not contain persistent side effects >> >> Add pure attribute to function pdx_to_pfn. >> Add const attribute to functions generated by TYPE_SAFE. >> >> These functions are used in initializer lists: adding >> the attributes ensures that no effect will be performed >> by them. > > Adding the attribute does, according to my understanding, ensure nothing > The compiler may (but isn't required to) diagnose wrong uses of the > attributes, but it may also make use of the attributes (on the > declaration) without regard to the attribute potentially being wrongly > applied. Since further for inline functions the compiler commonly infers > attributes from the expanded code (discarding the attribute), the only > thing achieved here is a documentation aspect, I think. Yes, you're right. I will rephrase the commit message. > >> --- a/xen/include/xen/pdx.h >> +++ b/xen/include/xen/pdx.h >> @@ -164,7 +164,7 @@ static inline unsigned long pfn_to_pdx(unsigned long pfn) >> * @param pdx Page index >> * @return Obtained pfn after decompressing the pdx >> */ >> -static inline unsigned long pdx_to_pfn(unsigned long pdx) >> +static inline __attribute_pure__ unsigned long pdx_to_pfn(unsigned long pdx) >> { >> return (pdx & pfn_pdx_bottom_mask) | >> ((pdx << pfn_pdx_hole_shift) & pfn_top_mask); > > Taking this as an example for what I've said above: The compiler can't > know that the globals used by the functions won't change value. Even > within Xen it is only by convention that these variables are assigned > their values during boot, and then aren't changed anymore. Which makes > me wonder: Did you check carefully that around the time the variables > have their values established, no calls to the functions exist (which > might then be subject to folding)? There is no need to check that, the GCC documentation explicitly says: However, functions declared with the pure attribute *can safely read any non-volatile objects*, and modify the value of objects in a way that does not affect their return value or the observable state of the program. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html > Additionally - what about the sibling function pfn_to_pdx()? > > Jan I will add the attribute also to the sibling. -- Simone Ballarin, M.Sc. Field Application Engineer, BUGSENG (https://bugseng.com)