Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp426360rwj; Thu, 22 Dec 2022 09:24:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXtWJdOaAcSxWFUPFVLmF/hvFUgW48Rmm5xu4R8PapQ6vdqxvoWWllcSXwCbPi5/3OJx2BS6 X-Received: by 2002:a05:6a21:c08d:b0:a3:a90f:5326 with SMTP id bn13-20020a056a21c08d00b000a3a90f5326mr8664145pzc.61.1671729867068; Thu, 22 Dec 2022 09:24:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671729867; cv=none; d=google.com; s=arc-20160816; b=bjoU8XkQkUb89OhnLqLE30cb02YHIqKDSBTtCc4ow1WNTApHH5znKwisnrMyMZ59ad 6fSYcNvoK8wlAzvOJZoJYrlUftwDJ3QSavK3q5zmJfSLzGw2B9E4s6VeDthrTw3Qy/yD mLwIJqfQuGQVtiVkGGTx9TLhrbDm+iEvScIvOMM/OgEO54cwht/myJD/74R0Oul0053/ bgMiKyMYdD4aGar+pnm6vEZ8l/P7dVNAFXp5pP7r+cF/gVUd08SiJKsw52sAr4TtdJ3d EH5juznE/V+OD3S/TTCHGnprG6PLfEGDToF6WX6J68UGLkenCNajFSuKjBuJ7Js4VRtS RunA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=8ei6rl3ZNeab/49wXlLNlZ6SWqggrvo5W8HYnPXVCZU=; b=n6VLRHHqXBqJxoupvLBPV1+QBfv7wSptSN0A7LyrFPCgD0Q5lb8opvJMOH2fq86Tx/ u2dR6TcnkZqmVm1voegNwC6rRFh08CJ0iIkk2KIfybG7ZPv6GOMAT1qTCRBpjIStuk/E klmlk2x++jS5oTpebh9rUiCd4xjrW7AlewUSInJpHCUiJEvWbfLfM8uiphm/17A0Vdx1 o9zdNcVEw2agT/1voBiIiRBqGHNCidAMAEuOAZDt98ZK5Z6aU7jecJcH3sKmPZGa+ttd D83GC2Rn9ID+zciBdambRHMB2hhe8F1Vweh19AM1tAMKm53QTKFFSaa4HFf/sHX1Nrxc jFBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=oaxbYeHT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u7-20020a63d347000000b0047859a0892dsi1250555pgi.340.2022.12.22.09.24.17; Thu, 22 Dec 2022 09:24:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=oaxbYeHT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230265AbiLVRVz (ORCPT + 68 others); Thu, 22 Dec 2022 12:21:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbiLVRVw (ORCPT ); Thu, 22 Dec 2022 12:21:52 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E54795BC; Thu, 22 Dec 2022 09:21:51 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3F708B81CFB; Thu, 22 Dec 2022 17:21:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11A31C433D2; Thu, 22 Dec 2022 17:21:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1671729708; bh=gMuNx/ETzj/pkU8IgNrRcwRUZ5VOS050gceZG8hotd0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=oaxbYeHTTs0NTDHc1u4zsA/7HQBinJcPSMs3s/KVnxc//k0Ntz1pPgpR2Dmr88gAU WZZBXxDL6Tc0QjJMc73INnk/0KOkuFbF7iNB59sbgWiBpJYnibIJSSo4sqTI8wPaJQ JdMD0wV8Cbb5JpT60P63Yh/T9dCr1NIuxhm2H/M4= Date: Thu, 22 Dec 2022 09:21:47 -0800 From: Andrew Morton To: Andrzej Hajda Cc: linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-hexagon@vger.kernel.org, linux-ia64@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@vger.kernel.org, linux-mips@vger.kernel.org, openrisc@lists.librecores.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-xtensa@linux-xtensa.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Arnd Bergmann , Rodrigo Vivi , Andy Shevchenko , Peter Zijlstra , Boqun Feng , Mark Rutland Subject: Re: [PATCH 00/19] Introduce __xchg, non-atomic xchg Message-Id: <20221222092147.d2bb177c67870884f2e59a9b@linux-foundation.org> In-Reply-To: <20221222114635.1251934-1-andrzej.hajda@intel.com> References: <20221222114635.1251934-1-andrzej.hajda@intel.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 22 Dec 2022 12:46:16 +0100 Andrzej Hajda wrote: > Hi all, > > I hope there will be place for such tiny helper in kernel. > Quick cocci analyze shows there is probably few thousands places > where it could be useful. So to clarify, the intent here is a simple readability cleanup for existing open-coded exchange operations. The intent is *not* to identify existing xchg() sites which are unnecessarily atomic and to optimize them by using the non-atomic version. Have you considered the latter? > I am not sure who is good person to review/ack such patches, I can take 'em. > so I've used my intuition to construct to/cc lists, sorry for mistakes. > This is the 2nd approach of the same idea, with comments addressed[0]. > > The helper is tiny and there are advices we can leave without it, so > I want to present few arguments why it would be good to have it: > > 1. Code readability/simplification/number of lines: > > Real example from drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c: > - previous_min_rate = evport->qos.min_rate; > - evport->qos.min_rate = min_rate; > + previous_min_rate = __xchg(evport->qos.min_rate, min_rate); > > For sure the code is more compact, and IMHO more readable. > > 2. Presence of similar helpers in other somehow related languages/libs: > > a) Rust[1]: 'replace' from std::mem module, there is also 'take' > helper (__xchg(&x, 0)), which is the same as private helper in > i915 - fetch_and_zero, see latest patch. > b) C++ [2]: 'exchange' from utility header. > > If the idea is OK there are still 2 qestions to answer: > > 1. Name of the helper, __xchg follows kernel conventions, > but for me Rust names are also OK. I like replace(), or, shockingly, exchange(). But... Can we simply make swap() return the previous value? previous_min_rate = swap(&evport->qos.min_rate, min_rate);