Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp175832pxb; Wed, 8 Sep 2021 21:09:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTxe+9xgbjxMRfgl39z0wma8hW3iElqnAsQB/fZPrJYzJHE6m4d7O0yG4XJ8Hj9+fZ9BV3 X-Received: by 2002:a05:6402:1d25:: with SMTP id dh5mr1014898edb.91.1631160545357; Wed, 08 Sep 2021 21:09:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631160545; cv=none; d=google.com; s=arc-20160816; b=V7E2qg8+96p/YftqsWfvxCNEGrcvsu4Gx+wTIMoIqxc5zm55bjvtVhXLOmlxAUG+Ma DBRHpyXotymffci/e+QX6qFs2ito5HnQ2uKKvezbhLBv+JGsPHU3piX9rvZ2N01r4Bud gkumI0m3OChdXpw5zJEPdM5ZoLL9Jo75XE6/w/A8tAzaahhC/KpQAmUi/ngu8HTcfC9k M0Scp9hW4JNI5HZE39/M8rYSoWSaC0B1ie4SrzrStcXnSTIFKiGI7wCnKEwhR06w4NSt 2tgHJUGwLPXwF34gSHiVI5PU+dHqnSOFm5iavE0cxJlLEBiIVd706SedI8ZL/LxN3/WK y8WA== 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 :message-id:date:subject:cc:to:from; bh=C/Xg72J6MuPYs42oTBkLu7zrmknuB/lH5HrySVbiiRM=; b=UKuKo+8fkZOWmPlYy+2fuOqJLg5Wvctmz6LU+l/6983erM3UkKeRMgw9OCG094tSQy WatX3PHgG/k8fZ5EMJUJjUabR8uRHFMJUm9pkDeARDrsXUM7qzKKX0X5XWbz8/cdG9mV QVdaF4L5XDn3EKZdCUodcT/MOAdDLbEwJjjAbRr7/CC5EC0Ar7e1UjYxoZHx5iszuH/L zC0YPo/5FzSWfS4+ShMqJiUM4gEU/YfkEPELjbM29R5s+WefQaETxQYZK8pCt6oYn5vK w3OxCo9IpKFh22gpY/EBT+Lne69U3i0dNG4e+a66WmYzaQirNMwJWIsNUXosAeAXyz5x qHJQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hk18si556085ejb.238.2021.09.08.21.08.02; Wed, 08 Sep 2021 21:09:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349852AbhIIDVs (ORCPT + 99 others); Wed, 8 Sep 2021 23:21:48 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:9017 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237357AbhIIDVo (ORCPT ); Wed, 8 Sep 2021 23:21:44 -0400 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-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/shmem 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 = round_up(index, HPAGE_PMD_NR); + index = round_up(index + 1, HPAGE_PMD_NR); i_size = round_up(i_size_read(inode), PAGE_SIZE); - if (i_size >= HPAGE_PMD_SIZE && (i_size >> PAGE_SHIFT) >= index) + if (i_size >> PAGE_SHIFT >= index) return true; fallthrough; case SHMEM_HUGE_ADVISE: -- 2.23.0