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 90A98C71136 for ; Fri, 13 Jun 2025 21:47:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FDED6B0088; Fri, 13 Jun 2025 17:47:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AF216B0089; Fri, 13 Jun 2025 17:47:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDFD16B008A; Fri, 13 Jun 2025 17:47:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CFE956B0088 for ; Fri, 13 Jun 2025 17:47:56 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 531B91612FF for ; Fri, 13 Jun 2025 21:47:56 +0000 (UTC) X-FDA: 83551715352.27.1DF7695 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf23.hostedemail.com (Postfix) with ESMTP id EDF1314000B for ; Fri, 13 Jun 2025 21:47:53 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="amu/e/j4"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=AfW23ToI; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="amu/e/j4"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=AfW23ToI; spf=pass (imf23.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749851274; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Z0Mxv7vMvbtG1IFq4FNjqvJLfb+OhVhpCKOxzSZ2rlo=; b=oE7uroAQUD7x4hMJtZbVLiRBrBWbIsf3X2Iv88WNIRJjKASf8lJtBYUkJ43vX65oRnSigU btjhKnQOWlKxdSjHJ7DbuG2ZC3u1TUxzjpcI5SgIF9ZbFqTe0++KvWgd1KWkN6Yy8ewrI/ 6CQcJx/ka5xnMMPe1Awzjn97H/3cZBM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749851274; a=rsa-sha256; cv=none; b=K+BnYjv2MEIhMtPyRgSp62xH0cz1NDk3oaCs7M3s1ydV6WQ4d9OZDj9zfYJr3LgPefak1f OkgUEBuWdyo63NI65oca7jaU/b+s/BljmiVBeTd29J4M8VG8DWoHxGFBmhxrO0a0FKGB0x N7DH1H8HF7NX/1sneP3yXu1S34MRRik= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="amu/e/j4"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=AfW23ToI; dkim=pass header.d=suse.de header.s=susede2_rsa header.b="amu/e/j4"; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=AfW23ToI; spf=pass (imf23.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 439AB1F390; Fri, 13 Jun 2025 21:47:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749851272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Z0Mxv7vMvbtG1IFq4FNjqvJLfb+OhVhpCKOxzSZ2rlo=; b=amu/e/j4HA/6/f74zZzwYswY/ofIksR6uzmlVhcnP4iJfJmZpAFSB2s++jtEP6vnwz5dgl LhgwejYF+75+eSseYsC7SLDYixVYtukBtQqh7AgaGfbKxHy7upTNOalYYwsyKdDg/cTtAx CW4pE50gFkCopUrTn/6GV4JGf7KUxSY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749851272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Z0Mxv7vMvbtG1IFq4FNjqvJLfb+OhVhpCKOxzSZ2rlo=; b=AfW23ToIN8oTeKGdSxKHJW+/TLKb5l1Lr5ua+2knUTcxwxjLSuWoCQxDByOHPn+hTdn6k/ EtcWPst4AuLSNPAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1749851272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Z0Mxv7vMvbtG1IFq4FNjqvJLfb+OhVhpCKOxzSZ2rlo=; b=amu/e/j4HA/6/f74zZzwYswY/ofIksR6uzmlVhcnP4iJfJmZpAFSB2s++jtEP6vnwz5dgl LhgwejYF+75+eSseYsC7SLDYixVYtukBtQqh7AgaGfbKxHy7upTNOalYYwsyKdDg/cTtAx CW4pE50gFkCopUrTn/6GV4JGf7KUxSY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1749851272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Z0Mxv7vMvbtG1IFq4FNjqvJLfb+OhVhpCKOxzSZ2rlo=; b=AfW23ToIN8oTeKGdSxKHJW+/TLKb5l1Lr5ua+2knUTcxwxjLSuWoCQxDByOHPn+hTdn6k/ EtcWPst4AuLSNPAg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B40B3137FE; Fri, 13 Jun 2025 21:47:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id IHoGKYecTGiVDgAAD6G6ig (envelope-from ); Fri, 13 Jun 2025 21:47:51 +0000 Date: Fri, 13 Jun 2025 23:47:50 +0200 From: Oscar Salvador To: David Hildenbrand Cc: Andrew Morton , Muchun Song , James Houghton , Peter Xu , Gavin Guo , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/5] mm,hugetlb: Document the reason to lock the folio in the faulting path Message-ID: References: <20250612134701.377855-1-osalvador@suse.de> <20250612134701.377855-3-osalvador@suse.de> <44f0f1cc-307a-46e3-9e73-8b2061e4e938@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Action: no action X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: EDF1314000B X-Stat-Signature: 3zfg3gup6zt9jj6q7d9drxawkr45gp5w X-HE-Tag: 1749851273-317219 X-HE-Meta: U2FsdGVkX1+Sbl6hmZ7p6qXUbTNjgtCbeoEyAE8OMSi0r92rJENlc5mK20ykq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U3qboBhESfgHvgXjmFaWfvQotx/qE3i4lR8AcI0sj+Mj+copc759HCCm+581n/5FaApy5aASJ+x1FLpeHXTrZBHXmL/txFFbHANn5lGmYPsQFJJP725zl/SpqW7xXr4JTy17Ukm1jNcgn/va58/fsFQrqKMwfiO 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: List-Subscribe: List-Unsubscribe: On Fri, Jun 13, 2025 at 09:57:23PM +0200, David Hildenbrand wrote: > What I meant is: > > Assume we have a pagecache page mapped into our page tables R/O (MAP_PRIVATE > mapping). > > During a write fault on such a pagecache page, we end up in > do_wp_page()->wp_page_copy() we perform the copy via __wp_page_copy_user() > without the folio lock. Yes, it would be similar to doing hugetlb_fault()->hugetlb_no_page() which would map it R/O. Then, if we write to it, we will go to hugetlb_wp(). Since it is a private mapping, we would only need to lock the folio to see if we can re-use it (the wp_can_reuse_anon_folio() analog to hugetlb). > In wp_page_copy(), we retake the pt lock, to make sure that the page is > still mapped (pte_same). If the page is no longer mapped, we retry the > fault. > > In that case, we only want to make sure that the folio is still mapped after > possibly dropping the page table lock in between. > > As we are holding an additional folio reference in > do_wp_page()->wp_page_copy(), the folio cannot get freed concurrently. > > > There is indeed the do_cow_fault() path where we avoid faulting in the > pagecache page in the first place. So no page table reference, an I can > understand why we would need the folio lock there. But do_cow_fault() does take a reference via __do_fault()->filemap_fault(). > Regarding hugetlb_no_page(): I think we could drop the folio lock for a > pagecache folio after inserting the folio into the page table. Just like > do_wp_page()->wp_page_copy(), we would have to verify again under PTL if the > folio is still mapped > > ... which we already do through pte_same() checks? But that is somewhat similar what we do in the generic faulting path. Assume you fault in a file for a private mapping and do COW. So, do_pte_missing()->do_fault()->do_cow_fault(). do_cow_fault()->__do_fault() will a) get a reference and b) lock the folio. And then we will proceed with copying the file to the page we will map privately. This would be something like hugetlb_fault()->hugetlb_no_page()->hugetlb_wp(). So we have to hold the lock throughout hugetlb_wp() for file pages we are copying to private mappings. Now, let us assume you map the file R/O. And after a while you write-fault to it. In the generic faulting path, that will go through: do_pte_missing()->do_fault()->do_read_fault() do_wp_page()->wp_page_copy() wp_page_copy(), which indeed doesn't hold the lock (but takes a reference). Maybe it's because it's Friday, but I'm confused as to why do_pte_missing()->do_fault()->do_cow_fault() holds the lock while do_wp_page() doesn't although it might the file's page we have to copy. -- Oscar Salvador SUSE Labs