From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andy Shevchenko Subject: Re: [RFC DO NOT MERGE] treewide: use __xchg in most obvious places Date: Tue, 10 Jan 2023 13:07:02 +0200 Message-ID: References: <20230110105306.3973122-1-andrzej.hajda@intel.com> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673348835; x=1704884835; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=rVhJmEIPGip77/doSmIyFH6N4wuSe2EOVqkHy4zJTJM=; b=Fz7z6D9L7sxNJHo/905AlQU6bncpn7A7DKRIeT94FEsr1nWSvHjebsiY +T5qmC2UB8KBgvmzcI1yN/Ng3og2IKZ/XhvgJG1zwEIb5p9GU4zAAamvI kbtT52cECkz9/ZTxTFdr7XO0SvSgWPGefzXukz/7tYD08ZIKuvWsbzW2u M5vbnK6iUKhU35c8JDII8gNnUOXfsqIiv2G5CXnLqxWPXK6b+rgxsSNUi nWDme6EBCexs4J6xu4TasCfy0XuB3l4wqQtnIQDHkA7BEoeylnYcIH7NK lOdRffNI/5rWV7aiiEIaPsB1ChAT7xFmh4dyKi5NARssmGj3DllDQS6WM Q==; Content-Disposition: inline In-Reply-To: <20230110105306.3973122-1-andrzej.hajda@intel.com> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Andrzej Hajda 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 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. > } ... Btw, is it done by coccinelle? If no, why not providing the script? -- With Best Regards, Andy Shevchenko