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 D279213DDD3 for ; Thu, 1 Aug 2024 06:36: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=1722494200; cv=none; b=BGEXTSeJrBI+L1NnlltC62GKZIa696+eu5XVUd6AVG1Tzsgs4kQ1yqnB49i36E7xF+tueMMuwheoDJDp46PubjYPS8QdPg1A7tuEN6okpP/uw7DfwYMENG71fQ9IntP4is/ziCn/ILrctN49zimB4/vOk05pQknZgB1f/VVMJuw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722494200; c=relaxed/simple; bh=RPeYw82iI2yi4hCIhCEySoLskBW6g5ofsGyj6THEYuM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=TAdVwipS7q1BB8X3SuB1yYNYj6a+cUBJPss1L7RdVsHGoCV0n58Ie1/FO2ENil74AT1UqCZdYeN0qnIuByyYfm7VuLUTx7C48ZypmqjZiDIFO3a8nwlccF+pKrqJk2LeDUYicDxy7BLDhDtQgm9k4eTrZpV0+qk8W06aObm8Q/A= 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=Uwnku9qR; 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="Uwnku9qR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722494197; 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=cb/totuq+JgGG0BzVAs7Wb+RYqNKxWlie6oJKLYe5lo=; b=Uwnku9qRMlGlhHiqBdMveWm10+hxvoH3DeVUAhShd0PeFZRMaTX7oA/1Tl8T7me2VNhLLs yrJNjViiAE7GrzxG6oly1H+1TorWBOcsE4WLfiRh/xCmxOgigpzb652ERZ4Vd65rwHYOxg lEEC9t/JAbKFxNtDv17H+/6NkdgfRps= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-353-WyQz8OKoPy6nsHmynj3zvQ-1; Thu, 01 Aug 2024 02:36:36 -0400 X-MC-Unique: WyQz8OKoPy6nsHmynj3zvQ-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-368442fef36so4246446f8f.0 for ; Wed, 31 Jul 2024 23:36:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722494195; x=1723098995; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from: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=cb/totuq+JgGG0BzVAs7Wb+RYqNKxWlie6oJKLYe5lo=; b=u2mB7Ja/MZmCQ+d8ZRt9jwhl2frPANu8f3Phi8qvn6CBkdnvMfOUW401OFVgdXynFq mR9XyAHfEashRs/nNNlg49+jV6GETfuZJ0d4v6n/BOZrflVAPDSSM7JoNKTuKDHNwKjH DZk9pvsJmVxdQCzCaEE3wZk3FQcLm6RP9vKVWUzMYHt7FsoW7XS9U3UvfekZS74ztEqY De7tpa3WZHXXk/cmdqpvSiVwADqR4BcihpVc9wnWzMFTcEDIzAvWAa4Iw3t9jmkTI/uG CTb92/193k+pA5XHR0oTj+G8N3OfCjM6TIb5zIv5KHBuB7pl8ARr+Kk+2j+0wyvEf5Nm njdg== X-Forwarded-Encrypted: i=1; AJvYcCXw9DjUboWN3Bpg2iKnQ6uev1AYHPG70JyyAJ4YxAPaE0FgnwvWG1bzKpuHUPaGdeTQVdw5dpPDw5LIAHd+KBWarSOIu9DHmDs3 X-Gm-Message-State: AOJu0YyZduKTSbb1qOgGDEKSfhsMR0v835FkGJxpkRP/P3E5DUVF+WHF wzkxWn2n60glTdWwO/sHjMq+IZZI6brn7H85NPwQh9C2YRDXJSXoE2ARN0Z2+4kxUS7quOEAsh3 5eu4p96tehcWviubzEqCUtNDkFoPd27CHmcoLmUvfsBOAVXhgNrMuc/dtlw== X-Received: by 2002:adf:e992:0:b0:367:dc45:55ab with SMTP id ffacd0b85a97d-36baade518bmr1452524f8f.25.1722494195235; Wed, 31 Jul 2024 23:36:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpxdLRTotVTjaWrDhucSBtKH5+dM+6yFVHA+rrdGMckWavDs9lo1Fs30CGNFwjyA3K7VXLvw== X-Received: by 2002:adf:e992:0:b0:367:dc45:55ab with SMTP id ffacd0b85a97d-36baade518bmr1452484f8f.25.1722494194697; Wed, 31 Jul 2024 23:36:34 -0700 (PDT) Received: from ?IPV6:2003:cb:c707:5c00:e650:bcd7:e2a0:54fe? (p200300cbc7075c00e650bcd7e2a054fe.dip0.t-ipconnect.de. [2003:cb:c707:5c00:e650:bcd7:e2a0:54fe]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367d95c7sm18524076f8f.37.2024.07.31.23.36.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 31 Jul 2024 23:36:34 -0700 (PDT) Message-ID: <58025293-c70f-4377-b8be-39994136af83@redhat.com> Date: Thu, 1 Aug 2024 08:36:32 +0200 Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/6] mm: split underutilized THPs To: Usama Arif , akpm@linux-foundation.org, linux-mm@kvack.org Cc: hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, roman.gushchin@linux.dev, yuzhao@google.com, baohua@kernel.org, ryan.roberts@arm.com, rppt@kernel.org, willy@infradead.org, cerasuolodomenico@gmail.com, corbet@lwn.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kernel-team@meta.com References: <20240730125346.1580150-1-usamaarif642@gmail.com> <3cd1b07d-7b02-4d37-918a-5759b23291fb@gmail.com> <73b97a03-3742-472f-9a36-26ba9009d715@gmail.com> <95ed1631-ff62-4627-8dc6-332096e673b4@redhat.com> <01899bc3-1920-4ff2-a470-decd1c282e38@gmail.com> <4b9a9546-e97b-4210-979b-262d8cf37ba0@redhat.com> <64c3746a-7b44-4dd6-a51b-e5b90557a30a@gmail.com> <204af83b-57ec-40d0-98c0-038bfeb393a3@gmail.com> From: David Hildenbrand 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/E8M7TEw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9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <204af83b-57ec-40d0-98c0-038bfeb393a3@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit >> I just added a bunch of quick printfs to QEMU and ran a precopy+postcopy live migration. Looks like my assumption was right: >> >> On the destination: >> >> Writing received pages during precopy # ram_load_precopy() >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Writing received pages during precopy >> Disabling THP: MADV_NOHUGEPAGE # postcopy_ram_prepare_discard() >> Discarding pages # loadvm_postcopy_ram_handle_discard() >> Discarding pages >> Discarding pages >> Discarding pages >> Discarding pages >> Discarding pages >> Discarding pages >> Registering UFFD # postcopy_ram_incoming_setup() >> > > Thanks for this, yes it makes sense after you mentioned postcopy_ram_incoming_setup. > postcopy_ram_incoming_setup happens in the Listen phase, which is after the discard phase, so I was able to follow in code in qemu the same sequence of events that the above prints show. I just added another printf to postcopy_ram_supported_by_host(), where we temporarily do a UFFDIO_REGISTER on some test area. Sensing UFFD support # postcopy_ram_supported_by_host() Sensing UFFD support Writing received pages during precopy # ram_load_precopy() Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Writing received pages during precopy Disabling THP: MADV_NOHUGEPAGE # postcopy_ram_prepare_discard() Discarding pages # loadvm_postcopy_ram_handle_discard() Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Discarding pages Registering UFFD # postcopy_ram_incoming_setup() We could think about using this "ever user uffd" to avoid the shared zeropage in most processes. Of course, there might be other applications where that wouldn't work, but I think this behavior (write to area before enabling uffd) might be fairly QEMU specific already. Avoiding the shared zeropage has the benefit that a later write fault won't have to do a TLB flush and can simply install a fresh anon page. >> >> Let me know if you need more information. >> >>> Thanks for pointing to mm_forbids_zeropage. Incorporating that into the code, and if I am (hopefully :)) right about qemu and kernel above, then I believe the right code should be: >> >> I'm afraid you are not right about the qemu code :) >> > > Yes, and also didn't consider MADV_DONTNEED! Thanks for explaining both of these things clearly. Its clear that pte_clear won't work in this case. > > We don't need to clear_pte, just use zero_page for all cases. The original series from Alex did tlb flush, but looking further at the code, thats not needed. try_to_migrate() flushes tlb and installs migration entries which are not ‘present’ so will never be tlb cached. remove_migration_ptes() restores page pointers so tlb flushing is not needed. When using zeropage, we don't need make a distinction if uffd is used or not. i.e. we can just do below: > > if (contains_data || mm_forbids_zeropage(pvmw->vma->vm_mm)) It's worth noting that on s390x, MMs that forbid the zeropage also have THPs disabled. So we shouldn't really run into that that often (of course, it's subject to change in the future, so we better have this check here). > return false; > > newpte = pte_mkspecial(pfn_pte(page_to_pfn(ZERO_PAGE(pvmw->address)), > pvmw->vma->vm_page_prot)); > > set_pte_at(pvmw->vma->vm_mm, pvmw->address, pvmw->pte, newpte); We're replacing a present page by another present page without doing a TLB flush in between. I *think* this should be fine because the new present page is R/O and cannot possibly be written to. -- Cheers, David / dhildenb