Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1496859rwb; Fri, 23 Sep 2022 13:37:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4vMypx9AA7VvnNWXPjZVPYfr/JkZoIb32ejQoa1MgipeidDhx9wEvboNbv8CiXQrA3RayX X-Received: by 2002:a05:6402:40d3:b0:451:5249:d516 with SMTP id z19-20020a05640240d300b004515249d516mr10218698edb.154.1663965449438; Fri, 23 Sep 2022 13:37:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663965449; cv=none; d=google.com; s=arc-20160816; b=X+zUSBAp4d3J5B27oglQQf81dTcmbWE9gm5pVwAjbjYVdTkn/yS/3LhPsVecue2GGu r0Go0F/jYquHsJHGOAxUIH4s2Y4ES7JCK22QVNQlEhx1t3Rdb1ZbCC7dgQFBR5Jf7lw7 zvzMH6g0V6MgNVbY0OkkpYftrOt6Lw8wGGkBx+en72Wwx7QCS7iBNY0ZDT3Pplnb6111 OLSYERbxS6VTR0aMP6bDzjQc2LRlvam0voIfvq/gxl8y2omiZEhLQewVUUxJ0rO8vMGL hv7/Fo+VlivT9n6mS0lV6NRvSm9LdAc3OMhg+ZkRZP+rz20fV7llSz0DsEZtbMukcUY0 +TZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9WHW9dcNJ7USo93XbepCTz52gEB8RJhZFTCAgOxjaWE=; b=oGgPNVL2bDR/muOCnB/pH/8+DynYy0hhiNx3TWu7wBbYadwiaeevjOviHTxrHjg5dS xFuPCPU6dVR7uq7v1nq16irsF8MLzXDO5TA26VFUCySFdDRY2tV2q5v6s56BWptp0Owc 9GrirQUmjHs+JYizb6VpJ2EPbcwEepQspDZnfPSh96aU85MrZM8VmRZeR/leY0ggMnxY TNfudjpVCq9X9dpeWqibRkIAiSO9vhDld0ZL8sxHIygf5ZQLGhUm3eP3tW8Kovk3Uioa WWyqkZCJ8Ryg4OMz7GM7ypOw8LBwjJsjVaaa+/gOE2kejfSiMSxyFdXtHsEAq0VzhpK5 VZwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MCrThrcp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ky18-20020a170907779200b0073da56756b8si6828969ejc.607.2022.09.23.13.36.59; Fri, 23 Sep 2022 13:37:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MCrThrcp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233201AbiIWUer (ORCPT + 99 others); Fri, 23 Sep 2022 16:34:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232723AbiIWUck (ORCPT ); Fri, 23 Sep 2022 16:32:40 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8209E14C040 for ; Fri, 23 Sep 2022 13:28:33 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id w13so1193590plp.1 for ; Fri, 23 Sep 2022 13:28:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=9WHW9dcNJ7USo93XbepCTz52gEB8RJhZFTCAgOxjaWE=; b=MCrThrcpHBXTtrPTMUDahiOxJ8D8ml0adnc2b/ZnUpLhKCWbzkicjK7DVQ0Out5WOD FREFmdsJO4H0m9nZW5aGp3kZr8NyuDEg+cjmHSd2PFaU5B93FD7J4xKRdTkOt4VgUbKn UMInxMGoGT3uNhBnpORcuWbRNV0aijKsC637c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=9WHW9dcNJ7USo93XbepCTz52gEB8RJhZFTCAgOxjaWE=; b=g0GVBUSiURBUGg1c50kxi8LKAhGEFbXpkfYbZvaELEUdI/KgtOJ6g3ptn1KyWtRtxB JJ7Uc8twplz4smzYWYCNqEThZVhOLut1rNe7XXpR2OIqbvmfxNLC6+8zVGzNXx3W0oCW nsXYnEwK81lsUX0GXRRVp7nnETuY7nUtxk/jjPvfhcfEvx9kBLjiXR4/M22ZfUXtsoTK Pc7rDbUyB9a4z8nEBZBHPsmGzABMI0/OGOfw8uEj2ZBvAWk5irsC0aOtYPnR97V3xhqf SGS04r3/b84MGFusMtFIfKvJFM+GGIBK3PErwjsTapODv2+wGzZKoB3atoyPP9OsNi1Y mc7Q== X-Gm-Message-State: ACrzQf3MIV4uOMAjpd2Rp8fyieZyc4WOFyaxkrB7COLkDdC1SLAalwZq mMMrGppDkpgd7YPnaRQxhMUcvw== X-Received: by 2002:a17:902:ce85:b0:178:2402:1f7d with SMTP id f5-20020a170902ce8500b0017824021f7dmr10208022plg.81.1663964912148; Fri, 23 Sep 2022 13:28:32 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n8-20020a170902d2c800b00174d9bbeda4sm6514749plc.197.2022.09.23.13.28.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Sep 2022 13:28:28 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, "Ruhl, Michael J" , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Greg Kroah-Hartman , Nick Desaulniers , Alex Elder , Josef Bacik , David Sterba , Jesse Brandeburg , Daniel Micay , Yonghong Song , Marco Elver , Miguel Ojeda , linux-kernel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, intel-wired-lan@lists.osuosl.org, dev@openvswitch.org, x86@kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: [PATCH v2 08/16] dma-buf: Proactively round up to kmalloc bucket size Date: Fri, 23 Sep 2022 13:28:14 -0700 Message-Id: <20220923202822.2667581-9-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220923202822.2667581-1-keescook@chromium.org> References: <20220923202822.2667581-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1536; h=from:subject; bh=1k9cluqwH+m+5FA9iHjyZRl7ltBBZL1qz3WU+O4eJDg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjLhbkkQMIyxW+p5Yq+8oZ2wcfayyBNx+y5dQbw2fi AE/i2oWJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYy4W5AAKCRCJcvTf3G3AJs+QD/ sFhRuVLRx0nMn49LZ3LgzAUGJu/OCv8HKw/plbSsv+vpxeMZ5Y4Z2yz+8pOjvg3cX6lD5EGOFnhfDh AkybvgBPzOD3UBEmc2+orSVNWeDOnYXG/2dyEL7KYashVN5HVyJ+SxZeRk177uhTjBzjbNRkAQ/SVs Oxp1fa8tJlAzSNr4khPvRqKxudlyHIfws9eHam1m+xu9Yx4IaRjlpv73TPFSCF3B4R61s5d9qdqmI6 QtxsboSAw9VmMnwtQ92JY/zE3XF4HhRRzOwCGtrvQkJf7xWir9bHo5kODtv29MGnYSHOD7MNuLNHvy yT9yMlcMCSUI01Gqw83MyXuzCvvbGT8fRIxDuE0OLV8AuytMSMYtLLvmi8aqicaaH1P85zLrbxJ7VQ ibgpFBaDwYITAulUo26Uezs0cpbObE4vDsRPIMUwW8KNHI4PXEY3ej4CfYwEi05WAoQDnHjsKiI3mw NgRo1Knltm0LSiFomNDPAPC2bLhoNZ5ap0NV7jcfaCdq1s+e8i6UWoxTNuvUmvNZIO/WymKW5MaK0w VFLb8zJYoMtYLSQQRa1twKC/oiHyzfBWzmy0upGh1vD2LcXJMTsecq1f9iPUzq0AYRDLPe3zIIPTo5 8qm3uJJ1hdyE1VeTF4mKkOhP+oujwwLKoB9fQmxF1d5+YH73zekSnLGtymEw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Instead of discovering the kmalloc bucket size _after_ allocation, round up proactively so the allocation is explicitly made for the full size, allowing the compiler to correctly reason about the resulting size of the buffer through the existing __alloc_size() hint. Cc: Sumit Semwal Cc: "Christian König" Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linaro-mm-sig@lists.linaro.org Signed-off-by: Kees Cook --- drivers/dma-buf/dma-resv.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c index 205acb2c744d..5b0a4b8830ff 100644 --- a/drivers/dma-buf/dma-resv.c +++ b/drivers/dma-buf/dma-resv.c @@ -98,12 +98,17 @@ static void dma_resv_list_set(struct dma_resv_list *list, static struct dma_resv_list *dma_resv_list_alloc(unsigned int max_fences) { struct dma_resv_list *list; + size_t size; - list = kmalloc(struct_size(list, table, max_fences), GFP_KERNEL); + /* Round up to the next kmalloc bucket size. */ + size = kmalloc_size_roundup(struct_size(list, table, max_fences)); + + list = kmalloc(size, GFP_KERNEL); if (!list) return NULL; - list->max_fences = (ksize(list) - offsetof(typeof(*list), table)) / + /* Given the resulting bucket size, recalculated max_fences. */ + list->max_fences = (size - offsetof(typeof(*list), table)) / sizeof(*list->table); return list; -- 2.34.1