Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp674069pxb; Wed, 3 Feb 2021 15:02:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJzwJfDlWJFVgTfdJosUGB9Cqg8WhpHw0EqWmiDsHZujBTkSWNDUQKilK2kXJexbkVX9K83i X-Received: by 2002:a05:6402:149:: with SMTP id s9mr5441595edu.247.1612393338675; Wed, 03 Feb 2021 15:02:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612393338; cv=none; d=google.com; s=arc-20160816; b=rVZHIs+7eLzi5Y1pZF5vit1qpieKSJookN4rwumHDv52miWD3ahoLohW0C9VzsRsDT S0HOVDAGcvvIZcAzS/28qu94Mcrg9XSyX38Mk5zCh7PV4/InAfEEoEJQ2IHRICIpFYnk nzmoDilY/pEwooINv8UOW8gAGPKcDlKrZhdBZT31LY3lMygtwEKuQYhDQEU7A3YYl/IQ xeFyyI3Q/MkNHhVHpJegX43gmC87+v9OS8ElnR6XbhRQPJp4pE651TISq5ttfKGJWDKo jQw6xmR5UcDS9c0pXHQsuXPZInQ35e7CwRHrxLlzsoR19vM0u2S8RxKH7UQLwVvJcCK1 jqyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=sv0hZWVDS2li3rTl9gU7Ri7taxYw6xwx9hD6/8lrJps=; b=bZLH7P5HHWA82DwdxDhcqVbB6lMph+ENo2EL07q7fXBiY3QhFxCSCVzba+4suwNn/6 GRW5YxeEkcIr+aFz+hgVOqDQHuxGqrlNh7fZ+JlgT2whuFi0B95C7v519Kmf9OtfFYdu UId2lSfXT1i5ePhD47CBrxG+zQD9dJbfrBXI9TxxjfrTsZir9lfcWLOwEQ4ZXQChks96 VttWyvbuqgyicwoM+YTYzxjAJMpR4uHdVO/j86xOVPGoy4Icu/ecWS263dt0ROjxoNqO BZPRecWOJyfoQ2EnfV51j4CWUd8gaA8tcj2xQTOMi/6gRFqp9NkjIY/jU9wlmrD9vVJV ZfCQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 t1si2318788ejd.359.2021.02.03.15.01.53; Wed, 03 Feb 2021 15:02:18 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233400AbhBCXAa (ORCPT + 99 others); Wed, 3 Feb 2021 18:00:30 -0500 Received: from mga06.intel.com ([134.134.136.31]:30209 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233278AbhBCW6X (ORCPT ); Wed, 3 Feb 2021 17:58:23 -0500 IronPort-SDR: S1Eb9i95ZQHzjp9ltMM478aa4Izc0u/pXn53lhEWr+OT4lRP4QPsu+PnzXnzXrcynoPMnUunDU 2+G4U54M1bOg== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642364" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642364" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:28 -0800 IronPort-SDR: MG0TPj0ZcGBLtI9T9agKV0cyhUgx6EvkJccxzrdriFMk34TZS16NQ0XTyhY8s0UfhUaBALRUXy jWIt+XqaY5TQ== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921126" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:28 -0800 From: Yu-cheng Yu 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" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu Cc: Yu-cheng Yu Subject: [PATCH v19 15/25] mm: Fixup places that call pte_mkwrite() directly Date: Wed, 3 Feb 2021 14:55:37 -0800 Message-Id: <20210203225547.32221-16-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210203225547.32221-1-yu-cheng.yu@intel.com> References: <20210203225547.32221-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 effect 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(). - In change_pte_range(), pte_mkwrite() is called directly. Replace it with maybe_mkwrite(). A shadow stack vma is writable but has different vma flags, and handled accordingly in maybe_mkwrite(). Signed-off-by: Yu-cheng Yu --- mm/memory.c | 5 ++--- mm/migrate.c | 3 +-- mm/mprotect.c | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index feff48e1465a..1de649c61013 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3549,8 +3549,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); @@ -4205,7 +4204,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); diff --git a/mm/migrate.c b/mm/migrate.c index c0efe921bca5..8878d75c0db7 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2964,8 +2964,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 ab709023e9aa..ed26e675c423 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.21.0