From nobody Thu Feb 12 04:37:41 2026 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 1469253E15 for ; Mon, 1 Apr 2024 20:26:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712003220; cv=none; b=LchHzKC6sRZvjckeereZR4LdU3QF0maG1IEAMK3NwF24TGdo6hPLh1VEqf/qiE6jVJCqeIesAYAXgd7G01dqjRXiQSfhKlNzr4mebegGFCrkc4Uz4D38WcNonEHiygnesI7qNFpAMxT80f2bUXyQ6jLVs1Ez7R8f3qAYOeHYxSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712003220; c=relaxed/simple; bh=sxYiMD+4k7jbC4+b4JZLo6dCWt7Ghka833gmn7CoIOQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ryRx5zhCkxkdOVQNDNhc+wbV6s2okt+Nj1lv99EOJVtTkXUY22eoBlZ6N5R8lqrkXh72IoIoMbksIlkWzgWQYCZcSFWfyTrTYepWUEnjgatMT9pTmvI+SPWWTjOw0kT0yF+EVCeWaPMA88VeHqL3BfxTxLtsG0984Z6XxqpJ6hA= 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=ngFewra4; arc=none smtp.client-ip=209.85.128.178 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="ngFewra4" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-6150c1fa3daso6467687b3.1 for ; Mon, 01 Apr 2024 13:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712003218; x=1712608018; 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=OLJjpIJjwOYqxNhCjRA5yTdkpLaMKAJot06YpeH+/j4=; b=ngFewra48yoC0n8rQMSo27sjM153zy/yXcB1UaZ/lszAbjUvRosQURH895Ly9MsPhu nNgRROjn201W4XgD+i0dsgaCOSc7EojG8uG9QHlbkIzPfA+2i8oNlSQTA10RAbH45rM2 gXyALopPQ/uXbJjKntWU1aZDaUb4F2uCKG/wcZpIrflUjPhA9B3E/rC7mNsNQqWUeY4z gpDcSEkm9EOD8L2f3Q97ZU6XKNEFSnheeuZ/e6RGNWo2fmpKHHjcsDdmDlO01KapW4UH nUkFvEVuymRqA0ctYxjkIuMsyBAOV0AOfjg7pIds4po7CyMHNKjzMtaeEaAsPoCSWSD1 TwAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712003218; x=1712608018; 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=OLJjpIJjwOYqxNhCjRA5yTdkpLaMKAJot06YpeH+/j4=; b=n0RrMubmmYMx03IzIpiBUg4xNnhl12UK9tz9GoDsVt6KkZQM/POv4sgdhfn4W4r833 hW4oQU68avQBnjAmeJVjm/+IhDFi6ZBzLTjbKED0i2hB0qwL1jDe0STy+sTymFs6Uz2C TDPmFq1uGh5Mit4H0Zij5WxxQU+Fd9GKTZxDuFRhFIFDwNhwtjYjcgJuG6HPJ+c35839 5wOsszdpPtbl+rFcl5zllXEA6BWptLhqFPCjwFIN7tl9B9QZTK19mbXO3FfItT1F+ibK dPDsDsFbrAN57hhigfqN+58tdpjcS/IqPURFNjhFp9DxfK4k7obOmB3knOm5vtHDjR1z FwzQ== X-Gm-Message-State: AOJu0YwiGCYF4E6kpm3JVHSw58ZkJZ2KTUOOn2KFHaGHmb0T546SJ4Uj qlVJDj/L16WKg7nnUJI27XW7RABgb6/jJqBOiRblR4P6dV4BISZT X-Google-Smtp-Source: AGHT+IFFZ8j7M33dVd9jUeXLmj/uGlOxSzCXNksanWE/EfYlyKZd130C69mAXSmbpVhDWvYUfcCEdg== X-Received: by 2002:a0d:e211:0:b0:60a:243:547c with SMTP id l17-20020a0de211000000b0060a0243547cmr9827366ywe.44.1712003216926; Mon, 01 Apr 2024 13:26:56 -0700 (PDT) Received: from fedora.attlocal.net ([2600:1700:2f7d:1800::23]) by smtp.googlemail.com with ESMTPSA id y72-20020a81a14b000000b006142210a31esm1171181ywg.23.2024.04.01.13.26.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 13:26:56 -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, willy@infradead.org, "Vishal Moola (Oracle)" Subject: [PATCH v2 1/3] hugetlb: Convert hugetlb_fault() to use struct vm_fault Date: Mon, 1 Apr 2024 13:26:49 -0700 Message-ID: <20240401202651.31440-2-vishal.moola@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240401202651.31440-1-vishal.moola@gmail.com> References: <20240401202651.31440-1-vishal.moola@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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) Reviewed-by: Muchun Song Reviewed-by: Oscar Salvador --- mm/hugetlb.c | 84 +++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8267e221ca5d..360b82374a89 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6423,8 +6423,6 @@ u32 hugetlb_fault_mutex_hash(struct address_space *ma= pping, 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 =3D NULL; @@ -6432,13 +6430,13 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, struct hstate *h =3D hstate_vma(vma); struct address_space *mapping; int need_wait_lock =3D 0; - unsigned long haddr =3D address & huge_page_mask(h); struct vm_fault vmf =3D { .vma =3D vma, - .address =3D haddr, + .address =3D address & huge_page_mask(h), .real_address =3D address, .flags =3D flags, - .pgoff =3D vma_hugecache_offset(h, vma, haddr), + .pgoff =3D vma_hugecache_offset(h, vma, + address & huge_page_mask(h)), /* TODO: Track hugetlb faults using vm_fault */ =20 /* @@ -6458,22 +6456,22 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, =20 /* * 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 =3D huge_pte_alloc(mm, vma, haddr, huge_page_size(h)); - if (!ptep) { + vmf.pte =3D 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; } =20 - entry =3D huge_ptep_get(ptep); - if (huge_pte_none_mostly(entry)) { - if (is_pte_marker(entry)) { + vmf.orig_pte =3D huge_ptep_get(vmf.pte); + if (huge_pte_none_mostly(vmf.orig_pte)) { + if (is_pte_marker(vmf.orig_pte)) { pte_marker marker =3D - pte_marker_get(pte_to_swp_entry(entry)); + pte_marker_get(pte_to_swp_entry(vmf.orig_pte)); =20 if (marker & PTE_MARKER_POISONED) { ret =3D VM_FAULT_HWPOISON_LARGE; @@ -6488,20 +6486,20 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct 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); } =20 ret =3D 0; =20 /* - * 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 @@ -6510,9 +6508,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 =3D VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h)); goto out_mutex; @@ -6526,13 +6524,13 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct 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 =3D 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); =20 pagecache_folio =3D filemap_lock_hugetlb_folio(h, mapping, vmf.pgoff); @@ -6540,17 +6538,17 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, pagecache_folio =3D NULL; } =20 - ptl =3D huge_pte_lock(h, mm, ptep); + vmf.ptl =3D huge_pte_lock(h, mm, vmf.pte); =20 /* 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; =20 /* 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); @@ -6560,18 +6558,18 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, return handle_userfault(&vmf, VM_UFFD_WP); } =20 - entry =3D huge_pte_clear_uffd_wp(entry); - set_huge_pte_at(mm, haddr, ptep, entry, + vmf.orig_pte =3D 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 */ } =20 /* - * 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 !=3D pagecache_folio or !pagecache_folio. */ - folio =3D page_folio(pte_page(entry)); + folio =3D page_folio(pte_page(vmf.orig_pte)); if (folio !=3D pagecache_folio) if (!folio_trylock(folio)) { need_wait_lock =3D 1; @@ -6581,24 +6579,24 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, stru= ct vm_area_struct *vma, folio_get(folio); =20 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { - if (!huge_pte_write(entry)) { - ret =3D hugetlb_wp(mm, vma, address, ptep, flags, - pagecache_folio, ptl, &vmf); + if (!huge_pte_write(vmf.orig_pte)) { + ret =3D 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 =3D huge_pte_mkdirty(entry); + vmf.orig_pte =3D huge_pte_mkdirty(vmf.orig_pte); } } - entry =3D pte_mkyoung(entry); - if (huge_ptep_set_access_flags(vma, haddr, ptep, entry, + vmf.orig_pte =3D 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 !=3D pagecache_folio) folio_unlock(folio); folio_put(folio); out_ptl: - spin_unlock(ptl); + spin_unlock(vmf.ptl); =20 if (pagecache_folio) { folio_unlock(pagecache_folio); --=20 2.43.0 From nobody Thu Feb 12 04:37:41 2026 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (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 D141853E12 for ; Mon, 1 Apr 2024 20:26:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712003220; cv=none; b=NuJtpRvjKsJ0KVhvl93xueLjA4CPmcFww/Jv5Jr1iscBvx0pLIDT69A0YCQX77Epc3vzyBOp2hufoD3S5gMyNfn6uiZr9YSzb9mjyaf/SRk/BTNe+PqlOmUi/sr1AVusKWrB/nVLsCXYThhxhC9KNykJ3I+Irm1rtLPi5sz0SOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712003220; c=relaxed/simple; bh=K2uUxOa/BPDSkpao/7L01Qy9c+58bEbnJO9fU8fkXq4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cc/cbgvBXjBwP+K2Hj2x8lC73YptAQyFY8iERYBVjDWWL4aLdrUsbsJcfU/OuflY0DuwYnJCzMfi1hpZqlBbtrRPZWMPuOCDB+fBvwFdFXm+REMhby0goSZlGJhAh7Rm0XzZyUiZgnwx3iWzkLi0Fak4+bY+1TumIuafKrKskNE= 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=Mjk1Sx6x; arc=none smtp.client-ip=209.85.128.170 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="Mjk1Sx6x" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-615038fc5baso7222667b3.3 for ; Mon, 01 Apr 2024 13:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712003218; x=1712608018; 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=WwRQK61zxAOtZTuzQkMVrkDP5lES+e4npSppnKc4AGI=; b=Mjk1Sx6x8CAgnv7UDMs2S+DU12YDXHVaTuumIfu3DF/NslvNiRskDCfCXglzlCNkb9 +zpmbUIRJlVT1WC/y5nuqTNkKskUO3dAXt0hKELOqY4Z1xynXA6ZMr1dnGgbs1EswKnB j/Sf0t/PV4C7P12T3ZHSjv3jluYHDXSo0jq5GvgKSXwomFCuLVGza2nTIcHArujmYSTO oopgHCCu3JzSwmUMVDr2Ept5az+sb4AVQ55IxxsphQji8PwoWVuqKLqTYtVEfw8l6y51 KBToE58dpxhGITHGmNOmwWaGOTkh5tAenOLnx/dd89d9tCNXDJF5AmZvMPSr3W+rebpc Eulg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712003218; x=1712608018; 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=WwRQK61zxAOtZTuzQkMVrkDP5lES+e4npSppnKc4AGI=; b=ttSFxrY3aevkQKeGgrNdiZl3ZFF9gskrCQKTbPMk+EbZKHjZADaQzJMNTxn0tSukRf 7ORK3XG92JS0kDFTJmzmf8bRpDP4JPgZiiHKieZXVCgOOLXms/iddry1+MGEHYzSOewN idoGULBn3NCENbnv2kCyNYMDlbZhKTrHpo1ynsZVWzGI9HsTL/4E3dzcwtsiqcp1qj+5 Tv+5yyqHuS/ugZAqSoNgdntGGIoda9AxMVeNmMFcUlXotky56Lyj3KQMtWSLiDbvDPxu MvGZ3HIaUEaKQ+trMj9oRj7YGhzzMxsxAYY65RZLziwi6LvecR4ULqzoRtHDlt2igvnf M1nw== X-Gm-Message-State: AOJu0Yw/q8EYzsghZQyWk6PDowBmB/e1dNKZ/NrQFP/ATz0Lw1in61l4 tyKY3zt25uLFbnj/5nIV9PHteSRiPtEmw2jIXUqk6x1dKIYPoKyJ X-Google-Smtp-Source: AGHT+IFisZq4EuxDLU4uS1EpjFvcKMqEqYfO+yamypsLgW77pMy3m4jr7Iv/oNYRYg9OIrA5cUAWAg== X-Received: by 2002:a0d:e7c6:0:b0:611:30a2:1758 with SMTP id q189-20020a0de7c6000000b0061130a21758mr9909937ywe.37.1712003217812; Mon, 01 Apr 2024 13:26:57 -0700 (PDT) Received: from fedora.attlocal.net ([2600:1700:2f7d:1800::23]) by smtp.googlemail.com with ESMTPSA id y72-20020a81a14b000000b006142210a31esm1171181ywg.23.2024.04.01.13.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 13:26:57 -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, willy@infradead.org, "Vishal Moola (Oracle)" Subject: [PATCH v2 2/3] hugetlb: Convert hugetlb_no_page() to use struct vm_fault Date: Mon, 1 Apr 2024 13:26:50 -0700 Message-ID: <20240401202651.31440-3-vishal.moola@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240401202651.31440-1-vishal.moola@gmail.com> References: <20240401202651.31440-1-vishal.moola@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hugetlb_no_page() can use the struct vm_fault passed in from hugetlb_fault(). This alleviates the stack by consolidating 7 variables into a single struct. Signed-off-by: Vishal Moola (Oracle) Reviewed-by: Oscar Salvador Suggested-by: Muchun Song Suggested-by: Oscar Salvador --- mm/hugetlb.c | 59 ++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 360b82374a89..aca2f11b4138 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6189,9 +6189,7 @@ static bool hugetlb_pte_stable(struct hstate *h, stru= ct mm_struct *mm, =20 static vm_fault_t hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, - struct address_space *mapping, pgoff_t idx, - unsigned long address, pte_t *ptep, - pte_t old_pte, unsigned int flags, + struct address_space *mapping, struct vm_fault *vmf) { struct hstate *h =3D hstate_vma(vma); @@ -6200,10 +6198,8 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *= mm, unsigned long size; struct folio *folio; pte_t new_pte; - spinlock_t *ptl; - unsigned long haddr =3D address & huge_page_mask(h); bool new_folio, new_pagecache_folio =3D false; - u32 hash =3D hugetlb_fault_mutex_hash(mapping, idx); + u32 hash =3D hugetlb_fault_mutex_hash(mapping, vmf->pgoff); =20 /* * Currently, we are forced to kill the process in the event the @@ -6222,10 +6218,10 @@ static vm_fault_t hugetlb_no_page(struct mm_struct = *mm, * before we get page_table_lock. */ new_folio =3D false; - folio =3D filemap_lock_hugetlb_folio(h, mapping, idx); + folio =3D filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff); if (IS_ERR(folio)) { size =3D i_size_read(mapping->host) >> huge_page_shift(h); - if (idx >=3D size) + if (vmf->pgoff >=3D size) goto out; /* Check for page in userfault range */ if (userfaultfd_missing(vma)) { @@ -6246,7 +6242,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *m= m, * never happen on the page after UFFDIO_COPY has * correctly installed the page and returned. */ - if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) { + if (!hugetlb_pte_stable(h, mm, vmf->pte, vmf->orig_pte)) { ret =3D 0; goto out; } @@ -6255,7 +6251,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *m= m, VM_UFFD_MISSING); } =20 - folio =3D alloc_hugetlb_folio(vma, haddr, 0); + folio =3D alloc_hugetlb_folio(vma, vmf->address, 0); if (IS_ERR(folio)) { /* * Returning error will result in faulting task being @@ -6269,18 +6265,20 @@ static vm_fault_t hugetlb_no_page(struct mm_struct = *mm, * here. Before returning error, get ptl and make * sure there really is no pte entry. */ - if (hugetlb_pte_stable(h, mm, ptep, old_pte)) + if (hugetlb_pte_stable(h, mm, vmf->pte, vmf->orig_pte)) ret =3D vmf_error(PTR_ERR(folio)); else ret =3D 0; goto out; } - clear_huge_page(&folio->page, address, pages_per_huge_page(h)); + clear_huge_page(&folio->page, vmf->real_address, + pages_per_huge_page(h)); __folio_mark_uptodate(folio); new_folio =3D true; =20 if (vma->vm_flags & VM_MAYSHARE) { - int err =3D hugetlb_add_to_page_cache(folio, mapping, idx); + int err =3D hugetlb_add_to_page_cache(folio, mapping, + vmf->pgoff); if (err) { /* * err can't be -EEXIST which implies someone @@ -6289,7 +6287,8 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *m= m, * to the page cache. So it's safe to call * restore_reserve_on_error() here. */ - restore_reserve_on_error(h, vma, haddr, folio); + restore_reserve_on_error(h, vma, vmf->address, + folio); folio_put(folio); goto out; } @@ -6319,7 +6318,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *m= m, folio_unlock(folio); folio_put(folio); /* See comment in userfaultfd_missing() block above */ - if (!hugetlb_pte_stable(h, mm, ptep, old_pte)) { + if (!hugetlb_pte_stable(h, mm, vmf->pte, vmf->orig_pte)) { ret =3D 0; goto out; } @@ -6334,23 +6333,23 @@ static vm_fault_t hugetlb_no_page(struct mm_struct = *mm, * any allocations necessary to record that reservation occur outside * the spinlock. */ - if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { - if (vma_needs_reservation(h, vma, haddr) < 0) { + if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { + if (vma_needs_reservation(h, vma, vmf->address) < 0) { ret =3D VM_FAULT_OOM; goto backout_unlocked; } /* Just decrements count, does not deallocate */ - vma_end_reservation(h, vma, haddr); + vma_end_reservation(h, vma, vmf->address); } =20 - ptl =3D huge_pte_lock(h, mm, ptep); + vmf->ptl =3D huge_pte_lock(h, mm, vmf->pte); ret =3D 0; /* If pte changed from under us, retry */ - if (!pte_same(huge_ptep_get(ptep), old_pte)) + if (!pte_same(huge_ptep_get(vmf->pte), vmf->orig_pte)) goto backout; =20 if (anon_rmap) - hugetlb_add_new_anon_rmap(folio, vma, haddr); + hugetlb_add_new_anon_rmap(folio, vma, vmf->address); else hugetlb_add_file_rmap(folio); new_pte =3D make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE) @@ -6359,17 +6358,18 @@ static vm_fault_t hugetlb_no_page(struct mm_struct = *mm, * If this pte was previously wr-protected, keep it wr-protected even * if populated. */ - if (unlikely(pte_marker_uffd_wp(old_pte))) + if (unlikely(pte_marker_uffd_wp(vmf->orig_pte))) new_pte =3D huge_pte_mkuffd_wp(new_pte); - set_huge_pte_at(mm, haddr, ptep, new_pte, huge_page_size(h)); + set_huge_pte_at(mm, vmf->address, vmf->pte, new_pte, huge_page_size(h)); =20 hugetlb_count_add(pages_per_huge_page(h), mm); - if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { + if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { /* Optimization, do the COW without a second fault */ - ret =3D hugetlb_wp(mm, vma, address, ptep, flags, folio, ptl, vmf); + ret =3D hugetlb_wp(mm, vma, vmf->real_address, vmf->pte, + vmf->flags, folio, vmf->ptl, vmf); } =20 - spin_unlock(ptl); + spin_unlock(vmf->ptl); =20 /* * Only set hugetlb_migratable in newly allocated pages. Existing pages @@ -6386,10 +6386,10 @@ static vm_fault_t hugetlb_no_page(struct mm_struct = *mm, return ret; =20 backout: - spin_unlock(ptl); + spin_unlock(vmf->ptl); backout_unlocked: if (new_folio && !new_pagecache_folio) - restore_reserve_on_error(h, vma, haddr, folio); + restore_reserve_on_error(h, vma, vmf->address, folio); =20 folio_unlock(folio); folio_put(folio); @@ -6485,8 +6485,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct= vm_area_struct *vma, * hugetlb_no_page will drop vma lock and hugetlb fault * mutex internally, which make us return immediately. */ - return hugetlb_no_page(mm, vma, mapping, vmf.pgoff, address, - vmf.pte, vmf.orig_pte, flags, &vmf); + return hugetlb_no_page(mm, vma, mapping, &vmf); } =20 ret =3D 0; --=20 2.43.0 From nobody Thu Feb 12 04:37:41 2026 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 07A8B53E3C for ; Mon, 1 Apr 2024 20:26:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712003221; cv=none; b=S1MgmPsuTFNC9UjZeKCq0yJ1KiReJMA1RL4f1xN45m+D4h8m1VQGBrj6rhov/YY0GiouLiVruymm0E+SVBxUuoV4lTBYhtkpNEgZtQxc1k+GejXU76VdhP3t1fJUtbkqC87JlpZ+ardz6LysoDgVMbPzvBiaAHeNH0/JOWxc2ys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712003221; c=relaxed/simple; bh=tiuLKvKM00xIkTz7M6ZfIlWlQS14HHGlVbF692jzUec=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vb9F4GvUX0vbWfv0/M5RSwL6C2ETo0LYG/GGisktFqlDz0sjl7IEnVWpaB1FlyNkad05Eirj8v5XRBr8ruLb7B5JMVYUSMcYvHRwysn4il6QS82nB40SeAx4FW8vUZABjFCMbXOirlmKPy1fJo0WvXIXO7zZpoIQyzMtjDoedj8= 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=EGC06imR; arc=none smtp.client-ip=209.85.128.171 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="EGC06imR" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-6150c1fa3daso6467767b3.1 for ; Mon, 01 Apr 2024 13:26:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712003219; x=1712608019; 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=09S6vJM8RrkdrC/rkJ0OHkr1C8kqrWar9y0fEbWEbHY=; b=EGC06imRb/Ypj6noRCP5eM+y+/UJ0Y8MNwrZ53fKirt0HdJ+0LoSaoep+RK75gAYcg +kXTBI6lWhYZDfPCEg2VWP0YJy8gUYnAy+un34V6nzpHAhgxYqT28quHqqj7rzdFbZi0 t1FrI933WImR6xkEKsHGBdzR9ELZEwoK67FWFd7qfl3dyXNxZfMtD6nUH/ywF+yF+7Nk ZhCZsB+zSK6OiP5flxv4XLmHL4ov6DaaZ6N85JvZ4/8pwxhMmqHphRlD3fbgvzxnxQ/B bITacghzH6L9rEwvOpR1f52qV2BjHgH+oeG+/HSkD2tIRaSZIp6Js8jkXSJwUL2jSYYO ns0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712003219; x=1712608019; 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=09S6vJM8RrkdrC/rkJ0OHkr1C8kqrWar9y0fEbWEbHY=; b=cMmwfMQk8IWYt0KAT2ChsXksu6SX7PKhC6259VENh+nCOxSkE7WLzpjzUvlNgZBsa3 tSjvjQluwicH2vLNjjvyww3H2GiB3k1PB7ZA0MjGXQ0MqSv0X1PCn1GpaOt3Wi96Udld M2weD7ERIPbVSBrEeon3C4j0BmSxO5RTftm30bRDtmoxwrgpW0e6M/kSND7XbUDW6JFc zRN0nDfF1F/d5d8zXQ+RGaGPAMlLHk8hJWj6yxphsdIR6jey/BxhuKpwe8QzOrKX0m08 6nQoE7h+XY8Fd5mBSCv8m0AuIi6usE7O22m7s4biezqKWup0aI7nRyqes0dTDyi9mwzj Sy8w== X-Gm-Message-State: AOJu0YwUHb3qLRa1gQlqqnbAU9CkB4SYKCqBVP76ok5FJWYSVw2EC5Hb QBYeb7YXFJInSu3IVpmGpFEQwI14BVznYJUG4X2KlYH2uqiKv85K X-Google-Smtp-Source: AGHT+IFZ180aOPTb1CRRZ2Bu4ACYmdeIb3qE3iLlY2duuKwa7eQd71eLTyZ9BqrB+5kaF1LaPNv20Q== X-Received: by 2002:a81:6f03:0:b0:610:1a19:14fa with SMTP id k3-20020a816f03000000b006101a1914famr9131808ywc.50.1712003218915; Mon, 01 Apr 2024 13:26:58 -0700 (PDT) Received: from fedora.attlocal.net ([2600:1700:2f7d:1800::23]) by smtp.googlemail.com with ESMTPSA id y72-20020a81a14b000000b006142210a31esm1171181ywg.23.2024.04.01.13.26.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 13:26:58 -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, willy@infradead.org, "Vishal Moola (Oracle)" Subject: [PATCH v2 3/3] hugetlb: Convert hugetlb_wp() to use struct vm_fault Date: Mon, 1 Apr 2024 13:26:51 -0700 Message-ID: <20240401202651.31440-4-vishal.moola@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240401202651.31440-1-vishal.moola@gmail.com> References: <20240401202651.31440-1-vishal.moola@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hugetlb_wp() can use the struct vm_fault passed in from hugetlb_fault(). This alleviates the stack by consolidating 5 variables into a single struct. Signed-off-by: Vishal Moola (Oracle) Reviewed-by: Oscar Salvador Suggested-by: Muchun Song Suggested-by: Oscar Salvador --- mm/hugetlb.c | 61 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index aca2f11b4138..d4f26947173e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5918,18 +5918,16 @@ static void unmap_ref_private(struct mm_struct *mm,= struct vm_area_struct *vma, * Keep the pte_same checks anyway to make transition from the mutex easie= r. */ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *= vma, - unsigned long address, pte_t *ptep, unsigned int flags, - struct folio *pagecache_folio, spinlock_t *ptl, + struct folio *pagecache_folio, struct vm_fault *vmf) { - const bool unshare =3D flags & FAULT_FLAG_UNSHARE; - pte_t pte =3D huge_ptep_get(ptep); + const bool unshare =3D vmf->flags & FAULT_FLAG_UNSHARE; + pte_t pte =3D huge_ptep_get(vmf->pte); struct hstate *h =3D hstate_vma(vma); struct folio *old_folio; struct folio *new_folio; int outside_reserve =3D 0; vm_fault_t ret =3D 0; - unsigned long haddr =3D address & huge_page_mask(h); struct mmu_notifier_range range; =20 /* @@ -5952,7 +5950,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, st= ruct vm_area_struct *vma, =20 /* Let's take out MAP_SHARED mappings first. */ if (vma->vm_flags & VM_MAYSHARE) { - set_huge_ptep_writable(vma, haddr, ptep); + set_huge_ptep_writable(vma, vmf->address, vmf->pte); return 0; } =20 @@ -5971,7 +5969,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, st= ruct vm_area_struct *vma, SetPageAnonExclusive(&old_folio->page); } if (likely(!unshare)) - set_huge_ptep_writable(vma, haddr, ptep); + set_huge_ptep_writable(vma, vmf->address, vmf->pte); =20 delayacct_wpcopy_end(); return 0; @@ -5998,8 +5996,8 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, st= ruct vm_area_struct *vma, * Drop page table lock as buddy allocator may be called. It will * be acquired again before returning to the caller, as expected. */ - spin_unlock(ptl); - new_folio =3D alloc_hugetlb_folio(vma, haddr, outside_reserve); + spin_unlock(vmf->ptl); + new_folio =3D alloc_hugetlb_folio(vma, vmf->address, outside_reserve); =20 if (IS_ERR(new_folio)) { /* @@ -6024,19 +6022,21 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, = struct vm_area_struct *vma, * * Reacquire both after unmap operation. */ - idx =3D vma_hugecache_offset(h, vma, haddr); + idx =3D vma_hugecache_offset(h, vma, vmf->address); hash =3D hugetlb_fault_mutex_hash(mapping, idx); hugetlb_vma_unlock_read(vma); mutex_unlock(&hugetlb_fault_mutex_table[hash]); =20 - unmap_ref_private(mm, vma, &old_folio->page, haddr); + unmap_ref_private(mm, vma, &old_folio->page, + vmf->address); =20 mutex_lock(&hugetlb_fault_mutex_table[hash]); hugetlb_vma_lock_read(vma); - spin_lock(ptl); - ptep =3D hugetlb_walk(vma, haddr, huge_page_size(h)); - if (likely(ptep && - pte_same(huge_ptep_get(ptep), pte))) + spin_lock(vmf->ptl); + vmf->pte =3D hugetlb_walk(vma, vmf->address, + huge_page_size(h)); + if (likely(vmf->pte && + pte_same(huge_ptep_get(vmf->pte), pte))) goto retry_avoidcopy; /* * race occurs while re-acquiring page table @@ -6058,37 +6058,38 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, = struct vm_area_struct *vma, if (unlikely(ret)) goto out_release_all; =20 - if (copy_user_large_folio(new_folio, old_folio, address, vma)) { + if (copy_user_large_folio(new_folio, old_folio, vmf->real_address, vma)) { ret =3D VM_FAULT_HWPOISON_LARGE; goto out_release_all; } __folio_mark_uptodate(new_folio); =20 - mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, haddr, - haddr + huge_page_size(h)); + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, vmf->address, + vmf->address + huge_page_size(h)); mmu_notifier_invalidate_range_start(&range); =20 /* * Retake the page table lock to check for racing updates * before the page tables are altered */ - spin_lock(ptl); - ptep =3D hugetlb_walk(vma, haddr, huge_page_size(h)); - if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) { + spin_lock(vmf->ptl); + vmf->pte =3D hugetlb_walk(vma, vmf->address, huge_page_size(h)); + if (likely(vmf->pte && pte_same(huge_ptep_get(vmf->pte), pte))) { pte_t newpte =3D make_huge_pte(vma, &new_folio->page, !unshare); =20 /* Break COW or unshare */ - huge_ptep_clear_flush(vma, haddr, ptep); + huge_ptep_clear_flush(vma, vmf->address, vmf->pte); hugetlb_remove_rmap(old_folio); - hugetlb_add_new_anon_rmap(new_folio, vma, haddr); + hugetlb_add_new_anon_rmap(new_folio, vma, vmf->address); if (huge_pte_uffd_wp(pte)) newpte =3D huge_pte_mkuffd_wp(newpte); - set_huge_pte_at(mm, haddr, ptep, newpte, huge_page_size(h)); + set_huge_pte_at(mm, vmf->address, vmf->pte, newpte, + huge_page_size(h)); folio_set_hugetlb_migratable(new_folio); /* Make the old page be freed below */ new_folio =3D old_folio; } - spin_unlock(ptl); + spin_unlock(vmf->ptl); mmu_notifier_invalidate_range_end(&range); out_release_all: /* @@ -6096,12 +6097,12 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, = struct vm_area_struct *vma, * unshare) */ if (new_folio !=3D old_folio) - restore_reserve_on_error(h, vma, haddr, new_folio); + restore_reserve_on_error(h, vma, vmf->address, new_folio); folio_put(new_folio); out_release_old: folio_put(old_folio); =20 - spin_lock(ptl); /* Caller expects lock to be held */ + spin_lock(vmf->ptl); /* Caller expects lock to be held */ =20 delayacct_wpcopy_end(); return ret; @@ -6365,8 +6366,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *m= m, hugetlb_count_add(pages_per_huge_page(h), mm); if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { /* Optimization, do the COW without a second fault */ - ret =3D hugetlb_wp(mm, vma, vmf->real_address, vmf->pte, - vmf->flags, folio, vmf->ptl, vmf); + ret =3D hugetlb_wp(mm, vma, folio, vmf); } =20 spin_unlock(vmf->ptl); @@ -6579,8 +6579,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct= vm_area_struct *vma, =20 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { if (!huge_pte_write(vmf.orig_pte)) { - ret =3D hugetlb_wp(mm, vma, address, vmf.pte, flags, - pagecache_folio, vmf.ptl, &vmf); + ret =3D hugetlb_wp(mm, vma, pagecache_folio, &vmf); goto out_put_page; } else if (likely(flags & FAULT_FLAG_WRITE)) { vmf.orig_pte =3D huge_pte_mkdirty(vmf.orig_pte); --=20 2.43.0