Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp653973rwj; Fri, 23 Dec 2022 06:29:33 -0800 (PST) X-Google-Smtp-Source: AMrXdXvTQTOtI53dbXIs092QfQy4Kjhdu/UJ/cSb8bi/c7Ufe+7SLuMpMmnmi4UE4OackcaTJFFl X-Received: by 2002:a05:6402:1454:b0:47d:8981:cbf7 with SMTP id d20-20020a056402145400b0047d8981cbf7mr7948730edx.13.1671805773568; Fri, 23 Dec 2022 06:29:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671805773; cv=none; d=google.com; s=arc-20160816; b=qZj1O+wl9qXqACA9JNRxCRUs7YGHozx2O4Uk7Dt3A+UtBZ3SVm/MfTMjvFJJe25gB4 L89SlKssNJfMCYPDGQcyK567HWwy0y02QIrtzSSk6b3R9B4BoerQEMfiDNK5v5EwPzUZ DauSJdX+NRob/uETvohU05Q2AE5TRDYriq4ajGCqY88JHr6iJTzgl57GTVfcvPnOwtvR pBDR+Y/l0CRkFhmnag9eqto6A5o2WZGSC9k2b3NX6mbR96TtVLWu8AjPtnMBeKPu2RHv /AjZtR1hi359zlqpr4P7u5spXj1B+c+wIUbG2SHgw2HOxWzpecVCmpQvcoO/krss35x3 tUwQ== 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:date:subject:cc:to:from :dkim-signature; bh=20cJ0sJYzoNFdF6CnIoFAwQ+5WmyzLZHBmzx8IWF7+k=; b=GNcyH2Lh++0FEQ6/5kv7xYI2dl638EskP6Qx6nqH25fv04KWtnK6JIsiCW/TDzdKYC mylSpSI9Y96jcQUrbnTFnTfLpWyG/tYg2g3shFCg+YdpCs9gFhylY1EaXiPYow4ELSCG ZAdVJjlqbaeUoirQ4tVujvHF9pVqL0AS6688XYGrAmith4FT8bYcTV4mUYKh9ALJXZWJ dPdAQBrkn56BacZmxljtJInEsvfoMCSnN6vrd9aVGJKmPPBMslfbTjIEaXQjlTERDXmF 7X6PBSQs6fslDYkG05rN4tZcWT5u2DeOFNH/lpD/tEgl5TL51FI4YE90stV/PdcAyOi1 NE8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=h6WHojs6; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go39-20020a1709070da700b0077cfdda438csi3162641ejc.35.2022.12.23.06.29.17; Fri, 23 Dec 2022 06:29:33 -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=@intel.com header.s=Intel header.b=h6WHojs6; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236389AbiLWOXV (ORCPT + 65 others); Fri, 23 Dec 2022 09:23:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230374AbiLWOXU (ORCPT ); Fri, 23 Dec 2022 09:23:20 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54BDB27CF8; Fri, 23 Dec 2022 06:23:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1671805399; x=1703341399; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ELTp1mLG7Y9aCL8aO+3yggqOoksNOYkU+LC/pmwgzyA=; b=h6WHojs6ZpmqWpG2ottoZdl2pYgLLN2M3eW5A5tU9xF/OQlsT/3lk21H c+27dZk3j5L1dN6yN+xTy/Nw2p/XcwLUfb5INN1T1g/0ViZeKJo5dasVa 2aM6Qe9tY1i/8kBLtimzGb9etHds0Vj1ICf1c6EWPlU8veg+mh8GlRw1f H1RVwid1/AgXqzvKKGomudHa9NdK5cWPCTVhnP/tSl3PViN1cgTQlPXxG AQqDeqzUnhnC4/YrPK4vlFEzcvUOFF7jMGVaZIH/9U4/Af3L+SOw42Fzh ybnz3reqKRmJRugCywjpmYvBfkXpozGjqKHcppjmrXomjjgBb2U1Jx0jJ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10570"; a="322279538" X-IronPort-AV: E=Sophos;i="5.96,268,1665471600"; d="scan'208";a="322279538" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Dec 2022 06:23:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10570"; a="897539227" X-IronPort-AV: E=Sophos;i="5.96,268,1665471600"; d="scan'208";a="897539227" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by fmsmga006.fm.intel.com with ESMTP; 23 Dec 2022 06:23:13 -0800 Received: from newjersey.igk.intel.com (newjersey.igk.intel.com [10.102.20.203]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 2BNENBXm013561; Fri, 23 Dec 2022 14:23:11 GMT From: Alexander Lobakin To: Andrew Morton Cc: Alexander Lobakin , Andrzej Hajda , 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 Date: Fri, 23 Dec 2022 15:23:00 +0100 Message-Id: <20221223142300.1820652-1-alexandr.lobakin@intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221222092147.d2bb177c67870884f2e59a9b@linux-foundation.org> References: <20221222114635.1251934-1-andrzej.hajda@intel.com> <20221222092147.d2bb177c67870884f2e59a9b@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 From: Andrew Morton Date: Thu, 22 Dec 2022 09:21:47 -0800 > 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); Unforunately, swap()'s arguments get passed by names, not as pointers, so you can't do swap(some_ptr, NULL); -- pretty common pattern for xchg. Thanks, Olek