Message ID | 20230118154450.73842-2-andrzej.hajda@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2408810wrn; Wed, 18 Jan 2023 07:47:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXsGq33AyQy4mQMGw4zR6Os1v2kcX+umz8c3Sqve146MqhAc/1wOsRMnyOEZXeEqn0OBri4K X-Received: by 2002:a17:906:a457:b0:7c1:ad6:638a with SMTP id cb23-20020a170906a45700b007c10ad6638amr7319487ejb.17.1674056820190; Wed, 18 Jan 2023 07:47:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674056820; cv=none; d=google.com; s=arc-20160816; b=NnYA9gZqwuD7hum/i2t0sej+Z9BaIrH7N/gFllIc7tZksctdfSq73paRJ0wWiJ43O5 yD3m4T9WwqsH8PgqvHfqDAuHHgw8GcO/ce26EAen2rIGIDQ/CbqAMfvEeNj5nNYd6IZt MTuAdC75zWNhihGGvmE92Mx/zzXcfEa3CM+uw8zpEYLOW0ZhW/0IgbG0vQuJw2rrkwNZ /qwUju13uYpZhcIuQgmgGhbsgFBT8zZBMNXgp9Tn7ZcXW5sDopgXZKmfVhy43wVEp8Qe ST5Ld/l6JRswqK/t0X9kE1lwQ0uieKw2XGPZcwxWcF0k8j6+hflmmB5WWzYeaVEhynSC XN0Q== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ck17-20020a170906c45100b0078356aaeb63si29015729ejb.288.2023.01.18.07.46.35; Wed, 18 Jan 2023 07:47:00 -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 <rfc822;lucaasribeiro@gmail.com> + 99 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 <rfc822;linux-kernel@vger.kernel.org>); 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 <andrzej.hajda@intel.com> 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 <andrzej.hajda@intel.com>, Arnd Bergmann <arnd@arndb.de>, Rodrigo Vivi <rodrigo.vivi@intel.com>, Andrew Morton <akpm@linux-foundation.org>, Andy Shevchenko <andriy.shevchenko@linux.intel.com>, Peter Zijlstra <peterz@infradead.org>, Boqun Feng <boqun.feng@gmail.com>, Mark Rutland <mark.rutland@arm.com> 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755375804012581402?= X-GMAIL-MSGID: =?utf-8?q?1755375804012581402?= |
Series |
Introduce __xchg, non-atomic xchg
|
|
Commit Message
Andrzej Hajda
Jan. 18, 2023, 3:44 p.m. UTC
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 <andrzej.hajda@intel.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> --- include/linux/non-atomic/xchg.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 include/linux/non-atomic/xchg.h
Comments
Hi Andrzej, On Wed, Jan 18, 2023 at 04:44:45PM +0100, Andrzej Hajda wrote: > 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 <andrzej.hajda@intel.com> > Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Andi Shyti <andi.shyti@linux.intel.com> Thanks, Andi
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