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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76FF5C19F21 for ; Wed, 27 Jul 2022 09:20:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BA2B94000C; Wed, 27 Jul 2022 05:20:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 069D1940009; Wed, 27 Jul 2022 05:20:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E99F594000C; Wed, 27 Jul 2022 05:20:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DA047940009 for ; Wed, 27 Jul 2022 05:20:30 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 937A6140B62 for ; Wed, 27 Jul 2022 09:20:30 +0000 (UTC) X-FDA: 79732334220.08.202F41B Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf21.hostedemail.com (Postfix) with ESMTP id 9F3711C00C5 for ; Wed, 27 Jul 2022 09:20:29 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Lt7Vx6bLyzmVBd; Wed, 27 Jul 2022 17:18:33 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 27 Jul 2022 17:20:13 +0800 Subject: Re: [RFC PATCH v4 4/8] hugetlbfs: catch and handle truncate racing with page faults To: Mike Kravetz , , CC: Muchun Song , Michal Hocko , Peter Xu , Naoya Horiguchi , David Hildenbrand , "Aneesh Kumar K . V" , Andrea Arcangeli , "Kirill A . Shutemov" , Davidlohr Bueso , Prakash Sangappa , James Houghton , Mina Almasry , Pasha Tatashin , Axel Rasmussen , Ray Fucillo , Andrew Morton References: <20220706202347.95150-1-mike.kravetz@oracle.com> <20220706202347.95150-5-mike.kravetz@oracle.com> From: Miaohe Lin Message-ID: Date: Wed, 27 Jul 2022 17:20:12 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <20220706202347.95150-5-mike.kravetz@oracle.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658913630; a=rsa-sha256; cv=none; b=shVsda/axKEbfln8Z76ZkWGhF/kbCXR7uDt9+5tg7utk+JkTrUzoaWWvsrBywUdF25g17m UrXrZB4zwxSlF6nTaLkVeLBN0RYIDowi7AmtrhZPcnc80dmF9N787F87FwAbRLV9ho1cgA QcPUqTQAWm+YYq75Gc2GbF8+qZ289OY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658913630; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HvYYjLBbX3hCqV1DW/zGXZqFeUOQfHns/gEDlk16nA8=; b=RWkhYM9J3BX05+4dC90mYosoEgakLDYtycJ6BLjOpU+1VxJvyLvLFEyu/1XWra9Zdxs2TJ pdZ2Zd/2NlhF9cWj/2Wa/JrSdne7NLPSwXtc7xAMjNJrfXoDG1b/qcere3LHo65rBa7pno qu6wSro4rXR9ArzKoumzNuwxNNC6LF8= X-Stat-Signature: qe7w7cftpbz5uoqxr54pozfufz5u5f36 X-Rspamd-Queue-Id: 9F3711C00C5 X-Rspam-User: Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam02 X-HE-Tag: 1658913629-676555 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: On 2022/7/7 4:23, Mike Kravetz wrote: > Most hugetlb fault handling code checks for faults beyond i_size. > While there are early checks in the code paths, the most difficult > to handle are those discovered after taking the page table lock. > At this point, we have possibly allocated a page and consumed > associated reservations and possibly added the page to the page cache. > > When discovering a fault beyond i_size, be sure to: > - Remove the page from page cache, else it will sit there until the > file is removed. > - Do not restore any reservation for the page consumed. Otherwise > there will be an outstanding reservation for an offset beyond the > end of file. > > The 'truncation' code in remove_inode_hugepages must deal with fault > code potentially removing a page/folio from the cache after the page was > returned by filemap_get_folios and before locking the page. This can be > discovered by a change in folio_mapping() after taking folio lock. In > addition, this code must deal with fault code potentially consuming > and returning reservations. To synchronize this, remove_inode_hugepages > will now take the fault mutex for ALL indices in the hole or truncated > range. In this way, it KNOWS fault code has finished with the page/index > OR fault code will see the updated file size. > > Signed-off-by: Mike Kravetz > --- > @@ -5606,8 +5610,10 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, > > ptl = huge_pte_lock(h, mm, ptep); > size = i_size_read(mapping->host) >> huge_page_shift(h); > - if (idx >= size) > + if (idx >= size) { > + beyond_i_size = true; Thanks for your patch. There is one question: Since races between hugetlb pagefault and truncate is guarded by hugetlb_fault_mutex, do we really need to check it again after taking the page table lock? BTW: I will learn more about this series when I have enough time. Thanks for your work. :)