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 BE4521E1A16 for ; Mon, 21 Oct 2024 20:17:40 +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=1729541862; cv=none; b=dVDMHAjbShFIYQRHObZLz1UN7G8CtQLrUu4qa6FHqNLznETqNgmsih9cI6zEJrk+1qJErrPMqt1Zsy5c8/3ivH0DrGHOKkW6mBtRLdIzzOBHGkF8uAFTd98vctsnFiugq76EFV/g3e3L++6TrZFC+UuGuqlpDhhWQnG0+w1WU1k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729541862; c=relaxed/simple; bh=gKlCBgfS0AAaKqcA8VmWYf3KLgGgMvP22We/vPMIQpE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=UOILnEnANHoyMAQu93X7mEhqvHMTPa6S6HXZGDr/KXSlcG2BWvEaaqw7zDHFov0LFttrH2QyR9sWT8hp56TLkBWO+Je5h3TOynu4okHqY2bAEaemSpVyLAbDiIXCqrcJlHVt573h2g13of6T5Pfv0u6uhHoi2KlcEfs0ZRcB51k= 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=H+g4FkVH; 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="H+g4FkVH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729541859; 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=/G1GqEMjdPi5AZxteZatISgExeCR/ovT8Q2adnrrCQ4=; b=H+g4FkVHqCV15NyhyW45SGN3cSbdwAoj3rfPFkNPcYaecFeHQR8FvpCnlpsuT9YV5FUKkq Sy0qt7R8Lk37JqWJ4EpAKuCNI8gNRHt9KR9VZyh2LpNCN1k7aqF9cqj0TtAypgejByFwBH y9d5sXjyRpzYg/M/tb7Jw1/0PskaoUw= 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-646-0oG7dw9aN2Koq5JxMIb-5g-1; Mon, 21 Oct 2024 16:17:38 -0400 X-MC-Unique: 0oG7dw9aN2Koq5JxMIb-5g-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4315afcae6cso26363625e9.0 for ; Mon, 21 Oct 2024 13:17:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729541857; x=1730146657; 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=/G1GqEMjdPi5AZxteZatISgExeCR/ovT8Q2adnrrCQ4=; b=jxWxyVq2pt3BghyGI4JX7OcYLiXPRqywm8tFnHS4Lqh7zcIO7DXSmMnoc/RY2uSnXV 0pcT/9pbZeAPXU6mCjCJOQcyNVHS3RVsv0WQmEeM9oo4TQwjg0wC2/hXJvJEywRgCKzh fI6v/oZfK6PlUQc4WU3AhgEiZiqnOD4844r2QzBBOnSeMFuisFJiVZa8YD2uxo6Z6EUM +L1VZ6YrTmXwoNml6dLqLLrW3gM1luSElPK3O8L5Rc0+4kfJMwfUfWziwvnGl4dVf1TZ dZODDz1v8Ke3JkGbzRaVhpBfA9CHxDSQycZpSOdZAXXpytAXAqnZJCAQSgRMbiavNS+p yuWQ== X-Forwarded-Encrypted: i=1; AJvYcCWdSXL91k43UYXoBo3ETH5dG171xkaCk33YeytIPQnbPVXVv8J8ScorXyYvE7IWSpvPQp/ZjgbsnX4=@vger.kernel.org X-Gm-Message-State: AOJu0YycQTzyURq/WnCDcuSI2gEz+eM995/xPDykJq/B2WUkEEERc3f5 M0iN46AxCIDn5Z+h6sR5bsVdYrrIEFN0KJvhxoMhblOokVVMH9h4UFeCj4cb7ExaPlK6hQx+L0w zOt84CL1JPWt6UQva/kWOIrHn2FMK/MuP6FxDC5kS2tqY//jdxKQUY6GEuA== X-Received: by 2002:a05:600c:1c22:b0:431:416e:2603 with SMTP id 5b1f17b1804b1-4317bd76f7fmr5414285e9.3.1729541856908; Mon, 21 Oct 2024 13:17:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcY1DD7dFo8tIwpxs1LdF1DOFzxE4mXucxTWHHXPyvc0KgSzW88PnW4jnVAg8xj9BhT6tJhA== X-Received: by 2002:a05:600c:1c22:b0:431:416e:2603 with SMTP id 5b1f17b1804b1-4317bd76f7fmr5414165e9.3.1729541856448; Mon, 21 Oct 2024 13:17:36 -0700 (PDT) Received: from [192.168.3.141] (p5b0c6747.dip0.t-ipconnect.de. [91.12.103.71]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4317d0ce992sm129745e9.1.2024.10.21.13.17.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Oct 2024 13:17:35 -0700 (PDT) Message-ID: <6c282299-506f-45c9-9ddc-9ef4de582394@redhat.com> Date: Mon, 21 Oct 2024 22:17: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: Vlastimil Babka , Lorenzo Stoakes , Andrew Morton Cc: 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: 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+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r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CrN+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 22:11, Vlastimil Babka wrote: > On 10/20/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 > > > >> +static long madvise_guard_poison(struct vm_area_struct *vma, >> + struct vm_area_struct **prev, >> + unsigned long start, unsigned long end) >> +{ >> + long err; >> + >> + *prev = vma; >> + if (!is_valid_guard_vma(vma, /* allow_locked = */false)) >> + return -EINVAL; >> + >> + /* >> + * If we install poison markers, then the range is no longer >> + * empty from a page table perspective and therefore it's >> + * appropriate to have an anon_vma. >> + * >> + * This ensures that on fork, we copy page tables correctly. >> + */ >> + err = anon_vma_prepare(vma); >> + if (err) >> + return err; >> + >> + /* >> + * Optimistically try to install the guard poison pages first. If any >> + * non-guard pages are encountered, give up and zap the range before >> + * trying again. >> + */ > > Should the page walker become powerful enough to handle this in one go? :) > But sure, if it's too big a task to teach it to zap ptes with all the tlb > flushing etc (I assume it's something page walkers don't do today), it makes > sense to do it this way. > Or we could require userspace to zap first (MADV_DONTNEED), but that would > unnecessarily mean extra syscalls for the use case of an allocator debug > mode that wants to turn freed memory to guards to catch use after free. > So this seems like a good compromise... Yes please, KIS. We can always implement support for that later if really required (leave behavior open when documenting). -- Cheers, David / dhildenb