From mboxrd@z Thu Jan 1 00:00:00 1970 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.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,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 29066C433FE for ; Thu, 9 Sep 2021 03:20:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 83DA46103D for ; Thu, 9 Sep 2021 03:20:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 83DA46103D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 8C8206B0071; Wed, 8 Sep 2021 23:20:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85075900002; Wed, 8 Sep 2021 23:20:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F0C66B0073; Wed, 8 Sep 2021 23:20:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0113.hostedemail.com [216.40.44.113]) by kanga.kvack.org (Postfix) with ESMTP id 58FFA6B0071 for ; Wed, 8 Sep 2021 23:20:37 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F164F2CFFD for ; Thu, 9 Sep 2021 03:20:36 +0000 (UTC) X-FDA: 78566582472.09.3C62596 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf14.hostedemail.com (Postfix) with ESMTP id F29046001982 for ; Thu, 9 Sep 2021 03:20:35 +0000 (UTC) Received: from dggeml765-chm.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4H4kl02pv4zW0wc; Thu, 9 Sep 2021 11:19:40 +0800 (CST) Received: from huawei.com (10.175.124.27) by dggeml765-chm.china.huawei.com (10.1.199.175) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.8; Thu, 9 Sep 2021 11:20:32 +0800 From: Liu Yuntao To: CC: , , , , , , , , Subject: [PATCH v2] fix judgment error in shmem_is_huge() Date: Thu, 9 Sep 2021 11:20:07 +0800 Message-ID: <20210909032007.18353-1-liuyuntao10@huawei.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggeml765-chm.china.huawei.com (10.1.199.175) X-CFilter-Loop: Reflected Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=huawei.com; spf=pass (imf14.hostedemail.com: domain of liuyuntao10@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liuyuntao10@huawei.com X-Stat-Signature: m7kmj1yiaz8iu1reii83uz7136rrnsai X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: F29046001982 X-HE-Tag: 1631157635-739328 Content-Transfer-Encoding: quoted-printable 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: In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one. an example: HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB. In shmem_is_huge(), the corresponding index happens to be 512. After rounded up by HPAGE_PMD_NR, it will still be 512 which is smaller than i_size, and shmem_is_huge() will return true. As a result, my buffer takes an additional huge page, and that shouldn't happen when shmem_enabled is set to within_size. Fixes: f3f0e1d2150b2b ("khugepaged: add support of collapse for tmpfs/shm= em pages") Signed-off-by: Liu Yuntao --- V1->V2: add simplification of the condition after round_up() --- mm/shmem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 88742953532c..b5860f4a2738 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -490,9 +490,9 @@ bool shmem_is_huge(struct vm_area_struct *vma, case SHMEM_HUGE_ALWAYS: return true; case SHMEM_HUGE_WITHIN_SIZE: - index =3D round_up(index, HPAGE_PMD_NR); + index =3D round_up(index + 1, HPAGE_PMD_NR); i_size =3D round_up(i_size_read(inode), PAGE_SIZE); - if (i_size >=3D HPAGE_PMD_SIZE && (i_size >> PAGE_SHIFT) >=3D index) + if (i_size >> PAGE_SHIFT >=3D index) return true; fallthrough; case SHMEM_HUGE_ADVISE: --=20 2.23.0