From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBF6E37C11B for ; Fri, 15 May 2026 12:43:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778849041; cv=none; b=F2XgXHCKjT68fgA8BP6jTuku/zKdZdQYwS7zEnnAzD1R9kKMLQw7jVtG/bl1LXL8FdxLWk9k8sRJqVOn9sSmTjZbjfHCxpbupWstKwuxrN4PkWd/ifQa/c9sQYp3E1O827qDwv0qeNXPmUyXlMvw0JFTSNu5Mw3FhS7TFW8m42s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778849041; c=relaxed/simple; bh=b6txlshbxD/7M6X+IhBOlbb5pIvAFx2deHcowSiuhlo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H9kIEzCiYxjqNaECvyhhwqk+hZMs/OMZ4kJmSOhPI0/58ccUwUABYWUepQ1QTNx88InlxelIP9mrX25UNSAtt9I++tlWbeXnMNJQIxm20zt6+m6TL20lG2lTlHGBRHxT5WzJACbtHRBr8Nxt+lqfCCzv4vVvrcAZE22xJf8s+YU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Z5icZP0J; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z5icZP0J" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-43d7645adbdso5030322f8f.1 for ; Fri, 15 May 2026 05:43:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778849038; x=1779453838; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XYm+RK+GFSRl+9igtw82HH43GYD2uH0hgYBQXA1Kmt8=; b=Z5icZP0JYvAjeWr/M8/lAlBoMkK4iXuxafqyNFzdrx2LB+Z8p2/wma+ZUtc2gNLKLC QqcsxALy6A13ZL6W2nYVIxQh+RkRoJJ4s+WQZx80WHO1HBFckArI2BuRXHFAyDGjkeyO UEbb3VoMR0QPum4wy5mto6c/AQzaUfcMVjdhn7RQ464hiqiRMh2kTASALxRaKTPBFmFe x5+BC/CJE7Jn9wPDjnYAJHTUKrWcVd8QtVdPDsOUah10BxIuOCfVSz6W9dOgTs8BeFe8 Oe9zxXYA4BWBbGOSPpqbTW3Ht0WwcRJr1cwlU1ACAJ8Yatxhx+eWdEPCUsoAtQjljrF7 b0NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778849038; x=1779453838; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XYm+RK+GFSRl+9igtw82HH43GYD2uH0hgYBQXA1Kmt8=; b=dsWTonAxxxpqidPKLW1xX2NN20rVZUpnwlKsk57DRBA/F4CalvBA8LABgfLci8RIDq 0EhenEQvuAlbQTKVnVcFE3BIHf6J1yaX7XLLvS3c0bj2C6fRTHo8lepMNvUW9uuz3Azw y52ioWzTyQromix4gLN5TULbaI8yfuTlyQSwzEd7hFF9I4rIiwS7JczDOTH5jXHVNhHd GAGiqV0HzloalkAbJPelB7VCet7h6XKNZqccn8Zb4wZw2mO6UHj01Eooe+Y+3WmKPoRn C+BCbLeJiBol27yo3CxTZwtkMixmGNMuUvaR/O9g73ruNy2AMJUNYOxgxXRUFOVQlYtp 0jCg== X-Gm-Message-State: AOJu0YxFe2xKUl3Hwr+g6UY6fekX5Qyx/JxLLSJFggJeBX13sNT0SICx I8+4vCRSGTCsUDy7uyxKgGqFYde1hezOzGS7eVFHmPzR2Gky4nKx/qthlUWbHZi7 X-Gm-Gg: Acq92OGxRjokqg+X6ERz0PRxwbNS9IggiFlfzdskK12QRyiiaFt8hOTULsuQy/RiIrS lXD7PPmzDRIp647rOR8ZeYTPlbq+9fD2w7wroiQeTpxxZgMZ//Qi2IeqRs9nIsxzLUD+mZGOASg ZP68BY+fjO3uMle7zkO1FSqEs1oloSpZ7G1Uhxe4Z/NYcT4E8ksdIRB0PffhAkV1DGuhTwdbO/+ 7VH0tyKF5atw0KHQCWoS7Bw3bnVt+E+OQCKA86iVys3gf0MJYdam/2MiNtLjMTuQhsIqF/Y1GOG s8paS5WTW6oj5SHvZQr5N5SvSLM4ln3auK8V9DNtRa3FrFqAiszacethPYhM3msdTIWVMBkTrUj B3G6EZzC7hEz9X9aNOSmdMgjrmHCi9ofhrQRNprvmY9igf78/sw3wxj3HJw2q9MTkxT2StXCCGw 5OUfFUbIRMRXcsfcriP47AIbxNNqlxz2/ArbCXTc2V X-Received: by 2002:a05:600c:4fc9:b0:485:3abe:ab86 with SMTP id 5b1f17b1804b1-48fe60e3b17mr50567285e9.4.1778849038068; Fri, 15 May 2026 05:43:58 -0700 (PDT) Received: from fedora ([156.207.183.142]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48fe4c8344asm100188115e9.1.2026.05.15.05.43.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 May 2026 05:43:57 -0700 (PDT) From: Ahmed Elaidy To: stable@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, ljs@kernel.org, avagin@gmail.com, Lorenzo Stoakes , Pedro Falcato , Vlastimil Babka , "David Hildenbrand (Red Hat)" , Baolin Wang , Barry Song , Dev Jain , Jann Horn , Jonathan Corbet , Lance Yang , Liam Howlett , "Masami Hiramatsu (Google)" , Mathieu Desnoyers , Michal Hocko , Mike Rapoport , Nico Pache , Ryan Roberts , Steven Rostedt , Suren Baghdasaryan , Zi Yan , Ahmed Elaidy Subject: [PATCH v4 5/9] mm: introduce copy-on-fork VMAs and make VM_MAYBE_GUARD one Date: Fri, 15 May 2026 15:42:15 +0300 Message-ID: <20260515124218.151966-7-elaidya225@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260515124218.151966-2-elaidya225@gmail.com> References: <20260515124218.151966-2-elaidya225@gmail.com> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Lorenzo Stoakes Gather all the VMA flags whose presence implies that page tables must be copied on fork into a single bitmap - VM_COPY_ON_FORK - and use this rather than specifying individual flags in vma_needs_copy(). We also add VM_MAYBE_GUARD to this list, as it being set on a VMA implies that there may be metadata contained in the page tables (that is - guard markers) which would will not and cannot be propagated upon fork. This was already being done manually previously in vma_needs_copy(), but this makes it very explicit, alongside VM_PFNMAP, VM_MIXEDMAP and VM_UFFD_WP all of which imply the same. Note that VM_STICKY flags ought generally to be marked VM_COPY_ON_FORK too - because equally a flag being VM_STICKY indicates that the VMA contains metadat that is not propagated by being faulted in - i.e. that the VMA metadata does not fully describe the VMA alone, and thus we must propagate whatever metadata there is on a fork. However, for maximum flexibility, we do not make this necessarily the case here. Link: https://lkml.kernel.org/r/5d41b24e7bc622cda0af92b6d558d7f4c0d1bc8c.1763460113.git.ljs@kernel.org Signed-off-by: Lorenzo Stoakes Reviewed-by: Pedro Falcato Reviewed-by: Vlastimil Babka Acked-by: David Hildenbrand (Red Hat) Cc: Andrei Vagin Cc: Baolin Wang Cc: Barry Song Cc: Dev Jain Cc: Jann Horn Cc: Jonathan Corbet Cc: Lance Yang Cc: Liam Howlett Cc: "Masami Hiramatsu (Google)" Cc: Mathieu Desnoyers Cc: Michal Hocko Cc: Mike Rapoport Cc: Nico Pache Cc: Ryan Roberts Cc: Steven Rostedt Cc: Suren Baghdasaryan Cc: Zi Yan Signed-off-by: Andrew Morton (cherry picked from commit ab04b530e7e8bd5cf9fb0c1ad20e0deee8f569ec) Signed-off-by: Ahmed Elaidy Cc: stable@vger.kernel.org # 6.18.x --- include/linux/mm.h | 26 ++++++++++++++++++++++++++ mm/memory.c | 18 ++++-------------- tools/testing/vma/vma_internal.h | 26 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ebc5fb0af5ce..2bad4bf67d0f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -538,6 +538,32 @@ extern unsigned int kobjsize(const void *objp); */ #define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +/* + * Flags which should result in page tables being copied on fork. These are + * flags which indicate that the VMA maps page tables which cannot be + * reconsistuted upon page fault, so necessitate page table copying upon + * + * VM_PFNMAP / VM_MIXEDMAP - These contain kernel-mapped data which cannot be + * reasonably reconstructed on page fault. + * + * VM_UFFD_WP - Encodes metadata about an installed uffd + * write protect handler, which cannot be + * reconstructed on page fault. + * + * We always copy pgtables when dst_vma has uffd-wp + * enabled even if it's file-backed + * (e.g. shmem). Because when uffd-wp is enabled, + * pgtable contains uffd-wp protection information, + * that's something we can't retrieve from page cache, + * and skip copying will lose those info. + * + * VM_MAYBE_GUARD - Could contain page guard region markers which + * by design are a property of the page tables + * only and thus cannot be reconstructed on page + * fault. + */ +#define VM_COPY_ON_FORK (VM_PFNMAP | VM_MIXEDMAP | VM_UFFD_WP | VM_MAYBE_GUARD) + /* * mapping from the currently active vm_flags protection bits (the * low four bits) to a page protection mask.. diff --git a/mm/memory.c b/mm/memory.c index dde20cd5fa5b..1dd68a0e2be4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1463,25 +1463,15 @@ copy_p4d_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, static bool vma_needs_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) { + if (src_vma->vm_flags & VM_COPY_ON_FORK) + return true; /* - * Always copy pgtables when dst_vma has uffd-wp enabled even if it's - * file-backed (e.g. shmem). Because when uffd-wp is enabled, pgtable - * contains uffd-wp protection information, that's something we can't - * retrieve from page cache, and skip copying will lose those info. + * The presence of an anon_vma indicates an anonymous VMA has page + * tables which naturally cannot be reconstituted on page fault. */ - if (userfaultfd_wp(dst_vma)) - return true; - - if (src_vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)) - return true; - if (src_vma->anon_vma) return true; - /* Guard regions have modified page tables that require copying. */ - if (src_vma->vm_flags & VM_MAYBE_GUARD) - return true; - /* * Don't copy ptes where a page fault will fill them correctly. Fork * becomes much lighter when there are big shared or private readonly diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index 19914c28977e..6ee803873e00 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -145,6 +145,32 @@ extern unsigned long dac_mmap_min_addr; */ #define VM_IGNORE_MERGE (VM_SOFTDIRTY | VM_STICKY) +/* + * Flags which should result in page tables being copied on fork. These are + * flags which indicate that the VMA maps page tables which cannot be + * reconsistuted upon page fault, so necessitate page table copying upon + * + * VM_PFNMAP / VM_MIXEDMAP - These contain kernel-mapped data which cannot be + * reasonably reconstructed on page fault. + * + * VM_UFFD_WP - Encodes metadata about an installed uffd + * write protect handler, which cannot be + * reconstructed on page fault. + * + * We always copy pgtables when dst_vma has uffd-wp + * enabled even if it's file-backed + * (e.g. shmem). Because when uffd-wp is enabled, + * pgtable contains uffd-wp protection information, + * that's something we can't retrieve from page cache, + * and skip copying will lose those info. + * + * VM_MAYBE_GUARD - Could contain page guard region markers which + * by design are a property of the page tables + * only and thus cannot be reconstructed on page + * fault. + */ +#define VM_COPY_ON_FORK (VM_PFNMAP | VM_MIXEDMAP | VM_UFFD_WP | VM_MAYBE_GUARD) + #define FIRST_USER_ADDRESS 0UL #define USER_PGTABLES_CEILING 0UL -- 2.54.0