From patchwork Sat Sep 14 19:41:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vishal Moola X-Patchwork-Id: 13804546 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 DBECEC021AF for ; Sat, 14 Sep 2024 19:43:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0E7B6B007B; Sat, 14 Sep 2024 15:43:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BE7F6B0082; Sat, 14 Sep 2024 15:43:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 885E86B0083; Sat, 14 Sep 2024 15:43:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6B7956B007B for ; Sat, 14 Sep 2024 15:43:34 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DBDD01A0A15 for ; Sat, 14 Sep 2024 19:43:33 +0000 (UTC) X-FDA: 82564368306.01.D8E8EF1 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf09.hostedemail.com (Postfix) with ESMTP id 17932140003 for ; Sat, 14 Sep 2024 19:43:31 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QgSzILIp; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.170 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=1726342892; 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=lMqUitFjkcZcjYpS18hn6NOxdl1sw7VhZop5Rjz/ORE=; b=yHnQTjNu5otdcBqr2lcwYDl8e+ally5tHhBfYuDACTYmT9s/+Jbh/zrnADxEwW1Uqx7fYO i9+h5lUVrMNr5qQB4Srx76pg5XBfFJ4maBh6hcICfjoxjdOlxqn1ClNHBnaZKffcCiZybT TpSiqEAEXwRSaDZv0Dj7rSxvaf9c8xc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726342892; a=rsa-sha256; cv=none; b=NEI7aatSBPpsEjXlaA4ZK/fU0NKpwSoDuaRmaealDuzTQ3rrftwtHKVoszKWlWE6gqtuk6 oGR367o7SZ7VwUrfjIDCYoQ763ClOk1caf5bdz59HF3NfF482jbu8AQELv8BisD3byIngM puOuLhsjgkSCQ5hhvme0pnjmyCw5JWA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QgSzILIp; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7191df6b5f5so2332437b3a.0 for ; Sat, 14 Sep 2024 12:43:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726343011; x=1726947811; 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=lMqUitFjkcZcjYpS18hn6NOxdl1sw7VhZop5Rjz/ORE=; b=QgSzILIp846BDkF+LU58FCwS3l0bcaA9juUQ+8JTFWgZkzvO2rThXnS7Xcuba+JAZs axY7v5foeDkJqfV43g7FvylsRWgJ1iuT+n8oF8Dkcxhz1bwU0LUy0+4KJrBkGZ6yU/5W Odj1DlMQhqFknoYrocI6EvfZ2gKg/2Ry9dxW6NMM6oKnWlkS/TyE1e3jNnGHy2L0QbAe s/k/8S3NdqApWmB+afu5iMu3jxuE7WMEZM2mJe8kBRt4b7rpiJoFZDwqSyvEt73SbCBm HcLPj0gjf7YUYCaOSopqEZB2++n1TEsfJY3aBzKxPswN6oK73MTpyqapNNVGtdlAdmcH T7VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726343011; x=1726947811; 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=lMqUitFjkcZcjYpS18hn6NOxdl1sw7VhZop5Rjz/ORE=; b=nhOh9ou4wst0rAV3+7zSKFrzYuREAxgQWLvDEKyn1jB+o9u0dIOCfbZS0YNkh6T5dR rCw6Y9WkeaD5o3OoURvFh3ALm+esrKcNpIdCVcN0Bbv734VUs7JB1/kl4HDxIp17WKb3 fLdsAKeXknHW8kPpLyU2nSsIFo0j2l5yycNv9epZMIQMvbIiQCaWI95R1P4ZyJ5HNKzS JJQ00yB0ezl+wIm1n1sSRvAdapcPFa9ZtP3JKnVyBns3YABnqeV86CTRYSpl72IsUmKT T7cm22hpFYEL3vl462R9uSPQfGBoUiCgTynSG0OlTlpAXTbdGagnH+QrX3xq8f02VSWS VoHg== X-Forwarded-Encrypted: i=1; AJvYcCWiq0TpvTT6gcBlwVcsGF/ZMwSCfu/N0/wCNsmPaAsouxAl0J0wi0o8eZ4C3IGlZplz+tOirF1bOA==@kvack.org X-Gm-Message-State: AOJu0YwLJpUq84KDEPFInupv/Tcs17k6J3B9h0/PM5w0RM7j/vqsAcBq vWbDQ6QepSJX1QYIGn1PC95wTAqjRtpz5QOuxiFzezwk1SXd4JO9 X-Google-Smtp-Source: AGHT+IGbSz5ZrbkdFVY2/IUM0yAh9WDYo41O7oqGgog7gsBuZxxoqHgZ7U8FgzUcNxgXkrRlAxoFeQ== X-Received: by 2002:a05:6a21:4a41:b0:1cf:3b0b:d723 with SMTP id adf61e73a8af0-1cf75c7ed30mr14321637637.8.1726343010479; Sat, 14 Sep 2024 12:43:30 -0700 (PDT) Received: from localhost.localdomain (c-67-164-59-41.hsd1.ca.comcast.net. [67.164.59.41]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71944a9cab5sm1294742b3a.5.2024.09.14.12.43.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 12:43:29 -0700 (PDT) From: "Vishal Moola (Oracle)" To: Muchun Song , syzbot , Andrew Morton , LKML , Linux Memory Management List , syzkaller-bugs@googlegroups.com Cc: "Vishal Moola (Oracle)" Subject: [PATCH 1/2] mm: Change vmf_anon_prepare() to __vmf_anon_prepare() Date: Sat, 14 Sep 2024 12:41:18 -0700 Message-ID: <20240914194243.245-1-vishal.moola@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <7B2E5B76-8FC6-49EA-B0B3-2452ED6ABC5D@linux.dev> References: <7B2E5B76-8FC6-49EA-B0B3-2452ED6ABC5D@linux.dev> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 17932140003 X-Stat-Signature: ayypsqdzoubfffqwempyf6zdtnuqnbxa X-Rspam-User: X-HE-Tag: 1726343011-752017 X-HE-Meta: U2FsdGVkX19cc4is8VU/qYfqsiw/jDuKTHolVyu3qRb3ZheSKYT610BuYEqVhUKaany5NydG/9HdnESwvdyCv0eygp1kk+N57zGSefRHr1oiCCHzm9WPh1j1wZ9rMnpuGlLGcLgbUoSNF9MsNpC3PHeOR4QR3d9C+cXmVAvuLSS41Ltmhmpvbwtr+zreJpHeSZzG+/IKkfb3ZIXjkfLimm27G9PF5eMl+AvXEPQhaKLZ0shYAd0pTEaxbYmHpu6iSZK0xzwJZTKdj+cG0TTxFO42TPMPK4QQH+ICcNpvuHewoKVrtHLHj2QC9I7vL7wBR7vvCKdp8TBKXut39NlCEtC20W4s4axnVrMPLEIwvw9//FRF7cD4f5Xcp0IyTACseklpCNJs4hAPol+0tkMA5uHCe4r/EpjTFQln1rz2rjyx6YQTEMeRft2sEvpEuvjwZSUjWF8xlTUIO3myRZ0f30NjTwGkTo2ALppzbthQYJVNXFb0kVuE/+mGJHyQwl8GRqAPBZYFrsXMvsdhsrnMmm83TCxrK2KvFeHMo7wtvspzUf1Tw6dfrBHeFU+CO+yQsgAl7omh0Bet2IibHlwxpAUHMjZG67X4McGuxpbfdRZwWLjLLd9XnvK+rSyadglz0jzzcXBKz9uumWtpp8QI3Bvesfg5TxLtnottsobEMwv8glXKScrFPPxMduD+cd0zM4NcdF/iT/je5ZAWd85aYFhlZuvxCzBq1z8GFrHHbLQ8ptyUFPYxxfTqLID0jOzW1vNMOrhjW3PdDDuM5EutYYPdocn1jWmk/PrQeKFVF0Znh4O1cVSUGUwR8K8UYshY4pyxZ8/RISkbQ/aAujcg4Q07CsZ/iM05y/KMh+4E3HhGHNW2HNOH9dWwUTN+JIqheDADfECEVL06FKeHs0wO2jmPelyDPS3FybvpSxWW93esC9l2w1duNZMftRwtHKGrfdPRIpnY5wA6PwN2a65 KJABVdET enuoXQt7K0ex+FUXKZCb2l6HDIYl1WSC+Nmpy7FgvFcwSlsOPdqANRwARwsNfOh2QoUFaz8idzQ3kDOYy/bCJ/CFSF0efDEpXyOIQyoiPCA3h/HBW53GM+WWDTOHWK6KCAABrAnGg0guWRU5GLnLe/2KyleDEU3X1O8SmM/nTAKuUgxo3iF2GfigOA0pmiPatmgJiFwzmPhCdpl1U6cUIqlSdWdSPVwcEeH6M6pRlE1VD4lPZbGcCnB5CcPwYNt9/qYf5L/0pV57sENnJf/wcT7z9AhSSjd/EnbCBh/eYdMb2t7NhqlyPaisaoN48xdVMvU5CdJxQTncvUDUliGPEFO+oKX6JHpaoDeVhNGT9+U/+w0m5mLXFNZEdkBws7HXKmAISUVkLjdHgvKLkbaX9SxeZy4ceg6Di7k1VIEenJtUsK17JJ5EkD7XVHXKMH1MbsG3+3wZ73AJvxX7ZIvaVavHYNtJj5DdCx/4fdX+9LNtBzKR0n8xecNFeurEMciTZ3K2Pg52Rvo/Tn6QNfOjnMTh8xsDiGTfcou5GqJKQWpYuhmMLWgIgjwFuOes/h45+SGBkc/rzIyNc+cmk6SV4tV5d85c6N5HxbcAl 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: Some callers of vmf_anon_prepare() may not want us to release the per-VMA lock ourselves. Rename vmf_anon_prepare() to __vmf_anon_prepare() and let the callers drop the lock when desired. Also, make vmf_anon_prepare() a wrapper that releases the per-VMA lock itself for any callers that don't care. This is in preparation to fix this bug reported by syzbot: https://lore.kernel.org/linux-mm/00000000000067c20b06219fbc26@google.com/ Signed-off-by: Vishal Moola (Oracle) --- mm/internal.h | 11 ++++++++++- mm/memory.c | 8 +++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 44c8dec1f0d7..93083bbeeefa 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -320,7 +320,16 @@ static inline void wake_throttle_isolated(pg_data_t *pgdat) wake_up(wqh); } -vm_fault_t vmf_anon_prepare(struct vm_fault *vmf); +vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf); +static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) +{ + vm_fault_t ret = __vmf_anon_prepare(vmf); + + if (unlikely(ret & VM_FAULT_RETRY)) + vma_end_read(vmf->vma); + return ret; +} + vm_fault_t do_swap_page(struct vm_fault *vmf); void folio_rotate_reclaimable(struct folio *folio); bool __folio_end_writeback(struct folio *folio); diff --git a/mm/memory.c b/mm/memory.c index 36f655eb66c4..d564737255f8 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3262,7 +3262,7 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) } /** - * vmf_anon_prepare - Prepare to handle an anonymous fault. + * __vmf_anon_prepare - Prepare to handle an anonymous fault. * @vmf: The vm_fault descriptor passed from the fault handler. * * When preparing to insert an anonymous page into a VMA from a @@ -3276,7 +3276,7 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) * Return: 0 if fault handling can proceed. Any other value should be * returned to the caller. */ -vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) +vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; vm_fault_t ret = 0; @@ -3284,10 +3284,8 @@ vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) if (likely(vma->anon_vma)) return 0; if (vmf->flags & FAULT_FLAG_VMA_LOCK) { - if (!mmap_read_trylock(vma->vm_mm)) { - vma_end_read(vma); + if (!mmap_read_trylock(vma->vm_mm)) return VM_FAULT_RETRY; - } } if (__anon_vma_prepare(vma)) ret = VM_FAULT_OOM; From patchwork Sat Sep 14 19:41:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vishal Moola X-Patchwork-Id: 13804547 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 8EAF3C021AF for ; Sat, 14 Sep 2024 19:43:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E85EE6B0083; Sat, 14 Sep 2024 15:43:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E2BC76B0088; Sat, 14 Sep 2024 15:43:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF34B6B0089; Sat, 14 Sep 2024 15:43:51 -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 B0FF76B0083 for ; Sat, 14 Sep 2024 15:43:51 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 33266160975 for ; Sat, 14 Sep 2024 19:43:51 +0000 (UTC) X-FDA: 82564369062.18.C4E54AD Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf17.hostedemail.com (Postfix) with ESMTP id 5941940008 for ; Sat, 14 Sep 2024 19:43:49 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M8pPC0ZK; spf=pass (imf17.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726342975; 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=qEFv/WIVVDnR9INyh6CO+aEW/z2pecEd+lx/1UNH+Es=; b=LzAtupWDoquhsnIt41vTGB5fhRoviRFc3YypzpVgcWwcZoHdlhVIuqIgVKaks3qptxEZ4l jq90GWjjG1eFxf61Y1jIoH9MtNG2peB1JvCOkYX2Ec+Sv2sQ88fdJS75tsHgL8GTZnad5J EME8XLmdKTGavfe2cUS3pOFUo6cM53s= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=M8pPC0ZK; spf=pass (imf17.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726342975; a=rsa-sha256; cv=none; b=Bq4YCUrX93Vm8EtuyDjoxw3a20SwkXtCKrkLOgcIi/1hRJ3tHxG72r40GOTrl5Q6LaTUY1 BQvPNS7BoJtjfiZ5HPiHO8sct8NlRXeg+H1uEl5nywSLjU2UQh49e31e/3jF/QhFC9qnTw M4BSS5gDoE/CtzpuaPTTlQGlUWjSXds= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-718e285544fso1616586b3a.1 for ; Sat, 14 Sep 2024 12:43:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726343028; x=1726947828; 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=qEFv/WIVVDnR9INyh6CO+aEW/z2pecEd+lx/1UNH+Es=; b=M8pPC0ZKg+Ao0lFls8tl01w+qPPD7urtduWzQDQllZQ5KLnbio6IznQdWtIaNLou6T fowE1ep+krexoD7xaGw/P1gdUnizTOdrB/sZZi4eMMYzT+Q5I+PmMBzHIfX7o6B6ebFI RxWXUj6LchAqWJBfX2ClBNPJWePxPOknKPZb8T5tnjquIiJuDfYmxnduPKpKvJq5P1jZ wtPiuYXYoS9VEEk4al/MfP/G8TEp1fGXVODVLPFzCk3vw4iIDTmb0aa4blFgSs15dD4K A7++gMrc13bR0o4A1dR4dsr5HL7TsUNiPZ7FvOdrhl4TVJKndMNH7wGmZ7NIY4Hs+LaU qj9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726343028; x=1726947828; 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=qEFv/WIVVDnR9INyh6CO+aEW/z2pecEd+lx/1UNH+Es=; b=U21s+3a2W1FJV+1ntX16rlmAtumy5B/wZWMQAXPBnlXh4YwHSZsFeXWFO7WwbNbuW3 XilvJCfhFPuyXg7WWJvxIk5o8kEnXwX7ISgIOIfCUJNBNGkYQDQgIn911yQ4l2+wFJr+ zyx99uuVkFuJ4MC93kMWU46dxR2CZhcvDKHoZV/Cas7KB1h1JrER9/+YL45ZWpLu+FqD uDv5yGEU84vuqnZSUTGixKGohWCnMGUII71cieB9rcm6X7ecoLzRobf3vYmC+0wmNUdd kmC/IXamFoxIiva6I2AN0WB/UyNyBkXbhELh7DTuYBEnhnwRKA9nk7GKlMqRjSdoDBCr HuIg== X-Forwarded-Encrypted: i=1; AJvYcCXEbIRD+QArs4Fs5RYkiS+y5Q+qARv2y59+o0NyL42Pc8lNF2/r4IChrOePeA17ZNUPP4LBb4gKDg==@kvack.org X-Gm-Message-State: AOJu0YwwkI0EEYD+ayNqqPgFxmMoJpnfq5z1jMo5kU2q11nbzOgeuBjB 4JnNrJ+fxp6fFKa5X5p8FjsiRGd3Iaa7NzhbNkMk5qqjwYr+m++H X-Google-Smtp-Source: AGHT+IHrDIsY/QTLODnUtzDg70cOoM1IO63V2p1YyrSgtGMrwYZoljG+kzn5MdMlfDTCg2BnwTHqUw== X-Received: by 2002:a05:6a00:1701:b0:717:8b4e:98b6 with SMTP id d2e1a72fcca58-71936adf4a5mr11079419b3a.21.1726343028008; Sat, 14 Sep 2024 12:43:48 -0700 (PDT) Received: from localhost.localdomain (c-67-164-59-41.hsd1.ca.comcast.net. [67.164.59.41]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71944a9cab5sm1294742b3a.5.2024.09.14.12.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 12:43:47 -0700 (PDT) From: "Vishal Moola (Oracle)" To: Muchun Song , syzbot , Andrew Morton , LKML , Linux Memory Management List , syzkaller-bugs@googlegroups.com Cc: "Vishal Moola (Oracle)" , stable@vger.kernel.org Subject: [PATCH 2/2] mm/hugetlb.c: Fix UAF of vma in hugetlb fault pathway Date: Sat, 14 Sep 2024 12:41:19 -0700 Message-ID: <20240914194243.245-2-vishal.moola@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240914194243.245-1-vishal.moola@gmail.com> References: <7B2E5B76-8FC6-49EA-B0B3-2452ED6ABC5D@linux.dev> <20240914194243.245-1-vishal.moola@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 5941940008 X-Stat-Signature: kwn118db9xt1gzhkoob4twesgqwrt341 X-HE-Tag: 1726343029-479584 X-HE-Meta: U2FsdGVkX1/rJYuz6EUVA/HBx+dW1TOaPtIw3a1Q6V8adH4qTXYurJCD63VeD6SWEmbcpB0ruc3Nlm9s+TdspQ5DCq8romE89kDiOF5jXQ0lptTfNZ+DzKcR4E5P4Hu8/4YSixx24rIURQsUWgtwNrjJqAv0wbrwbA1CdpwAK25b4amzH3FGHmBXBgeqetcMVBroVPhqpxW8b8oTo3IPTuD1z2uKGG97vB2Gear7U2ghwAEndKYTqEnaADye53mOHNU7BkckTszJfIbOlB5BCZ0Q2Wzj5+5WTCkjQ47xVP7jgP47a3zCzdUT9966QsXecvSyXfKWLVFRaCGJ3PnkWOHKkROy0+6dU7HP0NM59Qj2eSVKKuFQ1DcO9/Vetx3I6CfafBJEVBzuYtN62WmJZ/HeyVOYabTUjqUBKWOTFMnEVG/6Dacn1e+XqZ0/x0NQEu5GpQ2TnBGScgKl2Lcrc1NAJwJNc0Urb75EIdkGf6sBvEHBCnvID+WrNjPjaUgHYccozxHVG6NQm7Pt7Moc+WnjC6tRsQyadL2kRUJ6jvJ9qnlJZ0cA7/zyGafNMjrKexjO8mZX3rUlaPz9qWYvkwNQf2CIW1pqXdrKhejqI6KPrchl8XXnDc0nopAmsepMVkNyIhjkrR+d6uiNey7rh6IWs8/YFaxBYUJibAip+Y8uJ8Pe0cFMGslo4QTeoFxBLtnEgeJ4NZAIIgLKV0H+QW+tZ7bqw0zhyERcLyWsgzaGx+Aq1g9DrPqBLOhqRo2FfJM+0SkDLmxalSqDT0OgvJioAWwxKFmPpQ66tln/YjbKSIRJKPfX675Hk0i6XNeihYhXXehqrZBVKB/jQ70VUOWp9UNBKNRZB5/et9R/YSKFJOGIPW3R9fX3vmLR5YaWzzDWNYBRPpHQa+N5lApwI7HnQ7941Euv+vmm9nkZVM4B8ueyjpPFd2UGJplSq3G42O477jpcOZ0lYVlj/En xJMBRDlD mOsrJvFeQjHskTZQh1drhHAasZUe3I2A/kUIFHWZwYtNaFFVfqDF+oNugjlOKZh11kU/uQFYpOob77MH4Ou4L4tVb1e3gumcvCsE+OLwAePPvL2xJKG3ZauEdCTDEdWJpCCclYpWWGTmHvVjSWGPgZBw+lbXEbCu5K56z6aT2VOSRIzi6sn0fMZlOv7DmRIXPMgf4G5/f0668B4UPA6pLrv+rUDfObU8GWwEl6zkoArxQwuiIQ4N1477HAjR+th6jU81B5YH6RTyOs65B40+vQc6Y2laGGOXhz/jt1Y3J7iAOIqambs4bxqT5e3vC4jLDIzdPLqD2RTl6cgRtqWagKWCs6lAWC9D3XbfrscItH917IIFCOfg+tUy89oy2lTNSaen5Ly7jkSUDA6v5bmQcA0tnIOj4dQTnt0/T5TozqRtRvD+w8UnG8roTa1gYThNR3xUVTImtFDZrtvnkJeets7teVnEGqpUYu3dlLiucDAXpf5FXA2gkYzJRykrM4cb5mKUpmSchsx6Bah4KQqqr4cWqNlSOWAp+G/npfAPLtClj9cYXQLR2gFVyCBz0KqpZWQlT7Dtzp/hGJF/3l6Z+IQZLEDk+o9MV7AF74DqeXtBagXEoPlUZoeQecA== 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: Syzbot reports a UAF in hugetlb_fault(). This happens because vmf_anon_prepare() could drop the per-VMA lock and allow the current VMA to be freed before hugetlb_vma_unlock_read() is called. We can fix this by using a modified version of vmf_anon_prepare() that doesn't release the VMA lock on failure, and then release it ourselves after hugetlb_vma_unlock_read(). Reported-by: syzbot+2dab93857ee95f2eeb08@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-mm/00000000000067c20b06219fbc26@google.com/ Fixes: 9acad7ba3e25 ("hugetlb: use vmf_anon_prepare() instead of anon_vma_prepare()") Signed-off-by: Vishal Moola (Oracle) Cc: --- mm/hugetlb.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 5c77defad295..190fa05635f4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5915,7 +5915,7 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio, * When the original hugepage is shared one, it does not have * anon_vma prepared. */ - ret = vmf_anon_prepare(vmf); + ret = __vmf_anon_prepare(vmf); if (unlikely(ret)) goto out_release_all; @@ -6114,7 +6114,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, } if (!(vma->vm_flags & VM_MAYSHARE)) { - ret = vmf_anon_prepare(vmf); + ret = __vmf_anon_prepare(vmf); if (unlikely(ret)) goto out; } @@ -6245,6 +6245,14 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, folio_unlock(folio); out: hugetlb_vma_unlock_read(vma); + + /* + * We must check to release the per-VMA lock. __vmf_anon_prepare() is + * the only way ret can be set to VM_FAULT_RETRY. + */ + if (unlikely(ret & VM_FAULT_RETRY)) + vma_end_read(vma); + mutex_unlock(&hugetlb_fault_mutex_table[hash]); return ret; @@ -6466,6 +6474,14 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, } out_mutex: hugetlb_vma_unlock_read(vma); + + /* + * We must check to release the per-VMA lock. __vmf_anon_prepare() in + * hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY. + */ + if (unlikely(ret & VM_FAULT_RETRY)) + vma_end_read(vma); + mutex_unlock(&hugetlb_fault_mutex_table[hash]); /* * Generally it's safe to hold refcount during waiting page lock. But