From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE1C5C28B20 for ; Wed, 2 Apr 2025 12:20:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90FAB280006; Wed, 2 Apr 2025 08:20:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BE91280001; Wed, 2 Apr 2025 08:20:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7110E280006; Wed, 2 Apr 2025 08:20:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4E7D2280001 for ; Wed, 2 Apr 2025 08:20:31 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B4FC9599E6 for ; Wed, 2 Apr 2025 12:20:32 +0000 (UTC) X-FDA: 83289011904.03.1BB7222 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 1819940011 for ; Wed, 2 Apr 2025 12:20:29 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="MT/+5yre"; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743596430; a=rsa-sha256; cv=none; b=2gWJcdooE90kVgAcC1eKVY1CXqNDKz5ckPiWyWk12AT5swmn1Quw0J6DNw2u7raGqkdRU4 UXbXE+akl8KRjzvD/rFfWxBCsRN2JTNa2CoA5GYHMVbl/56O+MQBdZfl2z9JVQ0MwxFVno qDjAAXA5nODw+8nqmN2//H3Mkka/rlQ= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="MT/+5yre"; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743596430; h=from:from:sender: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:dkim-signature; bh=DEah2U8jPR7Oiio6Af0msiNDIaOYqYnOw4plTjzV214=; b=00fRoiwOSs9/EDVy8zT6m3dSflquIk2Z0ukjV2yjBS06q/F0E50dhiwBzVVg0CXoMj2RxE FvqmPBhOy2Vfum8rcCsuurOBVvPUwkAghsXedDBXGzqn/vrzy+LRO3Cj6/gFsOS/owkN9/ e8FEQPSafOya6mfdLvxU1VlByOPmthg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743596429; 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=DEah2U8jPR7Oiio6Af0msiNDIaOYqYnOw4plTjzV214=; b=MT/+5yredkSKohN/Ja6EUf+ZdGLkKe3Tfvod/x5NfSg6yNm6tEJ1YAoSuP5FdoPgyFtLOX 3Vwo7f2oyJKKnHEW9ps5BLH50bVucUcVsplnvDFBHYRqBAjzOcoUuLllKAJCTUUBsiAbAs 1hifXZK+0AT6AF70j8TTH2377OiJgv4= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-498-WyrFwwZGNaGOv4c3RinQGA-1; Wed, 02 Apr 2025 08:20:28 -0400 X-MC-Unique: WyrFwwZGNaGOv4c3RinQGA-1 X-Mimecast-MFC-AGG-ID: WyrFwwZGNaGOv4c3RinQGA_1743596427 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-39126c3469fso2196983f8f.3 for ; Wed, 02 Apr 2025 05:20:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743596427; x=1744201227; 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=DEah2U8jPR7Oiio6Af0msiNDIaOYqYnOw4plTjzV214=; b=KlQPRlVH87J7L7pMhxSluPqSND7gq3RWY4d/yW32T/5AT8r9IgtG3n28sxdyy9d9IF 7JwLNLVghh42U+oYsF11gNlL9D4aDdyFy0eprTurkQjTXGoowdztBZRuq5Bd3GumQGzW 2wK2MEE7vwhQahvCHj/2Z/1hbGETnH+6nleX02R/JDYOMDo8n4qJCNxCfHTgERbZzvlJ 2Uqj6euW7igdCnT74hv2NMkZpe5ZsBave6GGZ3qIFNw/pHEIJWbXHc65eoF7FLoDYUr7 JYe1pSFNtrysibSj2Z4JmJpfxY79F57/2Yc0hpDarx+i1Km/gzGw4PdTGLb0cB+aAAEN fmdw== X-Forwarded-Encrypted: i=1; AJvYcCVHi20PpnVqv6XSYq+saRhJI7AakOIJrfVpBkiTp5nfnuYObdZ3pBfPk9bFtIixsNntNijX9xgVkQ==@kvack.org X-Gm-Message-State: AOJu0YwWkJ+DTtuG7ovZLkg5XUHYpPWgKgIO2rw6rcuU5NgzVI01E3mO Nf8M8LP3qfx48gyHAR4zbo6SlovjaeJgSP0D5AiNyndhTslrKZ3EczbLKLSkgvgZ/sG3gQBMZEe Pd5CbyHQ5stJpJvDzzZZf61cJ574dGt+cpDs6zNNBLxfhO+6s X-Gm-Gg: ASbGnctxW5rOty0oGM3itzms6odJYH1QGc3s6+b9RMafOQG8FgMMCHlxlSMu9v9ZJWH oshIHmlwBUKyrKK3cw/lDs8pzmWN2INSZgMcVIlMR6RIcrfe95pTbUndcEPa7VnBF0lYqIBBQcf GjuNvq3fb3ZrcGTesdzbDKX+faRQ7nK2boM28rMhxtQ3AGvOjhWwUTbJOjuE6FPC29CjLzT9EHm V7U20YmSF0QF2QHfpp1qnMKNQUgGv65WVxliEurDnB8b3MpNLgcmPoTtJbFEx8WSXCJGTmjqMtL lw2llX6dD7cvoTaWE3Uo7JkLC+j8gLCc/+0ZyEIJ2zspZS52Jqn0FzGHZ/GtjYyNzrb/krZqX4S Y+IJ6cXgDICIvPkx1ikbLu+lsEHpjoi5Dn4ErBg5u X-Received: by 2002:a05:6000:2481:b0:391:4743:6db1 with SMTP id ffacd0b85a97d-39c2364b4b7mr6105595f8f.20.1743596426893; Wed, 02 Apr 2025 05:20:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2fK+PYI9NatoJ1q++HXLvbxiB7el20WXfIfvjzMc17tvjgwF879p7taXMqDFimiqQmWy9ZA== X-Received: by 2002:a05:6000:2481:b0:391:4743:6db1 with SMTP id ffacd0b85a97d-39c2364b4b7mr6105553f8f.20.1743596426304; Wed, 02 Apr 2025 05:20:26 -0700 (PDT) Received: from ?IPV6:2003:cb:c70f:cd00:4066:4674:d08:9535? (p200300cbc70fcd00406646740d089535.dip0.t-ipconnect.de. [2003:cb:c70f:cd00:4066:4674:d08:9535]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b79e14csm16821667f8f.76.2025.04.02.05.20.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Apr 2025 05:20:25 -0700 (PDT) Message-ID: Date: Wed, 2 Apr 2025 14:20:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range() To: Lorenzo Stoakes Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org, xingwei lee , yuxin wang , Marius Fleischer , Ingo Molnar , Borislav Petkov , Dan Carpenter , Andrew Morton , Linus Torvalds , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Rik van Riel , "H. Peter Anvin" , Peter Xu , Liam Howlett References: <20250325191951.471185-1-david@redhat.com> From: David Hildenbrand 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/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwAR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hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: gjuT6jtV6o1Z8v7_AOUA7-Hdjw7xxKOqYirsO6iYBX0_1743596427 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1819940011 X-Stat-Signature: gnwx638qukw8tx1xe5bdd3xhcodadrn6 X-Rspam-User: X-HE-Tag: 1743596429-982163 X-HE-Meta: U2FsdGVkX18p5RlL7dtWb3nt3ZvMamMd/iZjmaAcUckwK/4mFiyud+JarzZ5Q4843Z2iHoYnTjTLCZ1v5wYTSAcuX/FKT0jEtjVtortdqebk3HwPwz0dwzv1cMJ/l/l0jM0oOci8jm05Tr1QJnfHS2BEzBT2S9XQj4vSATzN2AS7iyZEEGIWL7PEUO6xE2JFgjsCp7R6tJaaDqMpp6zvUSoqL8V5yVaxXlrnKVE42cfg5wYFePWSY0ZKhE4VEhvsrubVAMJH03XxYIByYRvE9vuVb4XOVd2aybAWXIDgcCmv8sNxIoYX23JzkdfENvPh3ZRvUaYieTHkb5sZOg7xvYphsbKluMR2sXYEJ4RSspDPGXmHhBFdoVpgqz2my0yTjfpjpM+eEBXLFowpzUJ2yermvtYrXTfk7ts3VwVKkjc/RON+1hgJVcg4/eT9Rn1LZCp+T+AvGh33BcVLOFQb5Yzl7tsNU+qB21V6qcfzjlXJfu1nulrFq/r9ZFse364Bm9L9/m8yeO6BsoBhFyvztuNfvulTsxhMaMX6WYJPZU2txbjhezS1ZXoHcROfwjE4F9eCDeFfE6lXuBqvxQWTCSQlZBxn/vDufzH6zqu+kBPx5zipERtRufaugh150O8UikjlBiz79j+g2/QLZ286emcZIqE0PuAyOwAn3EW50F1nhEs1/aH1ZYNPXj4Ecdbpx28tD7dVBjQZzH8fRQYWbe27Ry9d0Fa0lDtt2rzQ2dEelKCZ613Baoq/HJqsRZdHP2MoVsNi6EXtA4GuJTylvMJ+RHmFfLgDOQ7WxdFioiEPBH5JcRpMeGsoPQIx1r/rsvYioGRwUQWyl6hw5Mo7c5Hieet9kvpyMEFQNjmJox7G/zDtLaXy/G0cu0VQasdo/MK/vILpOXAsQz8+2hN8JVsex09JZT8RJXkdfX13vrG6BLHLI8L6iB6BGi7XwMhWE77gTWIV6SDYbplSzyx wWdPbJS6 woxRXWlDStkQDIICEfJSnch0vnsIuV7FWYILEACbeuqXNpj6oC7cebcH55vOVL8iLeQOWyYHNWPKTWe+9zrzsUkIA3boONpTXtZD9ACF/f7RLemigPJtgPAa2xrz74w2wftYc5PLsA5KTeQMEszteUnH2dKdKDE+6/qZAIci3EJUr5Z6URCh6lqFX9YelGdwBl75kB6k/E/NQZbQfAgGKdVoIvvDI/6Y5TCPzYp2V0R/r6KxVTVzIwFY0QdqtAZck944SWliwL8OTBLtCm9vFhGLSCK65bGmAMdXW/q2DZjsqG2fz8/oQ+KnVydYcLJwzMKIv0OX+KqQME3VedRGnihurmiq3TQDi5eIjGShERnJpnMR8EW/kIY6rnE637h29Tv7DK4crHRuBy6j9JbTy3et2bO5u6AEEBFCRk1n9AR4ZfFO1Kxnh7uzwb1arHr4CwccaeQ4DNnhrtu6jzNgzzfA1lNdnjbyIEQRb X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: >> >> v2 -> v3: >> * Make some !MMU configs happy by just moving the code into memtype.c > > Obviously we need to make the bots happy once again, re the issue at [0]... > > [0]: https://lore.kernel.org/all/9b3b3296-ab21-418b-a0ff-8f5248f9b4ec@lucifer.local/ > > Which by the way you... didn't seem to be cc'd on, unless I missed it? I > had to manually add you in which is... weird. > >> >> v1 -> v2: >> * Avoid a second get_pat_info() [and thereby fix the error checking] >> by passing the pfn from track_pfn_copy() to untrack_pfn_copy() >> * Simplify untrack_pfn_copy() by calling untrack_pfn(). >> * Retested >> >> Not sure if we want to CC stable ... it's really hard to trigger in >> sane environments. > > This kind of code path is probably in reality... theoretical. So I'm fine > with this. > Thanks a bunch for your review! >> >> --- >> arch/x86/mm/pat/memtype.c | 52 +++++++++++++++++++++------------------ >> include/linux/pgtable.h | 28 ++++++++++++++++----- >> kernel/fork.c | 4 +++ >> mm/memory.c | 11 +++------ >> 4 files changed, 58 insertions(+), 37 deletions(-) >> >> diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c >> index feb8cc6a12bf2..d721cc19addbd 100644 >> --- a/arch/x86/mm/pat/memtype.c >> +++ b/arch/x86/mm/pat/memtype.c >> @@ -984,29 +984,42 @@ static int get_pat_info(struct vm_area_struct *vma, resource_size_t *paddr, >> return -EINVAL; >> } >> >> -/* >> - * track_pfn_copy is called when vma that is covering the pfnmap gets >> - * copied through copy_page_range(). >> - * >> - * If the vma has a linear pfn mapping for the entire range, we get the prot >> - * from pte and reserve the entire vma range with single reserve_pfn_range call. >> - */ >> -int track_pfn_copy(struct vm_area_struct *vma) >> +int track_pfn_copy(struct vm_area_struct *dst_vma, >> + struct vm_area_struct *src_vma, unsigned long *pfn) > > I think we need an additional 'tracked' parameter so we know whether or not > this pfn is valid. See below. > > It's kind of icky... see the bot report for context, but we we sort of need > to differentiate between 'error' and 'nothing to do'. Of course PFN can > conceivably be 0 so we can't just return that or an error (plus return > values that can be both errors and values are fraught anyway). > > So I guess -maybe- least horrid thing is: > > int track_pfn_copy(struct vm_area_struct *dst_vma, > struct vm_area_struct *src_vma, unsigned long *pfn, > bool *pfn_tracked); > > Then you can obviously invoke with track_pfn_copy(..., &pfn_tracked); and > do an if (pfn_tracked) untrack_pfn_copy(...). > > I'm really not in favour of just initialising PFN to 0 because there are > code paths where this might actually get passed around and used > incorrectly. > > But on the other hand - I get that this is disgusting. I'm in favor of letting VM_PAT take care of that. Observe how untrack_pfn_copy() -> untrack_pfn() takes care of that by checking for VM_PAT. So this should be working as expected? No need to add something on top that makes it even more ugly in the caller. > > >> { >> + const unsigned long vma_size = src_vma->vm_end - src_vma->vm_start; >> resource_size_t paddr; >> - unsigned long vma_size = vma->vm_end - vma->vm_start; >> pgprot_t pgprot; >> + int rc; >> >> - if (vma->vm_flags & VM_PAT) { >> - if (get_pat_info(vma, &paddr, &pgprot)) >> - return -EINVAL; >> - /* reserve the whole chunk covered by vma. */ >> - return reserve_pfn_range(paddr, vma_size, &pgprot, 1); >> - } >> + if (!(src_vma->vm_flags & VM_PAT)) >> + return 0; > > I do always like the use of the guard clause pattern :) > > But here we have a case where now error = 0, pfn not set, and we will try > to untrack it despite !VM_PAT. Right, and untrack_pfn() is smart enough to filter that out. (just like for any other invokation) > >> + >> + /* >> + * Duplicate the PAT information for the dst VMA based on the src >> + * VMA. >> + */ >> + if (get_pat_info(src_vma, &paddr, &pgprot)) >> + return -EINVAL; >> + rc = reserve_pfn_range(paddr, vma_size, &pgprot, 1); >> + if (rc) >> + return rc; > > I mean it's a crazy nit, but we use ret elsewhere but rc here, maybe better > to use ret in both places. > > But also feel free to ignore this. "int retval;" ? ;) > >> >> + /* Reservation for the destination VMA succeeded. */ >> + vm_flags_set(dst_vma, VM_PAT); >> + *pfn = PHYS_PFN(paddr); >> return 0; >> } >> >> +void untrack_pfn_copy(struct vm_area_struct *dst_vma, unsigned long pfn) >> +{ >> + untrack_pfn(dst_vma, pfn, dst_vma->vm_end - dst_vma->vm_start, true); >> + /* >> + * Reservation was freed, any copied page tables will get cleaned >> + * up later, but without getting PAT involved again. >> + */ >> +} >> + >> /* >> * prot is passed in as a parameter for the new mapping. If the vma has >> * a linear pfn mapping for the entire range, or no vma is provided, >> @@ -1095,15 +1108,6 @@ void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, >> } >> } >> >> -/* >> - * untrack_pfn_clear is called if the following situation fits: >> - * >> - * 1) while mremapping a pfnmap for a new region, with the old vma after >> - * its pfnmap page table has been removed. The new vma has a new pfnmap >> - * to the same pfn & cache type with VM_PAT set. >> - * 2) while duplicating vm area, the new vma fails to copy the pgtable from >> - * old vma. >> - */ > > This just wrong now? Note that I'm keeping the doc to a single place -- the stub in the header. (below) Or can you elaborate what exactly is "wrong"? > >> void untrack_pfn_clear(struct vm_area_struct *vma) >> { >> vm_flags_clear(vma, VM_PAT); >> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >> index 94d267d02372e..4c107e17c547e 100644 >> --- a/include/linux/pgtable.h >> +++ b/include/linux/pgtable.h >> @@ -1508,14 +1508,25 @@ static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, >> } >> >> /* >> - * track_pfn_copy is called when vma that is covering the pfnmap gets >> - * copied through copy_page_range(). >> + * track_pfn_copy is called when a VM_PFNMAP VMA is about to get the page >> + * tables copied during copy_page_range(). On success, stores the pfn to be >> + * passed to untrack_pfn_copy(). >> */ >> -static inline int track_pfn_copy(struct vm_area_struct *vma) >> +static inline int track_pfn_copy(struct vm_area_struct *dst_vma, >> + struct vm_area_struct *src_vma, unsigned long *pfn) >> { >> return 0; >> } >> >> +/* >> + * untrack_pfn_copy is called when a VM_PFNMAP VMA failed to copy during >> + * copy_page_range(), but after track_pfn_copy() was already called. >> + */ > > Do we really care to put a comment like this on a stub function? Whoever started this beautiful VM_PAT code decided to do it like that: and I think it's better kept at a single location. > >> +static inline void untrack_pfn_copy(struct vm_area_struct *dst_vma, >> + unsigned long pfn) >> +{ >> +} >> + >> /* >> * untrack_pfn is called while unmapping a pfnmap for a region. >> * untrack can be called for a specific region indicated by pfn and size or >> @@ -1528,8 +1539,10 @@ static inline void untrack_pfn(struct vm_area_struct *vma, >> } >> >> /* >> - * untrack_pfn_clear is called while mremapping a pfnmap for a new region >> - * or fails to copy pgtable during duplicate vm area. >> + * untrack_pfn_clear is called in the following cases on a VM_PFNMAP VMA: >> + * >> + * 1) During mremap() on the src VMA after the page tables were moved. >> + * 2) During fork() on the dst VMA, immediately after duplicating the src VMA. >> */ > > Can I say as an aside that I hate this kind of hook? Like quite a lot? > > I mean I've been looking at mremap() of anon mappings as you know obv. but > the thought of PFN mapping mremap()ing is kind of also a bit ugh. I absolutely hate all of that, but I'll have to leave any cleanups to people with more spare time ;) > >> static inline void untrack_pfn_clear(struct vm_area_struct *vma) >> { >> @@ -1540,7 +1553,10 @@ extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, >> unsigned long size); >> extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, >> pfn_t pfn); >> -extern int track_pfn_copy(struct vm_area_struct *vma); >> +extern int track_pfn_copy(struct vm_area_struct *dst_vma, >> + struct vm_area_struct *src_vma, unsigned long *pfn); >> +extern void untrack_pfn_copy(struct vm_area_struct *dst_vma, >> + unsigned long pfn); >> extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, >> unsigned long size, bool mm_wr_locked); >> extern void untrack_pfn_clear(struct vm_area_struct *vma); >> diff --git a/kernel/fork.c b/kernel/fork.c >> index 735405a9c5f32..ca2ca3884f763 100644 >> --- a/kernel/fork.c >> +++ b/kernel/fork.c >> @@ -504,6 +504,10 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) >> vma_numab_state_init(new); >> dup_anon_vma_name(orig, new); >> >> + /* track_pfn_copy() will later take care of copying internal state. */ >> + if (unlikely(new->vm_flags & VM_PFNMAP)) >> + untrack_pfn_clear(new); > > OK so maybe I'm being stupid here, but - is it the case that > > a. We duplicate a VMA that has a PAT-tracked PFN map > b. We must clear any existing tracking so everything is 'reset' to zero> c. track_pfn_copy() will later in fork process set anything up we need here. > > Is this correct? Right. But b) is actually not "clearing any tracking" (because there is no tracking/reservation for the copied version yet) but marking it as "not tracked". > >> + >> return new; >> } >> >> diff --git a/mm/memory.c b/mm/memory.c >> index fb7b8dc751679..dc8efa1358e94 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -1362,12 +1362,12 @@ int >> copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) >> { >> pgd_t *src_pgd, *dst_pgd; >> - unsigned long next; >> unsigned long addr = src_vma->vm_start; >> unsigned long end = src_vma->vm_end; >> struct mm_struct *dst_mm = dst_vma->vm_mm; >> struct mm_struct *src_mm = src_vma->vm_mm; >> struct mmu_notifier_range range; >> + unsigned long next, pfn; >> bool is_cow; >> int ret; >> >> @@ -1378,11 +1378,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) >> return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); >> >> if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { >> - /* >> - * We do not free on error cases below as remove_vma >> - * gets called on error from higher level routine >> - */ >> - ret = track_pfn_copy(src_vma); >> + ret = track_pfn_copy(dst_vma, src_vma, &pfn); >> if (ret) >> return ret; >> } >> @@ -1419,7 +1415,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) >> continue; >> if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, >> addr, next))) { >> - untrack_pfn_clear(dst_vma); >> ret = -ENOMEM; >> break; >> } >> @@ -1429,6 +1424,8 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) >> raw_write_seqcount_end(&src_mm->write_protect_seq); >> mmu_notifier_invalidate_range_end(&range); >> } >> + if (ret && unlikely(src_vma->vm_flags & VM_PFNMAP)) >> + untrack_pfn_copy(dst_vma, pfn); > > Yeah, the problem here is that !(src_vma->vm_flags & VM_PFNMAP) is not the > _only_ way we can not have a valid pfn. > > Do we still want to untrack_pfn_copy() even if !VM_PAT? Sure, let that be handled internally, where all the ugly VM_PAT handling resides. Unless there is very good reason to do it differently. Thanks! -- Cheers, David / dhildenb