From nobody Tue Dec 16 16:55:49 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D08A925DCF1 for ; Mon, 24 Mar 2025 10:52:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813564; cv=none; b=EtlrvRbc8Cd3gjhrNT1ZQU1MaPJiJtSCYWAgp7GJV2O/GHf50cMIObZmimIfm9w4eUfmfhjyRGVopjtFNQgiDMgdv+vZLH2BsGeMsc8QUI3fVVHUvv+wychWQMkaqF0t0riE2tz8kcAgo2ZV1EiT/rHnFt1MItafexSJzcqWcEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813564; c=relaxed/simple; bh=sZGc4JwXs7iJ8D/IHSbBoRpDBKmUfLLakBeMsg3vn3s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GlG1RoETwonK0oSD5Bjdj6y6v4qRq4jSRKPkSLI4gow9n86NejHh56q9zxT7Arhy1wTkty+r8FADfAs+U7UlwzfRmlE/121z5nlyQ6U+2LF+S8xki+vV0cj7bofcsz7i5PmJg054yJ+MjK2NePcb+J/f8Hlk5NXtlFBaBEiTsPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hWdhEUe0; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hWdhEUe0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1742813563; x=1774349563; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sZGc4JwXs7iJ8D/IHSbBoRpDBKmUfLLakBeMsg3vn3s=; b=hWdhEUe0hP5EnnnCOaQKVI0EZwlx2FcpMFXRO6as79rKoskOX9zMtzIw tVIYRyZUzZNNVDdvR6Bah505a4rtG9UdfoSuUTPKYkFuGkDo0b0LltO6l Wou8U3VlvkbEAPPvJaO4clqtNYe1Iu7h0lNfmLTiwAbIHECjmsVOu3R+t uWQEeFmAeoqg7WeklCbr0uTHgKTUbYH6gr60Q9ZOOk3YY2p5aInzVPlhZ zn34OmRTcbaF8dHto2sbghCqBO3gYvSsCrT/OPms+VhWX9zOGQplbG8b6 IJj1u8PnZtoHXN5HhsdPz9jIlr6ds7JKxbDLy188WhDz1MPeIwXp8+LaQ w==; X-CSE-ConnectionGUID: O8EX/k++R66qEpHxriS92Q== X-CSE-MsgGUID: an5Cd26/Q7S9bCMLB6ZeYw== X-IronPort-AV: E=McAfee;i="6700,10204,11382"; a="46756889" X-IronPort-AV: E=Sophos;i="6.14,271,1736841600"; d="scan'208";a="46756889" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2025 03:52:40 -0700 X-CSE-ConnectionGUID: W2ZE+m8aRG+4D0GYhxJ6tw== X-CSE-MsgGUID: 5aNeg8vqSb2B3TarIyTGUA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,271,1736841600"; d="scan'208";a="154916399" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa002.jf.intel.com with ESMTP; 24 Mar 2025 03:52:39 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 52014CC; Mon, 24 Mar 2025 12:52:37 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , Alexandru Ardelean , linux-kernel@vger.kernel.org Cc: Andrew Morton , Ingo Molnar Subject: [PATCH v1 1/2] kernel.h: Move READ/WRITE definitions to Date: Mon, 24 Mar 2025 12:50:24 +0200 Message-ID: <20250324105228.775784-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250324105228.775784-1-andriy.shevchenko@linux.intel.com> References: <20250324105228.775784-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ingo Molnar Headers shouldn't be forced to include just to gain these simple constants. Signed-off-by: Ingo Molnar Signed-off-by: Andy Shevchenko --- include/linux/kernel.h | 4 ---- include/linux/types.h | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index be2e8c0a187e..01bb0fac3667 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -41,10 +41,6 @@ =20 #define STACK_MAGIC 0xdeadbeef =20 -/* generic data direction definitions */ -#define READ 0 -#define WRITE 1 - #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) =20 #define u64_to_user_ptr(x) ( \ diff --git a/include/linux/types.h b/include/linux/types.h index 49b79c8bb1a9..6dfdb8e8e4c3 100644 --- a/include/linux/types.h +++ b/include/linux/types.h @@ -136,6 +136,10 @@ typedef s64 ktime_t; typedef u64 sector_t; typedef u64 blkcnt_t; =20 +/* generic data direction definitions */ +#define READ 0 +#define WRITE 1 + /* * The type of an index into the pagecache. */ --=20 2.47.2 From nobody Tue Dec 16 16:55:49 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ECA61C8605 for ; Mon, 24 Mar 2025 10:52:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813563; cv=none; b=kHoZeVYs6K8lWxf6ruMFeijzbAYV+1gbaBPTtiPPSvFMC71uqzXHCbUIWWUlnvXwpOP2tNwfHv2+ezImQQeyk7Enfw1rZqV0M4vUp+v2ftImER5IY6JXfAb0Hrvukqwn0+W87i7OI/wRC3qtV9RB88PJC3SUHOUCDFhfDFjPl7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742813563; c=relaxed/simple; bh=hzFZA5BJlWQnnmeaDLMalig5ESOluROqMB42yzZqNj0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VFCfTp72rM9pdO4887mPTXEKl1a2+IpHXWl9axkEHzfZGauDqOlAjiSJZErfc3ZklfPKFZi/viNTS9a00cdjwTQXQLpU6ZF135KHk0lWXleQgH5vReyamGbX5ROg/Dz9FDcl/NUVa/q/jT9jnLtlTsy18wBxITscN3e9jkgdOj8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=cEWe9AeB; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="cEWe9AeB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1742813563; x=1774349563; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hzFZA5BJlWQnnmeaDLMalig5ESOluROqMB42yzZqNj0=; b=cEWe9AeBJ5fcwk6wyAwYWA4nd/f1OwDYxQSNZo11z1GqKDeEg8T/yTLx p166HfGgg+xxb2mdSfFSBe3txIDrZbbpRbqe+zEzV85sXnf4eTTtk6kpI PqtLuEyBif2E56rltabSIE3RMgDacTns0u7dXgRCECS5StxPkuxRgsbIM +peWRpjCQqa3Xck0unMPvLkVuuzMPHDcqStYma/xKjebLji7TlTNwsH0p DvtuHNVnCRB9rp8T+qNeV7PQJrSJ2vigPYTv5bOUFD0PutGa3vjHlfof2 Wp5yMQLT6aRzRWv5g7OYNSGvv1R/PQWZNEky38d/K+nsuKc/0tX17YSDV w==; X-CSE-ConnectionGUID: QNaQPRteTLSa5jU5H4ZYXw== X-CSE-MsgGUID: +EgHwPyCR8C0UyPg6yZxnQ== X-IronPort-AV: E=McAfee;i="6700,10204,11382"; a="46756886" X-IronPort-AV: E=Sophos;i="6.14,271,1736841600"; d="scan'208";a="46756886" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2025 03:52:40 -0700 X-CSE-ConnectionGUID: y8/1LUGTSwqc/NOimrX/uA== X-CSE-MsgGUID: XM6VwjHJSkSFxjhnp4cDxg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,271,1736841600"; d="scan'208";a="154916398" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa002.jf.intel.com with ESMTP; 24 Mar 2025 03:52:39 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 659C9159; Mon, 24 Mar 2025 12:52:37 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , Alexandru Ardelean , linux-kernel@vger.kernel.org Cc: Andrew Morton Subject: [PATCH v1 2/2] kernel.h: Move PTR_IF() and u64_to_user_ptr() to util_macros.h Date: Mon, 24 Mar 2025 12:50:25 +0200 Message-ID: <20250324105228.775784-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250324105228.775784-1-andriy.shevchenko@linux.intel.com> References: <20250324105228.775784-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" While the natural choice of PTR_IF() is kconfig.h, the latter is too broad to include C code and actually the macro was moved out from there in the past. But kernel.h is neither a good choice for that. Move it to util_macros.h. Do the same for u64_to_user_ptr(). While moving, add necessary documentation. Signed-off-by: Andy Shevchenko --- include/linux/kernel.h | 10 +----- include/linux/util_macros.h | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 01bb0fac3667..1cce1f6410a9 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -33,6 +33,7 @@ #include #include #include +#include #include =20 #include @@ -41,15 +42,6 @@ =20 #define STACK_MAGIC 0xdeadbeef =20 -#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) - -#define u64_to_user_ptr(x) ( \ -{ \ - typecheck(u64, (x)); \ - (void __user *)(uintptr_t)(x); \ -} \ -) - struct completion; struct user; =20 diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h index 3b570b765b75..7b64fb597f85 100644 --- a/include/linux/util_macros.h +++ b/include/linux/util_macros.h @@ -79,6 +79,72 @@ (__fc_i); \ }) =20 +/** + * PTR_IF - evaluate to @ptr if @cond is true, or to NULL otherwise. + * @cond: A conditional, usually in a form of IS_ENABLED(CONFIG_FOO) + * @ptr: A pointer to assign if @cond is true. + * + * PTR_IF(IS_ENABLED(CONFIG_FOO), ptr) evaluates to @ptr if CONFIG_FOO is = set + * to 'y' or 'm', or to NULL otherwise. The (ptr) argument must be a point= er. + * + * The macro can be very useful to help compiler dropping dead code. + * + * For instance, consider the following:: + * + * #ifdef CONFIG_FOO_SUSPEND + * static int foo_suspend(struct device *dev) + * { + * ... + * } + * #endif + * + * static struct pm_ops foo_ops =3D { + * #ifdef CONFIG_FOO_SUSPEND + * .suspend =3D foo_suspend, + * #endif + * }; + * + * While this works, the foo_suspend() macro is compiled conditionally, + * only when CONFIG_FOO_SUSPEND is set. This is problematic, as there could + * be a build bug in this function, we wouldn't have a way to know unless + * the configuration option is set. + * + * An alternative is to declare foo_suspend() always, but mark it + * as __maybe_unused. This works, but the __maybe_unused attribute + * is required to instruct the compiler that the function may not + * be referenced anywhere, and is safe to remove without making + * a fuss about it. This makes the programmer responsible for tagging + * the functions that can be garbage-collected. + * + * With the macro it is possible to write the following: + * + * static int foo_suspend(struct device *dev) + * { + * ... + * } + * + * static struct pm_ops foo_ops =3D { + * .suspend =3D PTR_IF(IS_ENABLED(CONFIG_FOO_SUSPEND), foo_suspend= ), + * }; + * + * The foo_suspend() function will now be automatically dropped by the + * compiler, and it does not require any specific attribute. + */ +#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) + +/** + * to_user_ptr - cast a pointer passed as u64 from user space to void __us= er * + * @x: The u64 value from user space, usually via IOCTL + * + * to_user_ptr() simply casts a pointer passed as u64 from user space to v= oid + * __user * correctly. Using this lets us get rid of all the tiresome cast= s. + */ +#define u64_to_user_ptr(x) \ +({ \ + typecheck(u64, (x)); \ + (void __user *)(uintptr_t)(x); \ +}) + /** * is_insidevar - check if the @ptr points inside the @var memory range. * @ptr: the pointer to a memory address. --=20 2.47.2