From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 7949C2E642 for ; Mon, 18 Mar 2024 23:40:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710805234; cv=none; b=hXwmZjwscBtaybh/QGUZhTCBwXZ6d7SJbV8bb/vyjWBlqHWPEtvLY97ZZtoiHRhgv5yPzDbGp2JIBon4e/OpUq7TjzMv755AiWE9LK/0ZS7XQleem1VHjPGUWmoqTyipqgLbu7/knDDtAhV6q5v8zW0cfRihYW+D9Sj83nZyJiI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710805234; c=relaxed/simple; bh=Yijpf6hdcX9Zz+at/Pl05PNPDv5yz2KSyc2hARlGVyA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BS1WJA0FL24wNK9aYcCpGTw7K2hoCvIzurLpFNC/IFu18PwYGBbBUn7fTEhj1EUOgLFF1d/QpvUrCqBq8pafG7XtGOemhwlhvweDOOCdGLQLTrS3oxEwz5m0kDQacRIHugXL5Rs9rMtkmJzrRYfvdfB4iTCTYiADUWZ3V7H60nA= 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=LCWzjlg+; arc=none smtp.client-ip=209.85.214.177 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="LCWzjlg+" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1dddb160a37so35732495ad.2 for ; Mon, 18 Mar 2024 16:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1710805232; x=1711410032; 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=qGVjXx+j07fAtzWyhzuz39ABH1ricioExB9tBdSnz3U=; b=LCWzjlg+zokALRFDsK+PmKtA9cJKjFrbsPh5Q76Wk+vIfEd3TCLdznZ4ulGLVJRVke IVY0QTj71Q4/89HFogbjEJVNrVwm5+1/7u0ilJJD/KTFAu0ytxpDmJmhu1PyfZFl39mM 0OOH+UAkCncaOGAYAPcnA11gKT0DIOXlP/36U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710805232; x=1711410032; 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=qGVjXx+j07fAtzWyhzuz39ABH1ricioExB9tBdSnz3U=; b=kV8Luz9/Ud/6fmKDhacj1K0MZ7f47Dh6e42O86JMiPjO63jQzJnCuqHfduim3u/mmW uvOUjItBPQqMP1QHMVhheNi+oSJBMGq+lGbFseZdTcPCcSg3MYLprpDl5zI/jgwOFZcV S+W5eGW/cWZDnIr+RXIOpyds6AqCS1wYMimEUS8m/ua0qZWHENK9p81GZpZnvOZbqgmm d8eIxGldqG563+b7RkI8T5U+c2EPfMEQSrVfZLSlxY9HyucBRsWf9CErseBjS5IxXEbZ alAkxV03AkAvm45J85ukYv+XTxmwGxYWLyKC6qZVYpJ2oqwKjnxCh2BLH/cpVSLg0vnq j4GA== X-Forwarded-Encrypted: i=1; AJvYcCUh1K73zwhg/3BNdk60MhWLmK+pVb4yZA0Ko/EBlEtLasPMsSnxzotPUY2b+F3cf4pZ8abF2XlrLiM0Ka6Bf47gb0R5i8DALqlPl2ElCvHOrQ== X-Gm-Message-State: AOJu0Yx1ymWIHADMDvWgjJ/3PLjU5xm2ArXr7H1fmrIN90XrBQ/COabv 3ccQkdpN1414mIC+RwRqAlPKmjcItpDeVWW5SEmh92LGLEX4qHjVYTvIJ4rmIA== X-Google-Smtp-Source: AGHT+IE6ZJkwZUoHdQ1XKk7dfD4fL/GhYshEq7O4lP3zklm4arTM8pnOn3Dx/fJ9+Pp6EnYJuQnZ2g== X-Received: by 2002:a17:902:b68e:b0:1e0:f8:17ca with SMTP id c14-20020a170902b68e00b001e000f817camr1076507pls.39.1710805231878; Mon, 18 Mar 2024 16:40:31 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id s7-20020a170902a50700b001e0287592casm2566226plq.295.2024.03.18.16.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 16:40:31 -0700 (PDT) Date: Mon, 18 Mar 2024 16:40:30 -0700 From: Kees Cook To: Erick Archer Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , "Gustavo A. R. Silva" , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] perf/x86/rapl: Prefer struct_size over open coded arithmetic Message-ID: <202403181635.46CBABD994@keescook> References: <20240317164442.6729-1-erick.archer@gmx.com> Precedence: bulk X-Mailing-List: linux-perf-users@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: <20240317164442.6729-1-erick.archer@gmx.com> On Sun, Mar 17, 2024 at 05:44:42PM +0100, Erick Archer wrote: > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. > > As the "rapl_pmus" variable is a pointer to "struct rapl_pmus" and > this structure ends in a flexible array: > > struct rapl_pmus { > [...] > struct rapl_pmu *pmus[] __counted_by(maxdie); > }; > > the preferred way in the kernel is to use the struct_size() helper to > do the arithmetic instead of the calculation "size + count * size" in > the kzalloc() function. > > This way, the code is more readable and safer. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] > Link: https://github.com/KSPP/linux/issues/160 [2] > Signed-off-by: Erick Archer Thanks! Reviewed-by: Kees Cook I was inspired to come up with a Coccinelle script to find this pattern. This seems to do it, though it also removes the blank line. I'm not sure how to stop it from doing that. I'm running this treewide to see if I can find others... // Options: --no-includes --include-headers @allocation@ type SIZE_TYPE; identifier SIZE; type PTR_TYPE; PTR_TYPE *PTR; identifier ALLOC =~ "kmalloc|kzalloc|kmalloc_node|kzalloc_node|vmalloc|vzalloc|kvmalloc|kvzalloc"; @@ SIZE_TYPE SIZE; ... PTR = ALLOC(..., SIZE, ...) @structure@ type allocation.PTR_TYPE; type FLEX_TYPE; identifier FLEX; @@ PTR_TYPE { ... FLEX_TYPE FLEX[]; ... }; @single_shot_sizing@ type allocation.SIZE_TYPE; identifier allocation.SIZE; type allocation.PTR_TYPE; PTR_TYPE *allocation.PTR; identifier allocation.ALLOC; type structure.FLEX_TYPE; identifier structure.FLEX; expression COUNT; @@ - SIZE_TYPE SIZE; ... when != SIZE - SIZE = (\(sizeof(*PTR)\|sizeof(PTR_TYPE)\) + ((COUNT) * \(sizeof(*PTR->FLEX)\|sizeof(PTR->FLEX[0])\|sizeof(FLEX_TYPE)\))); ... when != SIZE PTR = ALLOC(..., - SIZE + struct_size(PTR, FLEX, COUNT) , ...) ... when != SIZE @reused_sizing@ type allocation.SIZE_TYPE; identifier allocation.SIZE; type allocation.PTR_TYPE; PTR_TYPE *allocation.PTR; identifier allocation.ALLOC; type structure.FLEX_TYPE; identifier structure.FLEX; expression COUNT; @@ SIZE_TYPE SIZE; ... SIZE = - (\(sizeof(*PTR)\|sizeof(PTR_TYPE)\) + ((COUNT) * \(sizeof(*PTR->FLEX)\|sizeof(PTR->FLEX[0])\|sizeof(FLEX_TYPE)\))) + struct_size(PTR, FLEX, COUNT) ; ... when != SIZE PTR = ALLOC(..., SIZE , ...) -- Kees Cook