From patchwork Fri Aug 9 16:08:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13758932 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 2F48EC52D7C for ; Fri, 9 Aug 2024 16:09:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9C66B00A6; Fri, 9 Aug 2024 12:09:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 741C96B00A7; Fri, 9 Aug 2024 12:09:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56EA66B00A8; Fri, 9 Aug 2024 12:09:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2F7406B00A6 for ; Fri, 9 Aug 2024 12:09:36 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EE6831A11CB for ; Fri, 9 Aug 2024 16:09:35 +0000 (UTC) X-FDA: 82433192310.28.567775F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id DBED38001F for ; Fri, 9 Aug 2024 16:09:33 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NEl6YNjs; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723219720; a=rsa-sha256; cv=none; b=MlvLZP1eKXRONrkWH6qhGzO/sw77R6c8MJiQxd4X4KzU+CJ82e6y1pCnGO9QW/DGk8Z9H2 X5Ix4ZYo+AoZ8cu/1lUCIO+D6iGmO/6ntwMNr9VlD1iscr8KbLl5NsCeFCNc+iP5WABLc6 DYp8PtvBJCW9gpijIv5eSCugJK07eFw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=NEl6YNjs; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723219720; 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=GAUMBfy0kdSy8bea6Q2/adQQTYYkrWuiUWfctkd/rW8=; b=kcyQNY7fGtTu6fZDXtUwIYdnzQCiUYLQSlD/8RCZMKI2y9j1i8uZsbqzi9qlrOZjl2ZFjg P1TBTGIy0UbIyzWKyvEf99nmlR2Fl96+m+VR//ZhA8/7BOoXM8w1UsDgf6pMDnjNea3bUn /tRGtP02BukLzSxnhDPM2H5gcdf030Q= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723219773; 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; bh=GAUMBfy0kdSy8bea6Q2/adQQTYYkrWuiUWfctkd/rW8=; b=NEl6YNjsZGDTrFfDymRE51xbhcfb4tlH5mhszKGFLWFgzdOKhrswl/Epk85m7TRsOSJFep PwmaPzlOFT5RT9ZfjpHaGHLvUNMBoswV7n4s0Nx/Rw7G16siLE/aSmJ6dmHYrLwtDI7GcG Jbetw5bya204NFUbNMI7eHC/Gn/ixjY= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-80W5_HAMNamnPtrtIMjWSg-1; Fri, 09 Aug 2024 12:09:32 -0400 X-MC-Unique: 80W5_HAMNamnPtrtIMjWSg-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4519ec19cb6so810441cf.1 for ; Fri, 09 Aug 2024 09:09:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723219771; x=1723824571; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GAUMBfy0kdSy8bea6Q2/adQQTYYkrWuiUWfctkd/rW8=; b=mtfTGdX3qsk9Zj2y+lm147lEpNMFMGkk3Ftwfi0aH4T3cxnuL5fyIifBD/a46kabos d7Il8DtF6ihwwHuijWt2BT9brp8W6/q6LLvnCCluvcxtafnjAI2hDDsV5oqn3YTB9UQ2 fwV1v+aYcla+HbKI3E33rDpouAeaZ02QiE5ikkutYiRd+LeUqX2Ha2APEkIlUa03M9OK fujuB6xuQW5zORlS+XeQubIgcH+MDb3fVgVSPWChDOyw916kQuDgUTxbmZ5KDVDU5h0q sUzhgbC9ssL9ylXi/5aArecXec08U1XTtNNnrZjXk93zz4SWgY0mdnaY8qQzCoHeEa+q VxmA== X-Gm-Message-State: AOJu0YyTNS6DEFqFnzv8Stm67Hpuje4AdEVxASGT2+nJ0653XckXsjgt 5FV0DGq3IfEGD7RF/buZKr3nWZo5OoCweDcaNm/7HE49MtKDCOX7dFxOri51gfMpsnAPIv0fgkS yo80OM7DX6w+8ew4T1Krjo+2uPhiVXnLDRGXiXs53TkEzM96Pzl4xLEONBMm9zRBLrhqsf69w8A TCBnhs2hz9OQ1Gpq/X4pfvs8ql2J+awYex X-Received: by 2002:a05:622a:180e:b0:44e:d016:ef7 with SMTP id d75a77b69052e-45312652444mr12909511cf.7.1723219771202; Fri, 09 Aug 2024 09:09:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH2AJ498flXqsf/RjsZizkWR5q4LBdwGhozxnqN6XMiL4rwNH8ESKXDSym68P6o8cpdHObfXw== X-Received: by 2002:a05:622a:180e:b0:44e:d016:ef7 with SMTP id d75a77b69052e-45312652444mr12908941cf.7.1723219770588; Fri, 09 Aug 2024 09:09:30 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-451c870016csm22526741cf.19.2024.08.09.09.09.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 09:09:30 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Sean Christopherson , Oscar Salvador , Jason Gunthorpe , Axel Rasmussen , linux-arm-kernel@lists.infradead.org, x86@kernel.org, peterx@redhat.com, Will Deacon , Gavin Shan , Paolo Bonzini , Zi Yan , Andrew Morton , Catalin Marinas , Ingo Molnar , Alistair Popple , Borislav Petkov , David Hildenbrand , Thomas Gleixner , kvm@vger.kernel.org, Dave Hansen , Alex Williamson , Yan Zhao Subject: [PATCH 07/19] mm/fork: Accept huge pfnmap entries Date: Fri, 9 Aug 2024 12:08:57 -0400 Message-ID: <20240809160909.1023470-8-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240809160909.1023470-1-peterx@redhat.com> References: <20240809160909.1023470-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: DBED38001F X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xTgv4DQ24YjezHt4StAJP6jihZ90i4+9kGEPmNgRrDB225p16eTOvXCWDeg9nwRjWQQ5PvT2YjvjSpIasIq1Fhk3ZOIBM2480BMNd4tX11y1Pi2aDjgIAQrxXHNqFkTOHU/noewjeubEAzjrsHh8T4EMc0x2SoqSYNYiZ5meIx7p8trVOIiEZLpP+qCqnEYUA2Buh05wlNuVKq1oRZ3gSkP/eyO5Xfe5mZgaBcJEx93sAmLDRJacRdpFKhPsSap40jtQibdztSchiLUgAbdm/I97PSDfE0NIP7TCyIDztzBRqo5ghMusPa+ynOxnpY87Nqz/vtgeVyWZtoqQEO0Cu3ClFlbBcvoZtTB4bDlOdBl2VBWGQCK6vFAF6GQWIPJS9GPRl+2YIhfL6k1Vi2yDQUDqM71xfP0JAk/8OF0q9KBLHg== 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: Teach the fork code to properly copy pfnmaps for pmd/pud levels. Pud is much easier, the write bit needs to be persisted though for writable and shared pud mappings like PFNMAP ones, otherwise a follow up write in either parent or child process will trigger a write fault. Do the same for pmd level. Signed-off-by: Peter Xu --- mm/huge_memory.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6568586b21ab..015c9468eed5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1375,6 +1375,22 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, pgtable_t pgtable = NULL; int ret = -ENOMEM; + pmd = pmdp_get_lockless(src_pmd); + if (unlikely(pmd_special(pmd))) { + dst_ptl = pmd_lock(dst_mm, dst_pmd); + src_ptl = pmd_lockptr(src_mm, src_pmd); + spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); + /* + * No need to recheck the pmd, it can't change with write + * mmap lock held here. + */ + if (is_cow_mapping(src_vma->vm_flags) && pmd_write(pmd)) { + pmdp_set_wrprotect(src_mm, addr, src_pmd); + pmd = pmd_wrprotect(pmd); + } + goto set_pmd; + } + /* Skip if can be re-fill on fault */ if (!vma_is_anonymous(dst_vma)) return 0; @@ -1456,7 +1472,9 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, pmdp_set_wrprotect(src_mm, addr, src_pmd); if (!userfaultfd_wp(dst_vma)) pmd = pmd_clear_uffd_wp(pmd); - pmd = pmd_mkold(pmd_wrprotect(pmd)); + pmd = pmd_wrprotect(pmd); +set_pmd: + pmd = pmd_mkold(pmd); set_pmd_at(dst_mm, addr, dst_pmd, pmd); ret = 0; @@ -1502,8 +1520,11 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, * TODO: once we support anonymous pages, use * folio_try_dup_anon_rmap_*() and split if duplicating fails. */ - pudp_set_wrprotect(src_mm, addr, src_pud); - pud = pud_mkold(pud_wrprotect(pud)); + if (is_cow_mapping(vma->vm_flags) && pud_write(pud)) { + pudp_set_wrprotect(src_mm, addr, src_pud); + pud = pud_wrprotect(pud); + } + pud = pud_mkold(pud); set_pud_at(dst_mm, addr, dst_pud, pud); ret = 0;