From patchwork Thu Dec 22 11:46:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 35705 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp279465wrn; Thu, 22 Dec 2022 03:54:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXtPMqaA8z1p1FAsZZf+vxv09lCVLTVIrTqVi/jYiqs9a4cCCWI+cht08La/pry4NEL92ytQ X-Received: by 2002:a17:907:80cb:b0:7c0:deb1:bb8a with SMTP id io11-20020a17090780cb00b007c0deb1bb8amr5526694ejc.28.1671710060230; Thu, 22 Dec 2022 03:54:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671710060; cv=none; d=google.com; s=arc-20160816; b=dmnZtrcY1ksHY9RrokVjmZ27bGgNrG2NFcYSe+OOJuW0I1N+cyx2qMAhz8kCCugEAU jhYgNjo6s7RFzt1R0qMGGmh1OdGGViJCXXO60ln4SjNqeQphmLSEizry16qXebsb8PuQ kCbunSby0dFDJrN4QRuy7jV0CbAQDIiUs6akTxwzC/2soiEkGI1RHdrBsSAhwiZ1rjkb qEjGIZ47ueeSiB8hLKJK9RZYGM8Ltx2y53EaP4U9dbU5lcpisCbEuBEO1C0zqbr5pP6W qk+/nwvsG28+xEU49v76IYvF6RXxNSTHpk5cFe+GY1Ez+twR4Gc1TwsL9QrJ/JT5yN6S 85Fw== 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=PTTx1b5cSz2WbmJDKMrMotNv1VAeqqkTaoJ56VMLD3s=; b=nr33QXFPOBVMnduVhQEuk68W4dE3dqq+m+izDAutMThDumMMy0WoB69AdHdxwGZDHw LRLrYoiiEjd4/GkIDWblCyLjPQc7Ol9/KBL9WDTXhLjtEEIoFw9M4U5d1PvbWVrVVahE wfYiUH+5J2qNrjivOkPcbR/N0X5qOFGrVt+/Y0VXraaPDIJfXYvMZD2iOjhljSWaTxro xhH/jy0oLdo59vo/+OK6386ntdU9MLu3uRR270McrLxTSvJKPAuv2wbMrBKt/+IaPE40 AvzV4rJI4ie17zw30QHtr0AAhApFdGGxPSLD0ho/n+7YKM74nw6wSShtKaBu8m0884j2 mUHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dnRQbvgK; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b26-20020a170906151a00b0079198b89adbsi240890ejd.890.2022.12.22.03.53.56; Thu, 22 Dec 2022 03:54:20 -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=dnRQbvgK; 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 S229742AbiLVLxe (ORCPT + 99 others); Thu, 22 Dec 2022 06:53:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231892AbiLVLwT (ORCPT ); Thu, 22 Dec 2022 06:52:19 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FB382B263; Thu, 22 Dec 2022 03:49:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1671709753; x=1703245753; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tIRocWwFeyLnkMyWNI6V183GDJq3vxxO+e0RyrK1iBA=; b=dnRQbvgKFGhPswUk8vFkjePyZPKIVjSmtsVioD7FCUk26tE9tj0FC2al YC28XUfKFU64LaQThPC0EoGptZwVdCbTVxTTZgoaCMHRrBVaFJq9W26kP 3xti6CpH2FSPNfSB3y/b5WMqCYCU07p7ZkqXraHIxZg7czoKodKVbFmPS zscAJVF+3fjgH51fnPwTnDgkW9J5WM7vadm+7f8x6ZuZ867zJN84jO2px +XFWiTNJowo1YqrDYfvS6A6M7zZjlOyIlKTkcvmzDCSjH6D/XqAMll3Bg jk/8p95GEznm0fyG1iy5+knk4J0rP+hFQDYc50iq24hUegZBGnsaOS7mV A==; X-IronPort-AV: E=McAfee;i="6500,9779,10568"; a="318804947" X-IronPort-AV: E=Sophos;i="5.96,265,1665471600"; d="scan'208";a="318804947" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Dec 2022 03:49:12 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10568"; a="629504949" X-IronPort-AV: E=Sophos;i="5.96,265,1665471600"; d="scan'208";a="629504949" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Dec 2022 03:49:06 -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 18/19] linux/include: add non-atomic version of xchg Date: Thu, 22 Dec 2022 12:46:34 +0100 Message-Id: <20221222114635.1251934-19-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222114635.1251934-1-andrzej.hajda@intel.com> References: <20221222114635.1251934-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752915048106661726?= X-GMAIL-MSGID: =?utf-8?q?1752915048106661726?= 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