From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) (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 97F63522C for ; Tue, 10 Jan 2023 12:46:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354812; x=1704890812; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=eslOY4/RyP8FxHyeCRrE19SI3cJMNIdhnh8JoA4TuBfkkrW3r2p0cCt2 rheIeTPHgYkQ0JHzek9qwqMWg1qcTnSnT0sExuaC2nbKqNnCi+xainCjS C9lzt8MTxD97249Oz4d8SXSGGt+l1u6tL/+MZ4n/cmPc8liyOQOG1f7sP 67NytSLJSJycEjm84crog0C2ual0d23YCtEy6xgVMPpTfAis8VdFISzxC Cib23aMN2gZqrBIjq+1rY12YLlXoPndpLXvXQtnn739OcK0v9AyYAraqM zcTAh/xH0S7Vxee7cuMkO1KQ7jSykT3AsnaUZJKUF+XlPhTPwrzkmL2wt Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="302834337" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="302834337" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="689393555" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="689393555" Received: from ahajda-mobl.ger.corp.intel.com (HELO [10.213.8.19]) ([10.213.8.19]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:39 -0800 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> Date: Tue, 10 Jan 2023 13:46:37 +0100 Precedence: bulk X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.6.1 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Content-Language: en-US To: Andy Shevchenko Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-mips@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, Boqun Feng , linux-xtensa@linux-xtensa.org, Arnd Bergmann , intel-gfx@lists.freedesktop.org, linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org References: <20230110105306.3973122-1-andrzej.hajda@intel.com> From: Andrzej Hajda Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 7F651C46467 for ; Tue, 10 Jan 2023 12:47:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=n05kAmn/8mXq1f3C/YIJYS/WIewJj9uTs5BF3oVPpn0=; b=jY9FzD6Ti5PArN /4I9/tRSR663jD7ZW1JySG7cl1HkFW/4PQhSSWXJSHK1vIrd9VCDNlRwP2SQIkabVxJ9usPoYr8sW GFHL+M+06yLFmnHDb3iLAzXwMql2wSShcJsVp7cj3xYcsnq7GsfG599kVpHqVDjutVDuskc4K3ixF phQ8fBqf0vnllrzZaW2GNWWDdjPcl5Tpvn8fBBxcwdPgoXe4ESChHZjXi3jFN271331uX9b7pKZjr QqnyGo6Ak2arQAsD+x8copY6p6YPDaNJN5ZV4Dgg6XQnrvgO+o+XkybLWb2YDcnibL7e1OT88mWqm ZBYBmqWLhE0aInUfKGmA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pFE1z-006vj7-Su; Tue, 10 Jan 2023 12:47:07 +0000 Received: from mga12.intel.com ([192.55.52.136]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pFE1o-006vet-3Q; Tue, 10 Jan 2023 12:46:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354816; x=1704890816; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=AF/LrIZHIjUrlEw05KEt5x3jhyEPffI6eAIkh+dvqgZbQ+tYXorF4F0l IGA4HFqP6hzls+8naTwSlQX/zcMtAVc7s2ThtIslH+qPHpI3Z/aQ7ERTP vdpLrC2CymAUlgEnt+efhhiCdFGbDc8VQayK0FYuPEQpRRFf51scNL7K1 vJr1lAFy8ZxmE8d24+8VASgVQux2owin41g1ALHyKOSMZYfqAi0pf++px OPX1h0Vg8PImDJV+JLHZWZaMIOcVpi38ouRfivcBNfzC7ZiUveISxB7Yu nXkIdc7ssoVI68oH9ykL81i5yvxKvNQj/9CHtlfGb6M+kXZ5GFyHBnkFi w==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="302834343" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="302834343" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="689393555" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="689393555" Received: from ahajda-mobl.ger.corp.intel.com (HELO [10.213.8.19]) ([10.213.8.19]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:39 -0800 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> Date: Tue, 10 Jan 2023 13:46:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.6.1 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Content-Language: en-US To: Andy Shevchenko Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-mips@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, Boqun Feng , linux-xtensa@linux-xtensa.org, Arnd Bergmann , intel-gfx@lists.freedesktop.org, linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org References: <20230110105306.3973122-1-andrzej.hajda@intel.com> From: Andrzej Hajda Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230110_044656_236527_8C230259 X-CRM114-Status: GOOD ( 22.57 ) X-BeenThere: linux-snps-arc@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux on Synopsys ARC Processors List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-snps-arc" Errors-To: linux-snps-arc-bounces+linux-snps-arc=archiver.kernel.org@lists.infradead.org On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej _______________________________________________ linux-snps-arc mailing list linux-snps-arc@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-snps-arc From mboxrd@z Thu Jan 1 00:00:00 1970 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 EF17DC54EBE for ; Tue, 10 Jan 2023 12:46:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 471C610E5B3; Tue, 10 Jan 2023 12:46:55 +0000 (UTC) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8BAC110E108; Tue, 10 Jan 2023 12:46:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354812; x=1704890812; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=eslOY4/RyP8FxHyeCRrE19SI3cJMNIdhnh8JoA4TuBfkkrW3r2p0cCt2 rheIeTPHgYkQ0JHzek9qwqMWg1qcTnSnT0sExuaC2nbKqNnCi+xainCjS C9lzt8MTxD97249Oz4d8SXSGGt+l1u6tL/+MZ4n/cmPc8liyOQOG1f7sP 67NytSLJSJycEjm84crog0C2ual0d23YCtEy6xgVMPpTfAis8VdFISzxC Cib23aMN2gZqrBIjq+1rY12YLlXoPndpLXvXQtnn739OcK0v9AyYAraqM zcTAh/xH0S7Vxee7cuMkO1KQ7jSykT3AsnaUZJKUF+XlPhTPwrzkmL2wt Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="302834333" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="302834333" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="689393555" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="689393555" Received: from ahajda-mobl.ger.corp.intel.com (HELO [10.213.8.19]) ([10.213.8.19]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:39 -0800 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> Date: Tue, 10 Jan 2023 13:46:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.6.1 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Content-Language: en-US To: Andy Shevchenko References: <20230110105306.3973122-1-andrzej.hajda@intel.com> From: Andrzej Hajda Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, intel-gfx@lists.freedesktop.org, linux-xtensa@linux-xtensa.org, Arnd Bergmann , Boqun Feng , linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-mips@vger.kernel.org, linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej From mboxrd@z Thu Jan 1 00:00:00 1970 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.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 97BC4C54EBC for ; Tue, 10 Jan 2023 12:48:01 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4NrrFW5SK3z3cgy for ; Tue, 10 Jan 2023 23:47:59 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=YmGLr+1/; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=intel.com (client-ip=192.55.52.136; helo=mga12.intel.com; envelope-from=andrzej.hajda@intel.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=YmGLr+1/; dkim-atps=neutral Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4NrrDQ6MlVz3c73 for ; Tue, 10 Jan 2023 23:46:56 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354823; x=1704890823; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=YmGLr+1/3fR3vFGtTdd1An6wts2zqokvSXmx5W5OU40sRdjfeAx/r7Uq DyyeBxupOtF9MyAIeIFAKkoSMl5tijxTr71MEypUZ5iEBdiUKE0VCnDwI P8qfEH5rWVRQN7vGUo6Rb27yrOu6hIi0bY0+JEeP7/xQ+DL72IeH3dEBo aE/WCUoxdbo4MrZ4tCIQW/8Ci/xi5owYmk32stDtlyprGwilQOiyUtvbl 6mjNh4rJZT61f2n/v8/ltmkwTJ25Mv3lVwYbuPMNgvGfet/iaXcmLVKOr SeBhtNJhT9hm2WKcGLYAQ34Ckw9+i+icz/jxsJ49tlP5huDhWcHdOBXhj Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="302834346" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="302834346" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="689393555" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="689393555" Received: from ahajda-mobl.ger.corp.intel.com (HELO [10.213.8.19]) ([10.213.8.19]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:39 -0800 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> Date: Tue, 10 Jan 2023 13:46:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.6.1 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Content-Language: en-US To: Andy Shevchenko References: <20230110105306.3973122-1-andrzej.hajda@intel.com> From: Andrzej Hajda Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, intel-gfx@lists.freedesktop.org, linux-xtensa@linux-xtensa.org, Arnd Bergmann , Boqun Feng , linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-mips@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 E83D5C46467 for ; Tue, 10 Jan 2023 12:47:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=2UittMjfcXLOhHemROBOGkpxDnPFCYMV0lxgGz5JfMw=; b=oIIL77BRXAl0Nz Rwle/Ys9MfDDg7W3plbjSUT9ioNrVlHCWZ++3zt3Pry7bAevCdOv0wR71UZMKeE2Z3o5vhC8rE2O4 xsLtUc/VVmqHmxtOOsBnYJL6FnOWQOHKFlzMbowLXhngYzTk67BdLGsRP4Nr7zvqCI9bturNYGI4U VfuFvYKSPPP47YBQi06Sy2PYxDLAvdsTHV685xl0mu9++q5EHYdURkcJuy79GfxAO3/15hJ3pY5zi skzCvpAoY0bFx16FpHxMJw5Xws5dSryUScfBPOtUul5RsWnBiOIONaHVC3netSGiev92i2dALZhFv tSYWDHulE1fZHvm3XVYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pFE20-006vjE-2l; Tue, 10 Jan 2023 12:47:08 +0000 Received: from mga12.intel.com ([192.55.52.136]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pFE1o-006vet-3Q; Tue, 10 Jan 2023 12:46:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354816; x=1704890816; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=AF/LrIZHIjUrlEw05KEt5x3jhyEPffI6eAIkh+dvqgZbQ+tYXorF4F0l IGA4HFqP6hzls+8naTwSlQX/zcMtAVc7s2ThtIslH+qPHpI3Z/aQ7ERTP vdpLrC2CymAUlgEnt+efhhiCdFGbDc8VQayK0FYuPEQpRRFf51scNL7K1 vJr1lAFy8ZxmE8d24+8VASgVQux2owin41g1ALHyKOSMZYfqAi0pf++px OPX1h0Vg8PImDJV+JLHZWZaMIOcVpi38ouRfivcBNfzC7ZiUveISxB7Yu nXkIdc7ssoVI68oH9ykL81i5yvxKvNQj/9CHtlfGb6M+kXZ5GFyHBnkFi w==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="302834343" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="302834343" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="689393555" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="689393555" Received: from ahajda-mobl.ger.corp.intel.com (HELO [10.213.8.19]) ([10.213.8.19]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:39 -0800 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> Date: Tue, 10 Jan 2023 13:46:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.6.1 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Content-Language: en-US To: Andy Shevchenko Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-mips@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, Boqun Feng , linux-xtensa@linux-xtensa.org, Arnd Bergmann , intel-gfx@lists.freedesktop.org, linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org References: <20230110105306.3973122-1-andrzej.hajda@intel.com> From: Andrzej Hajda Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230110_044656_236527_8C230259 X-CRM114-Status: GOOD ( 22.57 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 90FA0C46467 for ; Tue, 10 Jan 2023 12:48:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=FGYLZE/FqjUZLVmTSYRxDP7VyEE93JNj5YXoAdgULkc=; b=2WPYMxPbEiLszF dh8uBaZdLcnboMEFfXSh6d4pVj0oqtAWz3lSrocEMxkO7RcHDiGDZ8DmZd+3/dmf85Xg678aojlo7 OiaryG00kcw5p4QXCzfB2FW/wWC1h2p3PkJI/W4rWDWrG9PIl0ryuGR1qUoGOit5U8WXFh6RFpc2p E9VW3tQjgb3vMbsDaKQBfFT6Jlul9LUe+oZvVnJPY0sbihO+GIVtWFaeXzf+B0zhTwxa8ACswgDIJ LWkj13YLRqf6aUGPoHUZxvtYjJMACYNur8Aipie8hxHSb4d69omp3V0tQ/3rMmX+BrSfP01myDmWt 499L429hq2Bj97OSGlag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pFE1r-006vh8-V6; Tue, 10 Jan 2023 12:47:00 +0000 Received: from mga12.intel.com ([192.55.52.136]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pFE1o-006vet-3Q; Tue, 10 Jan 2023 12:46:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354816; x=1704890816; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=AF/LrIZHIjUrlEw05KEt5x3jhyEPffI6eAIkh+dvqgZbQ+tYXorF4F0l IGA4HFqP6hzls+8naTwSlQX/zcMtAVc7s2ThtIslH+qPHpI3Z/aQ7ERTP vdpLrC2CymAUlgEnt+efhhiCdFGbDc8VQayK0FYuPEQpRRFf51scNL7K1 vJr1lAFy8ZxmE8d24+8VASgVQux2owin41g1ALHyKOSMZYfqAi0pf++px OPX1h0Vg8PImDJV+JLHZWZaMIOcVpi38ouRfivcBNfzC7ZiUveISxB7Yu nXkIdc7ssoVI68oH9ykL81i5yvxKvNQj/9CHtlfGb6M+kXZ5GFyHBnkFi w==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="302834343" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="302834343" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="689393555" X-IronPort-AV: E=Sophos;i="5.96,315,1665471600"; d="scan'208";a="689393555" Received: from ahajda-mobl.ger.corp.intel.com (HELO [10.213.8.19]) ([10.213.8.19]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2023 04:46:39 -0800 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> Date: Tue, 10 Jan 2023 13:46:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.6.1 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Content-Language: en-US To: Andy Shevchenko Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-mips@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, Boqun Feng , linux-xtensa@linux-xtensa.org, Arnd Bergmann , intel-gfx@lists.freedesktop.org, linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org References: <20230110105306.3973122-1-andrzej.hajda@intel.com> From: Andrzej Hajda Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230110_044656_236527_8C230259 X-CRM114-Status: GOOD ( 22.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrzej Hajda Date: Tue, 10 Jan 2023 12:46:37 +0000 Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Message-Id: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> List-Id: References: <20230110105306.3973122-1-andrzej.hajda@intel.com> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Andy Shevchenko Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-mips@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, Boqun Feng , linux-xtensa@linux-xtensa.org, Arnd Bergmann , intel-gfx@lists.freedesktop.org, linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton , linuxppc-dev@lists.ozlabs.org On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrzej Hajda Subject: Re: [Intel-gfx] [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Date: Tue, 10 Jan 2023 13:46:37 +0100 Message-ID: <1bfae3d0-8c0b-ea83-7184-db847a4a969f@intel.com> References: <20230110105306.3973122-1-andrzej.hajda@intel.com> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673354813; x=1704890813; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=JjCURNXFRNcYm6qc3FnZISad0UB5+7XjMdbu/B0f1Fo=; b=dBrSK8pUnAS9DlR5De4m4pGHPecuaVHYMOsvE9RqaUG3SSw2qPeWTZfs W+PrO6Po2RHkqnb+9zT21BLK6wIzaPP8OsUZVMw8KFYBiNTXPXWMHIJpx AWF0Dj8pgsKkrLSvE1PjW87tU4ZXbtx2Ca9i3OezaoCL8hipF4R5Y9+O5 +PiifomgFNqirtFV9/ZIAuElGDzY1ncKdp5K1AqZke6sDbq5g8nI5vZdn QVNsAyernmi3Pbmbx9/MuV9liHxBZNFcbOchudK/9+WZXXOSCo0oys6ZB tdGdxNHzBAZzhJcmSH4eL9FBj7NvhMMWoE+MpU32zpeciq0FrBr/GYkPc Q==; Content-Language: en-US In-Reply-To: List-ID: Content-Type: text/plain; charset="us-ascii"; format="flowed" To: Andy Shevchenko Cc: Mark Rutland , linux-ia64@vger.kernel.org, linux-sh@vger.kernel.org, Peter Zijlstra , dri-devel@lists.freedesktop.org, linux-mips@vger.kernel.org, sparclinux@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-snps-arc@lists.infradead.org, Boqun Feng , linux-xtensa@linux-xtensa.org, Arnd Bergmann , intel-gfx@lists.freedesktop.org, linux-m68k@lists.linux-m68k.org, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Vetter , linux-alpha@vger.kernel.org, Andrew Morton On 10.01.2023 12:07, Andy Shevchenko wrote: > On Tue, Jan 10, 2023 at 11:53:06AM +0100, Andrzej Hajda wrote: >> This patch tries to show usability of __xchg helper. >> It is not intended to be merged, but I can convert >> it to proper patchset if necessary. >> >> There are many more places where __xchg can be used. >> This demo shows the most spectacular cases IMHO: >> - previous value is returned from function, >> - temporary variables are in use. >> >> As a result readability is much better and diffstat is quite >> nice, less local vars to look at. >> In many cases whole body of functions is replaced >> with __xchg(ptr, val), so as further refactoring the whole >> function can be removed and __xchg can be called directly. > > ... > >> arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, >> struct pt_regs *regs) >> { >> - unsigned long orig_ret_vaddr; >> - >> - orig_ret_vaddr = regs->ARM_lr; >> - /* Replace the return addr with trampoline addr */ >> - regs->ARM_lr = trampoline_vaddr; >> - return orig_ret_vaddr; >> + return __xchg(®s->ARM_lr, trampoline_vaddr); >> } > > If it's not a callback, the entire function can be killed. > And this is a good example of the function usage. > OTOH, these places might have a side effect (if it's in deep CPU > handlers), means we need to do this carefully. > > ... > >> static inline void *qed_chain_produce(struct qed_chain *p_chain) >> { >> - void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx; >> + void *p_prod_idx, *p_prod_page_idx; >> >> if (is_chain_u16(p_chain)) { >> if ((p_chain->u.chain16.prod_idx & >> @@ -390,11 +391,8 @@ static inline void *qed_chain_produce(struct qed_chain *p_chain) >> p_chain->u.chain32.prod_idx++; >> } >> >> - p_ret = p_chain->p_prod_elem; >> - p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) + >> - p_chain->elem_size); >> - >> - return p_ret; >> + return __xchg(&p_chain->p_prod_elem, >> + (void *)(((u8 *)p_chain->p_prod_elem) + p_chain->elem_size)); > > Wondering if you still need a (void *) casting after the change. Ditto for the > rest of similar cases. IMHO it is not needed also before the change and IIRC gcc has an extension which allows to drop (u8 *) cast as well [1]. [1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html > >> } > > ... > > Btw, is it done by coccinelle? If no, why not providing the script? > Yes I have used cocci. My cocci skills are far from perfect, so I did not want to share my dirty code, but this is nothing secret: @r1@ expression x, v; local idexpression p; @@ - p = x; - x = v; - return p; + return __xchg(&x, v); @depends on r1@ expression e; @@ __xchg( - &*e, + e, ...) @depends on r1@ expression t; @@ - if (t) { + if (t) return __xchg(...); - } @depends on r1@ type t; identifier p; expression e; @@ ( - t p; | - t p = e; ) ... when != p Regards Andrzej