From patchwork Fri Dec 9 15:48:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 31857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp852513wrr; Fri, 9 Dec 2022 07:54:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf4ZKQcGwJmeSyDqNkh3ceGo182DPjuvHp6iVH2xNT/fdhxBtQYueO5yYi9n3zzlMRmCZ3Wd X-Received: by 2002:a17:90a:5d97:b0:20d:bd60:911e with SMTP id t23-20020a17090a5d9700b0020dbd60911emr6376058pji.22.1670601255794; Fri, 09 Dec 2022 07:54:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670601255; cv=none; d=google.com; s=arc-20160816; b=MKENQvQdVZ7/RyXv9JiWEOCoThvlhr8cxn5iMpqpKS2eEYJe46hmlVv135oBKy6Iix S5ciPnYXlAZi2p/j+39a1mCjX+VyN/diQ7EBUJHS1qJY124S3em/Y6EOECb37YuKUIVZ 2ISVZPHb1m/hX8JsC+oX4OkDkmgB5cqBZbaLCBKSktyTK/ogLEgrxyoRYH9Sq6JwHN77 ie4ROSmoUgMNY/6lnLAHjYheek0Ds5yJupMRLn2FVIL9vzdSPyRaN5V+we7hegsPMFWs dk62g0y5WgjrgMewmjgiNAvRLDCZF5Fzl1VrnW6oGGsSd+MkvcXO+BErea0RwisIDpp9 uQXg== 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:message-id:date:subject:cc:to:from:dkim-signature; bh=63tMsF4qGUCuXmbcLeyacr6NVkikIR3jegYYF8hVOWg=; b=Rkq5yzo1lNtqp9qsFDHveUESC+oRwN6g6kHU/OkPZ+/2nTTn3fY9d8d6hZ9Qln9kAZ MN0a+WNGgD3BUPmiNrEQsvRuZ4E4EIhYE//i29qvTA8Xe1LN6GqqOw7xyytD+i7TfiRK a4QDTvBmdkNclaM8yxDuWXuLNyGEKqWEuJ4PDGjpCAauigJwskGghDMeha/4tOubooEk ZmMmXxcxaZ8n9ZxzqXI+SFsrxlLaNaJxwTYqtMngIK8pR458Q6JokPZy7pMqRtzpyVGD VActcVs2QgG9kicAVT0KzWnaJ5wAzeWpJ2i1sHVJ8TjvMFevHk9uldi4+Td9vZl1DkxD N1ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EKd59fFs; 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 lw3-20020a17090b180300b0021425cf457fsi2138210pjb.32.2022.12.09.07.54.02; Fri, 09 Dec 2022 07:54:15 -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=EKd59fFs; 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 S230450AbiLIPuk (ORCPT + 99 others); Fri, 9 Dec 2022 10:50:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230458AbiLIPuR (ORCPT ); Fri, 9 Dec 2022 10:50:17 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABE3D389CA for ; Fri, 9 Dec 2022 07:49:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670600957; x=1702136957; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=P6mxkpgd/Y8Bq72Ach78IUjaBu+/BBrZjDnHgxQR2ks=; b=EKd59fFs+WhI+Zt7IcOQEX81cwzhu+8EPAV5X8BRKyMOhlpUUIHW+tMH q6wORFIo/+5JTsLcGLdTXDDSgyl4GvFsBdw9LvczGBw0OtDXlF75crvBD f6cRq0Si/afMybQssVvuoS0GsBEq8Hx4gt34VsAv5rz9BS0Po9xZbooQT FrTq/Tfh58jhLkzymBKZkPyPvPbEYVQLgjj3tnVbhVPYu8nWpGGEeAvVe cN+fD7BBMfFBfsOukC38PkCkDFPEMUJEMycoA78mjD+GJ+DJD7jtD2r+n guDf6ri8ufEI/iRZCcw6v1VOUncDkB8K6bjPVIftk9loFh31uuhP3vGKs Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10556"; a="315119780" X-IronPort-AV: E=Sophos;i="5.96,230,1665471600"; d="scan'208";a="315119780" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Dec 2022 07:49:17 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10556"; a="647433321" X-IronPort-AV: E=Sophos;i="5.96,230,1665471600"; d="scan'208";a="647433321" Received: from lab-ah.igk.intel.com ([10.91.215.196]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Dec 2022 07:49:13 -0800 From: Andrzej Hajda To: linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Cc: Andrzej Hajda , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Andi Shyti , Andrew Morton , Andy Shevchenko , Arnd Bergmann Subject: [PATCH 1/5] linux/minmax.h: add non-atomic version of xchg Date: Fri, 9 Dec 2022 16:48:39 +0100 Message-Id: <20221209154843.4162814-1-andrzej.hajda@intel.com> X-Mailer: git-send-email 2.34.1 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=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751752382263066066?= X-GMAIL-MSGID: =?utf-8?q?1751752382263066066?= 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. Since name xchg is already in use and __xchg is used in architecture code, proposition is to name the macro exchange. Signed-off-by: Andrzej Hajda --- Hi, 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 used, of course I do not intend to do it :). I was not sure where to put this macro, I hope near swap definition is the most suitable place. Moreover sorry if to/cc is not correct - get_maintainers.pl was more confused than me, to who address this patch. Regards Andrzej --- include/linux/minmax.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 5433c08fcc6858..17d48769203bd5 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -144,4 +144,18 @@ #define swap(a, b) \ do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) +/** + * exchange - 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 exchange(ptr, val) ({ \ + typeof(ptr) __ptr = ptr; \ + typeof(*__ptr) __t = *__ptr; \ + *(__ptr) = (val); \ + __t; \ +}) + #endif /* _LINUX_MINMAX_H */