Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp48825imu; Thu, 8 Nov 2018 14:33:17 -0800 (PST) X-Google-Smtp-Source: AJdET5fGdWfYRQoEVKMvygqt/PnqjxEl4KnZsx46dAYAavlsoIo5l5GKBE7srO0lkwXNylKj7GjV X-Received: by 2002:a63:cf56:: with SMTP id b22mr5046037pgj.336.1541716397315; Thu, 08 Nov 2018 14:33:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541716397; cv=none; d=google.com; s=arc-20160816; b=SKvcz/lEXOA3rtVbZ0rpBYb+gpONc54+xROgc1xxyTeZKFZSl4He+vJ5yPZaHLZFI1 u6HUvu0dhjK33J11lZSc1LlWNr4lhrJ4MfwT0BnNdhOZE23bd0PxSIdPsFynwPZ3Xzxd QxYOaqKr6kOES47Aoy5UGawKYrZ9UYL9P0kRHbuESB/yBI+ZEJOz7vPnkncDRzrPd6EG PfyiFk229eu3MY2RHEUga64xEc9i/suRcDMx8meUDoFWFTyiBCvm83AEnJlsl9j/QBE1 UFNwkLF1V0AIXZMVaTfCZNYRkbSrja8DGamkE5u3tfZTzAcNMwDwSF0IkStYlTAMM8Kf KxAA== 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=jEMNuNxur2bgZNJYPMJP4rFeahvZKUXD+4OLrvlCK/U=; b=GWe+7Y7po5BUunDhOuFo+nllWmrmLBGN1Oyv4AAhzy9W2JVRx5UtLSmE4za9m/c9bx SfCBJkvcbmBKZdkQ2G8Zbj72O7X+H6hVFtALshRVsrIb9Uxaufnz8ewwGydj1uw2AC6b 7nxc7fjQwb/fKU4GVQ5zAVNON8bIiz5Cbv4sG2dZ+x5YS+yyMJaKttOcxc5Lm3YiL7j1 cu+BqwHYfe8672nV93f17KjATT0BFE+gZN0UbYhV5EgjqrJMyPgeR+114EG/OXHShW/w lMVzPIhiCuJpm2f+Yw+MB4Ar+iz6HcfKzMzgUSn3Vw+bEi00MbyPmKt/HsM0gcwXu4Df vj4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wOZavfY5; 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 c2-v6si4280754pgm.467.2018.11.08.14.33.01; Thu, 08 Nov 2018 14:33:17 -0800 (PST) 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=wOZavfY5; 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 S1730497AbeKIHhz (ORCPT + 99 others); Fri, 9 Nov 2018 02:37:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:56650 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728103AbeKIHhy (ORCPT ); Fri, 9 Nov 2018 02:37:54 -0500 Received: from localhost (unknown [208.72.13.198]) (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 7730620892; Thu, 8 Nov 2018 22:00:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541714425; bh=aKoEw8oP+Jgel6C28qhr9GK8pSof0pSC8i0lpUSwko4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wOZavfY5JNKa9affXm2zevc+meE4uzD7KnQb5dzXc8V8h/3EJMdjPdUpWIIwEGSWZ eS/hVZIojUNd+x1RlxgLkpvrJ1Qk/jLPj8fxww9cIvdasbuWYlTFCaDqE6mIuzMDf/ L8nIlirl6gNu1JpfaZ9Tc1T5Ivxe4wiMBtiAGkPw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Will Deacon , Mark Rutland , Steve Capper , Catalin Marinas , Sasha Levin Subject: [PATCH 4.4 045/114] arm64: Fix potential race with hardware DBM in ptep_set_access_flags() Date: Thu, 8 Nov 2018 13:51:00 -0800 Message-Id: <20181108215105.255949409@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181108215059.051093652@linuxfoundation.org> References: <20181108215059.051093652@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.4-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 6d332747fa5f0a6843b56b5b129168ba909336d1 ] In a system with DBM (dirty bit management) capable agents there is a possible race between a CPU executing ptep_set_access_flags() (maybe non-DBM capable) and a hardware update of the dirty state (clearing of PTE_RDONLY). The scenario: a) the pte is writable (PTE_WRITE set), clean (PTE_RDONLY set) and old (PTE_AF clear) b) ptep_set_access_flags() is called as a result of a read access and it needs to set the pte to writable, clean and young (PTE_AF set) c) a DBM-capable agent, as a result of a different write access, is marking the entry as young (setting PTE_AF) and dirty (clearing PTE_RDONLY) The current ptep_set_access_flags() implementation would set the PTE_RDONLY bit in the resulting value overriding the DBM update and losing the dirty state. This patch fixes such race by setting PTE_RDONLY to the most permissive (lowest value) of the current entry and the new one. Fixes: 66dbd6e61a52 ("arm64: Implement ptep_set_access_flags() for hardware AF/DBM") Cc: Will Deacon Acked-by: Mark Rutland Acked-by: Steve Capper Signed-off-by: Catalin Marinas Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/mm/fault.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 86485415c5f0..be7f8416809f 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -107,26 +107,27 @@ int ptep_set_access_flags(struct vm_area_struct *vma, /* only preserve the access flags and write permission */ pte_val(entry) &= PTE_AF | PTE_WRITE | PTE_DIRTY; - /* - * PTE_RDONLY is cleared by default in the asm below, so set it in - * back if necessary (read-only or clean PTE). - */ + /* set PTE_RDONLY if actual read-only or clean PTE */ if (!pte_write(entry) || !pte_sw_dirty(entry)) pte_val(entry) |= PTE_RDONLY; /* * Setting the flags must be done atomically to avoid racing with the - * hardware update of the access/dirty state. + * hardware update of the access/dirty state. The PTE_RDONLY bit must + * be set to the most permissive (lowest value) of *ptep and entry + * (calculated as: a & b == ~(~a | ~b)). */ + pte_val(entry) ^= PTE_RDONLY; asm volatile("// ptep_set_access_flags\n" " prfm pstl1strm, %2\n" "1: ldxr %0, %2\n" - " and %0, %0, %3 // clear PTE_RDONLY\n" + " eor %0, %0, %3 // negate PTE_RDONLY in *ptep\n" " orr %0, %0, %4 // set flags\n" + " eor %0, %0, %3 // negate final PTE_RDONLY\n" " stxr %w1, %0, %2\n" " cbnz %w1, 1b\n" : "=&r" (old_pteval), "=&r" (tmp), "+Q" (pte_val(*ptep)) - : "L" (~PTE_RDONLY), "r" (pte_val(entry))); + : "L" (PTE_RDONLY), "r" (pte_val(entry))); flush_tlb_fix_spurious_fault(vma, address); return 1; -- 2.17.1