Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp619972pxb; Tue, 1 Feb 2022 07:08:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJzbGAFaZq5XM6SMZVPyulDjFcaRRUj5gCW8uFJ1NdVqkglZ61843cyzAaCEvh0PAQvGR0wj X-Received: by 2002:a05:6402:2747:: with SMTP id z7mr25997298edd.227.1643728100797; Tue, 01 Feb 2022 07:08:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728100; cv=none; d=google.com; s=arc-20160816; b=OchhMXsa8LcQ86LfeTZlkgRUwvvuJoSlzAy38jH6Jj2dOQuju7hPH5dBNOpghc9Ct1 NXy5paALgYCG2NiMH627el5qf4anbVuOaiVj5qWd5Pg3t1Kh4dhCstUtfz4RVRppFnC3 HQh/Ntt1/fNOBd++dytO6qLVJ6bg4i3rm46lLOmNDeClyk2cmSfxQo3yCpIZ30Yw1nSJ rud0cWn708vyUzYEKhRJWxUhPG1hZlBKszQtjOtBQ/ZOdMpbVSjQWPd0YvQymqt1cSy5 eRnv9j+1BbgxQZ+wrT7TQgooyJIMDrC7swiobLo+qIiFIppMaof7+cenJayDBxMRoTPk 8MeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=g8u30FRNeQ4jnmmJBdqc/yOci/eo+W/FxTeeiITma9I=; b=efz+eFSsRZwNe8DnQB5oeeYSJHmG5C5W0h2zZJstRVCuCaVto4i3mqoMUifuGa1YaY 3CPBM5gQpAFlqpcDFrX3XRQSC5Je2/QxE9kyHUEotp11HFDB7W8Qufkm/6Nlh3+L8nj6 Nd2MQrlWSlvOUA2HomKwNPSVAUWiwRbXVFtXwQGigKUgZQtZ9LgK7aQwtvtwh4+o2dc9 oDXExaudI7O+8Ldxb6YDHMXrdAsTzhfd2R8rdy32Je63/9Ni4V5ohwG3HiAYT0H3k6Ts dE4150Rhb9jqrejqwl++1q3R07Pq3wvVzAOxIAKVDJV+23pKBTTWcsd0yKTaO3sJZDbT e1QQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GWQXs0in; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dr7si10908865ejc.798.2022.02.01.07.07.54; Tue, 01 Feb 2022 07:08:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GWQXs0in; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356907AbiA3VYC (ORCPT + 99 others); Sun, 30 Jan 2022 16:24:02 -0500 Received: from mga06.intel.com ([134.134.136.31]:52047 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356520AbiA3VWA (ORCPT ); Sun, 30 Jan 2022 16:22:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577720; x=1675113720; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=GcK8U902or9ILWaRmLm8X+dvVTHxi1AwyGT1VYKPQZ8=; b=GWQXs0inwx2P+HfxD6eYx1AK+PgaKj6mqxbGgxe8YNFpG9DpCjKshh/t GH0VmqyhEtAhd5Ut6CxkdUMzQL/IyzjiDNpoQ9GMJnyzIaYVr9oFv3IR/ YOWcEf/7A/vhwF28GP7/GOVAywfnI1TA5naQotnZkxXO1Fl4fN7nXCyXD xjf0+4j877nxxSX4zji0WasWM8XVNcMJyVnxYFcIIHnqCcEwlJtrdlf07 HH5WY3+uR9Fhsh1N7hhzQPRZjdNT07vBbfrAORFJ6fucAY5GakyT+jVJ4 Iz31mlyO6eHhuvqMfNCQAIvSMgoq40MsjmfBxCnD9u2czH50SQ0UyBw8Z w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104939" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104939" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:59 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856805" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:58 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH 17/35] mm: Fixup places that call pte_mkwrite() directly Date: Sun, 30 Jan 2022 13:18:20 -0800 Message-Id: <20220130211838.8382-18-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu When serving a page fault, maybe_mkwrite() makes a PTE writable if it is in a writable vma. A shadow stack vma is writable, but its PTEs need _PAGE_DIRTY to be set to become writable. For this reason, maybe_mkwrite() has been updated. There are a few places that call pte_mkwrite() directly, but have the same result as from maybe_mkwrite(). These sites need to be updated for shadow stack as well. Thus, change them to maybe_mkwrite(): - do_anonymous_page() and migrate_vma_insert_page() check VM_WRITE directly and call pte_mkwrite(), which is the same as maybe_mkwrite(). Change them to maybe_mkwrite(). - In do_numa_page(), if the numa entry was writable, then pte_mkwrite() is called directly. Fix it by doing maybe_mkwrite(). Make the same changes to do_huge_pmd_numa_page(). - In change_pte_range(), pte_mkwrite() is called directly. Replace it with maybe_mkwrite(). Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Cc: Kees Cook --- Yu-cheng v25: - Apply same changes to do_huge_pmd_numa_page() as to do_numa_page(). mm/huge_memory.c | 2 +- mm/memory.c | 5 ++--- mm/migrate.c | 3 +-- mm/mprotect.c | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2adedcfca00b..3588e9fefbe0 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1489,7 +1489,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) pmd = pmd_modify(oldpmd, vma->vm_page_prot); pmd = pmd_mkyoung(pmd); if (was_writable) - pmd = pmd_mkwrite(pmd); + pmd = maybe_pmd_mkwrite(pmd, vma); set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd); update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); spin_unlock(vmf->ptl); diff --git a/mm/memory.c b/mm/memory.c index c125c4969913..c79444603d5d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3793,8 +3793,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) entry = mk_pte(page, vma->vm_page_prot); entry = pte_sw_mkyoung(entry); - if (vma->vm_flags & VM_WRITE) - entry = pte_mkwrite(pte_mkdirty(entry)); + entry = maybe_mkwrite(pte_mkdirty(entry), vma); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); @@ -4428,7 +4427,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) pte = pte_modify(old_pte, vma->vm_page_prot); pte = pte_mkyoung(pte); if (was_writable) - pte = pte_mkwrite(pte); + pte = maybe_mkwrite(pte, vma); ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); diff --git a/mm/migrate.c b/mm/migrate.c index c7da064b4781..438f1e21b9c7 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2697,8 +2697,7 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate, } } else { entry = mk_pte(page, vma->vm_page_prot); - if (vma->vm_flags & VM_WRITE) - entry = pte_mkwrite(pte_mkdirty(entry)); + entry = maybe_mkwrite(pte_mkdirty(entry), vma); } ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); diff --git a/mm/mprotect.c b/mm/mprotect.c index 0138dfcdb1d8..b0012c13a00e 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -135,7 +135,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, if (dirty_accountable && pte_dirty(ptent) && (pte_soft_dirty(ptent) || !(vma->vm_flags & VM_SOFTDIRTY))) { - ptent = pte_mkwrite(ptent); + ptent = maybe_mkwrite(ptent, vma); } ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent); pages++; -- 2.17.1