From mboxrd@z Thu Jan 1 00:00:00 1970 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 25F2712AAF3 for ; Thu, 4 Apr 2024 15:44:24 +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=1712245466; cv=none; b=a0LTWowLO+Oqr57O9aRMdAKjWSXTkmG14FqJPSzVptb39SB3B2PmrMPo31U79vkzJiBJ9q2ALM8sqjIZJpTHEy7Gns2HXEEacRjnWndRkBKzrUszLzBniNXiI4PIKrxo4kD3CcahB+TQvfdL6e/Cd1DRVwUeiAq3Tle+cH6pjQE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712245466; c=relaxed/simple; bh=vUEXAE1ExZO22iPT/l5kPykxw4x6Cg0n7E0NIdHXubk=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=u0D65s40RJp3cqyI2jUPnPs4kj6iRecjH/qEmrDtUv9a1ocihoTbDG3NsSZDMwkBCOhy6RKlzIdM5lCeQ4XptJ14CrK4nAtPeBNjw0W1eB0BFMNV+V77oxWIn4BZHcSug50BURaD8QiF9/5aNFNNyndn5XESZnl1w+0CZKsI3ac= 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=MtK9UE+G; 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="MtK9UE+G" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712245464; 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=vSFCbNcwIq07vP682jf5aQYwrzaaEsHEuv2Mm/EFPIc=; b=MtK9UE+GgYtWxMWaOGmqZ7Q6oE1wcSFIeXyPLbusnlosW+RdvN/DqLKEgKywg3tIQn+/Lo VQMxy6ISen/2+dJxOzzEtxcEEcrKLAQQQZv2W6sO1Ics1EellMFhCw2zvIyImUOqlSqfZ6 djO6r2tbttc5Uo3ocn5NGFfYGb8+CmM= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-369-TzsA5RFtMbu9UdjAXBLz1A-1; Thu, 04 Apr 2024 11:44:21 -0400 X-MC-Unique: TzsA5RFtMbu9UdjAXBLz1A-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3437032fe82so1171917f8f.0 for ; Thu, 04 Apr 2024 08:44:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712245460; x=1712850260; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vSFCbNcwIq07vP682jf5aQYwrzaaEsHEuv2Mm/EFPIc=; b=B3zKMuUzZNPwH/WmqXfKGKRUWPuJ3wJx0Y9gRMPDwNp0ZpeBcWzmqbYL/8ufSisOY1 IVytXpEkqoBGvDmg+OZOIusD7StHS7OqLP49sFgtvU5DqM+tWeyapzcJI2vlUc619a1D NcNFEGuFAEOCOp5KZ8SI29TkEcpgrrx27Q0kE7sn+izpRwMOj1agaT77S64z8zC4rYix sVuVw0gc4cO163Rrp6YsP3AXTkRgR20TSd7TdzAUzIeCQ/SOZvqUPStOc/qPL31OAlNw 5msZDj8XMHtVse2SRCE7oKICJ/gNxN3IttitJWmpLsLJkWGPT0NLNgqnRBgb+ktaPzYK Sccw== X-Forwarded-Encrypted: i=1; AJvYcCXseuOAiMum8TU8dObPXP1C1yfWOVKSmRJQUMpOhAvRyrXycnWzcZ/Eton1CXOISCBpw65qFtDE1Ktx3bMARQEdn8UFyJXkToE6JoJ/ X-Gm-Message-State: AOJu0YyOu/TGkrR7hNBvkAiVrayQiddXWjRyn+oi2tYy1cdHuhtHII2z lIAvgpE18el5L9ZjCOR602CoFiFaDrLT/fvhbeVWEQtQizJe7NWMc+9fGHnBe4ulI95OIIKD0O6 RXPSUIWlnLhPcBrp1NfPOAwhbeDhtf449k1LZI12Eps7nAf+k3lLFg5O7Ey49yw== X-Received: by 2002:adf:e702:0:b0:343:8022:dd08 with SMTP id c2-20020adfe702000000b003438022dd08mr3103487wrm.0.1712245460464; Thu, 04 Apr 2024 08:44:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFN2CJF3oIK4dnV/TEXgxl3MIzYjlSGFX1sv9jk5lJQFvPHeGcLCBfb34ox+kHl6yHVh+2enw== X-Received: by 2002:adf:e702:0:b0:343:8022:dd08 with SMTP id c2-20020adfe702000000b003438022dd08mr3103464wrm.0.1712245460113; Thu, 04 Apr 2024 08:44:20 -0700 (PDT) Received: from ?IPV6:2003:cb:c743:de00:7030:120f:d1c9:4c3c? (p200300cbc743de007030120fd1c94c3c.dip0.t-ipconnect.de. [2003:cb:c743:de00:7030:120f:d1c9:4c3c]) by smtp.gmail.com with ESMTPSA id bs26-20020a056000071a00b003439b45ca08sm4625123wrb.17.2024.04.04.08.44.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Apr 2024 08:44:19 -0700 (PDT) Message-ID: <42dbf562-5eab-4f82-ad77-5ee5b8c79285@redhat.com> Date: Thu, 4 Apr 2024 17:44:18 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH 0/4] KVM: x86/mmu: Rework marking folios dirty/accessed To: Sean Christopherson Cc: David Matlack , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Stevens , Matthew Wilcox References: <20240320005024.3216282-1-seanjc@google.com> <4d04b010-98f3-4eae-b320-a7dd6104b0bf@redhat.com> Content-Language: en-US From: David Hildenbrand 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+N42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Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 04.04.24 00:19, Sean Christopherson wrote: > On Wed, Apr 03, 2024, David Hildenbrand wrote: >> On 03.04.24 02:17, Sean Christopherson wrote: >>> On Tue, Apr 02, 2024, David Hildenbrand wrote: >>> Aha! But try_to_unmap_one() also checks that refcount==mapcount+1, i.e. will >>> also keep the folio if it has been GUP'd. And __remove_mapping() explicitly states >>> that it needs to play nice with a GUP'd page being marked dirty before the >>> reference is dropped. >> >>> >>> * Must be careful with the order of the tests. When someone has >>> * a ref to the folio, it may be possible that they dirty it then >>> * drop the reference. So if the dirty flag is tested before the >>> * refcount here, then the following race may occur: >>> >>> So while it's totally possible for KVM to get a W=1,D=0 PTE, if I'm reading the >>> code correctly it's safe/legal so long as KVM either (a) marks the folio dirty >>> while holding a reference or (b) marks the folio dirty before returning from its >>> mmu_notifier_invalidate_range_start() hook, *AND* obviously if KVM drops its >>> mappings in response to mmu_notifier_invalidate_range_start(). >>> >> >> Yes, I agree that it should work in the context of vmscan. But (b) is >> certainly a bit harder to swallow than "ordinary" (a) :) > > Heh, all the more reason to switch KVM x86 from (b) => (a). :) > >> As raised, if having a writable SPTE would imply having a writable+dirty >> PTE, then KVM MMU code wouldn't have to worry about syncing any dirty bits >> ever back to core-mm, so patch #2 would not be required. ... well, it would >> be replaces by an MMU notifier that notifies about clearing the PTE dirty >> bit :) > > Hmm, we essentially already have an mmu_notifier today, since secondary MMUs need > to be invalidated before consuming dirty status. Isn't the end result essentially > a sane FOLL_TOUCH? Likely. As stated in my first mail, FOLL_TOUCH is a bit of a mess right now. Having something that makes sure the writable PTE/PMD is dirty (or alternatively sets it dirty), paired with MMU notifiers notifying on any mkclean would be one option that would leave handling how to handle dirtying of folios completely to the core. It would behave just like a CPU writing to the page table, which would set the pte dirty. Of course, if frequent clearing of the dirty PTE/PMD bit would be a problem (like we discussed for the accessed bit), that would not be an option. But from what I recall, only clearing the PTE/PMD dirty bit is rather rare. I think this GUP / GUP-user setting of folio dirty bits is a bit of a mess, but it seems to be working so far, so ... who am I to judge :) -- Cheers, David / dhildenb