From patchwork Wed Aug 21 08:18:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13770985 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 01195C52D7C for ; Wed, 21 Aug 2024 08:19:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 694816B00C1; Wed, 21 Aug 2024 04:19:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 642CB6B00C2; Wed, 21 Aug 2024 04:19:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E4886B00C3; Wed, 21 Aug 2024 04:19:43 -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 2EA9C6B00C1 for ; Wed, 21 Aug 2024 04:19:43 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C67CDC0855 for ; Wed, 21 Aug 2024 08:19:42 +0000 (UTC) X-FDA: 82475553804.04.6D81D9B Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf10.hostedemail.com (Postfix) with ESMTP id D57B6C002C for ; Wed, 21 Aug 2024 08:19:40 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=LgLLmggg; spf=pass (imf10.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724228302; 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=arLLKSkNLaMIjiTVBc0cMKPJV4f+y4Msx8WJt/66k5c=; b=AaA3yh8fyKBHkKo0PPdq67zoYYadtcbqvQ75BGBnm1Vo6WUcMQ7GF/HvCBwfFOGjEu9yZt 54x5rcnE44Sco5QQOBymia3wZ9h4ELq0nxSXZ46uYJet8dqGaWO4S1hlOqtW0LkimuyAdi MgEpH3/T1qn7EEzWKTXP0KViplyTWQc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724228302; a=rsa-sha256; cv=none; b=EDP7jO6DTSX0d46RLbOMAL4WEgHuIaOf8pWBCLr5kz89w2BGi1Vzz0iu34FVdqx0svosO4 BX/HxeUWNc1MI8GLlvbYf0zYqNt+JLAa46qhsI9iHQEoBO2qDLpKYMrdcCbmMv7X5UVwtN vlXpPkPhfm4AFVr3xows1a7/R1noLfw= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=LgLLmggg; spf=pass (imf10.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-7bcf8077742so4723020a12.0 for ; Wed, 21 Aug 2024 01:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724228379; x=1724833179; 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=arLLKSkNLaMIjiTVBc0cMKPJV4f+y4Msx8WJt/66k5c=; b=LgLLmggg+bMyiFAaytP1QABJscooNxFzOuvtEcAVsokhKQFOj9iVXDS9TvaqBR/diO 8rzU12VeN20OBnXhYm0oUiKwSztN3UyW6AqcubY2PW92t1y3fzmMW8uiZ1MBhHYsVqKv t6TX8a7oI+zytifvA1GoJQx0vC6rgGrGPzBxopPotgUaR+QlQBjiVzsEuzqgwYzPHFkN g2GDwwuFMekD9DYs1pYCbiQv4m3+HDuT9D+2ZnaOeF7tZ9jhAUTU3jjgR6azahfk/LYr 4RTFfpINM6kSZlbFe09YSJAabdBfw6m7Cmi5Ut4QyVwJsJ317P0vqtBV8Ljnbblwg6Bf UK3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724228379; x=1724833179; 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=arLLKSkNLaMIjiTVBc0cMKPJV4f+y4Msx8WJt/66k5c=; b=aVo6CkFXdMools94EKB1WQUPCEd1Eg6g77s9y2gILlwEA2H9vrp/9AZELNLg06KW25 WdYUnwai6A3vm1rDm71VuU0NkMc0NoYD9Qza9R2KMvlIXxOyljzBKgCqAPMnU8QGIVRb EG/mV6+rK6ytPm2O2To4An3ze/1x8sW5FMW4WTcAatmWLCQbt/vUJR/VVaKWbvGblyo4 Z+ocxbGMpI7pCsIyU0LCUOv/sTX6aox8ZueRueo524+YX5tm4Qlrk5JodK58z2dyoyfR n5L/VgUufion4FGVvDGfuuhUcvI6Z9k/OmYnBBBaezrN0x2pry1EHyjvawaSzGxeleuB rBTw== X-Forwarded-Encrypted: i=1; AJvYcCUfkeEDVqQiLpaRn9PblYCYAQGlsuIHdTtoLizxCln6Ugxys5ILhTs30TLm0r3hVMYVSme6kJpybg==@kvack.org X-Gm-Message-State: AOJu0YyuXSKKZ8MjMfJilchAWqhIXp6ozwV/ETYh+14gZAlWpMvhnFPy SBwHCstf2QBay87n6XEvr7umCqEI+bulFPa0QKHFDlw+HbGfLly0/7UbUg391mM= X-Google-Smtp-Source: AGHT+IEvHWRxH/KV5glvGK0Xiv2dJ13pnLs9scNXFJwmOSFepkjBPO6rW4EjBZpYY7GPQ4yq94i7jA== X-Received: by 2002:a05:6a21:168e:b0:1c4:d05c:a967 with SMTP id adf61e73a8af0-1cada179499mr1734402637.51.1724228379371; Wed, 21 Aug 2024 01:19:39 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9049b0sm1091453a91.17.2024.08.21.01.19.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 01:19:38 -0700 (PDT) From: Qi Zheng To: david@redhat.com, hughd@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, akpm@linux-foundation.org, rppt@kernel.org, vishal.moola@gmail.com, peterx@redhat.com, ryan.roberts@arm.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, Qi Zheng Subject: [PATCH 02/14] arm: adjust_pte() use pte_offset_map_maywrite_nolock() Date: Wed, 21 Aug 2024 16:18:45 +0800 Message-Id: <3d151aaf876b93a97fd3f070780345a3bf572123.1724226076.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: D57B6C002C X-Stat-Signature: 9nfdwrcgb9k6mu1whbcdii84sewi376e X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1724228380-946448 X-HE-Meta: U2FsdGVkX1/PI99iFR07iiTfyNqWBqaNEysSlN6toAd5ZppVYOo/MU2rFpuwMUOrzEvJjKi8Q7QxZRIvIDqKlcVuSiuvU89HwUGI1FjuneDOHS0wf9HhYeE0u1ouN5MKx6rEESCYSaD4tM7UzQh9VXysq5NpFu/BDdUQBgzMkQ69LxOlTc1lW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5C5L3ue43W/EGzjdiLXaqXfXx+Bb/LbrUzn7FHEe+iSvVJIDq1ZqpuQZgjD3FmuLzH4en1NE2V0vcExzHbJlBrTqsoGU+VDGg+bZTI35tLHFS3W76ykeIu9HtHeDNSSF2YKSASuqXjruST0IEuFY9MmLOYW4QI+ntByrWsflA4sWxhJ81krZz 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: In do_adjust_pte(), we may modify the pte entry. At this time, the write lock of mmap_lock is not held, and the pte_same() check is not performed after the PTL held. The corresponding pmd entry may have been modified concurrently. Therefore, in order to ensure the stability if pmd entry, use pte_offset_map_maywrite_nolock() to replace pte_offset_map_nolock(), and do pmd_same() check after holding the PTL. Signed-off-by: Qi Zheng --- arch/arm/mm/fault-armv.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/arm/mm/fault-armv.c b/arch/arm/mm/fault-armv.c index 831793cd6ff94..5371920ec0550 100644 --- a/arch/arm/mm/fault-armv.c +++ b/arch/arm/mm/fault-armv.c @@ -94,6 +94,7 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address, pud_t *pud; pmd_t *pmd; pte_t *pte; + pmd_t pmdval; int ret; pgd = pgd_offset(vma->vm_mm, address); @@ -112,16 +113,22 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address, if (pmd_none_or_clear_bad(pmd)) return 0; +again: /* * This is called while another page table is mapped, so we * must use the nested version. This also means we need to * open-code the spin-locking. */ - pte = pte_offset_map_nolock(vma->vm_mm, pmd, address, &ptl); + pte = pte_offset_map_maywrite_nolock(vma->vm_mm, pmd, address, &pmdval, &ptl); if (!pte) return 0; do_pte_lock(ptl); + if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) { + do_pte_unlock(ptl); + pte_unmap(pte); + goto again; + } ret = do_adjust_pte(vma, address, pfn, pte);