Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp702585rdb; Thu, 15 Feb 2024 12:41:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUsJWUngM5CBJuLcM45BwDeAccYJMkR3pVlUf1np6mgoXbJp1R4z30LUvLoLneHXv5OjUsHTbTE7SgSe06UxGw73T65A7DYIzyynTZ6xg== X-Google-Smtp-Source: AGHT+IHHZ6rzXoMHkfo1CScYaHQ14RqIA3/2vdz/qA5YslF7+PMStBDMd0SIwX0XIHg4hIPXOdwP X-Received: by 2002:a05:620a:1342:b0:785:dad3:2ce0 with SMTP id c2-20020a05620a134200b00785dad32ce0mr3246687qkl.72.1708029659883; Thu, 15 Feb 2024 12:40:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708029659; cv=pass; d=google.com; s=arc-20160816; b=lTvTGEq6vHilkFp1JQDDHw7ZBMHufqYTGQLiyj3X9qEeI4+BYVl07qDiTugFI7/qO+ DAHaa3sJJuea3d/m0jlhUE7kwunSylACfUWE/z9VJl8km0XyO3bpPl3M7F+qVeY1Bx4W zPECPB+GUB4VlZ8EbhvnkaWyrLROVsIGH6bwk3tdIudsW1uswP3HUFG6E7Lb29aVrDJA J7H+Q2plcCzQiWs8CducaKyjPxF0hCgnHAbPJDxm5MVuBb7MkJ5Dmm+ec3WEiGmYh7fe udmng/KmLDKSLbBBUF9xf5pDDZBMfhifTTxTTexsxkrc965fZWt8tmbvgy+h3sLgldfh M45g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=iPg6tnCDEzD31Kj/SgxJk7mTaAskbQ2MnwKG6pq8DQU=; fh=EOHghtb/+9Fjv70yUJSxjW9mga7MYntvIwM15mT85Xw=; b=vlDvo8ogKtMTG0d5iWTHgmN4gXZYZjVNE97CIHsqiisHa7X4BmqdxazHn6L+a2hZHg p4fgEf+LTPrYUUJEtM4ylhacaOT35CI7T1f8T4p5IDbP8ByG+TaCaY8htVLrr9CAcvYu IEjovGkvL8B1EnJjakQtHLJImr+jue0dowE9AFe5IwziPcQqpOdxq6tRI1G7s/eYDm6e oQXNa+dNBX3ihaFbkk+IA0QfXKTs/SHASABdevnM/hJite1ITVgTDvFTzFh00Nx+p/Dr OW/i3yDbyeNQZxcV5uf1A1qqw6JVtwUvROelQyCCz/BZkZJ1Yor+2PmKemN9kYfFu/PM h0Ow==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VHmBjrL6; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-67661-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67661-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id vz25-20020a05620a495900b007872878b83bsi2315519qkn.211.2024.02.15.12.40.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 12:40:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67661-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VHmBjrL6; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-67661-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67661-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A25871C20FE0 for ; Thu, 15 Feb 2024 20:40:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3CFCA13B292; Thu, 15 Feb 2024 20:40:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VHmBjrL6" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) (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 CF2BE1386A2 for ; Thu, 15 Feb 2024 20:40:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708029652; cv=none; b=VS//HnA54jF8dwz5fkNpMc59Z1sGTMTSAIrGSwJ130qg6QD6u47xzekt7XCTaQJCoLw+oa2AcKoymIXkPOfbyJOIj+NtqVwyEwYBXc0xBO187DtlcM171sgX+xDSQYBfzQe4FH6CZAMU4Rp5TQDS8pwK6u3HUXArggtP3HJW6I0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708029652; c=relaxed/simple; bh=RNbgm2PSnyK3LVzCwTFXtsWgZ5d1/ZKQNi1RrhGFaSM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=q9E01QJBFma3c71jyypoHvLgmv48wMEungmp5Hav85FZIMGqgYVHCnxl1kaW1hMQO+MYEkLgl2FDF2cVvfuVuUkFBTW4MI5nj2RaStfJe9bVpbosomr9vpwdYR3frUaSnNVK9oFVpb48KLjvnc6apvUxMhzY/WATRNLq4LDrODQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=VHmBjrL6; arc=none smtp.client-ip=192.198.163.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708029651; x=1739565651; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=RNbgm2PSnyK3LVzCwTFXtsWgZ5d1/ZKQNi1RrhGFaSM=; b=VHmBjrL6QVsFhJ/vhxyGS+4Godmmiq/RhIBZgF4dfLZITbEJcIn8pnxW cfhPuUPWoM05t+efVgitwStl3+tj2jBNqJ20N3f6e2l3atK2X8a7yjn9w VQ54l0qE3t6ai1xSmGoDVmzUE8AKWF85Uup4k0XcGsYFxxRpz/1hWIWoA b5nB5et3auhR12NcN8uTXt2Z+rK+r40RsuceRZQGY/J4AM79+Dqlsx6JN zzjazI6kW3AaQaKTCvtqSe/yaAOYVrZQm6gFYZQ/o4iG7GHo5fk8WSFaU TtRFEc4uBG8LJ4IT9ccMuWRrEWom/O4FySDxqbtSG9PvIuw0S3glcBXAz w==; X-IronPort-AV: E=McAfee;i="6600,9927,10985"; a="5927781" X-IronPort-AV: E=Sophos;i="6.06,162,1705392000"; d="scan'208";a="5927781" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Feb 2024 12:40:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,162,1705392000"; d="scan'208";a="3733053" Received: from irvmail002.ir.intel.com ([10.43.11.120]) by orviesa010.jf.intel.com with ESMTP; 15 Feb 2024 12:40:43 -0800 Received: from [10.249.139.11] (mwajdecz-MOBL.ger.corp.intel.com [10.249.139.11]) by irvmail002.ir.intel.com (Postfix) with ESMTP id 2F98F2818D; Thu, 15 Feb 2024 20:40:41 +0000 (GMT) Message-ID: Date: Thu, 15 Feb 2024 21:40:40 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] wordpart.h: Helpers for making u16/u32/u64 values Content-Language: en-US To: Kees Cook Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , Alexey Dobriyan , Jani Nikula References: <20240214214654.1700-1-michal.wajdeczko@intel.com> <202402141408.0E78D47@keescook> From: Michal Wajdeczko In-Reply-To: <202402141408.0E78D47@keescook> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 14.02.2024 23:09, Kees Cook wrote: > On Wed, Feb 14, 2024 at 10:46:53PM +0100, Michal Wajdeczko wrote: >> It is quite common practice to make u16, u32 or u64 values from >> smaller words. Add simple helpers for that. >> >> Signed-off-by: Michal Wajdeczko >> --- >> v2: new macro names due to conflict with crypto/aria.h >> explicit cast and truncation everywhere (Alexey) >> moved to wordpart.h (Andy) >> --- >> Cc: Kees Cook >> Cc: Andy Shevchenko >> Cc: Alexey Dobriyan >> Cc: Jani Nikula >> --- >> include/linux/wordpart.h | 32 ++++++++++++++++++++++++++++++++ >> 1 file changed, 32 insertions(+) >> >> diff --git a/include/linux/wordpart.h b/include/linux/wordpart.h >> index f6f8f83b15b0..8c75a5355112 100644 >> --- a/include/linux/wordpart.h >> +++ b/include/linux/wordpart.h >> @@ -31,6 +31,38 @@ >> */ >> #define lower_16_bits(n) ((u16)((n) & 0xffff)) >> >> +/** >> + * make_u16_from_u8 - make u16 value from two u8 values >> + * @hi: value representing upper 8 bits >> + * @lo: value representing lower 8 bits >> + */ >> +#define make_u16_from_u8(hi, lo) ((u16)((u16)(u8)(hi) << 8 | (u8)(lo))) > > Do we want to actually do type validation here? Right now it's just > cast/truncating, which based on the version log is by design. Is silent > truncation the right thing to do? note that even FIELD_PREP() is doing silent truncation and these macros here could be treated as specialized/simplified variants of FIELD_PREP() as alternate implementation can look like: #define make_u16_from_u8(hi, lo) \ ((u16)(FIELD_PREP_CONST(GENMASK(15, 8), (hi)) | \ FIELD_PREP_CONST(GENMASK(7, 0), (lo)))) #define make_u32_from_u16(hi, lo) \ ((u32)(FIELD_PREP_CONST(GENMASK(31, 16), (hi)) | \ FIELD_PREP_CONST(GENMASK(15, 0), (lo)))) #define make_u64_from_u32(hi, lo) \ ((u64)(FIELD_PREP_CONST(GENMASK_ULL(63, 32), (hi)) | \ FIELD_PREP_CONST(GENMASK_ULL(31, 0), (lo)))) but then it will not match simplicity of the lower|upper_XX_bits macros