Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 58EB119F430 for ; Mon, 11 Nov 2024 15:19:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731338380; cv=none; b=JGLyK8K9W162GeyVnVcJ2hPlJuZCRu64jmhy5+0Ifu6aQSl785PP229/syoXjvpCcYDlYbuoAdpkXlx37Y4aPtRjzND37q3X/DAPKLhg1FsTy25eoEyqVsagwnXraPuVk0duU9LTJuWtAct7UjozQQNijfyFVmrOidPzaBICq+M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731338380; c=relaxed/simple; bh=1YsuSbZdhdj+nzOteuEHJKEXq368N1weX0JfmHBo9GE=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=VjABTFcx7ndfS2pi2ti18B0c3f2L2MIQtMlhrrY5La2f/y1mc1//vDcRAEPCNUR2H7B+/rjpCl64tSF3fOPwFOtHQ6pIeDbdjqUTm8gnGLZMYcQT55p5pie0rw4mqvjfwSxjoprjYGMo3+RA2f/KYIxYW6oLFcrEMhdnlEMzXtY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Cgd81Iqv; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Cgd81Iqv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731338377; h=from:from: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:autocrypt:autocrypt; bh=XR+uLywBYEsaPwET8kKINJkIHaBnwvoIX1ih5UVS1og=; b=Cgd81IqvWSqLqXX1Bpc4w37HRve7wBXd2Bx2UrdB5FhIwQtAEKT+uavo3Ow8bXUYf5UW0g VG7/Fgcf5ZRpc2LYE5124LuSFaiLR94lb4qvpD+2jhksOpoKUvvJNTEsFymMDA4fz52AiA ufYrSY/96ktFmjXdF9jUzXpqpdh+NqI= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-379-5VjmjKVQPzywf6LG3Rk9HA-1; Mon, 11 Nov 2024 10:19:35 -0500 X-MC-Unique: 5VjmjKVQPzywf6LG3Rk9HA-1 X-Mimecast-MFC-AGG-ID: 5VjmjKVQPzywf6LG3Rk9HA Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4316e350d6aso33078405e9.3 for ; Mon, 11 Nov 2024 07:19:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731338374; x=1731943174; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=XR+uLywBYEsaPwET8kKINJkIHaBnwvoIX1ih5UVS1og=; b=QojFqrmmuk5CKEPnVx1gVMUs6+16/gPlNeXNSBD0iCzlhFoOJodCYXRo8jReHtqHhO TVm84sMoO6v4y7hTsLaqbitCsaR7HPn1OKe5KG0ry950GBDUDGWyMJv0QBKSa7OesTkt I/actBlhWmram9lTNqulrKLPmnatD1/1x+MLa//diVN7sorTQh/tOvxlp7pjU8wE9fLy xK53MVUVROgX1DYrNlKdoeMkAzERTyb7kCrgDF5PG6nJXTyhrWUOXt/fFuekRv8mE5nh CdsvbAaZANBNgwZ6JFOQ4nL/BUm1WybAlAWvIZAeyJFHHMOgs6gp0yUKVGKL6+Q1KzzR r3mA== X-Forwarded-Encrypted: i=1; AJvYcCW1BD52qr6Sr8D03JyAQQ8RkwDm6wvMw9rhiTLbSO9qTVlWsILz53l+YZx07jni2xDCtcI=@vger.kernel.org X-Gm-Message-State: AOJu0YxrtMOWZa8yuM3HMADKFnCj4nxUxThc71MHx5EGANfS/hbyhxKm qK3pZGli89ysySQvt5fEzjOZy9yAV/ku0Q1Dy85ljIUqOKTRG2ZC06SfP38t0u1dVZGedr2ieK5 j+WVJL+qJOWZ7boNpkw7VrC+wmSvOlneziH7r2FlH6WRx8ZTlbw== X-Received: by 2002:a05:600c:4508:b0:431:50cb:2398 with SMTP id 5b1f17b1804b1-432b74fecacmr105970205e9.2.1731338374644; Mon, 11 Nov 2024 07:19:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IHh+hmkIOXMCP4yAas6F1+n0X5j9NmS/QYrj3JR8+dkAxkVub/nmTpxMiLZzsi4GwGQHiaClA== X-Received: by 2002:a05:600c:4508:b0:431:50cb:2398 with SMTP id 5b1f17b1804b1-432b74fecacmr105969975e9.2.1731338374250; Mon, 11 Nov 2024 07:19:34 -0800 (PST) Received: from ?IPV6:2003:cb:c730:4300:18eb:6c63:a196:d3a2? (p200300cbc730430018eb6c63a196d3a2.dip0.t-ipconnect.de. [2003:cb:c730:4300:18eb:6c63:a196:d3a2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432aa70a1d8sm220764755e9.27.2024.11.11.07.19.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Nov 2024 07:19:33 -0800 (PST) Message-ID: Date: Mon, 11 Nov 2024 16:19:31 +0100 Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [ISSUE] split_folio() and dirty IOMAP folios From: David Hildenbrand To: Matthew Wilcox Cc: linux-fsdevel@vger.kernel.org, "linux-mm@kvack.org" , kvm@vger.kernel.org, Zi Yan , Christian Brauner , "Darrick J. Wong" , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Thomas Huth References: <4febc035-a4ff-4afe-a9a0-d127826852a9@redhat.com> <6099e202-ef0a-4d21-958c-2c42db43a5bb@redhat.com> Content-Language: en-US Autocrypt: addr=david@redhat.com; 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 B3JfpUqcWwE7vUaYrHG1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0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <6099e202-ef0a-4d21-958c-2c42db43a5bb@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 08.11.24 10:11, David Hildenbrand wrote: > On 07.11.24 21:20, Matthew Wilcox wrote: >> On Thu, Nov 07, 2024 at 05:34:40PM +0100, David Hildenbrand wrote: >>> On 07.11.24 17:09, Matthew Wilcox wrote: >>>> On Thu, Nov 07, 2024 at 04:07:08PM +0100, David Hildenbrand wrote: >>>>> I'm debugging an interesting problem: split_folio() will fail on dirty >>>>> folios on XFS, and I am not sure who will trigger the writeback in a timely >>>>> manner so code relying on the split to work at some point (in sane setups >>>>> where page pinning is not applicable) can make progress. >>>> >>>> You could call something like filemap_write_and_wait_range()? >>> >>> Thanks, have to look into some details of that. >>> >>> Looks like the folio_clear_dirty_for_io() is buried in >>> folio_prepare_writeback(), so that part is taken care of. >>> >>> Guess I have to fo from folio to "mapping,lstart,lend" such that >>> __filemap_fdatawrite_range() would look up the folio again. Sounds doable. >>> >>> (I assume I have to drop the folio lock+reference before calling that) >> >> I was thinking you'd do it higher in the callchain than >> gmap_make_secure(). Presumably userspace says "I want to make this >> 256MB range secure" and we can start by writing back that entire >> 256MB chunk of address space. >> >> That doesn't prevent anybody from dirtying it in-between, of course, >> so you can still get -EBUSY and have to loop round again. > > I'm afraid that won't really work. > > On the one hand, we might be allocating these pages (+disk blocks) > during the unpack operation -- where we essentially trigger page faults > first using gmap_fault() -- so the pages might not even exist before the > gmap_make_secure() during unpack. One work around would be to > preallocate+writeback from user space, but it doesn't sound quite right. > > But the bigger problem I see is that the initial "unpack" operation is > not the only case where we trigger this conversion to "secure" state. > Once the VM is running, we can see calls on arbitrary guest memory even > during page faults, when gmap_make_secure() is called via > gmap_convert_to_secure(). > > > I'm still not sure why we see essentially no progress being made, even > though we temporarily drop the PTL, mmap lock, folio lock, folio ref ... > maybe related to us triggering a write fault that somehow ends up > setting the folio dirty :/ Or because writeback is simply too slow / > backs off. > > I'll play with handling -EBUSY from split_folio() differently: if the > folio is under writeback, wait on that. If the folio is dirty, trigger > writeback. And I'll look into whether we really need a writable PTE, I > suspect not, because we are not actually "modifying" page content. The following hack makes it fly: case -E2BIG: folio_lock(folio); rc = split_folio(folio); + if (rc == -EBUSY) { + if (folio_test_dirty(folio) && !folio_test_anon(folio) && + folio->mapping) { + struct address_space *mapping = folio->mapping; + loff_t lstart = folio_pos(folio); + loff_t lend = lstart + folio_size(folio); + + folio_unlock(folio); + /* Mapping can go away ... */ + filemap_write_and_wait_range(mapping, lstart, lend); + } else { + folio_unlock(folio); + } + folio_wait_writeback(folio); + folio_lock(folio); + split_folio(folio); + folio_unlock(folio); + folio_put(folio); + return -EAGAIN; + } folio_unlock(folio); folio_put(folio); I think the reason why we don't make any progress on s390x is that the writeback will mark the folio clean and turn the folio read-only in the page tables as well. So when we lookup the folio again in the page table, we see that the PTE is not writable and trigger a write fault ... ... the write fault will mark the folio dirty again, so the split will never succeed. In above diff, we really must try the split_folio() a second time after waiting, otherwise we run into the same endless loop. I'm still not 100% sure if we need a writable PTE; after all we are not modifying page content. But that's just a side effect of not being able to wait for the split_folio() to make progress in the writeback case so we can retry the split again. -- Cheers, David / dhildenb