Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6660911rwb; Wed, 18 Jan 2023 07:55:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXtGf3yhB+lxH8GRqchteTvcT8Rtl3wMCxFp/ktR0T0TCKsK8bQbbI2YOT9VIpATYFJGJ+Yh X-Received: by 2002:a05:6402:174d:b0:494:eb9:54c4 with SMTP id v13-20020a056402174d00b004940eb954c4mr6834621edx.19.1674057353902; Wed, 18 Jan 2023 07:55:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674057353; cv=none; d=google.com; s=arc-20160816; b=pXD5+YPz+rf01GYoVX+w0NOW4rMqmiLpHa9nbKJoKMT1ogFNlvK7J9M5RFKwodMpSf 8do7v3lAn93soqVAXNrGvIPckQwd/JOvB4JVpJwM0RPvAX0hb3E4sxPrysiJLDCVhHpv mvnYDaGCGBKQ63dlRXkUI+v2g7bVSeHzDOTAg1X75j6HmOZIWHOSp/JC+s+YNJCB3yiB OKaIrYHQ2j+7c8+WN9J8YBIOWmhduyDduupyG6ctvfxGDIrOnhNxy7jCBObVFz+77Y0n vkDRK0KNU5HhzFEmxCbsIiBE2j4/klCKG7AudSolLCHZNROvSK+kCB+39f89g0WvZBa0 JCbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XHr5KtuIml8p6WfM1mvvD4H6k7rK4fw6TnJABKHksDs=; b=J097kBZK3Q/8hQ3VVycVqTlzkX/zj+zYkePAKLo7C8DIgjSDZiJUdCU6pyONKb2GP6 emz1pTQ49qpojjlLYq1LbGJObANpwsEsMYVfH62tgifN7dYW6vU5U5VqaMIiRXdXMfok z/+4spE64ZSWconmbxeBj7JEryKP7RvWbn5V/F9cuJd0FZl/UwRD0uLNQFLnwDXxzlgO 9Or11RdNCymz6iiA9hs7gLh25bFCBC4Xq0cahCD+ePqbbe1DPDqm3vOCBIxVPBL2FeuP dgvVfPaLwwOc8SMbVVq1kia45ROquVvQu0jZJrCtg/6LMKqxNsb/DfRylSLzZpbvn5am afog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BvP0XNQH; 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 d33-20020a056402402100b0049dd955ded4si14326603eda.524.2023.01.18.07.55.41; Wed, 18 Jan 2023 07:55:53 -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=BvP0XNQH; 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 S230309AbjARPpz (ORCPT + 46 others); Wed, 18 Jan 2023 10:45:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbjARPpZ (ORCPT ); Wed, 18 Jan 2023 10:45:25 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 906E42DE49; Wed, 18 Jan 2023 07:45:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674056720; x=1705592720; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Z8X5newU9XL5BwcTC700iSbaht4sEux85LO6OUO0+Oc=; b=BvP0XNQHeXk6p+qln7ZGsB1KC8oCqQmaV0kVFcfMGatASB9gg/94YUEu ctk3E9Nl1OB/snlgQddHEUAFNEi5UiTWzxbHv2CijaNX0A4lSNEY4MeIG qEEpoq+ul7yQ2K7SYNYFe/qR2uQMGUpgS+W3zFiCR0tiQ8tLAb59Nthdd QdViGRN5euo7mlMyINisdkOXbmiU0yu2+GaL8nCh0jHIGPmZurkopSm5P 7bt2x3yKy4Qn9HDap48Ljp5D5C9V6gvNboG6gq1MBn6tWGiM0teSBXRwD p7M4YGENstUhXS3dLfmEu07PvGvmbyx/DJHdnzAyV8qHkKAKoRFNcUa6+ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10594"; a="322701222" X-IronPort-AV: E=Sophos;i="5.97,226,1669104000"; d="scan'208";a="322701222" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Jan 2023 07:45:20 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10594"; a="661759280" X-IronPort-AV: E=Sophos;i="5.97,226,1669104000"; d="scan'208";a="661759280" Received: from lab-ah.igk.intel.com ([10.102.42.211]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Jan 2023 07:45:14 -0800 From: Andrzej Hajda To: 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@lists.linux-m68k.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 Cc: Andrzej Hajda , Arnd Bergmann , Rodrigo Vivi , Andrew Morton , Andy Shevchenko , Peter Zijlstra , Boqun Feng , Mark Rutland Subject: [PATCH v5 2/7] linux/include: add non-atomic version of xchg Date: Wed, 18 Jan 2023 16:44:45 +0100 Message-Id: <20230118154450.73842-2-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230118154450.73842-1-andrzej.hajda@intel.com> References: <20230118153529.57695-1-andrzej.hajda@intel.com> <20230118154450.73842-1-andrzej.hajda@intel.com> MIME-Version: 1.0 Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 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=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 The pattern of setting variable with new value and returning old one is very common in kernel. Usually atomicity of the operation is not required, so xchg seems to be suboptimal and confusing in such cases. Signed-off-by: Andrzej Hajda Reviewed-by: Andy Shevchenko --- include/linux/non-atomic/xchg.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 include/linux/non-atomic/xchg.h diff --git a/include/linux/non-atomic/xchg.h b/include/linux/non-atomic/xchg.h new file mode 100644 index 00000000000000..f7fa5dd746f37d --- /dev/null +++ b/include/linux/non-atomic/xchg.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_NON_ATOMIC_XCHG_H +#define _LINUX_NON_ATOMIC_XCHG_H + +/** + * __xchg - set variable pointed by @ptr to @val, return old value + * @ptr: pointer to affected variable + * @val: value to be written + * + * This is non-atomic variant of xchg. + */ +#define __xchg(ptr, val) ({ \ + __auto_type __ptr = ptr; \ + __auto_type __t = *__ptr; \ + *__ptr = (val); \ + __t; \ +}) + +#endif -- 2.34.1