From patchwork Wed Feb 28 20:41:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 13575962 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7BB76C5478C for ; Wed, 28 Feb 2024 20:50:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XrRLMDu/DR2U4bF8CTkFQji9lSCZSVfaD2IwMJaHVPE=; b=k8KxL2ITirndhl jNJ/XYtXOcASKa6CijX+96UsyOFFNGFwDJFxoBpuELGm0BWDmCp8deuYb6mdJq6W8x5g2G6KNCP/k rl0KSsh2xA8nDhBhVBIwneoXB2fOZfcVS3V/JqmYHlce+KKGlMSaY8MF+1094qtUe6lXRK0slJxjN WV/KERnmtu4fpeMtrB58MYIhydCELT8XLJFsTJa6Q6tcgn5pwjYm42FkakMdV8dUoBWXuL1x6Sblz EAyu45fNMxp3V3qMz0lgasX4aUyXV1nuJAeRPJzHknE0CmXhEAxnV1gZr7tYBirBXjAlxX2/igW6g G/fP1LV3+dXuDZx1V9Zw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rfQsD-0000000AtBM-38eu; Wed, 28 Feb 2024 20:49:53 +0000 Received: from mgamail.intel.com ([192.198.163.17]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rfQs2-0000000At7F-1BFr for linux-arm-kernel@lists.infradead.org; Wed, 28 Feb 2024 20:49:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709153382; x=1740689382; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5rz+s7/pr5Tr5G4CC4rkjs7MtaG8CkV7L7yDYm59sps=; b=EL/38B51SKp86BtuPXAGtnVx4V60WhrLGGoeUl0aM3O8cJR2xy4tBr1D UVh9+Gz9NwaSPj52avN1bOOAyDa6dpATc3znNooGpewOYVQTKXZarqrFn SZP2NO/x2IiIigYGMMf58GNRkymuWJlfn/JxJ6rTNg/JDZpF7OVk+960P e2PNEsoAG8d10J0Phq+LqMHpyZCMfD5HmSPXY1ZyeqsPyPf+jcinyv7UC WmYNzlM0gYZL8IeomHj8KX+mgUfYZQs3DUybRa58f954vzxztq+VX9vqO ICyO3mqhO+EI2my8/ILQhM2nzWk9+4qK84XfU84DQsSQZBZsOJ9e3ey0t Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="3428683" X-IronPort-AV: E=Sophos;i="6.06,191,1705392000"; d="scan'208";a="3428683" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 12:49:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="937034656" X-IronPort-AV: E=Sophos;i="6.06,191,1705392000"; d="scan'208";a="937034656" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 28 Feb 2024 12:49:35 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id 1D9D5284; Wed, 28 Feb 2024 22:49:34 +0200 (EET) From: Andy Shevchenko To: Vinod Koul , Linus Walleij , Jonathan Cameron , Andy Shevchenko , Mark Brown , Kees Cook , linux-arm-kernel@lists.infradead.org, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-spi@vger.kernel.org, netdev@vger.kernel.org, linux-hardening@vger.kernel.org Cc: Jonathan Cameron , Lars-Peter Clausen , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Gustavo A. R. Silva" Subject: [PATCH v4 2/8] overflow: Add struct_size_with_data() and struct_data_pointer() helpers Date: Wed, 28 Feb 2024 22:41:32 +0200 Message-ID: <20240228204919.3680786-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1.gbec44491f096 In-Reply-To: <20240228204919.3680786-1-andriy.shevchenko@linux.intel.com> References: <20240228204919.3680786-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240228_124942_360114_7CEFD30E X-CRM114-Status: GOOD ( 15.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Introduce two helper macros to calculate the size of the structure with trailing aligned data and to retrieve the pointer to that data. Signed-off-by: Andy Shevchenko --- include/linux/overflow.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index bc390f026128..b93bbf1b6aaa 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -2,9 +2,10 @@ #ifndef __LINUX_OVERFLOW_H #define __LINUX_OVERFLOW_H +#include #include -#include #include +#include /* * We need to compute the minimum and maximum values representable in a given @@ -337,6 +338,30 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) */ #define array3_size(a, b, c) size_mul(size_mul(a, b), c) +/** + * struct_size_with_data() - Calculate size of structure with trailing aligned data. + * @p: Pointer to the structure. + * @a: Alignment in bytes before trailing data. + * @s: Data size in bytes (must not be 0). + * + * Calculates size of memory needed for structure of @p followed by an + * aligned data of size @s. + * + * Return: number of bytes needed or SIZE_MAX on overflow. + */ +#define struct_size_with_data(p, a, s) size_add(ALIGN(sizeof(*(p)), (a)), (s)) + +/** + * struct_data_pointer - Calculate offset of the trailing data reserved with + * struct_size_with_data(). + * @p: Pointer to the structure. + * @a: Alignment in bytes before trailing data. + * + * Return: offset in bytes to the trailing data reserved with + * struct_size_with_data(). + */ +#define struct_data_pointer(p, a) PTR_ALIGN((void *)((p) + 1), (a)) + /** * flex_array_size() - Calculate size of a flexible array member * within an enclosing structure.