From patchwork Mon Mar 25 22:33:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vishal Moola X-Patchwork-Id: 13603118 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 779C4CD11DC for ; Mon, 25 Mar 2024 22:33:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEF7E6B0082; Mon, 25 Mar 2024 18:33:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9D716B0083; Mon, 25 Mar 2024 18:33:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C63636B0085; Mon, 25 Mar 2024 18:33:46 -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 B79496B0082 for ; Mon, 25 Mar 2024 18:33:46 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 863E81C0B16 for ; Mon, 25 Mar 2024 22:33:46 +0000 (UTC) X-FDA: 81937014852.28.6236315 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf16.hostedemail.com (Postfix) with ESMTP id B22C7180003 for ; Mon, 25 Mar 2024 22:33:44 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ARjN5ctS; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711406024; a=rsa-sha256; cv=none; b=FPGgkmOMQveDjtPqtyAJoLmG0F2D6TGUywTKpGLFCT3OMqqBxGIHIo9NNcCepAN8Ef/t3E KZRDD4dv7mTsm5+uWLTuABsawD3912tj/tgVmrspRatk6CHXARcT2G7tiH2z6dFzgAXD1F UeITNgV3Po+14nZKUY3UPi/EGqvjU6M= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ARjN5ctS; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711406024; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7wuUkLEO8wS3NnK8pO29sbwl4okjZZVm7bwiudBCZKQ=; b=i+hzY0NEGFJgIOUFosrpuAEGK46u3rutcvkY24fuKiUJMPM7uRi+yg1ajVJ/S2GdXOFTjq QKhhGanntTYNjDmo8n1w/OkaXc62i6NCkZ8Xgs3HVGwuc/yU5AkVM7TUl6OdIbtxuc2Ppi +PsN+esqMXCj9+qkRVSj0SqMSPXEXO4= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6e6f4ad4c57so3669526b3a.2 for ; Mon, 25 Mar 2024 15:33:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711406023; x=1712010823; darn=kvack.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=7wuUkLEO8wS3NnK8pO29sbwl4okjZZVm7bwiudBCZKQ=; b=ARjN5ctSp95n0RRJ2l1dEhCHo8RqqFxaei78f21s2rkpAemHRwpfa+o7PDM9qQzhRo 9pNqT9xRCe0g4V2iwvXfR3W/ZAeXt3ar2n86FCXab2jQ946Z+XaauV15XJ2F+jtXB2Uw XfNdNc+hh+wgfPLDfJrwNekf39aMQJuFVo3zkU+lNn0WlqIw2j+fbS0INmM7Y2WYu+M2 zB/q3HoqeUyt/NuoIX4leM0ELpIbvZZxb2rMA6KR+jhc83RfKGvExw/wtNNI10H6rNLr eHyzgUSPqcBKd8S6wEcP6jfllH37GDWcCtdidgrP4eNJRsIZ6YqlfqPjaNaz2nr7bJiK ZLmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711406023; x=1712010823; 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=7wuUkLEO8wS3NnK8pO29sbwl4okjZZVm7bwiudBCZKQ=; b=tFBe7axyQ3IfSK6YCuO9br0XXfRIzUYsXrFkRmQooChW5YGV76Q06x657RrtOALx2D 9gjJSqWyo/7dP1ElOp6HWghzm2cyLzPeqAe3Mh2eKpQ81mNWMsueG+JCISy9auDntkVy HDSR6CA5DDD0B6JXZ2b3gKv35bNaFtpEw/ekQuLfwmedQQoO/pDdeWmaZXSuFSxvuAui KojLuSplp1ymsSgT5029DDaiqsSNTY98w3OITl2yDiBcMkgWL7M89lmGoRcM2VVH+Avo aUxyAHfUiUnpsS/4kWaAYAmaoQuoKN+nKfAlm0Uzz8NiEE7JquknXYSMrLoxr9Vioxhq QgPA== X-Gm-Message-State: AOJu0Yyxf6vqBqaXanSIUpyPu4+9I4vX4U6CoEQysrp3a8Y3lyZpK6n9 2WWWKHhDf7WKczwYJ/oo7dz5I3b4CGSVjcAE9O0AlEcxiyDIG2QhBDqzyaiBlG8= X-Google-Smtp-Source: AGHT+IFl6KFavtRUNTSrxnwpqS8RSMDEAwpK70R/1RBLnR40J5C5dnwH9mZD8fjRJHapYDc5t8I8Zw== X-Received: by 2002:a05:6a21:3942:b0:1a3:cd06:20cf with SMTP id ac2-20020a056a21394200b001a3cd0620cfmr4148137pzc.16.1711406023376; Mon, 25 Mar 2024 15:33:43 -0700 (PDT) Received: from fedora.. (c-73-170-51-167.hsd1.ca.comcast.net. [73.170.51.167]) by smtp.googlemail.com with ESMTPSA id sx16-20020a17090b2cd000b002a053cdd4e5sm4356173pjb.9.2024.03.25.15.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 15:33:43 -0700 (PDT) From: "Vishal Moola (Oracle)" To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, muchun.song@linux.dev, "Vishal Moola (Oracle)" Subject: [PATCH 1/5] hugetlb: Convert hugetlb_fault() to use struct vm_fault Date: Mon, 25 Mar 2024 15:33:35 -0700 Message-ID: <20240325223339.169350-2-vishal.moola@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240325223339.169350-1-vishal.moola@gmail.com> References: <20240325223339.169350-1-vishal.moola@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B22C7180003 X-Stat-Signature: 9qe3icsnh7j9t31xn1yyuhurymq7xnye X-HE-Tag: 1711406024-183630 X-HE-Meta: U2FsdGVkX18YdG7zbDxHXCHHaXL9EfmLmbFNteRGmKH0tpRqzVIl+Jut/XtzkO8sFhbdmbuFIQMKW+CYHaWPubEGxw/C+Ibk9cuH4iMYiZlqGifwan9pK8Iv1Mek1bgRMfIGQbzmzgW7+ZiDdyczQeAIlsnHJ1v1xIGCkp5G/Fh//YKqfX/9KQMYX/g0wC7w5SmegXwwGfcGIwea4nri1PZKz5ySnGs8kjtuan44Q2Jm8GOW1W+JSOpdo4ypuvWC9NyX+syV3X7/IiQiL1CuJZ1SLcZp9SDQWB6f83crhxlJw9kcxhd87fG3A81l2yfmP8zidTe6kuJf0zjBI3qZKgL+muALGWT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LMTRjJFMxd+KUqOQ8k5Ip4dcLtocH4pyotMTj2pwBzT2THXVRO/2U/dTS0Bs2gxiQjakijStZbBWgXNu44+tT1wp2j55goFcQJPbJTvgSRQOeT0aU+UnHJIbNonC8I8ehmkZR/SPA3TC4/A== 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: Now that hugetlb_fault() has a vm_fault available for fault tracking, use it throughout. This cleans up the code by removing 2 variables, and prepares hugetlb_fault() to take in a struct vm_fault argument. Signed-off-by: Vishal Moola (Oracle) --- mm/hugetlb.c | 84 +++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 688017ca0cc2..81e8ade53b64 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6330,8 +6330,6 @@ u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags) { - pte_t *ptep, entry; - spinlock_t *ptl; vm_fault_t ret; u32 hash; struct folio *folio = NULL; @@ -6339,13 +6337,13 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, struct hstate *h = hstate_vma(vma); struct address_space *mapping; int need_wait_lock = 0; - unsigned long haddr = address & huge_page_mask(h); struct vm_fault vmf = { .vma = vma, - .address = haddr, + .address = address & huge_page_mask(h), .real_address = address, .flags = flags, - .pgoff = vma_hugecache_offset(h, vma, haddr), + .pgoff = vma_hugecache_offset(h, vma, + address & huge_page_mask(h)), /* TODO: Track hugetlb faults using vm_fault */ /* @@ -6365,22 +6363,22 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, /* * Acquire vma lock before calling huge_pte_alloc and hold - * until finished with ptep. This prevents huge_pmd_unshare from - * being called elsewhere and making the ptep no longer valid. + * until finished with vmf.pte. This prevents huge_pmd_unshare from + * being called elsewhere and making the vmf.pte no longer valid. */ hugetlb_vma_lock_read(vma); - ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h)); - if (!ptep) { + vmf.pte = huge_pte_alloc(mm, vma, vmf.address, huge_page_size(h)); + if (!vmf.pte) { hugetlb_vma_unlock_read(vma); mutex_unlock(&hugetlb_fault_mutex_table[hash]); return VM_FAULT_OOM; } - entry = huge_ptep_get(ptep); - if (huge_pte_none_mostly(entry)) { - if (is_pte_marker(entry)) { + vmf.orig_pte = huge_ptep_get(vmf.pte); + if (huge_pte_none_mostly(vmf.orig_pte)) { + if (is_pte_marker(vmf.orig_pte)) { pte_marker marker = - pte_marker_get(pte_to_swp_entry(entry)); + pte_marker_get(pte_to_swp_entry(vmf.orig_pte)); if (marker & PTE_MARKER_POISONED) { ret = VM_FAULT_HWPOISON_LARGE; @@ -6395,20 +6393,20 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, * mutex internally, which make us return immediately. */ return hugetlb_no_page(mm, vma, mapping, vmf.pgoff, address, - ptep, entry, flags, &vmf); + vmf.pte, vmf.orig_pte, flags, &vmf); } ret = 0; /* - * entry could be a migration/hwpoison entry at this point, so this - * check prevents the kernel from going below assuming that we have - * an active hugepage in pagecache. This goto expects the 2nd page - * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will - * properly handle it. + * vmf.orig_pte could be a migration/hwpoison vmf.orig_pte at this + * point, so this check prevents the kernel from going below assuming + * that we have an active hugepage in pagecache. This goto expects + * the 2nd page fault, and is_hugetlb_entry_(migration|hwpoisoned) + * check will properly handle it. */ - if (!pte_present(entry)) { - if (unlikely(is_hugetlb_entry_migration(entry))) { + if (!pte_present(vmf.orig_pte)) { + if (unlikely(is_hugetlb_entry_migration(vmf.orig_pte))) { /* * Release the hugetlb fault lock now, but retain * the vma lock, because it is needed to guard the @@ -6417,9 +6415,9 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, * be released there. */ mutex_unlock(&hugetlb_fault_mutex_table[hash]); - migration_entry_wait_huge(vma, ptep); + migration_entry_wait_huge(vma, vmf.pte); return 0; - } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) + } else if (unlikely(is_hugetlb_entry_hwpoisoned(vmf.orig_pte))) ret = VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h)); goto out_mutex; @@ -6433,13 +6431,13 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, * determine if a reservation has been consumed. */ if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && - !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(entry)) { - if (vma_needs_reservation(h, vma, haddr) < 0) { + !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(vmf.orig_pte)) { + if (vma_needs_reservation(h, vma, vmf.address) < 0) { ret = VM_FAULT_OOM; goto out_mutex; } /* Just decrements count, does not deallocate */ - vma_end_reservation(h, vma, haddr); + vma_end_reservation(h, vma, vmf.address); pagecache_folio = filemap_lock_hugetlb_folio(h, mapping, vmf.pgoff); @@ -6447,17 +6445,17 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, pagecache_folio = NULL; } - ptl = huge_pte_lock(h, mm, ptep); + vmf.ptl = huge_pte_lock(h, mm, vmf.pte); /* Check for a racing update before calling hugetlb_wp() */ - if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) + if (unlikely(!pte_same(vmf.orig_pte, huge_ptep_get(vmf.pte)))) goto out_ptl; /* Handle userfault-wp first, before trying to lock more pages */ - if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) && - (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) { + if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(vmf.pte)) && + (flags & FAULT_FLAG_WRITE) && !huge_pte_write(vmf.orig_pte)) { if (!userfaultfd_wp_async(vma)) { - spin_unlock(ptl); + spin_unlock(vmf.ptl); if (pagecache_folio) { folio_unlock(pagecache_folio); folio_put(pagecache_folio); @@ -6467,18 +6465,18 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, return handle_userfault(&vmf, VM_UFFD_WP); } - entry = huge_pte_clear_uffd_wp(entry); - set_huge_pte_at(mm, haddr, ptep, entry, + vmf.orig_pte = huge_pte_clear_uffd_wp(vmf.orig_pte); + set_huge_pte_at(mm, vmf.address, vmf.pte, vmf.orig_pte, huge_page_size(hstate_vma(vma))); /* Fallthrough to CoW */ } /* - * hugetlb_wp() requires page locks of pte_page(entry) and + * hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) and * pagecache_folio, so here we need take the former one * when folio != pagecache_folio or !pagecache_folio. */ - folio = page_folio(pte_page(entry)); + folio = page_folio(pte_page(vmf.orig_pte)); if (folio != pagecache_folio) if (!folio_trylock(folio)) { need_wait_lock = 1; @@ -6488,24 +6486,24 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, folio_get(folio); if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { - if (!huge_pte_write(entry)) { - ret = hugetlb_wp(mm, vma, address, ptep, flags, - pagecache_folio, ptl, &vmf); + if (!huge_pte_write(vmf.orig_pte)) { + ret = hugetlb_wp(mm, vma, address, vmf.pte, flags, + pagecache_folio, vmf.ptl, &vmf); goto out_put_page; } else if (likely(flags & FAULT_FLAG_WRITE)) { - entry = huge_pte_mkdirty(entry); + vmf.orig_pte = huge_pte_mkdirty(vmf.orig_pte); } } - entry = pte_mkyoung(entry); - if (huge_ptep_set_access_flags(vma, haddr, ptep, entry, + vmf.orig_pte = pte_mkyoung(vmf.orig_pte); + if (huge_ptep_set_access_flags(vma, vmf.address, vmf.pte, vmf.orig_pte, flags & FAULT_FLAG_WRITE)) - update_mmu_cache(vma, haddr, ptep); + update_mmu_cache(vma, vmf.address, vmf.pte); out_put_page: if (folio != pagecache_folio) folio_unlock(folio); folio_put(folio); out_ptl: - spin_unlock(ptl); + spin_unlock(vmf.ptl); if (pagecache_folio) { folio_unlock(pagecache_folio);