From patchwork Sat Jun 1 16:56:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13682526 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2101.outbound.protection.outlook.com [40.92.89.101]) (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 76FDE153BF3; Sat, 1 Jun 2024 16:56:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.101 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717261014; cv=fail; b=bTun9TpzWq2sGYGKv93hiyYxkWmf2XKaSnyH3sRZtpd5sVof/KC8EVaYi4PJOvl6QFyKV6/ZyQIpQMQsfwqUWRBv3aIHlEYFsZjuNk5415swCS6I5JD37S3t4PL5sfrXycUsycGAQk1K0s/Qz+atZdnYPoi9/DNLrmG4+CmtVqI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717261014; c=relaxed/simple; bh=h7kkD7qwDYExse+DmBsFiFlScC1HUR2YMmPYX4hZypE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=rSGaJxWi8qnsALTG6re6fJmYd0CzV0sLIr/bOe3+OfHum7jSHqz1Fmb/choSxiuKxI/XmEvv2wRzItYcydL1u5CY3YslW0RnnQvHzidh02Vncu9ArfsWvrfhsUajHLjlzHFLw9aNw8DdorBZOu+EI9O1SXnOTKkLiTsKbZRbUGA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=es3c5fym; arc=fail smtp.client-ip=40.92.89.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="es3c5fym" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CyttXt64af9sRzXwQmVAroePGnOL5Zb3IhwL0yoX4LIT1lyh+5b03OUE6+VTO2JkI7YTC5sPMdLBa7r40VQwVEWO6bc3Fkh8FOUISJwnwYL95wnkNfOSAMI07DeGG0dCBTd8T/Nd8NVUQFrxPs7lRuPvUaQcrq8DQJ1CvirW4hzVBpZDdUzSy370eaSTS4xSfEa05Ei0ykDSBKWZoH71v7oE109vSTSruPxWl1nGZAAFJ7PbNSr2S/t5MAnqwDiIoTSkF1TpKrfEdfmGMii9pPu97CAZ9IHujQDXkH7Hpb4xUNzbPLa1/GuQO7mnLKuqr/V5woSCSL5LKTqrlytDBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cdB4GW5zXXUBcRZ7W70LZgkxQ7agBowrJVl9zTX93Bo=; b=Sxja4aPgAAo3qer7F1w7BI4YzNTYaRxsA4KchEzsDoVKnC/zXHK3ENVZnzdBvScq2y2YwNLWUayR4HmDyeoEjKumQkKGZniyJ/h2OpHgvbb+cFSTRffASMeyCevtj0NHGquX7iqhPrG6dwAl3f+dOyTR0zIX/rh+i5uaKA3LrMiB0R3RMGZOVCvh9qLmw1s2EJ/oa1wLl+RbC80KimUNRfjsPfHAHzEDebbhIYu4qQ5jl7EESaj0k5nzxf3zUazSgpYfLN0doKw27SmKhF5MZzByz+uQ80RZKXqp8yc9E1lQgMPU1ZPXrp8R8pv01UhQjr5xkkNZqrChxFet9FflEw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cdB4GW5zXXUBcRZ7W70LZgkxQ7agBowrJVl9zTX93Bo=; b=es3c5fymx+5LZy+2VgzNjcTPfgiRS7Bbj8TPM7XzH4XVUjyWJ/YGa21/LFleueP1rUJeLwrxpZ6JlylnZVxc0KK286RyM/hNRq+GOPanB6t0x3YRJsWRnnOr+odqnE+2Av3xSpjU6a+Um3G3L7w0nuFWOeUdi62xQk7bU+Pg3VWD5Po2p+bR+iJa8uUjdgKQb0A5/spZeb6OkPJF7lKABVbU9VNvgylDgMrF2+ZLB4n3MR5lRaMpMki4eJ13Y2aM4JoIuLOskPjOtXh3K5SlioKpve4XpvSfp64qGUL96aQ//NnEX5n2mL9Smcws5MiLFQXRjfMV3nbIac6i2CDJXg== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by GV2PR02MB9373.eurprd02.prod.outlook.com (2603:10a6:150:d2::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Sat, 1 Jun 2024 16:56:48 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658%5]) with mapi id 15.20.7633.021; Sat, 1 Jun 2024 16:56:48 +0000 From: Erick Archer To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Christophe JAILLET , Matthew Wilcox Cc: Erick Archer , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v4 1/3] perf/x86/amd/uncore: Add flex array to struct amd_uncore_ctx Date: Sat, 1 Jun 2024 18:56:16 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240601165618.10020-1-erick.archer@outlook.com> References: <20240601165618.10020-1-erick.archer@outlook.com> X-TMN: [i94Mbi7ElxHnp5a41LsmNLJifsZHa14I] X-ClientProxiedBy: MA2P292CA0022.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::11) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240601165618.10020-2-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|GV2PR02MB9373:EE_ X-MS-Office365-Filtering-Correlation-Id: b0eabea1-9349-4c30-4724-08dc825bd371 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199019|3412199016|440099019|1602099003|1710799017; X-Microsoft-Antispam-Message-Info: G3rmGR/fozQ+WJVS+RM+1dfhwgnuPDhZxK5Bhizaca91ZCGv/gbajd6P0lAKFVcEf0jCLaudbhEyYXNxzZZwtBY7X/Cr0Sy8LWp7ntDZ0GBEfPmLGvjqRDUVNC9vv5ED7tVtHGLeZEwZFUeL4VqWWDJBTplCZlB45WO4veF6du9jSehAj5SI/RY9OQax+SG1VLhFkq/biIXTz9+fgbEd44BDgVuG/JtE+NGUGe2Zwia2/RtX18tiKGEcbp7vDRb4F8PIJgRrBZc0xGu/Hz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ssJ1FYZTQspLDnPO4W8yF6OvL/yfeKMoDCH/oEVfUqZybGY8db4xPG9XGH581qFJkeIa5tBU2jet7j8NW6JkdH1Z9i2tCEBLWbKyM81xUNQk2hHYNFlTbIk9bjAi3TnpPG6hXm+aO71jmW360bIw8URaOCJqFSy6fi1uU+BqMksj5DvvNMPoP2rYpxPXz5Q2ZiODHSbpszQ9W9ghE/pqgxe4AW5+1XGanV+ZtD5chMk/b23bdBmqFwMGaTSpw2DTia3ka8P7kcDkZRx9wyyImASPLhH6PKExkLxU+yu49PZtYMMhzH3KrPZDkuz7+RH/Cs19LjtCXBfylARoRkqN1/VEn3uYisaJ0+FwqkhFNrViXzFz6i0Xgez8EoXUN6p1VFDidBKll1kLs9v7ivRV23N2eogLuxRuYOG076aSUt2ChivB3eErbE+efOlxQ3lOSBDZ15oFHE9pUF5yS+spNgRK5LtMwBOJKfLXZ74gukiEyNmtB6v9OI7sYMhzd6AwQYBARKnwRcvUuv4r/LEBJTY04Pi6aqYbRucVSWyQjqgEjUKFAvHb1gXuNLHwSaOiOUPp/AuCqG0anMasoqQgQyjoG3zonSDwUd0D67TVlowVzgQZfekwBeST+bVYHp1x7FxeEs9aBUoMxiGI9qeeqDOK2KIO7xbEP9PtaBFDRSupZEfXcYfVJuxMaPVmzA5p1bCMC0SxC0sOW X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b0eabea1-9349-4c30-4724-08dc825bd371 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jun 2024 16:56:48.2130 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR02MB9373 This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. The "struct amd_uncore_ctx" can be refactored to use a flex array for the "events" member. This way, the allocation/freeing of the memory can be simplified. Specifically, as the "curr" variable is a pointer to the amd_uncore_ctx structure and it now ends up in a flexible array: struct amd_uncore_ctx { [...] struct perf_event *events[]; }; the two-step allocation can be simplifief by using just one kzalloc_node function and the struct_size() helper to do the arithmetic calculation for the memory to be allocated. 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] Suggested-by: Christophe JAILLET Reviewed-by: Kees Cook Signed-off-by: Erick Archer --- arch/x86/events/amd/uncore.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c index 0fafe233bba4..9d43048bfdab 100644 --- a/arch/x86/events/amd/uncore.c +++ b/arch/x86/events/amd/uncore.c @@ -37,8 +37,8 @@ static int pmu_version; struct amd_uncore_ctx { int refcnt; int cpu; - struct perf_event **events; struct hlist_node node; + struct perf_event *events[]; }; struct amd_uncore_pmu { @@ -430,10 +430,8 @@ static void amd_uncore_ctx_free(struct amd_uncore *uncore, unsigned int cpu) if (cpu == ctx->cpu) cpumask_clear_cpu(cpu, &pmu->active_mask); - if (!--ctx->refcnt) { - kfree(ctx->events); + if (!--ctx->refcnt) kfree(ctx); - } *per_cpu_ptr(pmu->ctx, cpu) = NULL; } @@ -478,19 +476,13 @@ static int amd_uncore_ctx_init(struct amd_uncore *uncore, unsigned int cpu) /* Allocate context if sibling does not exist */ if (!curr) { node = cpu_to_node(cpu); - curr = kzalloc_node(sizeof(*curr), GFP_KERNEL, node); + curr = kzalloc_node(struct_size(curr, events, + pmu->num_counters), + GFP_KERNEL, node); if (!curr) goto fail; curr->cpu = cpu; - curr->events = kzalloc_node(sizeof(*curr->events) * - pmu->num_counters, - GFP_KERNEL, node); - if (!curr->events) { - kfree(curr); - goto fail; - } - cpumask_set_cpu(cpu, &pmu->active_mask); } From patchwork Sat Jun 1 16:56:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13682527 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2101.outbound.protection.outlook.com [40.92.89.101]) (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 8CF86154431; Sat, 1 Jun 2024 16:56:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.101 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717261016; cv=fail; b=elloCiq3aLWQAFvskhdCySsIsWB+L1srJoBjsGe5WXXWj/hIpeM8D0d0ajW/YgiaoiQsNQpX/3nFn3ZieKJu/rCdtU0w9m4UjvgFC+fx4IAydTpIX7aHj1yLzeFnl3qIInuc0XWS8Hv4BpKT0aKABra4u43aRHpHYlxd4S8u18E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717261016; c=relaxed/simple; bh=aO8MR/KDAQKBHp0hJAX551MaLlnFDSkAq10aMt5WLis=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=J5zgZ+YZz8ggBnJ7reDEK79PRVJQH5e5yeKQWp58tJPmgU9Wb6e+OZNo5p2LPvBJoNxRhBnWsZZuDfuJYu5EjdafnXTi3oGEe0KuJ1pIq6nz3g1jbLNGHPZ+1XYhkqb/rb8naL//oawQ85qtesquO6me4LS/rMkzLZlCjMSp3Tc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=pxQOthYf; arc=fail smtp.client-ip=40.92.89.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="pxQOthYf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JHNy60TU0WKxkmTjQiDJdlxYdSKb2o3Ga/nozbkVBLePjTWTg0SOTXYU5f5HdHZI3NkeJqj1VbA9n9beFGTFjuhRzpQLbj9VC2yhNAtF4rKCep8TeRW6K2/w5E0B1wkGNH+jF4EOa0OD+CMrBYUfhpZK1peJm5vh4LMVAxFmmrTxgg5ekD+1aIirqDTUisNJB8sgKv/zeRHOtOvz6tizPXZ54AkFJ/dl3iOgK91Sj05k2UoKtDocwBBhBZXGmFAqAgpXDuK6V5lb6lxfbvqAW3NBS6ecsq8zHewjiTfgmiRG9AjzkPYL48h+tXVnMdv6ZCp+7njUzipTQekg6nvtug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ooalRmn3AeJ2A0MZTXg18zbF4oZYzV7yZ46QzQOYVgc=; b=OH+CsEWzaTjmW9vWpxE73vBhks83Hm5NULsVOB1sk1e+xI7ckwIDvQ33xosBWZBQvH27emyRRp6FLlPd7zwf0tm4oRIuGTBNfEoATeP2cirRH8F77NtZ8AxeBRRYSOy8zND7DnzGh38z6eXEF0y7L11OyUjUufmu0e2axd50qcKTGW7MMUYroz5XtvawzIaxtcMxVnldLdnx/Za1/V+orPIb219DCu1daH6V0Z3N9uDJaMnrAA9by754ZH81IB07lg0AJtWXZaql73MUt/O/izPrTY62XQRmicb6LbXuSkBh581ru9PJCE55b3uWc5J4fEf4ocAPl/IUcp3nn9dP8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ooalRmn3AeJ2A0MZTXg18zbF4oZYzV7yZ46QzQOYVgc=; b=pxQOthYfL75n10sD2lWAAn+xD3mmMl8l7ZABVSa3//NqYg2bo+ADka2oZ1zL+o4LTi9AJkT36B3TTHfasuiNGFP5+QEOBEX5evThbCJ3wIMs2JTzSUhK2h7jfoGQSAIzg2B382tKfeUXCVNhaGyt4BRqdg4YeP2Vb4MYEuQh5Z5tnucWnhYzbSM9lxiaDGHg/3FjpTX3i8jS2EuA7qD8vTtuny5AhWGbQIbkkjW0WdiyAOzKJPdKveNWEdP7TUD9qGNyT9rchtKaZU9txzwqLP4oMQrB98KxzOOwKJM6QkHgvoqIQvmAxhgf4Yr2g7T5g6ga0WMphXZsY31Ws2gajA== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by GV2PR02MB9373.eurprd02.prod.outlook.com (2603:10a6:150:d2::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Sat, 1 Jun 2024 16:56:49 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658%5]) with mapi id 15.20.7633.021; Sat, 1 Jun 2024 16:56:49 +0000 From: Erick Archer To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Christophe JAILLET , Matthew Wilcox Cc: Erick Archer , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v4 2/3] perf/x86/intel/uncore: Prefer struct_size over open coded arithmetic Date: Sat, 1 Jun 2024 18:56:17 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240601165618.10020-1-erick.archer@outlook.com> References: <20240601165618.10020-1-erick.archer@outlook.com> X-TMN: [ev/iZ1o+Q9rUBbIisNPY6HmHQ1mxNtmm] X-ClientProxiedBy: MA2P292CA0022.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::11) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240601165618.10020-3-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|GV2PR02MB9373:EE_ X-MS-Office365-Filtering-Correlation-Id: 06e3cf8f-f815-4923-d50d-08dc825bd450 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199019|3412199016|440099019|1602099003|1710799017; X-Microsoft-Antispam-Message-Info: YuYgWqoh+Seb/cgJb4cNATq3of1/0TqV2kAuRO9e3o6BgzAgNhC768tgSO2s99/OMr1B/SDcc2kdXyTd2/m9TYCSijShDJEKkTB9BYZbY7NO+R445Mi/IqiQl5gGLQ6IqNvVO9dCEc1P0To1YphL3APKGV9F40d4tv59+4SiaojVs35kGm7tJeJFGDd/XfQv7bKnxusJ8/6CFY8WcK1q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PU2XdjOrS6KMOLnz3oBtYrP3g/Ei75sp9fYzccTFJi6AZ5mv/EvT+g0dk6z2dyZ5R8rtNm9ypimm5enIm9tROjl5gKXo0EqeElrbjRo7KUMSSUYrBrNhUp0fQOg3UQ0QT90Ew8bO0bZJrc5RQ3VIZNcNasMj/JYP8exztEGc0l3epIO462VWyQS06yXc7QEgLirxCDT+ckOVO33XmnCphAQ8rDh3T8xxE0O/j245r3ojvtusOCW3s84oxKV1SdPzrYOr4Rg2r0xJq+ThRla+d/8lX1nJL1yu9suR8oBHUYIq5UH66yqBzeC4qr1jUWSOgchpgjTicpg+/QEwcjBwnj1gLlpnBos7RdPvA/OFBVYCYeFi0taQ8gszXyGBMDrjaaKm3jFwdh2c+C6E5u3yr4NtNdh1jLqS1Y/ksioNW9fZxe4T21psZE/Ux/hv1FwIJEfzudJ/Q9fuGhxjQw7qGDPQvww27EJKnsL+XzsBfHjNP1fcDF9ZIsMzV59u6BYhvWbRaMBgj2QV5PmusUIPyc98LSOE+uotTI1m/8uhwRG6w9Qsads/xjW+Nd9W3ugevX+hn1RJBdPr8hP2bVTHzSjOKtGXwGmKZZQ7gbGdUMwj2LH5veLJChhi6tbcHJfm4qNuGDUyORhVDKGNlBY6mAxaPP2pRmu/8AZtYH6PYWo9IkrWaT/H00Yok0lmPk2n595ex+B8f8lFB3tj2vCcsQDET3fiwUFyIeH5Pz9rp/EOX66XWGYJ6pMK7DJfHCHFeF X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 06e3cf8f-f815-4923-d50d-08dc825bd450 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jun 2024 16:56:49.6741 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR02MB9373 This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "box" variable is a pointer to "struct intel_uncore_box" and this structure ends in a flexible array: struct intel_uncore_box { [...] struct intel_uncore_extra_reg shared_regs[]; }; 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_node() function. This way, the code is more readable and safer. This code was detected with the help of Coccinelle, and audited and modified manually. 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] Reviewed-by: Kees Cook Signed-off-by: Erick Archer --- arch/x86/events/intel/uncore.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c index 419c517b8594..b0d518d752aa 100644 --- a/arch/x86/events/intel/uncore.c +++ b/arch/x86/events/intel/uncore.c @@ -350,12 +350,11 @@ static void uncore_pmu_init_hrtimer(struct intel_uncore_box *box) static struct intel_uncore_box *uncore_alloc_box(struct intel_uncore_type *type, int node) { - int i, size, numshared = type->num_shared_regs ; + int i, numshared = type->num_shared_regs; struct intel_uncore_box *box; - size = sizeof(*box) + numshared * sizeof(struct intel_uncore_extra_reg); - - box = kzalloc_node(size, GFP_KERNEL, node); + box = kzalloc_node(struct_size(box, shared_regs, numshared), GFP_KERNEL, + node); if (!box) return NULL; From patchwork Sat Jun 1 16:56:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13682528 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2101.outbound.protection.outlook.com [40.92.89.101]) (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 7CD5A154455; Sat, 1 Jun 2024 16:56:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.101 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717261018; cv=fail; b=K7vYrO43gvGab1FfHjEHzjVjS1Gi3iNh5IDh2wrOLg9FNCvi/Bs+JrhIgWuMBXO5xlvNXnM+UdYijf9FDR1ClfRyPWpMAO8GsHuoC9IosvfzXsR7V95hZ3INzkZMbdBM3FF+Vvkv1Js8knUegnAC+CJaHpn40FY+9B/9ZPfoaZY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717261018; c=relaxed/simple; bh=gOtst8h9RuljVsy21oj8WcjbC8wfPg/lGHLpU3534lQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Zy43gxvq/mYQMN97/NRBypzuddrqhmo3OFzOkqI/9qNoxI64zHNosm67pGjgZ4RMATww3unX9ffz6TKcE+LLUvJ1A2/hERZdL9b4dmwuDFONrtXssC88df/NTNRQ6m4Y7E6sfna5TKnTJ5BCU3mrNLYP4sqHXwrqUqK2ia38Ah4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=lg0W9sSd; arc=fail smtp.client-ip=40.92.89.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="lg0W9sSd" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NczhW1JIaHQQ1n9RydYpOEcf60530rnMMovH80nQyPGhz7jqadKiv7JSdf+wjRjZ888inz0k05XFW4UptK4yOrLDAbqNsu6Cxf+857uyKgTsS8QuBK1UK1LHvvzJKn2DkDVSBZ4WPgLDLi8S9AZ4SAXqJLMevoAUaPSBeV5jsr/ziNNVA9ei+xR2AMW2psioIeDLn+BZMbSvCNnvRG5pvw62zYRFROKglJzQ0eEujYlPXk4DkcbFL7h5zJdogzk+zXEQjRT7juGrhDrobMSYuQdF7AIzqJdmsB5P8XbAVRxjslM4mEv80srg2X2kb9aa9H29u/qwmehsc8f143aevQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=o0LRofeL7sn2e6PoBCwIy2JEjQsAf4pdnyi3QAUeokU=; b=F0LJ+UyZcRdUtDVMy5PBzNpIB2j6ww2dRDCH0aM6PWG+/lkwUTDHq2WEZJJAEfzDSwOBbWIUORN6ulB8ruAgh2d9UsDn4j0PLzQ59/dmVoWrtmfxhqanpdTEaUax5av1bifGHSLak75I94dlJzjNGXVWvEmT9yo4Tttvr2fe18rQ6dbFy9ie8oBOYYtOg5qbWlMEkC3cdQxH7wdRNew7x67fbXXUqH3J/aLdui6Ejo5iavGVcDRy7a2xusgNv2HmDskbidA7BDo/yuFHqc3GPrT0h4UQ9qV7mfxNA0/l6Yphd1Zugqhx2jw/qAKKhr0hAHw/0Re2WTmFPZIdVQEQ4w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=o0LRofeL7sn2e6PoBCwIy2JEjQsAf4pdnyi3QAUeokU=; b=lg0W9sSdxc/Tq3VPh1+gpWNV5HWYKjl6Qyu4J1FosrmgcJxVN1CWK7Q1rvSzLY0gfEfn24pea5Fn+bC8QZGt+ac/ZE4TDf2bzd4y9ooA1KVHERIu3VkBt3+HlNvisTMqd8NsjPOhIXovICuIiVF9soKGOBzLL1tcQWNFPr8mmPtFRyda2suWevrry5z15E727VhtQG2XSHQRJcQad95q7yJN8NlOW1rmhrHXwDaxneDX3LJZTkIHn2YRp1SA9K5R5TeNVxBe1XFPzrQ5e2QpnFjzq8cYrDjKIoFJJPO2KjdRdXp0aM6NXzwr0/C/DAhUDZW/uyfekMiBFUkgoNFgSA== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by GV2PR02MB9373.eurprd02.prod.outlook.com (2603:10a6:150:d2::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Sat, 1 Jun 2024 16:56:51 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658%5]) with mapi id 15.20.7633.021; Sat, 1 Jun 2024 16:56:51 +0000 From: Erick Archer To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Christophe JAILLET , Matthew Wilcox Cc: Erick Archer , x86@kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v4 3/3] perf/ring_buffer: Prefer struct_size over open coded arithmetic Date: Sat, 1 Jun 2024 18:56:18 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240601165618.10020-1-erick.archer@outlook.com> References: <20240601165618.10020-1-erick.archer@outlook.com> X-TMN: [Cqac6udP/mVsrAgL9nOq8iGXfFGVlXqx] X-ClientProxiedBy: MA2P292CA0022.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::11) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240601165618.10020-4-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|GV2PR02MB9373:EE_ X-MS-Office365-Filtering-Correlation-Id: e3f81c36-2bec-4744-8d4d-08dc825bd52a X-Microsoft-Antispam: BCL:0;ARA:14566002|461199019|3412199016|440099019|1602099003|1710799017; X-Microsoft-Antispam-Message-Info: npApg7Iy88JN6VNfIRSeDn9zCfGdPflyt+yleOZjxP/GqbVa/GZPXjgD9886TpRUa4Xj/T1ELIfaCTpOlLc7/iRGlpOOgdpGN+5mfDCC32AlRybvyTf2OQCR2cWcd91U+T0/iFY8eVl8BRptWtzeW/AFckfuzf/5RXvWet4JYTGAoDucFSfvA4/JvBN4HWrigyklBcAnbkBUSziKu/h6a/63hoa7VFg20hvJ3EGd6khWp9/NFlv60LvMfC2J0Kcxo3LPh1AqlLn4KAIqAQn++QUPE4XQWfBez2IY+hpZHqh8ULovV1ai5/t9JiHKO432CZZ3wgxRHcaF4bpiLoyd6nO4UDRwCDfZNtYoaELDxKbdLWfTZiqOIX1TbWrvFv8AF/rbAXnWwH5rezOH+541/lCJeJOmBDaQVDPF4QS3eruE6XBiRzg2WGkhLnmO1hlOEvviR2tySaGJQ+IuZumJS4ZW2Pv9YbJ3+s4oq9eJEPsXhTm7T/0oksYQOHcqo/h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x9QIMygJ3JzbUQPT742tEN91h8/g0V6Ekz7IctDzmD5iKYLBjIiXs2IDOe1My8aJCcRItrJjY7cUr8EonZclQjTlHmftFlrxU+zykOXtAk5TA2sgrJc6zhiEyDwVXNbGgJs/rrWRNGO5v1IG2XVZ1d6dcaGStrbfVPsHEE9JEOq53wBJ54chit8jrKtedblu0QJYrIiwkTdbKy9CiMl7AaNQi04H4EisVQyRQcb19R860i62rZVK0oY4adQXqXOQu7Tl57hfmtb0w0IEVIGx3vk5JrmiPPsG36r4C4/h6p1gJiZWEtT8uMXfszguUDAxKwKjOoKXQjC6LWgoWegnNXeXYpzfPtBB8BcEYLW08A79PyxWdxMn4jSgJk7Lu6PkZWvs6yboLtinSeHB96yrVJpyFRJ8m2qsldHa5qT5b/cpgLcd2w15MJyG4N/o04Ub7now7UVMpeiSizDiXA88zoQpMSV/ X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e3f81c36-2bec-4744-8d4d-08dc825bd52a X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jun 2024 16:56:51.1191 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR02MB9373 This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "rb" variable is a pointer to "struct perf_buffer" and this structure ends in a flexible array: struct perf_buffer { [...] void *data_pages[]; }; 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_node() functions. In the "rb_alloc" function defined in the else branch of the macro #ifndef CONFIG_PERF_USE_VMALLOC the count in the struct_size helper is the literal "1" since only one pointer to void is allocated. Also, remove the "size" variable as it is no longer needed. At the same time, prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). In this case, it is important to note that the logic needs a little refactoring to ensure that the "nr_pages" member is initialized before the first access to the flex array. In one case, it is only necessary to move the "nr_pages" assignment before the array-writing loop while in the other case the access to the flex array needs to be moved inside the if branch and after the "nr_pages" assignment. This way, the code is more safer. This code was detected with the help of Coccinelle, and audited and modified manually. 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] Reviewed-by: Kees Cook Signed-off-by: Erick Archer --- kernel/events/internal.h | 2 +- kernel/events/ring_buffer.c | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/kernel/events/internal.h b/kernel/events/internal.h index 5150d5f84c03..dc8d39b01adb 100644 --- a/kernel/events/internal.h +++ b/kernel/events/internal.h @@ -55,7 +55,7 @@ struct perf_buffer { void *aux_priv; struct perf_event_mmap_page *user_page; - void *data_pages[]; + void *data_pages[] __counted_by(nr_pages); }; extern void rb_free(struct perf_buffer *rb); diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index 4013408ce012..d123fa2096cf 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -822,9 +822,7 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) unsigned long size; int i, node; - size = sizeof(struct perf_buffer); - size += nr_pages * sizeof(void *); - + size = struct_size(rb, data_pages, nr_pages); if (order_base_2(size) > PAGE_SHIFT+MAX_PAGE_ORDER) goto fail; @@ -833,6 +831,7 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) if (!rb) goto fail; + rb->nr_pages = nr_pages; rb->user_page = perf_mmap_alloc_page(cpu); if (!rb->user_page) goto fail_user_page; @@ -843,8 +842,6 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) goto fail_data_pages; } - rb->nr_pages = nr_pages; - ring_buffer_init(rb, watermark, flags); return rb; @@ -916,15 +913,11 @@ void rb_free(struct perf_buffer *rb) struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) { struct perf_buffer *rb; - unsigned long size; void *all_buf; int node; - size = sizeof(struct perf_buffer); - size += sizeof(void *); - node = (cpu == -1) ? cpu : cpu_to_node(cpu); - rb = kzalloc_node(size, GFP_KERNEL, node); + rb = kzalloc_node(struct_size(rb, data_pages, 1), GFP_KERNEL, node); if (!rb) goto fail; @@ -935,9 +928,9 @@ struct perf_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) goto fail_all_buf; rb->user_page = all_buf; - rb->data_pages[0] = all_buf + PAGE_SIZE; if (nr_pages) { rb->nr_pages = 1; + rb->data_pages[0] = all_buf + PAGE_SIZE; rb->page_order = ilog2(nr_pages); }