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 CF0C51FDF85 for ; Mon, 21 Oct 2024 20:37:46 +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=1729543068; cv=none; b=KxPzEpi1pw7HddX5ndtQKkYHOIkQmngq+I+QJiZ1AvbcsYWKCpJxRP1hgJRu3Q8KSW4BTOLHhyPc6W8oVyj2Q1lPK0nOQF4MZNXlhFIAsCTKUG4A68+a1/JAN/sDm6kNIkzjo0ZvYk3EaaJjqSuVbQ8sDUclmmmEqrdmZITYkBs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729543068; c=relaxed/simple; bh=4T2FKoZ4mH+ucoOjwg7YHPEHwCt6rtp5Fs2QjggPzUM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Fx2ehINNcSZOVE3NjbRlJulNwuCSTFwJQPT6Ig19lrAATKPwVCD8fTtqkkmn1dQkjUfpTU4U/4r2FKX+y99qSZGhg9o+A9QMmEZdWrvMoh7DsW6rck7cP5LHoFdPcDuJ8IwUaJa5EpNDr/V+kXofmlduOKM5Ifcw41UK0ebH118= 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+FA37YR; 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+FA37YR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729543065; 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=J0pdqvyCZGSuoD31ctx2SsdibfeZpHT/ebecW+GGQCc=; b=Y+FA37YRiplW47vswjcdWjRZcDkczzvL/i3YIc9Hm6Sws5ODYEWTA85/2rWsWV7Y1m6i8S 3uOMhPBPShSHzPdtklnhJrqFlBNE+ddmDUSzYfcbA+KCWTfrjhD8yV2ZB7RJ2yndJHbmdw S/VHBqI4+0FfQO8kGCqz71TuTRV92Fg= 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-695-4fMfk9P8OzGxPr7B_UNktg-1; Mon, 21 Oct 2024 16:37:44 -0400 X-MC-Unique: 4fMfk9P8OzGxPr7B_UNktg-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-37d45f1e935so2273352f8f.0 for ; Mon, 21 Oct 2024 13:37:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729543063; x=1730147863; 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=J0pdqvyCZGSuoD31ctx2SsdibfeZpHT/ebecW+GGQCc=; b=CAi4ghWHJ0q10dbpro2f2qSuwGuhjJ/b1hRSxMWndoXzyEt/CYIHr2PHv2aTL65w1h SLNqny5jXB2f818dIvm2rawYBkAKzb/Y3srXpKFskffs4xWMy5kNlxPCbeVeETS718ky GamG1Lr3oVJXYEOGYJjxy0Nz2oug5w9KEemfFt6oe8SpOddi9aOHzndX0QyTrehLnkQ7 Dz5srwqOw5lgagakw1Dmx5aDqvznuik2mmHiSI8QQ3frGDfCaniSXwyuFYKV4WhuddKT PVTwzTxzbBFKeHa8ByqqnWyPVeeqjcoNofAO76CgVTQzOYg0tJNALO0e4k+QJHHQE+If ZYlg== X-Forwarded-Encrypted: i=1; AJvYcCV9azYeDS5CYYCdbLp59I7mKNVxdAtqas34zlzvppBO+ZSzbp27UViWngGpvWg8OU6AVgzQkYTjuk8=@vger.kernel.org X-Gm-Message-State: AOJu0YzARxjv6bR1AhCjsQVVtDbsZq3/l0ptCzbF1dMzr6qt/tD/r3ga 3knDZsWk/tURp0uue8bZon6T+5mm2KqLe9vM79TkunS14a8+8P06/p3y/CUbDkeTxZtic2CyDv8 KYmmUaGwb06YXUJUNmhZzmVKO6+P+iayZfLx2MZjLWa45bjCt67eudOz/KA== X-Received: by 2002:adf:e643:0:b0:37d:4e20:9be0 with SMTP id ffacd0b85a97d-37eab6ebdadmr7980211f8f.51.1729543063292; Mon, 21 Oct 2024 13:37:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHRpKAC5c6xVw1HVZUSluiOBC9oQK+3adL0UR00BpCUmVvqAx9WFzjCXc6djAwNuS668eKPQA== X-Received: by 2002:adf:e643:0:b0:37d:4e20:9be0 with SMTP id ffacd0b85a97d-37eab6ebdadmr7980191f8f.51.1729543062921; Mon, 21 Oct 2024 13:37:42 -0700 (PDT) Received: from [192.168.3.141] (p5b0c6747.dip0.t-ipconnect.de. [91.12.103.71]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ee0a485dcsm5088945f8f.34.2024.10.21.13.37.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Oct 2024 13:37:41 -0700 (PDT) Message-ID: <9727ada4-0048-499b-a43f-ac0a625bae5d@redhat.com> Date: Mon, 21 Oct 2024 22:37:39 +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: <6c282299-506f-45c9-9ddc-9ef4de582394@redhat.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/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SY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NVfEQMQ 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 22:25, Vlastimil Babka wrote: > On 10/21/24 22:17, David Hildenbrand wrote: >> On 21.10.24 22:11, Vlastimil Babka wrote: >>> On 10/20/24 18:20, Lorenzo Stoakes wrote: >>> >>> >>> >>>> +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. > > You mean "require userspace to zap first (MADV_DONTNEED)" ? Yes, I see from Lorenzo's reply that there is apparently some history to this (maybe it's all nicely summarized in the cover letter / this patch, have to dig further). Not sure yet what the problem is, I would have thought it's all protected by the PTL, and concurrent faults are user space doing something stupid and we'd detect it. Have to do some more reading on this. > > I'd normally agree with the KIS principle, but.. > >> We can always implement support for that later if > > it would either mean later we change behavior (installing guards on > non-zapped PTEs would have to be an error now but maybe start working later, > which is user observable change thus can break somebody) > >> really required (leave behavior open when documenting). > > and leaving it open when documenting doesn't really mean anything for the > "we don't break userspace" promise vs what the implementation actually does. Not quite I think. You could start return -EEXIST or -EOPNOTSUPP and document that this can change in the future to succeed if there is something. User space can sense support. Something failing that at one point starts working is not really breaking user space, unless someone really *wants* to fail if there is already something (e.g., concurrent fault -> bail out instead of hiding it). Of course, a more elegant solution would be GUARD_INSTALL vs. GUARD_FORCE_INSTALL. .. but again, there seems to be more history to this. -- Cheers, David / dhildenb