Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2001965rda; Tue, 24 Oct 2023 09:20:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIecTO03dAV8srL3BwbjYZ3hDtv1V3CgA/tqq7Vg0HAsLnL3kKFvG/N6XWQG6evAtrOxun X-Received: by 2002:a17:903:5d0:b0:1c7:27a1:a9e5 with SMTP id kf16-20020a17090305d000b001c727a1a9e5mr12530054plb.33.1698164405185; Tue, 24 Oct 2023 09:20:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698164405; cv=none; d=google.com; s=arc-20160816; b=M9eSAvyrcl4f8Pdm4bfBLlOuR2IiLm8rAi0SWztIW++K8/3IPI3a09uH2ME4/PUrZ7 3WrNKQavk/yCenGfB4v76OxoVSdplEbjSw9Dxm5hkLM0UNhg4Vq7bcIOqdNkgPRghOpk MuLrwpuKYwT9ZwHvdB0u0xkIsTkLtCU+jFhzF71e8Jzsdup5q4xOKOn8TQ6ooG6kmISi VVF/8+8rj6LhQcviMxy5zOeSFRqzKykXHOlzkyTr8a6eyMQdwSXpPE7xSUL7VN7QOXLb OgNzPcg3YFZ+7Sbwhzhk+LQoWt52mRd1kk15ybI8BYMiRsnwfVfo2erSV1mGrlNdtoue gLjg== 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=XrCfxU281GA7yl5FhpL/HQmnx/wYsZjm+RV/1Xa97BA=; fh=16efit02Asb5+tiyZo0SWiPH7DlgL0AMnchLBL4NNdU=; b=IHdBTu7ovzP2ix5xH6//NWiOaBjybQci48zxGqQnXR5EUjHf+TXxGyip0pw7ItxBd/ T+7SsFixINl+vXq5pMTtbS4MK+YYINili+K0rP2kEU80QnAj9rSOMrw44YIuvR4xgUeG 6TEn4N8iCq0WJxuBa5B1dbKqUVJlcOb6tgFGTOj+tue5881QCek0zvZtZnG3wKdgE/40 ao/Vh4+wWyVSxjtDvfKOkvfbtzbqTxKK5z1PgaSZxP9iGqrpZErsA+BdihqhWJ5jCF+f GD/HXX6TEvsBx/WdjssPxMw6fbSKkVFEBB8DlhPW6XrgduZOu6o2lbDvk/iGRI5rMLH5 yFPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=A2hGpIqV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id c5-20020a170902c1c500b001b9c3498526si8543404plc.433.2023.10.24.09.20.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 09:20:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=A2hGpIqV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 98F868046F37; Tue, 24 Oct 2023 09:19:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234719AbjJXQTm (ORCPT + 99 others); Tue, 24 Oct 2023 12:19:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233585AbjJXQTi (ORCPT ); Tue, 24 Oct 2023 12:19:38 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B39E111; Tue, 24 Oct 2023 09:19:36 -0700 (PDT) Received: from jupiter.universe (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sre) by madras.collabora.co.uk (Postfix) with ESMTPSA id 264DD66072A2; Tue, 24 Oct 2023 17:19:35 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1698164375; bh=SqKf4LNwvpIYJ9bi8s2HSgXa96OgNa0HbeVAslfV1tU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A2hGpIqV1ZIGRSd6+nUApqHDqsXu2jtpQy8gHMlDfLtCXGef8ux0LOtJzTL4eLJu2 DUMaQUGSKyHGUHrOvPJjQgrhf4TD/pV9lYOP0rursa+TUzxci2toviIHXet9ksthcq hQWjVEeC4WAMatPGuv3DUauC5Q/LsNcEW9MCtVp9Y6JLS1ZUNV7klUwT8MQTe6IrDx w1z244Fv0hXQ/z3SmxPaRtFOXFIx6aMuTUzJ6SAVx/BbgDZHcdVh+qfxMzJwdKFoGo gAjeXhoZdk/zOXs1qMzCcLtDvfXn1gfJllO7jvBePlPNqf4hhc1VTbjfO3YhdAg1FG Z9ZOc+kgg7ZaA== Received: by jupiter.universe (Postfix, from userid 1000) id 9BC2B482AD8; Tue, 24 Oct 2023 18:19:32 +0200 (CEST) From: Sebastian Reichel To: Andy Shevchenko , Michael Turquette , Stephen Boyd , David Laight , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vasily Gorbik , Niklas Schnelle , Linus Torvalds , Sebastian Reichel , kernel@collabora.com Subject: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW Date: Tue, 24 Oct 2023 18:18:15 +0200 Message-ID: <20231024161931.78567-2-sebastian.reichel@collabora.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231024161931.78567-1-sebastian.reichel@collabora.com> References: <20231024161931.78567-1-sebastian.reichel@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 24 Oct 2023 09:19:59 -0700 (PDT) Add a new DIV_ROUND_UP helper, which cannot overflow when big numbers are being used. Signed-off-by: Sebastian Reichel --- include/linux/math.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/math.h b/include/linux/math.h index dd4152711de7..f80bfb375ab9 100644 --- a/include/linux/math.h +++ b/include/linux/math.h @@ -36,6 +36,17 @@ #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP +/** + * DIV_ROUND_UP_NO_OVERFLOW - divide two numbers and always round up + * @n: numerator / dividend + * @d: denominator / divisor + * + * This functions does the same as DIV_ROUND_UP, but internally uses a + * division and a modulo operation instead of math tricks. This way it + * avoids overflowing when handling big numbers. + */ +#define DIV_ROUND_UP_NO_OVERFLOW(n, d) (((n) / (d)) + !!((n) % (d))) + #define DIV_ROUND_DOWN_ULL(ll, d) \ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) -- 2.42.0