Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp1093286rwl; Thu, 5 Jan 2023 08:34:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXsnanLRezcQbxDqULgWA8Sf5D3iub1Krms93wTsXiFq6hWMmcX5tLztVTN3rZeY8TgCvI2u X-Received: by 2002:a17:90a:a597:b0:226:587:7911 with SMTP id b23-20020a17090aa59700b0022605877911mr35751980pjq.20.1672936471922; Thu, 05 Jan 2023 08:34:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672936471; cv=none; d=google.com; s=arc-20160816; b=hv8YB2ms0Ur5t+yRKApTqTz51iS3mj0gxtikHY9Ej1Drf10/etgCclHUtD6QfPrpPI 2P8ksJDhcy+I3O6/bnIxEQS9W+ek033ZsI/AMGB7iaHQoxVoN4RjBpUIUtOD3BY0bivT 4RwY2wyY/6fr3eIq9e864DVcT4SaklXZFur6tEfCWoF2JJedo40GOHkZeC9ujGX5u3+M CwY++KZ9hLr5oRhv67MmzxkQNJRIVLUcrwMGccnb6q0MUun64d+QJvzGgDmhBjTqyJxD HrOcSkKLxWnYVJF0wSJ2AC00f6dTsGm+JSs1OCSDN/pJ66KHW6fRofkIc6hrLC4OtMkj IO1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:mail-followup-to :message-id:subject:cc:to:from:date:dkim-signature; bh=hriix7PNjl3Fx5Lwj5vGvmpV6J+F4oqIBHmzWDNV1zY=; b=TxIRig89akAleXWnbWdVU8VPc6jAEEs7GDBH2XQL1/8Ez6G6siSFjaPjhP9I1qOKUQ Rlvu3gUNP/GGytI4wxvEK/smqvtkLweHeMswfbB5Yrb5BxLebGL15I3lYrr3PKG4/fL2 pgA7rX9t2VWG6TPhN5L+H+OzObHyRdYXCJOfyNvTqfiAcbMT70d9qKtQU1YWXmHhJs99 xO8sAE/i9MPnDrW48SoZCtyfuXTibVMxn09ePYK54VPUorxMvzjsZKI9dbsHDbevXqq1 5waXXi/4ZGILCvPtvSoBNCgFuNEAJCghriCCFEOXoLAjPOt3SuptmCVRbX7d61rNGOE0 WqYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=Xkf28zar; 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 pw1-20020a17090b278100b002192eb3532bsi2286380pjb.116.2023.01.05.08.34.24; Thu, 05 Jan 2023 08:34:31 -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=@ffwll.ch header.s=google header.b=Xkf28zar; 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 S234094AbjAEQaS (ORCPT + 55 others); Thu, 5 Jan 2023 11:30:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233714AbjAEQ36 (ORCPT ); Thu, 5 Jan 2023 11:29:58 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 010615933E for ; Thu, 5 Jan 2023 08:29:56 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id d17so16922700wrs.2 for ; Thu, 05 Jan 2023 08:29:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:from:to:cc:subject:date:message-id:reply-to; bh=hriix7PNjl3Fx5Lwj5vGvmpV6J+F4oqIBHmzWDNV1zY=; b=Xkf28zaruVrLmqa0GfGmuAvqQQX+dB8u4vBIZu3Zqzswl7V0McjDKsYjGqvgAnDPVt JAfXVw2P8vm4NtD5ER0v9cpo5fq34lBw+gZ7lyOmQSBDrvgtT42XqrYiXIz43PC1aoqp dm4offnZy34FIeCS0B3FEa0zUz3dEn5dYFCKk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hriix7PNjl3Fx5Lwj5vGvmpV6J+F4oqIBHmzWDNV1zY=; b=DR/0xsLYR2rWzTxy2e7G6FiNcAtl9ND/L9JuEZU4OVdb8NAIoBnKRiE/ZfW1W80rIE tr4D4mIVzbl8r3EwI1ZKu33D+6hEO9jM+C9kfLlff0YJUL5ZbXb037+Q4nbJ/AnVXAP6 dg3j8Th4/lnd1lwaSBEjjl3BsQm33vFhH9clsNJX+gcvHQtzBDixOQ3xPVjqPEtx7QMy pt1Tkt4GDADPl5WDRzR+zThni2e+ePHtPwr50DrsCsvmubBP/fa9JSP9tNDMAjYRHc+Z 5Q/zt0w1dMeO63aoltAGOQH6JrwtHDCZu4SVEjeBk9yBi40rqLiIQR7Q/rQRBmEA9A6L ZGOQ== X-Gm-Message-State: AFqh2kpkThid2OjWve/7qGNAyoIlUkb2uxUIY+My3x1EfMdmfuvZVx+R sK2Sd3S+iGiuGBhfvA1Sc3+9jA== X-Received: by 2002:adf:f9cb:0:b0:285:d0ba:92e2 with SMTP id w11-20020adff9cb000000b00285d0ba92e2mr22521193wrr.47.1672936194547; Thu, 05 Jan 2023 08:29:54 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id x15-20020a5d490f000000b00236883f2f5csm36488463wrq.94.2023.01.05.08.29.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 08:29:53 -0800 (PST) Date: Thu, 5 Jan 2023 17:29:51 +0100 From: Daniel Vetter To: Andrzej Hajda Cc: Andrew Morton , Mark Rutland , linux-m68k@vger.kernel.org, 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, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , Andy Shevchenko , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: Re: [PATCH 00/19] Introduce __xchg, non-atomic xchg Message-ID: Mail-Followup-To: Andrzej Hajda , Andrew Morton , Mark Rutland , linux-m68k@vger.kernel.org, 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, openrisc@lists.librecores.org, loongarch@lists.linux.dev, Rodrigo Vivi , Andy Shevchenko , linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org, linuxppc-dev@lists.ozlabs.org References: <20221222114635.1251934-1-andrzej.hajda@intel.com> <20221222092147.d2bb177c67870884f2e59a9b@linux-foundation.org> <6e727952-3ad0-fcc3-82f1-c465dcffd56f@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6e727952-3ad0-fcc3-82f1-c465dcffd56f@intel.com> X-Operating-System: Linux phenom 5.19.0-2-amd64 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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, Dec 29, 2022 at 10:54:50AM +0100, Andrzej Hajda wrote: > Forgive me late response - Holidays, > > On 22.12.2022 18:21, Andrew Morton wrote: > > 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. > > And replace private helpers with common one, see the last patch - the > ultimate goal > would be to replace all occurrences of fetch_and_zero with __xchg. > > > 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? > > If you mean some way of (semi-)automatic detection of such cases, then no. > Anyway this could be quite interesting challenge. My take is that unless there is very clear demand for this macro from outside of i915, it's not worth it. All that fetch_and_zero zero achieved is make i915 code a lot more confusing to read for people who don't know this thing. And it replaces 2 entirely standard lines of 0, every often clearing pointers in data structures where you really want the verbosity to have a reminder and thinking about the locking. Plus it smells way too much like the cmpxchg family of atomic functions, addig further to the locking confuion. Imo the right approach is to just open code this macro in i915 and then drop it. Again, unless enough people outside of i915 really really want this, and want to lift this to a kernel idiom. -Daniel > > > > > > 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); > > As Alexander already pointed out, swap requires 'references' to two > variables, > in contrast to xchg which requires reference to variable and value. > So we cannot use swap for cases: > ??? old_value = __xchg(&x, new_value); > > Regards > Andrzej > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch