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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A856CC54EF0 for ; Thu, 5 Jan 2023 16:30:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233519AbjAEQaP (ORCPT ); Thu, 5 Jan 2023 11:30:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232319AbjAEQ35 (ORCPT ); Thu, 5 Jan 2023 11:29:57 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F15BB59308 for ; Thu, 5 Jan 2023 08:29:55 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id z16so20178252wrw.1 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=UNbHPyGWJ5CswVJRw9mS6eeAQiWXUrX3RHPOtFXeSG3AZSXT9BEyoRYJ6t5VP5K2DQ OznNdKtk/3ERxkIWZhLmZDBSK6xQGxT2By4GRJVVHQWbRLNSCU6E8ISOO7ZW62K3yPHK TIXC9qlxx3e6dmzJuyGFbYljyol9iait8/xGKBPBWz95IPGKcW2G6VByBDMYH7Nb0Wu3 KLNLNPk+x8RXcXOKLFjKtT3RvL9LBtI8MO5PAH+j4iHElBBogtCwq4gBYMfGzJaiFQm1 bw7teqDMqSSSh6VE85DipwXEylM9K9PLXjLoPhDaa+yaCwedWhdw4gz6SD3sXe0xHgJX P5QA== X-Gm-Message-State: AFqh2koM4RGqk5zBZ/vniKr9HF32LFl5Phsm50pqgtVLueew82ywjW94 wJFeQmqv5RPpmJ7saCuWnQz24w== X-Google-Smtp-Source: AMrXdXvytt3BictUpSrNMsVMjqYxBg9ULQXEcgl2Nkqae4pxiPmHRRghamOTQFr1zyRYajkEyGj8ag== 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 Precedence: bulk List-ID: X-Mailing-List: linux-mips@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