From nobody Sat Oct 11 00:39:30 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77C2B29DB7E; Thu, 12 Jun 2025 17:36:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749749811; cv=none; b=kz2DthY7XAbdNfwDw4Oa6tC8LtiAnh6eHuMDhcHceHVKzSEOi2rdMOxjFQuoX48EfrqHYp7HuR3AO/+z7wGs8EX20/AbjN28ncnElzUQMCHkrpW+wlcNMRF+JpAFfuflgVhh50/wi2MLsBvr19DcG5fD9wHWXeR0j8+OS6viPTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749749811; c=relaxed/simple; bh=raje+Qi5vZjGXqBAYcs41/lc0szO3wDpV/wLMhyLClU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KngGXKYLlzQ5w27ft8XFQ2EszayLe7H8fEgWrQkkpR3Cs8PSv9CZ2tAHEPDPlpGhFU3AtSQzW3hm5lvz/sO7jm8bAp6tK4+iSsiHMzH0rLdsBmyQn6MZaSxxBo2k9VaMPa1YBAzQj3ho0Ss4zuleb+s86KBI5QbIHJTAV05qgkc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=jqMrmg9P; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="jqMrmg9P" Received: from pps.filterd (m0360083.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 55CDJP2V021059; Thu, 12 Jun 2025 17:36:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=lzDJpUIXBz/mvS0mb FYxHi8XUevGZ8hUcNGEGhasovg=; b=jqMrmg9P9k4as1yQGOTnccjGWG4WF7DEd kBXZ1I/97XSYVJbQ+zJBG+JgVx32TNUOqAFmET9+GHhIolrdsQYgYJ2idUI/YD4s /xUki5YiOsCOWB0+AVe6rTijtZk1rs6M50nDxRavBOMoS4SGnhy/c7sgJNTotjGi 6sHMEPK/4npj5FfsZvDdx7jicu1aU5DQMLUhDTl2TpSTAMuwTZfKAnXRxT3UkPF3 ZIbIMaHnARDwFnGT4yVd8DLscyKbq+WfXuWGn4G+cz7HIOzlYlL6g2UMRNBBQL+M 8Zy82y8ly2Bvl1bgxesKbgKejydEi6fT68K8HNFyFta7GIJKlnKig== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 474cxjmf0n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 12 Jun 2025 17:36:17 +0000 (GMT) Received: from m0360083.ppops.net (m0360083.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 55CHZKKa027345; Thu, 12 Jun 2025 17:36:16 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 474cxjmf0g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 12 Jun 2025 17:36:16 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 55CF2fhL003405; Thu, 12 Jun 2025 17:36:15 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4751ykwr2m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 12 Jun 2025 17:36:15 +0000 Received: from smtpav05.fra02v.mail.ibm.com (smtpav05.fra02v.mail.ibm.com [10.20.54.104]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 55CHaE2d23134776 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Jun 2025 17:36:14 GMT Received: from smtpav05.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 01F1220040; Thu, 12 Jun 2025 17:36:14 +0000 (GMT) Received: from smtpav05.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DF1282004B; Thu, 12 Jun 2025 17:36:13 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by smtpav05.fra02v.mail.ibm.com (Postfix) with ESMTPS; Thu, 12 Jun 2025 17:36:13 +0000 (GMT) Received: by tuxmaker.boeblingen.de.ibm.com (Postfix, from userid 55669) id A01ECE1994; Thu, 12 Jun 2025 19:36:13 +0200 (CEST) From: Alexander Gordeev To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, sparclinux@vger.kernel.org, xen-devel@lists.xenproject.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, Hugh Dickins , Nicholas Piggin , Guenter Roeck , Juergen Gross , Jeremy Fitzhardinge , Ryan Roberts Subject: [PATCH 2/6] mm: Lock kernel page tables before entering lazy MMU mode Date: Thu, 12 Jun 2025 19:36:09 +0200 Message-ID: <7bd3a45dbc375dc2c15cebae09cb2bb972d6039f.1749747752.git.agordeev@linux.ibm.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: 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 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: Tb8VA6zUpjW5k1WLGI1WWxPCHUixj1UW X-Proofpoint-GUID: EYQIsAXzx8ssoXkn44spur2V7kFJyFNc X-Authority-Analysis: v=2.4 cv=fZWty1QF c=1 sm=1 tr=0 ts=684b1011 cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=6IFa9wvqVegA:10 a=VnNF1IyMAAAA:8 a=nMoVGufP4iL6dWhmM1oA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNjEyMDEzMiBTYWx0ZWRfX7wfxBioE0ali sfc9SNNLuo/uwJWDA3c2xkqqH6rfdv8VXDTQiKWPjoWwN5+YNEn8jTxawLHSc7kSvcbTRzlAEEL 6pE4EG7uVAXP0cTCdUVctrhAA4dPIy9K280bl6EZsHWeiac5dQkU3M23hors8FvuTbzgDf1h9Fp 4/5hS9LAZnskUMvo6VVxjD1ejeCUnSJ9GDLuknHP+02KFBqLWFqJX4Uj1GH9ITY9ehAeYHg8CPo +NXj5bCNfgunV/T0oR4Y7LXv81qmzH4CP/fN90LYdPGDzAKY9KoFLxiARID9WfB4E8JGYFRMR8X KU0u1kTIIScAeHrSUf7iG+tUJ+YeiYm2VyY0+i/fL8MGK6FCaPHEVVMgFI0JFtKLssBxImKqXFF +8XQ7Gu5M/Tz5OkRu7P75cBzlchWZR0cnEVFWg54GBSdDJyxQ+nNXw3MOOvDg1EdtYnL8aNA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-06-12_10,2025-06-12_02,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1011 impostorscore=0 priorityscore=1501 spamscore=0 bulkscore=0 phishscore=0 suspectscore=0 adultscore=0 mlxscore=0 malwarescore=0 lowpriorityscore=0 mlxlogscore=965 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2506120132 Content-Type: text/plain; charset="utf-8" As a follow-up to commit 691ee97e1a9d ("mm: fix lazy mmu docs and usage") take a step forward and protect with a lock not only user, but also kernel mappings before entering the lazy MMU mode. With that the semantics of arch_enter|leave_lazy_mmu_mode() callbacks is consolidated, which allows further simplifications. The effect of this consolidation is not fully preemptible (Real-Time) kernels can not enter the context switch while the lazy MMU mode is active - which is easier to comprehend. Signed-off-by: Alexander Gordeev --- include/linux/pgtable.h | 12 ++++++------ mm/kasan/shadow.c | 5 ----- mm/memory.c | 5 ++++- mm/vmalloc.c | 6 ++++++ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 0b6e1f781d86..33bf2b13c219 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -224,12 +224,12 @@ static inline int pmd_dirty(pmd_t pmd) * a raw PTE pointer after it has been modified are not guaranteed to be * up to date. * - * In the general case, no lock is guaranteed to be held between entry and= exit - * of the lazy mode. So the implementation must assume preemption may be e= nabled - * and cpu migration is possible; it must take steps to be robust against = this. - * (In practice, for user PTE updates, the appropriate page table lock(s) = are - * held, but for kernel PTE updates, no lock is held). Nesting is not perm= itted - * and the mode cannot be used in interrupt context. + * For PREEMPT_RT kernels implementation must assume that preemption may + * be enabled and cpu migration is possible between entry and exit of the + * lazy MMU mode; it must take steps to be robust against this. There is + * no such assumption for non-PREEMPT_RT kernels, since both kernel and + * user page tables are protected with a spinlock while in lazy MMU mode. + * Nesting is not permitted and the mode cannot be used in interrupt conte= xt. */ #ifndef __HAVE_ARCH_ENTER_LAZY_MMU_MODE #define arch_enter_lazy_mmu_mode() do {} while (0) diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index d2c70cd2afb1..45115bd770a9 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -313,12 +313,10 @@ static int kasan_populate_vmalloc_pte(pte_t *ptep, un= signed long addr, __memset(page_to_virt(page), KASAN_VMALLOC_INVALID, PAGE_SIZE); pte =3D pfn_pte(page_to_pfn(page), PAGE_KERNEL); =20 - spin_lock(&init_mm.page_table_lock); if (likely(pte_none(ptep_get(ptep)))) { set_pte_at(&init_mm, addr, ptep, pte); data->pages[index] =3D NULL; } - spin_unlock(&init_mm.page_table_lock); =20 return 0; } @@ -465,13 +463,10 @@ static int kasan_depopulate_vmalloc_pte(pte_t *ptep, = unsigned long addr, =20 page =3D (unsigned long)__va(pte_pfn(ptep_get(ptep)) << PAGE_SHIFT); =20 - spin_lock(&init_mm.page_table_lock); - if (likely(!pte_none(ptep_get(ptep)))) { pte_clear(&init_mm, addr, ptep); free_page(page); } - spin_unlock(&init_mm.page_table_lock); =20 return 0; } diff --git a/mm/memory.c b/mm/memory.c index 71b3d3f98999..1ddc532b1f13 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3017,6 +3017,7 @@ static int apply_to_pte_range(struct mm_struct *mm, p= md_t *pmd, pte =3D pte_offset_kernel(pmd, addr); if (!pte) return err; + spin_lock(&init_mm.page_table_lock); } else { if (create) pte =3D pte_alloc_map_lock(mm, pmd, addr, &ptl); @@ -3042,7 +3043,9 @@ static int apply_to_pte_range(struct mm_struct *mm, p= md_t *pmd, =20 arch_leave_lazy_mmu_mode(); =20 - if (mm !=3D &init_mm) + if (mm =3D=3D &init_mm) + spin_unlock(&init_mm.page_table_lock); + else pte_unmap_unlock(mapped_pte, ptl); =20 *mask |=3D PGTBL_PTE_MODIFIED; diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ab986dd09b6a..57b11000ae36 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -105,6 +105,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long add= r, unsigned long end, if (!pte) return -ENOMEM; =20 + spin_lock(&init_mm.page_table_lock); arch_enter_lazy_mmu_mode(); =20 do { @@ -132,6 +133,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long add= r, unsigned long end, } while (pte +=3D PFN_DOWN(size), addr +=3D size, addr !=3D end); =20 arch_leave_lazy_mmu_mode(); + spin_unlock(&init_mm.page_table_lock); *mask |=3D PGTBL_PTE_MODIFIED; return 0; } @@ -359,6 +361,7 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long = addr, unsigned long end, unsigned long size =3D PAGE_SIZE; =20 pte =3D pte_offset_kernel(pmd, addr); + spin_lock(&init_mm.page_table_lock); arch_enter_lazy_mmu_mode(); =20 do { @@ -379,6 +382,7 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long = addr, unsigned long end, } while (pte +=3D (size >> PAGE_SHIFT), addr +=3D size, addr !=3D end); =20 arch_leave_lazy_mmu_mode(); + spin_unlock(&init_mm.page_table_lock); *mask |=3D PGTBL_PTE_MODIFIED; } =20 @@ -525,6 +529,7 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned lo= ng addr, if (!pte) return -ENOMEM; =20 + spin_lock(&init_mm.page_table_lock); arch_enter_lazy_mmu_mode(); =20 do { @@ -542,6 +547,7 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned lo= ng addr, } while (pte++, addr +=3D PAGE_SIZE, addr !=3D end); =20 arch_leave_lazy_mmu_mode(); + spin_unlock(&init_mm.page_table_lock); *mask |=3D PGTBL_PTE_MODIFIED; return 0; } --=20 2.48.1