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 CEFFC1EB9F4 for ; Mon, 21 Oct 2024 17:23:39 +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=1729531421; cv=none; b=AliC1Mse/vVhh/fAwVVL0lxdEuo5q74jglaIhtEl0Y55OaOQoOqoSJeO55RM9Uk6L4Dj9LDTgpGKW62kIL/+zlbhDMCUX0ZU58gQ/ZPNITVE6k77/LDjl+nyg9qNoW5hEYM/2Gfv3hh86+NIV/vpOJ4lZtfZHKzWRY48S2kKU/8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729531421; c=relaxed/simple; bh=oKEWf7RfP+QVQ2j4D3wNCFmA7AZfNFJWNtDM/GP2Y90=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=MTKiqW2+mvgw6zvTzKDjN7q2fYBsjstdPvwHmHt2N2zgH/GJiEoOGjAWUU6h6S+xvLM2JNEkICAsfv75JtmWtVi8oFevgTEbda9R00bAeK8MZ1x2zq8TFmJc0LQyqNMAG+wZjvKdV3RND46XgpJsV55km3lDLdT8O4xewV0LHbA= 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=Y+V8S9Uo; 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="Y+V8S9Uo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729531418; 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=rbtgwTTdsrp9C2vvtezGlmmInm+A/tBxzwCFhKgoKoU=; b=Y+V8S9UotZ9u0flnD8EpXXGsDpss+JjMi14gN8rik8flcUb9r3R8PgwF4B34Z0OHg33HmU d9lyrgKIMhvUuH+jnLHLP5OUIj1wAztg7swImFE8iCRuLPvfD9DwDL8fytB4JKR7yxKT75 oI2katCZ/qKt+wZcq3g+osaISKxl3tQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-xAVuVzSMOFmnuguh_NYeNQ-1; Mon, 21 Oct 2024 13:23:37 -0400 X-MC-Unique: xAVuVzSMOFmnuguh_NYeNQ-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-43159603c92so33241725e9.2 for ; Mon, 21 Oct 2024 10:23:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729531416; x=1730136216; 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=rbtgwTTdsrp9C2vvtezGlmmInm+A/tBxzwCFhKgoKoU=; b=I4Kxp7r/Dnqa3mJdSpnp3du6lTPJ8kejsg6JvxHBp0piB6m08ffiJO6aoA+GPyTX9M BD5m062/knJXjXiWhw1oprCv3rJE9bSlx/rgrl9ngVw9DmVgIM99bpKQOUqp0cRxW/gW xd3rZi07hgdjTWTq6AcDXS1H3utloFDvFAYPMB1PpzMm2IE7HRM4SRdJBmzXTEP//11w jrluh+WDRE8fm/wGozIAo4IQ3lCJO5LihknBDM7DyAoxgpRB+ogBWQwu1xiqsp8ZHsVw hR3xofuZsEzDzodnNRSRDgmf8K07sUo20rXXcfA2DBgGOKRYewnpdgKXS2eEMK3hmxWa nAhA== X-Forwarded-Encrypted: i=1; AJvYcCUaMUvIRqBfdNckoF6otmn1WuyUAQEz0CLFNOmR2T3TU4icMR2yYBYiLKZzw7I0UBeYxaBoQK3P8uQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yzuso7MphZCs8I2U5IbA0f2tqx+pGrLRg7Kcz1unSaEkxRdsqng OvdNYvIEZsGaFRQnDDehlcM7j6PrrFKTHtfEt7H3ThLRHOmA5wgGXxyQUgvfS/O0/ZAXJYVQWik ZpwNBo4gKYMYgsAIXJN8rer7G0PGw6XuXTNB2KPRp3FyUNz/UTBrh75WKpg== X-Received: by 2002:a05:600c:1c1e:b0:431:5f3b:6ae4 with SMTP id 5b1f17b1804b1-4316165c74bmr97523375e9.17.1729531416341; Mon, 21 Oct 2024 10:23:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzP1uIzlXg5GGh+u/9IkOAPz8nGqfNtQ9p+gPX86cgcCut8zBrFia3MvvSCDy9Y4jnTClc5A== X-Received: by 2002:a05:600c:1c1e:b0:431:5f3b:6ae4 with SMTP id 5b1f17b1804b1-4316165c74bmr97523135e9.17.1729531415890; Mon, 21 Oct 2024 10:23:35 -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 5b1f17b1804b1-4316f571201sm64618285e9.5.2024.10.21.10.23.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Oct 2024 10:23:34 -0700 (PDT) Message-ID: Date: Mon, 21 Oct 2024 19:23:32 +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 3/5] mm: madvise: implement lightweight guard page mechanism To: Lorenzo Stoakes Cc: Andrew Morton , Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , Vlastimil Babka , "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: 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/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+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xKNVfEQMQ 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: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 21.10.24 19:15, Lorenzo Stoakes wrote: > On Mon, Oct 21, 2024 at 07:05:27PM +0200, David Hildenbrand wrote: >> On 20.10.24 18:20, Lorenzo Stoakes wrote: >>> Implement a new lightweight guard page feature, that is regions of userland >>> virtual memory that, when accessed, cause a fatal signal to arise. >>> >>> Currently users must establish PROT_NONE ranges to achieve this. >>> >>> However this is very costly memory-wise - we need a VMA for each and every >>> one of these regions AND they become unmergeable with surrounding VMAs. >>> >>> In addition repeated mmap() calls require repeated kernel context switches >>> and contention of the mmap lock to install these ranges, potentially also >>> having to unmap memory if installed over existing ranges. >>> >>> The lightweight guard approach eliminates the VMA cost altogether - rather >>> than establishing a PROT_NONE VMA, it operates at the level of page table >>> entries - poisoning PTEs such that accesses to them cause a fault followed >>> by a SIGSGEV signal being raised. >>> >>> This is achieved through the PTE marker mechanism, which a previous commit >>> in this series extended to permit this to be done, installed via the >>> generic page walking logic, also extended by a prior commit for this >>> purpose. >>> >>> These poison ranges are established with MADV_GUARD_POISON, and if the >>> range in which they are installed contain any existing mappings, they will >>> be zapped, i.e. free the range and unmap memory (thus mimicking the >>> behaviour of MADV_DONTNEED in this respect). >>> >>> Any existing poison entries will be left untouched. There is no nesting of >>> poisoned pages. >>> >>> Poisoned ranges are NOT cleared by MADV_DONTNEED, as this would be rather >>> unexpected behaviour, but are cleared on process teardown or unmapping of >>> memory ranges. >>> >>> Ranges can have the poison property removed by MADV_GUARD_UNPOISON - >>> 'remedying' the poisoning. The ranges over which this is applied, should >>> they contain non-poison entries, will be untouched, only poison entries >>> will be cleared. >>> >>> We permit this operation on anonymous memory only, and only VMAs which are >>> non-special, non-huge and not mlock()'d (if we permitted this we'd have to >>> drop locked pages which would be rather counterintuitive). >>> >>> Suggested-by: Vlastimil Babka >>> Suggested-by: Jann Horn >>> Suggested-by: David Hildenbrand >>> Signed-off-by: Lorenzo Stoakes >>> --- >>> arch/alpha/include/uapi/asm/mman.h | 3 + >>> arch/mips/include/uapi/asm/mman.h | 3 + >>> arch/parisc/include/uapi/asm/mman.h | 3 + >>> arch/xtensa/include/uapi/asm/mman.h | 3 + >>> include/uapi/asm-generic/mman-common.h | 3 + >>> mm/madvise.c | 168 +++++++++++++++++++++++++ >>> mm/mprotect.c | 3 +- >>> mm/mseal.c | 1 + >>> 8 files changed, 186 insertions(+), 1 deletion(-) >>> >>> diff --git a/arch/alpha/include/uapi/asm/mman.h b/arch/alpha/include/uapi/asm/mman.h >>> index 763929e814e9..71e13f27742d 100644 >>> --- a/arch/alpha/include/uapi/asm/mman.h >>> +++ b/arch/alpha/include/uapi/asm/mman.h >>> @@ -78,6 +78,9 @@ >>> #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ >>> +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ >>> +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ >> >> Just to raise it here: MADV_GUARD_INSTALL / MADV_GUARD_REMOVE or sth. like >> that would have been even clearer, at least to me. > > :) > > It still feels like poisoning to me because we're explicitly putting > something in the page tables to make a range have different fault behaviour > like a HW poisoning, and 'installing' suggests backing or something like > this, I think that's more confusing. I connect "poison" to "SIGBUS" and "corrupt memory state", not to "there is nothing and there must not be anything". Thus my thinking. But again, not the end of the world, just wanted to raise it ... > >> >> But no strong opinion, just if somebody else reading along was wondering >> about the same. >> >> >> I'm hoping to find more time to have a closer look at this this week, but in >> general, the concept sounds reasonable to me. > > Thanks! Thank you for implementing this and up-streaming it! -- Cheers, David / dhildenb