From patchwork Thu Aug 22 07:13:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13772866 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 00732C531DF for ; Thu, 22 Aug 2024 07:16:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=d/m1IXPfk7xvDU+Qn41r4NiqBFnWc/gEWMU/opSilGI=; b=CobBl77/CaXlISUZBRF7zVm++N EyI166pU2Okul97hDwfq9MkRn7SEk6/bk5e4k8X0pV+KgeONANHCHVYUVXA1Jray9CAHZeCtTGfOU vVxbQjueAb+a24BOu5/JBrNilnZVXm/jGZvaFaesgjAAlnrySB41h+Elj9S6dyZFLRA47mFklsQlL kA4otoUSK4aDG6ZVhTH1XvKrgFjcEaYvWO3vQBA5M2ZZEWnxt+9nsct6c/+aQykdyNnIK48A7aWhC NHPXYQGHZwNiNlCWCohVo6oHRF6yH7u+YnW3xQdzmYOPYWPKozf42mlp/SLKVgw47rvr/OgnNXAEo ZfXEGVwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh23X-0000000BoBI-15Xs; Thu, 22 Aug 2024 07:16:27 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh21B-0000000BmpZ-0oHw for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 07:14:03 +0000 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-714187df604so1006290b3a.1 for ; Thu, 22 Aug 2024 00:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724310840; x=1724915640; darn=lists.infradead.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=d/m1IXPfk7xvDU+Qn41r4NiqBFnWc/gEWMU/opSilGI=; b=caSGutUze0wUd1bT/j1bP9AFpVoDzEBspl8zIgOtjMYxH5VR1Xsqy3HJ7W0owFmiwq hFR4GclqKlQxdYkJA1XOZoKRLLjrKiUU7ovpTUFdxy6v0nT/aCVri6HaoCI7bRDg+2oK dlccR/N8xUx5uUskAjp7+kfObJNlIQSn3X6XuGU69+Z15kGJl84OwLmwOGw9yRx2GaUn wlG+CXIfKQv5bOaT8Da4VSJqzfoefQB1o4l9RsZTs+0C5inYakip60GVD/gChsYx9W3j 13Rswiv6ewPemwGb8ZQGonNyDFuu/lNiy6zZmFXRPxxtGsbgHUW5L9U9/hOUT+XQlORk 5J7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724310840; x=1724915640; 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=d/m1IXPfk7xvDU+Qn41r4NiqBFnWc/gEWMU/opSilGI=; b=PDAKlQwR/GObz7DmoKm5CX2K4JT1soBMqjJy985VOHk3dIQbqTAv7MF4WcyOoU5V4D MfFJwp1SHbiqvDPtm//UrokK8laMXSBYZ0TVUmgwo3vjAOEpGjixJztBcq5NGiwVzAGL QwaeK4KYBVgIwOXDiTbRVTOoW2jVIwiV57tlY+AqjGEOXVWG+kaCs5Dr2EwETXzBHI7s 2q6bTqoP3tOEEm/wmjaIdClD75/BrF7yhlZQ88akGf4NCw+B646Nne+VRAqDf1KhaM7t JzqbinpErheuIwh2AfyQmHhJLd032pMquP12FOgd+ZFaJUY3ccPF65lGuMLtK8uZh/jA H0RA== X-Forwarded-Encrypted: i=1; AJvYcCUV4Cyv6IEs7Se0qb7fyxr4JXAswG/fH/z+b8Y4q/qJ0a9yep15QDUAsbHvi40tccG5Ifp1bNbHkXf96PdJdZBd@lists.infradead.org X-Gm-Message-State: AOJu0YwyN66ccE0JQkVqwTWXBq4YYtRgR7S2FKM2OsXjT/YUotqnBrH3 NYwRkCcF4d5ysRwWVzsw6V95rbycPWJfqkEoFTWFs1sPqLZ5BXs3wbEGRdOkZ44= X-Google-Smtp-Source: AGHT+IEhsa9RMy8FC6eeC7Dc/qzW9nWoiqPGY6DtNfgP/dDTOo2T0oKgHsMPC17UGp2n78kSJG5uNg== X-Received: by 2002:a05:6a20:6a2a:b0:1c4:9c08:5df3 with SMTP id adf61e73a8af0-1cae51cf01cmr3205085637.7.1724310840022; Thu, 22 Aug 2024 00:14:00 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([240e:473:c90:f96:d029:ea8a:4e6d:d272]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac994a3sm695095a12.16.2024.08.22.00.13.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 00:13:59 -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, christophe.leroy2@cs-soprasteria.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 v2 02/14] arm: adjust_pte() use pte_offset_map_rw_nolock() Date: Thu, 22 Aug 2024 15:13:17 +0800 Message-Id: <7915acf5887e7bf0c5cc71ff30ad2fe8447d005d.1724310149.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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_001401_275444_2F84CAB4 X-CRM114-Status: GOOD ( 12.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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_rw_nolock() to replace pte_offset_map_nolock(), and do pmd_same() check after holding the PTL. Signed-off-by: Qi Zheng Acked-by: David Hildenbrand Reviewed-by: Muchun Song --- 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..de6c7d8a2ddfc 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_rw_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);