From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795044341; b=W9D3vDiPgSYLbJFZhpAN02Npp8UrGzeaLiXocr2aPA60rhRFGEBRVLFxPgV22OJFV6Yb f661M5Xq1+6MpIxN29KmPIxzNjofi+kZfg4jk4rdCKQBSvjTCrg7l27yvdIh36M1vXY4j DprsprLkWcRjb4NG08pLK7Wdh5FXv/lc9Cjlhk1Rr3LzM8jSgNaFaSlThMr47gyJJz0Fo hODod15HZvxv8/i1R/5U8cw2QCfo0sbAe4Sq/xGk/72V+HVCuQsp4ZtoLZmncsjGTXSy+ JFZSHd1zkm7xKBDpWuKfku78tCw9zgyJMz5K/df7HgAAUeRRM1PHOgjJqQ2FPm1Xrwg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795044341; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=WOYda5e3A5KKvpqZuIuLhWTICxZNXazCCYpYFCxSe84=; b=x6guCawJxYup4JW0DOdFXH9ZQNcQMb5n9zUTGYEi6cH2EjTJNCn89ea7d8e276gCh9aZ Kn5QXQj+wEXzosl4h2Xz13L6Kf7dCUVGtZ5PUaOCIMzHNDeSFMraONZPJen8aPS+t0Zbp 4tvJjYlpusXYnZpC8hUFqH+/rnXykAhTwskMxNA+ic8wEccoOcUMaaM0iO5+RNik6KGZQ BuoLhvh5pclnnN2u8vbBHF6C/+heTDNJgB6/axXXKlC72kYrKU5gSh8hXmjnwBA8r0KrV jcNYDGtTLL/r0zhRWe7C3bO1h0xltY+nPVQ9jbFEQWC4t3/0GkmOuDZc62Uq3G2X5jA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XSiYbTJC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A58AB606A31C; Wed, 18 Nov 2026 23:25:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XSiYbTJC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F8531F000C5; Wed, 18 Nov 2026 23:25:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795044341; bh=WOYda5e3A5KKvpqZuIuLhWTICxZNXazCCYpYFCxSe84=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=XSiYbTJCEe0e0ylf9UWuraISb3AzIJ/LS5jmctUicP9oaWDAPyxWCDQpGH4Q56k36 LUt2l4uzZW4xTSJazzR4gkHzmM/mRn9+jBpqa6NpGYlOiXjtuwLuEdAtz3hRywGIRB dmlm6xW7btWMGgHuASEZ1ysowr9I4l/fq7K4zqMHbfyxhDFr7RmNVZwLtPRJ0IlcDf wFzjz03CiHoQWgXj92mjkmmCOt2RJOVy1AvDPcJPAFs7pJNiL9v5fqPOCdBU5nW09Q 9BtW4/+FXhjaLNy2ISv3IaEF5FRVT1549fsrDebR9exH9XWhND7vbuoQ4vBaJq2gGw so5R22LhA3YSA== Message-ID: Date: Thu, 19 Nov 2026 00:25:31 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] mm/gup: make mlock() and MAP_POPULATE handle VM_FAULT_RETRY properly To: harrison@crosse.dev, Jason Gunthorpe , John Hubbard , Peter Xu , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Shuah Khan , Yeoreum Yun Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Josef Bacik , Johannes Weiner , Jan Kara , stable@vger.kernel.org References: <20261118-mmpop-populate-v1-0-9c561d178d11@crosse.dev> <20261118-mmpop-populate-v1-1-9c561d178d11@crosse.dev> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHw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TnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <20261118-mmpop-populate-v1-1-9c561d178d11@crosse.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/18/26 22:56, Harrison Crosse via B4 Relay wrote: > From: Harrison Crosse > > If every read of a page of a file mapping fails, mmap(MAP_POPULATE) > and mlock() of that mapping do not return: the task spins in > __mm_populate(), submitting a new read of the page on each iteration, > until it gets a fatal signal. Touching the same page gets SIGBUS, and > madvise(MADV_POPULATE_READ) fails with -EFAULT. > > The first fault on the page reaches filemap_fault() with > FAULT_FLAG_ALLOW_RETRY and without FAULT_FLAG_TRIED. filemap_fault() > releases the mmap_lock to read the page and, because it released the > lock, returns VM_FAULT_RETRY before it looks at the result of the read. > It cannot report the error then: a fault that has released the lock > returns VM_FAULT_RETRY or VM_FAULT_COMPLETED, and neither carries an > error. faultin_page() turns VM_FAULT_RETRY into -EBUSY, so > __get_user_pages() stops at that page without an error, and > __mm_populate() calls populate_vma_page_range() again at that address, > again without FOLL_TRIED. The page fault handlers and > __get_user_pages_locked() retry with FAULT_FLAG_TRIED instead, and on > that attempt filemap_fault() reads the page with the lock held and > returns VM_FAULT_SIGBUS if the read fails. > > Commit 631426ba1d45 ("mm/madvise: make MADV_POPULATE_(READ|WRITE) handle > VM_FAULT_RETRY properly") fixed the same loop for MADV_POPULATE_* by > calling __get_user_pages_locked(). That does not fit here: > __get_user_pages_locked() retakes the mmap_lock itself and carries on > with the GUP flags it was given, while populate_vma_page_range() > derives those flags from the VMA that __mm_populate() looks up again, > and may skip, after each unlock. Retrying with a NULL locked instead > would drop FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_KILLABLE, so a > userfaultfd range would fail rather than wait, and folio lock waits > would not be killable. > > Instead, have __mm_populate() remember the address of the page whose > fault released the mmap_lock. When the next iteration starts at that > address, fault in that page alone with FOLL_TRIED, as > __get_user_pages_locked() does, then continue with the rest of the > range as before. The retry reads the page with the mmap_lock held, as > filemap_fault() did for a page that was not uptodate before v5.1, and > as the other GUP retries do. With a persistent read error the retry > fails with -EFAULT: mlock() returns -ENOMEM > (__mlock_posix_error_return() maps -EFAULT to it), and MAP_POPULATE, > which ignores populate errors, returns the mapping. A fatal signal > still ends the loop, with -EINTR from __get_user_pages() or, if it > interrupts the FOLL_TRIED read, with -EFAULT. The other callers of > populate_vma_page_range() pass a NULL locked, so they cannot get > VM_FAULT_RETRY and are unchanged. > > Reproduced with ext4 on dm-linear over a loop device, with the eight > sectors of one page of a file mapped to the dm "error" target, on arm64 > (Debian's 6.12.116 and v7.4-rc3) and x86_64 (v7.4-rc3): mmap(MAP_POPULATE) > and mlock() of the file were still running in the kernel after 5 seconds > and ended when sent SIGKILL. On 6.12.116, the block layer tracepoints > showed about 280,000 bios a second being submitted and failed. > > The analysis, the patch and this changelog were prepared with the help > of an AI coding assistant (Claude). Yes, that's rather obvious from looking at it, thanks for documenting that :) Thanks for the report! [...] > + * > + * filemap_fault() reports a read error only when > + * FAULT_FLAG_TRIED is set: on a first attempt it releases the > + * mmap_lock for the read and returns VM_FAULT_RETRY whatever > + * the result. So when the last call released the lock at > + * nstart, retry that page with FOLL_TRIED, and only that page, > + * so that later pages still get a first attempt that releases > + * the lock for the read. Compare the address because nstart > + * moves past that page if it is no longer mapped, or its VMA > + * is skipped, when the lock is retaken. > */ > - ret = populate_vma_page_range(vma, nstart, nend, &locked); > + tried = retry && nstart == retry_addr; > + ret = __populate_vma_page_range(vma, nstart, > + tried ? nstart + PAGE_SIZE : nend, > + &locked, tried); Yeah, that's quite the ugly code. Let me take care of this (or find someone more familiar with that code that can help). @Yeoreum, do you have some spare cycles? -- Cheers, David