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; } /** From patchwork Sun Dec 20 21:10:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Randy Dunlap X-Patchwork-Id: 11984027 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 DD207C4361B for ; Sun, 20 Dec 2020 21:11:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6612222273 for ; Sun, 20 Dec 2020 21:11:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6612222273 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 026956B005D; Sun, 20 Dec 2020 16:11:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F173B6B0068; Sun, 20 Dec 2020 16:11:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E07096B006C; Sun, 20 Dec 2020 16:11:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0187.hostedemail.com [216.40.44.187]) by kanga.kvack.org (Postfix) with ESMTP id C9C4F6B005D for ; Sun, 20 Dec 2020 16:11:04 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 80851180AD82F for ; Sun, 20 Dec 2020 21:11:04 +0000 (UTC) X-FDA: 77614905648.21.heat76_480391427451 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 5FE5F180442C0 for ; Sun, 20 Dec 2020 21:11:04 +0000 (UTC) X-HE-Tag: heat76_480391427451 X-Filterd-Recvd-Size: 3660 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Sun, 20 Dec 2020 21:11:03 +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=NUqXj/POrl622LAUb62LCHSZurTWKJa2BeQnkYK49wc=; b=FPcvcPrKrhzWPxAmQIQUK56PGU Gl4asoz4GFZ50DLgihfaToYj5BUITjB4brnnYeeKn1Rhz84vY12DZY6Bklz0oN7pVvNGKrZWcN2zT Z/OzTO6+ew3dhZWP7gKR36yJoXirtRQdkEa8MG1AyoGyS9bcw0rjTWZU5GY28aCcSyHOmJziA+hhM ONjjVOJHHt9xZwB2V7GyfmC0NtLP0d+FejMskoSMwNkp9R2GAHIWHUxrChVvlZIA5XO9y0Jmjgkk7 2D52lZcyGi430lzzlwcOtz5/7ujlgvysAk/bTK4P/XXW3CKksqnj/Z+oWBctl3KGUN262NOnhoEf5 A6VFoV7Q==; 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 1kr5yh-0005CV-KA; Sun, 20 Dec 2020 21:10:56 +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 2/2] mm: readahead: handle LARGE input to get_init_ra_size() Date: Sun, 20 Dec 2020 13:10:51 -0800 Message-Id: <20201220211051.1416-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: Add a test to detect if the input ra request size has its high order bit set (is negative when tested as a signed long). This would be a really Huge readahead. If so, WARN() with the value and a stack trace so that we can see where this is happening and then make further corrections later. Then adjust the size value so that it is not so Huge (although this may not be needed). Also correct a comment: the return value is not squared for small size. Signed-off-by: Randy Dunlap Cc: Jens Axboe Cc: Andrew Morton Cc: Toralf Förster Cc: linux-mm@kvack.org Reported-by: and Tested-by: on the patch? --- Notes: - Look for "WARNING:.*get_init_ra_size" - If panic_on_warn is set, this will cause a kernel panic(). mm/readahead.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- linux-5.10.1.orig/mm/readahead.c +++ linux-5.10.1/mm/readahead.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -303,14 +304,21 @@ void force_page_cache_ra(struct readahea } /* - * Set the initial window size, round to next power of 2 and square + * Set the initial window size, round to next power of 2 * for small size, x 4 for medium, and x 2 for large * for 128k (32 page) max ra * 1-8 page = 32k initial, > 8 page = 128k initial */ static unsigned long get_init_ra_size(unsigned long size, unsigned long max) { - unsigned long newsize = roundup_pow_of_two(size); + unsigned long newsize; + + if ((signed long)size < 0) { /* high bit is set: ultra-large ra req */ + WARN_ONCE(1, "%s: size=0x%lx\n", __func__, size); + size = -size; /* really only need to flip the high/sign bit */ + } + + newsize = roundup_pow_of_two(size); if (newsize <= max / 32) newsize = newsize * 4;