From patchwork Mon Jul 10 20:43:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13307628 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01E26EB64DC for ; Mon, 10 Jul 2023 20:44:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C5658E001E; Mon, 10 Jul 2023 16:43:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2273C8E001B; Mon, 10 Jul 2023 16:43:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0286A8E001E; Mon, 10 Jul 2023 16:43:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E3FDA8E001B for ; Mon, 10 Jul 2023 16:43:57 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 75D1680250 for ; Mon, 10 Jul 2023 20:43:57 +0000 (UTC) X-FDA: 80996878914.03.D2546CD Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf20.hostedemail.com (Postfix) with ESMTP id D05A91C0015 for ; Mon, 10 Jul 2023 20:43:55 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=WgSSkXyo; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689021835; a=rsa-sha256; cv=none; b=Plarq9s+n+XjZc/TQ6IHW2MMqPV/hCFSWb+aBZj7hWYv6a2XsLxn9ETVCNIx7Oc1kfYZ7o oc9XlSG+agAc1sm9KC/sUEa1Pe4NEKILZcXilmYB/cV+UNaR0YNGBfBO/YpFX7BqoDmu+S yy2onlJ60BHK47Em9R4pIm3cacBHl4c= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=WgSSkXyo; spf=none (imf20.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689021835; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=A7RcetqmNW3btMImHl/BZfdcqRl9coUa9L9XZZv1508=; b=n+d5IB7YhxEYOdYb1p9tp3WHQdmnemKPANYANPx+t2MBQ55S8M68BnhOGjXHWOmZ609fcx ukYUsE4j2Dir9WicIp/JAjKzrbimEggrxM8d+VWLJJ0doJkRszhb01x/0sxciA9X+UDb3N /6aiY6oPJ1Nk6KED7s+2bcYYolHMM0c= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=A7RcetqmNW3btMImHl/BZfdcqRl9coUa9L9XZZv1508=; b=WgSSkXyo6wHIO77DHamsAtL5yS QPw+3CNlMjYUWUL/cmn1npceGfaf+29pfTh1AWihDxltm3qKIFPVy5zVYjvclsnFpXKYetM/YBD+t pDQiNiNl9LpEoGB8KmBRwWmkDxMJDuHj5GlmENIK0nNp8MTh/wrwhCI2z1kpRHwF5n0P8/2o0TOIw lnX0H/DLnXPWpzmVouo1J7c0g3n5+cegiGdOuSGV97zLi9NShKpz20OELw2TBUrUBocDgTJYn7oY0 d4t9+1dGZsxpyK/6oTDLRRXJ10U4eXy3U5L8PQIq7FLntXSPG6i3HPw2LOBx3gqBtNDv1YMvvQTgb wQnxk6dA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qIxjR-00EuoE-3L; Mon, 10 Jul 2023 20:43:41 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 01/38] minmax: Add in_range() macro Date: Mon, 10 Jul 2023 21:43:02 +0100 Message-Id: <20230710204339.3554919-2-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230710204339.3554919-1-willy@infradead.org> References: <20230710204339.3554919-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: D05A91C0015 X-Stat-Signature: ucuk9hjg3imjkbpswicyrrems48x7ky4 X-Rspam-User: X-HE-Tag: 1689021835-384370 X-HE-Meta: U2FsdGVkX18vtk9Mxe+9tTDPvnSmqwJYysqfCTQLKAaahv/D8hXLx+Hao9BGqZi0DuX5EVSDbp51G63XKkf538dS6kSHPHwQPLEr7nwqAxifKiWPAIVGWs7PNbHbT+aPO/9kRXHr+8lDq7Z+WpmW9+MZwtc6I5QKWiFqhUgBCAaz3siFZyihdhWHxvqQQSlqZybeIshIawsYHQYB88fQM1hmj+nA6+gUo+bCmuE7TWdmdD8ZiMw9T0221tsz2nkg9SjdsuNMZaw9iv2BaBIw0Nm4Rp+uICbihTIzmMP+vduIiB7fp39PpFTPuA35ono8QaF9xu8I+tk7OSYcv/t+45YdPcZHj9iOAOnejG62YafsQyDV1dTNS6CscbC7eIykK+Mr12vH01Gg+qvv2CBzDiIuD9jUAPvQH0rJXmjh3yDWAl7FCWsANpcB4FxmFQOohHbGao4uPJX9hUzh/IUmMZSCbhauNROCVxDo+AwSXxsVRh3v4suXB3kNvbgf3Vsg/IKi1nO5gXG2zWxJNDCfvenPfCNjwkjJh5C0iXrSPjS3hs54icZGZ3/B1zUh+klRAf4wlvBZ70767lsM8dVGMKLeurKq2acBptm0l1IZA5q8RJ9s3ymeQxUiCCm7+45+UFjRo1w0rVpI5PcpjapBeYMSiB3NZIw9ZZrCK8sSkzvkBEkNf2fj+Kn1Zi66o/ghEgTBLloyzWurcVCj4xLaom+WFhZv6xo7+YVgDlz3TbLK10sI7YB9N9z75/gOj/j3B0fCNFmxfWgR9MEJGW2wDlMRnNdeXFmjqK1GOIIft9H73yt8afQwet48iGuSMNl0EpeSUXp5g9DMtFObtqC77TWTJ2/1vzo8bTVk+rbzM+o5pK0LJvD/tdWohww0zGwBGSwKE0/KI0kJEp+MgiSmAkcSi0LyhUhID6G3oWIEQ59BMZgab/nhhkSHdtBPSw1FebETqLXM7eF+KEcMu9c kwSHETij VsuWvikecEhBRNuVg2r3GzJVNyRW6Gydlt/lgnbUQn89BAWhrqOOfec2JQBjH/MVCsFK4hqvOyiFILdyztAaZqGMfS8Sp6ofO2t2T2/BfexHg7pBTx3O0ObTn+hj5uSIGFrNlT09OGwnNTc65OmtwF+UvvhexVId3ILQkazZJoGf6XEz93YSSawcSouwgJq8ii3hkWRkVPDO19AQLxx9HTn6+28k9Kzjly3fy X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Determine if a value lies within a range more efficiently (subtraction + comparison vs two comparisons and an AND). It also has useful (under some circumstances) behaviour if the range exceeds the maximum value of the type. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/minmax.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 396df1121bff..028069a1f7ef 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -158,6 +158,32 @@ */ #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) +static inline bool in_range64(u64 val, u64 start, u64 len) +{ + return (val - start) < len; +} + +static inline bool in_range32(u32 val, u32 start, u32 len) +{ + return (val - start) < len; +} + +/** + * in_range - Determine if a value lies within a range. + * @val: Value to test. + * @start: First value in range. + * @len: Number of values in range. + * + * This is more efficient than "if (start <= val && val < (start + len))". + * It also gives a different answer if @start + @len overflows the size of + * the type by a sufficient amount to encompass @val. Decide for yourself + * which behaviour you want, or prove that start + len never overflow. + * Do not blindly replace one form with the other. + */ +#define in_range(val, start, len) \ + sizeof(start) <= sizeof(u32) ? in_range32(val, start, len) : \ + in_range64(val, start, len) + /** * swap - swap values of @a and @b * @a: first value