From patchwork Fri Mar 21 11:23:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 14025280 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 D04FAC35FFF for ; Fri, 21 Mar 2025 11:23:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79D8C280002; Fri, 21 Mar 2025 07:23:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74D55280001; Fri, 21 Mar 2025 07:23:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C8CC280002; Fri, 21 Mar 2025 07:23:34 -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 36C23280001 for ; Fri, 21 Mar 2025 07:23:34 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4DE36142337 for ; Fri, 21 Mar 2025 11:23:34 +0000 (UTC) X-FDA: 83245322748.05.3D1BCAD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id A76791C0008 for ; Fri, 21 Mar 2025 11:23:31 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VVLmW0MP; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1742556212; a=rsa-sha256; cv=none; b=eNhxcVhagALHf+k8MYEMDu4e1por+TOfj6ocJsU+Oaaj5AFEqzK0ykoj0wshgmRIwE75qn Im1laJSltahhoRMWyofh8y9RlMpFv20xT+33+BEUBb2LS1C4UI+hp+nOHvmYywxCYbvRUW Cp4QUK4OOZp1B9XVaG/hfOKWPcFH1Q0= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VVLmW0MP; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1742556212; 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: references:dkim-signature; bh=s/md+4NTjaWUNzHR/fDVjCHKrpiH7LiUEFL7MRG8nvU=; b=PJSo/kbIh0fpTyD7nR9zBhop091L+v5DV3UnwnJj0bpjEXV78QZcTaetim6J7wVpVN9P3x p1a7/B7MCS98oJHyKkBTx2KdLqCLFJQ1pf98zRKcIIudUQn9Z0ZgnxAF9Rt7dwCmjErbuD 6azYOOiw1I6wFdSrFUyJBSEfYuZWjqI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1742556211; 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; bh=s/md+4NTjaWUNzHR/fDVjCHKrpiH7LiUEFL7MRG8nvU=; b=VVLmW0MPyy2jMiPewSJXclA6z+HTtQevtE7X5NEu0d4M5j8vycIQJlO6ji/XxqLiG0EN1q FR33hSpZJ5oo519EqCpJjlLhZo92VgNAThc+dtLKJQTdezh7BlR13qbtqHPZhBjv84tq7t ZXb4DYptHyf1UCxgCD8dlSzLyv91wJg= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-59-bfGIOX4rOcqNwWvLSvnQlA-1; Fri, 21 Mar 2025 07:23:27 -0400 X-MC-Unique: bfGIOX4rOcqNwWvLSvnQlA-1 X-Mimecast-MFC-AGG-ID: bfGIOX4rOcqNwWvLSvnQlA_1742556206 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-394bbefc98cso1116126f8f.1 for ; Fri, 21 Mar 2025 04:23:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742556206; x=1743161006; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=s/md+4NTjaWUNzHR/fDVjCHKrpiH7LiUEFL7MRG8nvU=; b=dRYKM0t25oJAP+ZdSpUeFy92A77qLm+BQGFgz42zedAbwfOAM1ACDgZ9JP8fq8v+SN ImWYwwlqOGvr3kf/Kp10TdWfPMY4tg6q+LOoaPKWOChutbXtG3mc3O1V/E7BaxmsHr6T ayqiyxRjc4byu2nD+V6aMxIWjT1TVAVGIylpgogg5yv6N2UiralB4hOamlidh+95gXYQ 0cnKqKGoPNlZqNRqV/ezYWMniukesCbFLAGSL2WNGl5Mg+3CD9cFQlC5L/0XrwyIikRI gMPhiZ9HfVL7juiKpMUmIB4njtzvGLrjpOSAQguc+YSZJboLQ3vMde27FDG+0nGAWfvS cSLg== X-Gm-Message-State: AOJu0YwzAYG01z2ldwMcvO4usphEAkjq7hlqXkLcNR2csXRxglSbCygp Le6Cj/xv6yVNBi1RRuKfjP5ElEMly5/bfEkbNiUfrLd8/bl/L27C4+uELAQd7mhKR2/xYYpLeSY QS7ceA4uwHViqnNvusdtMPpPCKFekPwLR8PQEHBciW45rLCok X-Gm-Gg: ASbGncu2rFnceOkF6I2PYEKMHPVV0FieiZtSBp6WmXI/CX2h72U2nsqcykOqWM+KedE JdEPPWktYZblu24daIF7sMgIjJzAFsxBFvLaxjGVgmRIMcDZUhSaB28vHB9fPjCd3HvKaMb8OAS d1zalLrymriqXkVS+/dfe5Plf+CJNnxtGsnJUWm4BBjA7WQ40w8NDnuQR44Y8DPL31yGwCj86Yl 0V84CqGwNQywgmgtuoQTDe7IVRs3iI5qgl8QwQbVk4CQ/Qg5fbo5Jp3sjm1VS20RzDYABMl0b// h613VYDknr50dvbwOWojBAlRQKKErEcpVFfYewg5d+X8g55TT87u2leDfsK+/B/n9pUkRW6pZlz x X-Received: by 2002:a5d:64c7:0:b0:390:d73a:4848 with SMTP id ffacd0b85a97d-3997f94035cmr3215262f8f.47.1742556206231; Fri, 21 Mar 2025 04:23:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYuZSe0DTbFu+RBbXBxnS/gPdmj7yPzI1Yx5Cf0QQX6uqSW0HICnEbt4GhRaACxTadtFYxEw== X-Received: by 2002:a5d:64c7:0:b0:390:d73a:4848 with SMTP id ffacd0b85a97d-3997f94035cmr3215219f8f.47.1742556205703; Fri, 21 Mar 2025 04:23:25 -0700 (PDT) Received: from localhost (p200300cbc72a910023d23800cdcc90f0.dip0.t-ipconnect.de. [2003:cb:c72a:9100:23d2:3800:cdcc:90f0]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-43d4fd27b59sm24111505e9.23.2025.03.21.04.23.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Mar 2025 04:23:25 -0700 (PDT) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, x86@kernel.org, David Hildenbrand , 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 Subject: [PATCH v2] x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range() Date: Fri, 21 Mar 2025 12:23:23 +0100 Message-ID: <20250321112323.153741-1-david@redhat.com> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 3_1MLcMmuKF8rQDk02DoO8N0gpf2PDlktbkCYo5L-vs_1742556206 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A76791C0008 X-Stat-Signature: cgignc7co11hixbwuyjqisc6oqq8uj6u X-HE-Tag: 1742556211-597446 X-HE-Meta: U2FsdGVkX19Dw5Iq+zcxE/hjo8W6BGRWJQZcvLaNy3grXBVipIEl5F4E2bq68dBfdI2jVcb/WK46Q1A7uzYqyJlFjSvScEm/3aZ0sc5GkIoh6gD36vmzapoIKPJI8D+mkc6Sat0vcJQhZ/JMIFTow3rpJNz8oVRYgLfFXXxI8uqH5O3NDo7ADjF5gJ7OiKMDFeZdDhfj7S11LAK6vJuZRgkcEVoE5tEFVcdivwr2uE0yXcgr70TsCp1QCboUemjvCY9UBNb0em/O/EG6MUj1b6JXm9X+aE1gva7ygvgQ1p9fBZG9fJtOgbbJzozJyqCEffJti7hPE+KxMUBIWXU9QC5gMzgslZkYckdfmA/LH9M8fOXQ+U572/NYAbT/sXnH9gXMTokJ6a5EUcapDRIzRL+ro0VKX9SjPqI1/Nlz2tLABYV2aBH3Zew90hgjDvWrekt6cVvkVreVTrWjAgs3iJ9WQ526ASqphehOjbXWqdOPThKrW61VQEg+Ejqx8qgwi7gsyWvsPjZLWZRr7Nx4BN+zjvJ+5KVorLKXjwHhlGe3v511YdlAsfuimqZNBfMfsbhBFN7hHyxAHBBwE81dssciXuyJc9luTFf+CvP9gz5Y9g3aNfMKeBKfrKVCnjKwutXnvNWxNCZZX0PigTVWbn3fjgHNIMmBQE2ZtepIqgCWVuVJ2PIoB9d6LVuMWLzvL2kKemxBXOX79kO86tvm2sUORWs8j40B5QOyEbH952B4V6nfpWIUF3z3lDavh0HQ6OI2Xy5B2y4iKhaSWnwxX1Fd26x2q+MdAjF8eoi1efS9F2TzLPemdkbJOJnkWyxpODU4vVRWfVcD029TKP9Xw04/+gGUJCSonQdsYFpPYRNx9aH8sCIgtawFpYL8IV+DgM7f3s/F9xTPHCbd6B+uG+NuRucuK3SLFgrxnhHxbklSNdosMqdYwS1WQwz/VdByzF6SPvVSizYDSs7P5Bu u3CukJdG 7q+d6uSxEL+yWhCfr2ZnGn5+MZBHs4F5TxImWZedael5sjlLzjNxPG1e5q154i+jdQW3YaSwUs+y4x5Ukkgu+4l1o0rJtsyYXM9ze38n3CqnMVzsB4Uq9fhrrzxpVezqqnnT7ghxJjIf19fcjq/5/MxL2PrE1Dxn83Fq7iW2057fHAr+8TtLq1Lv2ou8FcUMyl1CG7b07NNOY9mhM8Oc7ioIEWAnc/qjwRAnV1rch7XtFjNoXACstP46NEJ5NWzDKNZ5qp9EPl+tih0sk+nhxKeBHKAaYtMLMnoEj+o9NA2s5olAFRwWfyFM4G5kBqBM4UWcYwlOad1t/04KlPfwVv1rzwHvihaB7DV9a+a5Rg58+PU2xdThhU9iHe8aLcL5HexoFCt+kh/xmQW6J9vrlkkoAgpx9xlcxKmU4SC8ruei9W6eM5Qvzo3fAVlrp6+m2OgLXOEWC9AYmKjag4ZFd1Jb/5BQIJ1R0i9hM7DjiC5HOEiobO2AxhPBZeCP+1WR6/8X989G/2EOZQ7K8GrWPJjWJrlZadAWN3XtveXiMySYo2iF3erWYHlwI+cpTbWoGBzGAPmCn4Fh5BKnc2K2snmANxemc5khEnldpGYjccxfHmiSAXHvtVZLAnlPuKBW5CQlqz+/cFqJOOE//aLWHH4hgdJSCkQRXGZGg46GnO6DZOV2SxwitWxwRjv7e7QXg1nNfwIanfDSdDj8TRdfRKtK0QcEpVe0tsH8t0WbY5xucZM5egb3Bsbg/uTTkR9icwW/2s0zxz2nIBqo5bV6IUDm3QtTHZH0Y1f8k2jvPnPqVWbtPz4a9Z/NDxh9a/ShIfeYB5y+yhg/fcke/jz6FTbTSV6GBWgP0ZKIoReWWwijl5hjHq6lyXW7mjTPMx7fjdX39R/7DeMQExcEidXn7i1/B1fFIGiyv48vjfvtzI5FYcq3hiyuq0Q9DhekqovYnvfB7GGsFk+hlzIT/JTcpNbJ2bSOE +AFZ6GQa TFkt2yEsP6CH4qzorVjDkZRASdqdlb6EgvwZQFk1DmFJa/mXLaf6vA== 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: If track_pfn_copy() fails, we already added the dst VMA to the maple tree. As fork() fails, we'll cleanup the maple tree, and stumble over the dst VMA for which we neither performed any reservation nor copied any page tables. Consequently untrack_pfn() will see VM_PAT and try obtaining the PAT information from the page table -- which fails because the page table was not copied. The easiest fix would be to simply clear the VM_PAT flag of the dst VMA if track_pfn_copy() fails. However, the whole thing is about "simply" clearing the VM_PAT flag is shaky as well: if we passed track_pfn_copy() and performed a reservation, but copying the page tables fails, we'll simply clear the VM_PAT flag, not properly undoing the reservation ... which is also wrong. So let's fix it properly: set the VM_PAT flag only if the reservation succeeded (leaving it clear initially), and undo the reservation if anything goes wrong while copying the page tables: clearing the VM_PAT flag after undoing the reservation. Note that any copied page table entries will get zapped when the VMA will get removed later, after copy_page_range() succeeded; as VM_PAT is not set then, we won't try cleaning VM_PAT up once more and untrack_pfn() will be happy. Note that leaving these page tables in place without a reservation is not a problem, as we are aborting fork(); this process will never run. A reproducer can trigger this usually at the first try: https://gitlab.com/davidhildenbrand/scratchspace/-/raw/main/reproducers/pat_fork.c [ 45.239440] WARNING: CPU: 26 PID: 11650 at arch/x86/mm/pat/memtype.c:983 get_pat_info+0xf6/0x110 [ 45.241082] Modules linked in: ... [ 45.249119] CPU: 26 UID: 0 PID: 11650 Comm: repro3 Not tainted 6.12.0-rc5+ #92 [ 45.250598] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-2.fc40 04/01/2014 [ 45.252181] RIP: 0010:get_pat_info+0xf6/0x110 ... [ 45.268513] Call Trace: [ 45.269003] [ 45.269425] ? __warn.cold+0xb7/0x14d [ 45.270131] ? get_pat_info+0xf6/0x110 [ 45.270846] ? report_bug+0xff/0x140 [ 45.271519] ? handle_bug+0x58/0x90 [ 45.272192] ? exc_invalid_op+0x17/0x70 [ 45.272935] ? asm_exc_invalid_op+0x1a/0x20 [ 45.273717] ? get_pat_info+0xf6/0x110 [ 45.274438] ? get_pat_info+0x71/0x110 [ 45.275165] untrack_pfn+0x52/0x110 [ 45.275835] unmap_single_vma+0xa6/0xe0 [ 45.276549] unmap_vmas+0x105/0x1f0 [ 45.277256] exit_mmap+0xf6/0x460 [ 45.277913] __mmput+0x4b/0x120 [ 45.278512] copy_process+0x1bf6/0x2aa0 [ 45.279264] kernel_clone+0xab/0x440 [ 45.279959] __do_sys_clone+0x66/0x90 [ 45.280650] do_syscall_64+0x95/0x180 Likely this case was missed in commit d155df53f310 ("x86/mm/pat: clear VM_PAT if copy_p4d_range failed") ... and instead of undoing the reservation we simply cleared the VM_PAT flag. Keep the documentation of these functions in include/linux/pgtable.h, one place is more than sufficient -- we should clean that up for the other functions like track_pfn_remap/untrack_pfn separately. Reported-by: xingwei lee Reported-by: yuxin wang Closes: https://lore.kernel.org/lkml/CABOYnLx_dnqzpCW99G81DmOr+2UzdmZMk=T3uxwNxwz+R1RAwg@mail.gmail.com/ Reported-by: Marius Fleischer Closes: https://lore.kernel.org/lkml/CAJg=8jwijTP5fre8woS4JVJQ8iUA6v+iNcsOgtj9Zfpc3obDOQ@mail.gmail.com/ Fixes: d155df53f310 ("x86/mm/pat: clear VM_PAT if copy_p4d_range failed") Fixes: 2ab640379a0a ("x86: PAT: hooks in generic vm code to help archs to track pfnmap regions - v3") Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dan Carpenter Cc: Andrew Morton Cc: Linus Torvalds Cc: Dave Hansen Cc: Andy Lutomirski Cc: Peter Zijlstra Cc: Rik van Riel Cc: "H. Peter Anvin" Cc: Peter Xu Signed-off-by: David Hildenbrand --- 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. --- arch/x86/mm/pat/memtype.c | 43 +++++++++++++++++---------------------- include/linux/pgtable.h | 31 ++++++++++++++++++++++------ kernel/fork.c | 4 ++++ mm/memory.c | 11 ++++------ 4 files changed, 52 insertions(+), 37 deletions(-) base-commit: b3ee1e4609512dfff642a96b34d7e5dfcdc92d05 diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index feb8cc6a12bf2..ffddbf52cd478 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -984,26 +984,30 @@ 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) { + 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; + /* + * 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; + + /* Reservation for the destination VMA succeeded. */ + vm_flags_set(dst_vma, VM_PAT); + *pfn = PHYS_PFN(paddr); return 0; } @@ -1095,15 +1099,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. - */ 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..df2aff504da69 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1508,10 +1508,12 @@ 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; } @@ -1528,8 +1530,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. */ static inline void untrack_pfn_clear(struct vm_area_struct *vma) { @@ -1540,12 +1544,27 @@ 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(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); #endif +/* + * 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. + */ +static inline 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. + */ +} + #ifdef CONFIG_MMU #ifdef __HAVE_COLOR_ZERO_PAGE static inline int is_zero_pfn(unsigned long pfn) 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); + 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); return ret; }