Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2225220imm; Thu, 18 Oct 2018 11:00:15 -0700 (PDT) X-Google-Smtp-Source: ACcGV61f3EL+Y65QFsDURjBtrMC9vaeiZvE/9YbeuUtVKlPrKYcXgXWQ4Yc0/Dy5PEbiyda0rjpb X-Received: by 2002:a63:c5a:: with SMTP id 26-v6mr29388606pgm.372.1539885615333; Thu, 18 Oct 2018 11:00:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539885615; cv=none; d=google.com; s=arc-20160816; b=gmyjiy7KtgkQIbWe3bxGZCjh+Y5m3oR1OGhGU0hKYWZjSCreoeZvYFuLxvLGZuoHpo ThNJri9mrRJjM5IQytv9L0+9hmTLQNAxbL0AXOlXnHnQaFfX4vEar1vepP2LjPWmcnJI 86ApHFHSWl5bTHm3L8FTsmJf8ZWl9pmM1k+poGlpBmqeNBNIbeTT9nbTCxTQiQTYO6G6 f5FYnTaC6Cb1wL2K/A0Pk3nUmRaCWWWSMBCmmAejklWpUaIdvUJgZJUYjgvgUCOzh3Dz NrH/7A+xerGlW+I89utgrJz9GgedhKRPOmo9el/89ICxrV8Noo7xrfUfm1KZniPCZsMq 1yhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=oIALJgH6JfMHqq3UVucYqpqVT2KNa9EiPU09uWh9CUM=; b=EQFTjkisIiq36HWgAYNcS2GPruYCN4ScnmSknLyNOhc3D94sChFixATAYPGFdrjc7i m40PfnJCnECYQZTpBT5+oyCZrjVgXRwsEqOdH6+EVu9QCcOhMSyVXfDAdEvDMEqfmccy Xj4EWAv7m/PhgGtB9Pl18WnpAJS6KxCsgPOjYPH5w8eW3X1siMlLCiX9xY4075Ihx4PL ljPBUFvZb89IyAydV6XDTvUuqYHi7w/Gs4bAOdoKKJI0nhQEqSaiJ17MAVM/y8tV6IuO 2YKf/T92ZUxIb6CeIqmngYXCwj7NRzOBx19Za7B/4XOW6o/EGDs+QdMgOpsqxkmYQkWS MGGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IaIOCLER; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f88-v6si21124026pfh.33.2018.10.18.10.59.59; Thu, 18 Oct 2018 11:00:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IaIOCLER; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729115AbeJSCAx (ORCPT + 99 others); Thu, 18 Oct 2018 22:00:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:48510 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726534AbeJSCAw (ORCPT ); Thu, 18 Oct 2018 22:00:52 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9817E21473; Thu, 18 Oct 2018 17:58:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539885526; bh=ZwtyvTv6iSXJnm1KUwe/2W+di8zo0W+4VWLzkL8W1ek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IaIOCLERx6ENfvT9hHKQMjU5zae35QrT0tTsyOSU6zM1txw5+eCmHdM+BIwWJuI9L AF1lHwINJoi3y1aYpzyVSx9QtKPeKLd4WOdKPGCw70OcnnR1ZYb7YD5xRLYYKJDMcL lxB1sdKIvG30Aix9vLDsC3eBGvf8lzV7hkk2FWI8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Will Deacon , Ingo Molnar , "Peter Zijlstra (Intel)" , Linus Torvalds Subject: [PATCH 4.18 51/53] mremap: properly flush TLB before releasing the page Date: Thu, 18 Oct 2018 19:54:44 +0200 Message-Id: <20181018175425.272206123@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181018175416.561567978@linuxfoundation.org> References: <20181018175416.561567978@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Linus Torvalds commit eb66ae030829605d61fbef1909ce310e29f78821 upstream. Jann Horn points out that our TLB flushing was subtly wrong for the mremap() case. What makes mremap() special is that we don't follow the usual "add page to list of pages to be freed, then flush tlb, and then free pages". No, mremap() obviously just _moves_ the page from one page table location to another. That matters, because mremap() thus doesn't directly control the lifetime of the moved page with a freelist: instead, the lifetime of the page is controlled by the page table locking, that serializes access to the entry. As a result, we need to flush the TLB not just before releasing the lock for the source location (to avoid any concurrent accesses to the entry), but also before we release the destination page table lock (to avoid the TLB being flushed after somebody else has already done something to that page). This also makes the whole "need_flush" logic unnecessary, since we now always end up flushing the TLB for every valid entry. Reported-and-tested-by: Jann Horn Acked-by: Will Deacon Tested-by: Ingo Molnar Acked-by: Peter Zijlstra (Intel) Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/huge_mm.h | 2 +- mm/huge_memory.c | 10 ++++------ mm/mremap.c | 30 +++++++++++++----------------- 3 files changed, 18 insertions(+), 24 deletions(-) --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -42,7 +42,7 @@ extern int mincore_huge_pmd(struct vm_ar unsigned char *vec); extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, unsigned long new_addr, unsigned long old_end, - pmd_t *old_pmd, pmd_t *new_pmd, bool *need_flush); + pmd_t *old_pmd, pmd_t *new_pmd); extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, pgprot_t newprot, int prot_numa); --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1778,7 +1778,7 @@ static pmd_t move_soft_dirty_pmd(pmd_t p bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, unsigned long new_addr, unsigned long old_end, - pmd_t *old_pmd, pmd_t *new_pmd, bool *need_flush) + pmd_t *old_pmd, pmd_t *new_pmd) { spinlock_t *old_ptl, *new_ptl; pmd_t pmd; @@ -1809,7 +1809,7 @@ bool move_huge_pmd(struct vm_area_struct if (new_ptl != old_ptl) spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd); - if (pmd_present(pmd) && pmd_dirty(pmd)) + if (pmd_present(pmd)) force_flush = true; VM_BUG_ON(!pmd_none(*new_pmd)); @@ -1820,12 +1820,10 @@ bool move_huge_pmd(struct vm_area_struct } pmd = move_soft_dirty_pmd(pmd); set_pmd_at(mm, new_addr, new_pmd, pmd); - if (new_ptl != old_ptl) - spin_unlock(new_ptl); if (force_flush) flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE); - else - *need_flush = true; + if (new_ptl != old_ptl) + spin_unlock(new_ptl); spin_unlock(old_ptl); return true; } --- a/mm/mremap.c +++ b/mm/mremap.c @@ -115,7 +115,7 @@ static pte_t move_soft_dirty_pte(pte_t p static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, unsigned long old_addr, unsigned long old_end, struct vm_area_struct *new_vma, pmd_t *new_pmd, - unsigned long new_addr, bool need_rmap_locks, bool *need_flush) + unsigned long new_addr, bool need_rmap_locks) { struct mm_struct *mm = vma->vm_mm; pte_t *old_pte, *new_pte, pte; @@ -163,15 +163,17 @@ static void move_ptes(struct vm_area_str pte = ptep_get_and_clear(mm, old_addr, old_pte); /* - * If we are remapping a dirty PTE, make sure + * If we are remapping a valid PTE, make sure * to flush TLB before we drop the PTL for the - * old PTE or we may race with page_mkclean(). + * PTE. * - * This check has to be done after we removed the - * old PTE from page tables or another thread may - * dirty it after the check and before the removal. + * NOTE! Both old and new PTL matter: the old one + * for racing with page_mkclean(), the new one to + * make sure the physical page stays valid until + * the TLB entry for the old mapping has been + * flushed. */ - if (pte_present(pte) && pte_dirty(pte)) + if (pte_present(pte)) force_flush = true; pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr); pte = move_soft_dirty_pte(pte); @@ -179,13 +181,11 @@ static void move_ptes(struct vm_area_str } arch_leave_lazy_mmu_mode(); + if (force_flush) + flush_tlb_range(vma, old_end - len, old_end); if (new_ptl != old_ptl) spin_unlock(new_ptl); pte_unmap(new_pte - 1); - if (force_flush) - flush_tlb_range(vma, old_end - len, old_end); - else - *need_flush = true; pte_unmap_unlock(old_pte - 1, old_ptl); if (need_rmap_locks) drop_rmap_locks(vma); @@ -198,7 +198,6 @@ unsigned long move_page_tables(struct vm { unsigned long extent, next, old_end; pmd_t *old_pmd, *new_pmd; - bool need_flush = false; unsigned long mmun_start; /* For mmu_notifiers */ unsigned long mmun_end; /* For mmu_notifiers */ @@ -229,8 +228,7 @@ unsigned long move_page_tables(struct vm if (need_rmap_locks) take_rmap_locks(vma); moved = move_huge_pmd(vma, old_addr, new_addr, - old_end, old_pmd, new_pmd, - &need_flush); + old_end, old_pmd, new_pmd); if (need_rmap_locks) drop_rmap_locks(vma); if (moved) @@ -246,10 +244,8 @@ unsigned long move_page_tables(struct vm if (extent > next - new_addr) extent = next - new_addr; move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma, - new_pmd, new_addr, need_rmap_locks, &need_flush); + new_pmd, new_addr, need_rmap_locks); } - if (need_flush) - flush_tlb_range(vma, old_end-len, old_addr); mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);