From patchwork Sun Dec 20 21:10:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Randy Dunlap X-Patchwork-Id: 11984025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DEC5C4361B for ; Sun, 20 Dec 2020 21:10:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 212C122287 for ; Sun, 20 Dec 2020 21:10:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 212C122287 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F42396B005C; Sun, 20 Dec 2020 16:10:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EF1666B005D; Sun, 20 Dec 2020 16:10:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E07556B0068; Sun, 20 Dec 2020 16:10:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0222.hostedemail.com [216.40.44.222]) by kanga.kvack.org (Postfix) with ESMTP id C4D7A6B005C for ; Sun, 20 Dec 2020 16:10:54 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 822A9181AEF39 for ; Sun, 20 Dec 2020 21:10:54 +0000 (UTC) X-FDA: 77614905228.14.pan90_5c1294027451 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 5FCC118229835 for ; Sun, 20 Dec 2020 21:10:54 +0000 (UTC) X-HE-Tag: pan90_5c1294027451 X-Filterd-Recvd-Size: 3316 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Sun, 20 Dec 2020 21:10:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:In-Reply-To:References; bh=uEe14I67YY4WiKXf+Vv9Ka5L+pRt+5C3knvLfa48ujk=; b=RvnkEbjCvQg4j0ZNpjnOzSykCY WwDxO5+lk7Zd++qtIEaWZoHrRvXYQh9k3zhLmtIcjDtBgNDefG105D5Gi1IzKbHtUENAFvUv3kF/R zErks05mFQhibhns2MKZRt1Q4LzQL9lHxdPPtdB/6kPbHgy8Ocj3l5Hjg5vWTR+re7UpnLuLm+6ID MBYYiYNS/LgLXt//ZAieJyXTWwgcA3AXTCS9LHNKT09F0/TV+dhM6MJkQaxrFmHRACF1538cmuXNh JBfaUHw/WdEQXOK/4FZadzqyw5q5B0xgjV5qL+TxpQR+GjcrLcwYGEL8NOq1lvmsjeIdRwr7qi6Df JohS7Dcg==; Received: from [2601:1c0:6280:3f0::64ea] (helo=smtpauth.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kr5yV-0005CC-Qc; Sun, 20 Dec 2020 21:10:44 +0000 From: Randy Dunlap To: linux-kernel@vger.kernel.org Cc: Randy Dunlap , Jens Axboe , Andrew Morton , =?utf-8?q?Toralf_F=C3=B6rster?= , linux-mm@kvack.org Subject: [RFC PATCH 1/2] log2: handle LARGE input to __roundup_pow_of_two() Date: Sun, 20 Dec 2020 13:10:37 -0800 Message-Id: <20201220211037.1354-1-rdunlap@infradead.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 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: UBSAN detected a 64-bit shift in log2.h:__roundup_pow_of_two(): UBSAN: shift-out-of-bounds in ./include/linux/log2.h:57:13 shift exponent 64 is too large for 64-bit type 'long unsigned int' This is during a call from mm/readahead.c:ondemand_readahead(), get_init_ra_size(), where the 'size' parameter must have been extremely large (or "negative"). fls() can legitimately return 32 or 64 when the MSbit is set in a 32-bit or 64-bit unsigned long. For these values, doing "1UL << shiftcout" is invalid or undefined, so catch when this happens. When the MSbit is 32 or 64, we cannot roundup to the next power of 2, so just return n (the input value), which is >= 0x8000...0000 and probably not a power of 2 (unless it is exactly 0x8000...0000). Signed-off-by: Randy Dunlap Cc: Jens Axboe Cc: Andrew Morton Cc: Toralf Förster Cc: linux-mm@kvack.org --- include/linux/log2.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) --- linux-5.10.1.orig/include/linux/log2.h +++ linux-5.10.1/include/linux/log2.h @@ -54,7 +54,17 @@ bool is_power_of_2(unsigned long n) static inline __attribute__((const)) unsigned long __roundup_pow_of_two(unsigned long n) { - return 1UL << fls_long(n - 1); + unsigned int lastset = fls_long(n - 1); /* this can be 64 or 32 */ + + /* + * for high bit set (64 or 32), we can neither round up nor + * make it a power or 2 + */ + if ((sizeof(n) == 4 && lastset == 32) || + (sizeof(n) == 8 && lastset == 64)) + return n; + + return 1UL << lastset; } /**