Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp105000rbe; Wed, 28 Feb 2024 13:46:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUForByRR9ZkujADNO8/brbos2QEde/fja8WU+IzU5fkaPlF91mZYEDJefDhVVWqq+MIPO8uIKCl6HH0sIM27AhItze7g8Bc+S2ypffMA== X-Google-Smtp-Source: AGHT+IGEowhIwDDDj3sOWrv/vg1OeogxG5B5h8bzaPaH59GhqS5DQLHwDgVYBLWJfPgdlPbr+ZPs X-Received: by 2002:a17:906:79a:b0:a42:fb21:5066 with SMTP id l26-20020a170906079a00b00a42fb215066mr155694ejc.31.1709156785583; Wed, 28 Feb 2024 13:46:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709156785; cv=pass; d=google.com; s=arc-20160816; b=k2vNWVNA1E7t1CHnqDB0F+EAzumR1RUYPBZClTjoObUVvjsizcFCePPOTdPTkr8HXp cdO3UEVYB84k6q1QmAT8Mm1bTuXtYsoaWbWLL3E6lLZ8BZ5n7rRmy//Z/nXiSLbaOVCI EOMWVJRvASYx0qiRHFVYjiDof3WS9IZdPNV6Wrh8c0u30B3LYTIgIe35bFkbgGDFsVqt Fgf+diykCuXEI0+5jxRaBp2sIhNlJ9jOPoQjDO1Va7pKcVFSozoxJMwb7PbQvuLK430X SOsxO2v0fWuin3zsdU9soThzKoPprJtwDUyvw8hKh7MVFITbE93FqoebUTuL43Rf/RMr Tlvw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=YdpR7cAKDSC45Ra+lrCp3FSf1aPEjcGJZRBQhc0h7f4=; fh=PBa8H7/hf5zeWgw29PIZpSQnpPrd95P8mKIvWKZGZ7M=; b=qjNcfweOGIXp9PhHc7EN0uxght8+V8unP+yfuLYuHVVu4AW/fL9GZmLIO+y6X55E/F Qjef8CPbRKfrhpELFjoWREfOGE+Bnvc809Ix+FEdgjVMpb52kMJh5lk4sAxqMxb1mZ4R t8SUuI1GwRJbrL/IKItVhd9QK3Bt692TxErnzVMu1LHGHbuIaZDOQ/9Vqtk8GvYY0UfH JADWpW90vU/0+Vusctyq2dCAIvCNQCRXP758gvG8QyX0eAlZ0KxW8gVZ4T0/BFBJwweY vnZ0F3eMONS6/eD1Y8SSgCfChzNgsfeFnxsuMQYtqE5V6DiWDcZloocgpDc3MSHAK9QA FUqw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="ZtLJz/W4"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-85740-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85740-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id f15-20020a1709067f8f00b00a3e8be9c95fsi2083917ejr.642.2024.02.28.13.46.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 13:46:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85740-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="ZtLJz/W4"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-85740-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85740-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 151841F265FE for ; Wed, 28 Feb 2024 21:46:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B35A971EAD; Wed, 28 Feb 2024 21:46:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ZtLJz/W4" Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA6827003E for ; Wed, 28 Feb 2024 21:46:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709156773; cv=none; b=h3i2pGOmdq+JcUHv8yPIQCpbQxVOJP7sVJ6/Z9bRj4HMVgypgBd8g9EZJCXHj1eElZAtZGtX94PckzO2N60y95MLGqPUE4ywFTL4q+skjQ1Wh5AZDtPErHuJf8n9mEGIgnTml1ndhzQrY1/PuuVSz80Uyg4rjIJWRe7tcm4j7ck= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709156773; c=relaxed/simple; bh=fcDVrwhc94lvkdvuNxLBpqQuiN5giB/sSTSBYGUgGIc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=T7BH1VeYxgPfg3SPZE3alGTsluqYq6kA4+/brabsyTHob2sH3BTjknBLWrislnSucmSsx0AvjbsGdDS4JSN1PHnFWzC03Nv80UZTbh1hNR9xN+AlWBg+nHQ99GUS26uLgZ9M6SVfDQOj/+UNC9NWaWiRLzMW0qlD4MyptTqvjhY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=ZtLJz/W4; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1dc1e7c0e29so2810535ad.1 for ; Wed, 28 Feb 2024 13:46:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709156771; x=1709761571; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=YdpR7cAKDSC45Ra+lrCp3FSf1aPEjcGJZRBQhc0h7f4=; b=ZtLJz/W4FVvtDRebNbxjo9KAbyrivuwW7+Y8BDkVKdrGwX6LFTJ541BhPJLPafiylL L1pr8exLx7rY4kcmGR46OVXlksD5v1A+XiM1EMekDxwic601QGOOpe9j/cXgjsOoXyoy eX4Xe+VYEYoDAYDRSvSJtR2m0DxJiFFjHoCIw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709156771; x=1709761571; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=YdpR7cAKDSC45Ra+lrCp3FSf1aPEjcGJZRBQhc0h7f4=; b=peBvGlutxs87KSlCtnPcZKH1HncyF8mrOagPIjfqbX6npRzbC92pAWHnDysQ1IkFpT X1aymQjnWfjjwFcQly61HZjXX2sBFnBOTnqpi0BiUt7WFCs0uXK7NjVLwMS6B1VjETJh vaIJy7sXbEUgf9PIGDYWLAjAL0Ikh01UsSjaC3mR+l6wTLEti5oDq/Xnj7y8IDiSORYs cqQi3CNhfCH6D+eA08FrI99njgFSnHvRUoD+eSn4pnYZkQik/JzvPXirwbob15RFfnKx KusX5oZHC6++OmkLSXmP0oMTopEuaj/mtnUyfC62xF4ulJyuHYGMtLcYBnGVM9NRm07x M/2g== X-Forwarded-Encrypted: i=1; AJvYcCVywQ3xE2RLwRx3sNmEUo4hXiuf0mWhDZj8VBXGOJcL8ouqZYGCUn1+3bnMtlBYolyuAYcF1MMKloUhQ/AJiv3dnUM7heIrq1KCGtPn X-Gm-Message-State: AOJu0Yx/tpUuimuXkTOAMxnyx66uUW4IdoOw3XM+6YMvgvT2F+ueHg2A KQl6H9FtKzR4FmScarWzyKbsVS0x6Yu0KS46fxxmPILVK7KhwWSHAHeSMX2gXQ== X-Received: by 2002:a17:902:ec81:b0:1dc:696d:6bb0 with SMTP id x1-20020a170902ec8100b001dc696d6bb0mr246471plg.6.1709156771102; Wed, 28 Feb 2024 13:46:11 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id je6-20020a170903264600b001dc23e877bfsm3733240plb.268.2024.02.28.13.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 13:46:10 -0800 (PST) Date: Wed, 28 Feb 2024 13:46:10 -0800 From: Kees Cook To: Andy Shevchenko Cc: Vinod Koul , Linus Walleij , Jonathan Cameron , Mark Brown , 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, Jonathan Cameron , Lars-Peter Clausen , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Gustavo A. R. Silva" Subject: Re: [PATCH v4 7/8] net-device: Use new helpers from overflow.h in netdevice APIs Message-ID: <202402281341.AC67EB6E35@keescook> References: <20240228204919.3680786-1-andriy.shevchenko@linux.intel.com> <20240228204919.3680786-8-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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240228204919.3680786-8-andriy.shevchenko@linux.intel.com> On Wed, Feb 28, 2024 at 10:41:37PM +0200, Andy Shevchenko wrote: > We have two new helpers struct_size_with_data() and struct_data_pointer() > that we can utilize in alloc_netdev_mqs() and netdev_priv(). Do it so. > > Signed-off-by: Andy Shevchenko > --- > include/linux/netdevice.h | 3 ++- > net/core/dev.c | 10 +++++----- > 2 files changed, 7 insertions(+), 6 deletions(-) > > diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h > index c41019f34179..d046dca18854 100644 > --- a/include/linux/netdevice.h > +++ b/include/linux/netdevice.h > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -2668,7 +2669,7 @@ void dev_net_set(struct net_device *dev, struct net *net) > */ > static inline void *netdev_priv(const struct net_device *dev) > { > - return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN); > + return struct_data_pointer(dev, NETDEV_ALIGN); > } I really don't like hiding these trailing allocations from the compiler. Why can't something like this be done (totally untested): diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 118c40258d07..dae6df4fb177 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -2475,6 +2475,8 @@ struct net_device { /** @page_pools: page pools created for this netdevice */ struct hlist_head page_pools; #endif + u32 priv_size; + u8 priv_data[] __counted_by(priv_size) __aligned(NETDEV_ALIGN); }; #define to_net_dev(d) container_of(d, struct net_device, dev) @@ -2665,7 +2667,7 @@ void dev_net_set(struct net_device *dev, struct net *net) */ static inline void *netdev_priv(const struct net_device *dev) { - return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN); + return dev->priv_data; } /* Set the sysfs physical device reference for the network logical device diff --git a/net/core/dev.c b/net/core/dev.c index cb2dab0feee0..afaaa3224656 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10814,18 +10814,14 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, return NULL; } - alloc_size = sizeof(struct net_device); - if (sizeof_priv) { - /* ensure 32-byte alignment of private area */ - alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); - alloc_size += sizeof_priv; - } + alloc_size = struct_size(p, priv_data, sizeof_priv); /* ensure 32-byte alignment of whole construct */ alloc_size += NETDEV_ALIGN - 1; p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); if (!p) return NULL; + p->priv_size = sizeof_priv; dev = PTR_ALIGN(p, NETDEV_ALIGN); dev->padded = (char *)dev - (char *)p; -- Kees Cook