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.133.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 1551A1CF28A for ; Mon, 21 Oct 2024 16:00:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729526429; cv=none; b=GLwtThZEKHEOQNjE7uSBiDCptyNVEYnoD7lEK1Uxby29ZhRgGde8i7+RROt7LI+DNM7gOSkXwbilCVd7iop6qGuTivVji2EdbRJazMaFfIUS35eizD4c12K9OZlWwVI7QLAH1gkMa9mImVw4JUDQMK728v/znfH7nLryVtUss5I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729526429; c=relaxed/simple; bh=U8P/OFCCPZoyYnSkjN2vuTZuaoFFmM2gAUQ3I1AfQYg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=AWF8D+y8E/S0m244eD/cn/UmwqKnad0lCQ+sfSBB7mH+9xSqbp2xF7V7p9I5FKnLx/u7U30+m/Hj+jVN8w8eznSffQm/80FRnb6NLFp5AobLyTH4Ukf13qhjOac+p6cbsQJ41jAJ1K0ZVTobnmuzcLoiwM6FNI4ro4wDUDh+qnE= 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=KaH9gEEc; arc=none smtp.client-ip=170.10.133.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="KaH9gEEc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729526426; 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=5VsY7FPrNp4m1XJEjZKgoy8LpJYgb+68r1Vy+j6mDVY=; b=KaH9gEEcpRsZaTUv28CDpmilibl63T+okKfOJl/woebX5BMtuBUsgvlo1su4upCMEGLNRV p27feBC50FPX3g731Qg6C6NYWc1K3D7dBOQBjSBRM+yTzpXSxRCkzMiHxFo0c9i+CPQ6Zf XXTTrqN47cWtvqIVCd4G07Thf4fpMzw= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-458-Tsw-w2AxM2eBFquRin4vSg-1; Mon, 21 Oct 2024 12:00:24 -0400 X-MC-Unique: Tsw-w2AxM2eBFquRin4vSg-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4315ad4938fso34080495e9.0 for ; Mon, 21 Oct 2024 09:00:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729526423; x=1730131223; 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=5VsY7FPrNp4m1XJEjZKgoy8LpJYgb+68r1Vy+j6mDVY=; b=kcII+laSUzQGvJsVVGISyw8K8lkRx8NwNnG/yeKd1jqZY2GJSFxSuPYz1pFmt/AI0i 8g/RFryZKB23vvnQELXCRAKrsHjHFmvTqNWdF9AIfqo1xBW2daVB3ARMMbwtpwq7EhfF EzmHHMTYriqdEt13CEhSvU8REF4BOolCRpk/FhxZZ/5eZJxOmA4VDXy0vH0nwaJQoWa+ m2Q/yWjYFyXxkUSeJlA6okMw/1w4xqJIl5+WT4DG3hjRSI/lrxzlOtW2e7TzCsIwn31P Qf8HWmHpQmlLYTR2S6GM+7C22mrqpYr++sNT4m/jGx/6+r1HV4IPw1IZ4BStKgKylKV9 lD2Q== X-Forwarded-Encrypted: i=1; AJvYcCVNJJ8dMSQLjyb80mZfjSKPYMmBrRo5XSQCCVxn4pEwcPYmT+g+GnUaIkzYjqQnS6XMtRBod6quY9U=@vger.kernel.org X-Gm-Message-State: AOJu0Yy87DzyJDD4T+3jkA5p9neK83pWUpV+sVTi4WwyP0CjnAn02dsK 6E+YPNiJkd4hOo0E7oTfDdpIt68yNJPiRvQtLxijgDQytokig0mXcwU7Wn38XnzGe2oxfg+dkqG yauorju4rpKKqLo3rYzrnJUhs0wX62kZ2V7Bul9fPRPic6AqtjfpgIh4hnQ== X-Received: by 2002:a5d:4d92:0:b0:37d:4527:ba1c with SMTP id ffacd0b85a97d-37eb48a0950mr8454102f8f.49.1729526423166; Mon, 21 Oct 2024 09:00:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFqzeoFoaFHGhJoRGq8gloEj+8bzb49r8wGRuqyWhN1x9H9EeLTdyAQ/MXpihhxtbbMiv9R/Q== X-Received: by 2002:a5d:4d92:0:b0:37d:4527:ba1c with SMTP id ffacd0b85a97d-37eb48a0950mr8454067f8f.49.1729526422732; Mon, 21 Oct 2024 09:00:22 -0700 (PDT) Received: from ?IPV6:2003:cb:c728:1600:e899:f836:758b:6fef? (p200300cbc7281600e899f836758b6fef.dip0.t-ipconnect.de. [2003:cb:c728:1600:e899:f836:758b:6fef]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ee0a37b7dsm4645112f8f.24.2024.10.21.09.00.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Oct 2024 09:00:22 -0700 (PDT) Message-ID: Date: Mon, 21 Oct 2024 18:00:20 +0200 Precedence: bulk X-Mailing-List: linux-api@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/5] mm: add PTE_MARKER_GUARD PTE marker To: Lorenzo Stoakes , Vlastimil Babka Cc: Andrew Morton , Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , "Paul E . McKenney" , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E . J . Bottomley" , Helge Deller , Chris Zankel , Max Filippov , Arnd Bergmann , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, Shuah Khan , Christian Brauner , linux-kselftest@vger.kernel.org, Sidhartha Kumar , Jeff Xu , Christoph Hellwig , linux-api@vger.kernel.org, John Hubbard References: <081837b697a98c7fa5832542b20f603d49e0b557.1729440856.git.lorenzo.stoakes@oracle.com> <470886d2-9f6f-4486-a935-daea4c5bea09@suse.cz> <434a440a-d6a4-4144-b4fb-8e0d8535f03f@lucifer.local> <4f4e41f1-531c-4686-b44d-dacdf034c241@lucifer.local> 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+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OS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+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: <4f4e41f1-531c-4686-b44d-dacdf034c241@lucifer.local> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 21.10.24 17:33, Lorenzo Stoakes wrote: > On Mon, Oct 21, 2024 at 04:54:06PM +0200, Vlastimil Babka wrote: >> On 10/21/24 16:33, Lorenzo Stoakes wrote: >>> On Mon, Oct 21, 2024 at 04:13:34PM +0200, Vlastimil Babka wrote: >>>> On 10/20/24 18:20, Lorenzo Stoakes wrote: >>>>> Add a new PTE marker that results in any access causing the accessing >>>>> process to segfault. >>>>> >>>>> This is preferable to PTE_MARKER_POISONED, which results in the same >>>>> handling as hardware poisoned memory, and is thus undesirable for cases >>>>> where we simply wish to 'soft' poison a range. >>>>> >>>>> This is in preparation for implementing the ability to specify guard pages >>>>> at the page table level, i.e. ranges that, when accessed, should cause >>>>> process termination. >>>>> >>>>> Additionally, rename zap_drop_file_uffd_wp() to zap_drop_markers() - the >>>>> function checks the ZAP_FLAG_DROP_MARKER flag so naming it for this single >>>>> purpose was simply incorrect. >>>>> >>>>> We then reuse the same logic to determine whether a zap should clear a >>>>> guard entry - this should only be performed on teardown and never on >>>>> MADV_DONTNEED or the like. >>>> >>>> Since I would have personally put MADV_FREE among "or the like" here, it's >>>> surprising to me that it in fact it's tearing down the guard entries now. Is >>>> that intentional? It should be at least mentioned very explicitly. But I'd >>>> really argue against it, as MADV_FREE is to me a weaker form of >>>> MADV_DONTNEED - the existing pages are not zapped immediately but >>>> prioritized for reclaim. If MADV_DONTNEED leaves guard PTEs in place, why >>>> shouldn't MADV_FREE too? >>> >>> That is not, as I understand it, what MADV_FREE is, semantically. From the >>> man pages: >>> >>> MADV_FREE (since Linux 4.5) >>> >>> The application no longer requires the pages in the range >>> specified by addr and len. The kernel can thus free these >>> pages, but the freeing could be delayed until memory pressure >>> occurs. >>> >>> MADV_DONTNEED >>> >>> Do not expect access in the near future. (For the time >>> being, the application is finished with the given range, so >>> the kernel can free resources associated with it.) >>> >>> MADV_FREE is 'we are completely done with this range'. MADV_DONTNEED is 'we >>> don't expect to use it in the near future'. >> >> I think the description gives a wrong impression. What I think matters it >> what happens (limited to anon private case as MADV_FREE doesn't support any >> other) >> >> MADV_DONTNEED - pages discarded immediately, further access gives new >> zero-filled pages >> >> MADV_FREE - pages prioritized for discarding, if that happens before next >> write, it gets zero-filled page on next access, but a write done soon enough >> can cancel the upcoming discard. >> >> In that sense, MADV_FREE is a weaker form of DONTNEED, no? >> >>>> >>>> Seems to me rather currently an artifact of MADV_FREE implementation - if it >>>> encounters hwpoison entries it will tear them down because why not, we have >>>> detected a hw memory error and are lucky the program wants to discard the >>>> pages and not access them, so best use the opportunity and get rid of the >>>> PTE entries immediately (if MADV_DONTNEED doesn't do that too, it certainly >>>> could). >>> >>> Right, but we explicitly do not tear them down in the case of MADV_DONTNEED >>> which matches the description in the manpages that the user _might_ come >>> back to the range, whereas MADV_FREE means they are truly done but just >>> don't want the overhead of actually unmapping at this point. >> >> But it's also defined what happens if user comes back to the range after a >> MADV_FREE. I think the overhead saved happens in the case of actually coming >> back soon enough to prevent the discard. With MADV_DONTNEED its immediate >> and unconditional. >> >>> Seems to be this is moreso that MADV_FREE is a not-really-as-efficient >>> version of what Rik wants to do with his MADV_LAZYFREE thing. >> >> I think that further optimizes MADV_FREE, which is already more optimized >> than MADV_DONTNEED. >> >>>> >>>> But to extend this to guard PTEs which are result of an explicit userspace >>>> action feels wrong, unless the semantics is the same for MADV_DONTEED. The >>>> semantics chosen for MADV_DONTNEED makes sense, so MADV_FREE should behave >>>> the same? >>> >>> My understanding from the above is that MADV_FREE is a softer version of >>> munmap(), i.e. 'totally done with this range', whereas MADV_DONTNEED is a >>> 'revert state to when I first mapped this stuff because I'm done with it >>> for now but might use it later'. >> >> From the implementation I get the opposite understanding. Neither tears down >> the vma like a proper unmap(). MADV_DONTNEED zaps page tables immediately, >> MADV_FREE effectively too but with a delay depending on memory pressure. >> > > OK so based on IRC chat I think the conclusion here is TL;DR yes we have to > change this, you're right :) > > To summarise for on-list: > > * MADV_FREE, while ostensibly being a 'lazy free' mechanism, has the > ability to be 'cancelled' if you write to the memory. Also, after the > freeing is complete, you can write to the memory to reuse it, the mapping > is still there. > > * For hardware poison markers it makes sense to drop them as you're > effectively saying 'I am done with this range that is now unbacked and > expect to get an empty page should I use it now'. UFFD WP I am not sure > about but presumably also fine. > > * However, guard pages are different - if you 'cancel' and you are left > with a block of memory allocated to you by a pthread or userland > allocator implementation, you don't want to then no longer be protected > from overrunning into other thread memory. Agreed. What happens on MADV_DONTNEED/MADV_FREE on guard pages? Ignored or error? It sounds like a usage "error" to me (in contrast to munmap()). -- Cheers, David / dhildenb