From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from szxga07-in.huawei.com (szxga07-in.huawei.com [45.249.212.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A883D154423 for ; Sat, 11 Jan 2025 03:54:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.35 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736567692; cv=none; b=hFAYHmH1dsGsCyvP7/IOTYKYRilvcu4jDZ7AZnsS8VHpkmrIWnFqY2ij6RI53IGe2WzgcsOmhIv/kIRQRlDuEKejt1pjStODgXtV41AbvwNDikfqOj3qbg8pWYiKFlyiP7dUSKufCND6WjHrQf1gF8omQGubZJ6CFbJza7d4zcM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736567692; c=relaxed/simple; bh=8YmJu1LMMScARn9UOFBq4QLm/pBbfxlZwrzJoRRZEjc=; h=Subject:To:References:CC:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=pBJIu9RDmQ8Uj2RVanYjVxnHNDXdGP9PloZZQyBrgyDlVELjWeVM+YRtIhoN85bBH/YMyHCnL0HCsFko2OdxFH2zJfX2NNinl9SKJkGnrwd5hnfqabPbU/Nhpnji5YxBM4KED+nHUblCTEE29p3NrSa2S4KYvWQ9vUs3trTxpiY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4YVPhr1nM2z1V4cW; Sat, 11 Jan 2025 11:51:40 +0800 (CST) Received: from kwepemg200013.china.huawei.com (unknown [7.202.181.64]) by mail.maildlp.com (Postfix) with ESMTPS id AC58A1402CA; Sat, 11 Jan 2025 11:54:45 +0800 (CST) Received: from [10.174.179.24] (10.174.179.24) by kwepemg200013.china.huawei.com (7.202.181.64) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Sat, 11 Jan 2025 11:54:44 +0800 Subject: Re: [PATCH] mm: khugepaged: fix call hpage_collapse_scan_file() for anonymous vma To: Yang Shi , Matthew Wilcox References: <20250109070059.369257-1-liushixin2@huawei.com> <037d4442-4d2d-4aeb-8091-5efffc374d36@os.amperecomputing.com> <8e773a7c-e1dd-4e0e-8349-8aa52f39d85a@os.amperecomputing.com> <58595f1f-7959-4dc1-9ea2-470c44709d31@os.amperecomputing.com> CC: Andrew Morton , Chengming Zhou , Kefeng Wang , Nanyong Sun , Muchun Song , Qi Zheng , Johannes Weiner , , From: Liu Shixin Message-ID: Date: Sat, 11 Jan 2025 11:54:43 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <58595f1f-7959-4dc1-9ea2-470c44709d31@os.amperecomputing.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemg200013.china.huawei.com (7.202.181.64) On 2025/1/11 3:40, Yang Shi wrote: > > > > On 1/10/25 11:01 AM, Matthew Wilcox wrote: >> On Fri, Jan 10, 2025 at 10:04:42AM -0800, Yang Shi wrote: >>> On 1/9/25 8:31 PM, Matthew Wilcox wrote: >>>> On Thu, Jan 09, 2025 at 09:00:24AM -0800, Yang Shi wrote: >>>>> Thanks for catching this. It sounds a little bit weird to have vm_file for >>>>> an anonymous VMA. I'm not sure why we should keep such special case. It >>>>> seems shared mapping is treated as shmem file mapping. So can we set vm_file >>>>> to NULL when mmap'ing /dev/zero for private mapping? Something like: >>>>> >>>>> diff --git a/drivers/char/mem.c b/drivers/char/mem.c >>>>> index 169eed162a7f..fc332efc5c11 100644 >>>>> --- a/drivers/char/mem.c >>>>> +++ b/drivers/char/mem.c >>>>> @@ -527,6 +527,7 @@ static int mmap_zero(struct file *file, struct >>>>> vm_area_struct *vma) >>>>> if (vma->vm_flags & VM_SHARED) >>>>> return shmem_zero_setup(vma); >>>>> vma_set_anonymous(vma); >>>>> + vma->vm_file = NULL; >>>>> return 0; >>>>> } >>>> I'm wary this might cause other bugs somewhere. rc6 is a bit late to be >>>> introducing such a subtle change. >>> Thanks for the extra caution. Applying the proposed fix in khugepaged code >>> is fine to me either. We can try to kill the special case later. >>> >>> Looking at the code further, I think we should do more to make private >>> /dev/zero mapping an anonymous mapping: >> I'm still nervous about this. We map device inodes in a lot of places. > > Yes, we do. But I don't think this change actually changes the semantic of /dev/zero. Shared /dev/zero mapping is still treated as shmem mapping, private /dev/zero mapping is treated as anonymous mapping, but the current implementation is actually half-baked. It has NULL vma->vm_ops which is used to tell kernel whether it is an anonymous vma or not, but it also has valid vma->vm_file and vma->vm_pgoff as in file offset. > > So this special case makes kernel has 3 types of VMA: > - anonymous VMA: vm_ops is NULL, vm_file is NULL, vm_pgoff is the linear address pgoff > - file VMA: vm_ops is *NOT* NULL, valid vm_file and vm_pgoff is index in file > - private /dev/zero mapping VMA > I have posted v2 to fix it in a safe way. Link: https://lore.kernel.org/all/20250111034511.2223353-1-liushixin2@huawei.com/ Maybe we can also revisit commit bfd40eaff5ab ("mm: fix vma_is_anonymous() false-positives") and fix it by another way? By the way, it seems we collpase the file even after cow for a private file mapping. Is that so? Thanks, > > . >