From patchwork Mon Mar 12 15:34:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 131382 Delivered-To: patch@linaro.org Received: by 10.46.84.17 with SMTP id i17csp890296ljb; Mon, 12 Mar 2018 08:37:57 -0700 (PDT) X-Google-Smtp-Source: AG47ELsF1aTkRN77o1+R5ZzOYR9d4ytISvT/XbPV0mPCGCn8WykQW6nTHBVMLx1oGqCkb3J8LaHn X-Received: by 10.36.167.12 with SMTP id a12mr8772439itf.56.1520869077635; Mon, 12 Mar 2018 08:37:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520869077; cv=none; d=google.com; s=arc-20160816; b=fYeNDt5/fQQOx+5sP/Vc6U1zOedJbTU/illamd0+o8XLOkFgVrnLG32nMNpxrlYB5N BkFyzfm3Pw9n7rwKfNoQtwjiXi+tkhqOWQgwulHqAy0eVZXf7h1N/N+Xq+qn7E/yxPzv c4BJUhnR7C3wqJ8dh77hLce1ylYqZ4jo9HX/wFESAJ2Jh7qOxjU0c3/+U2FEVxwqiDnw lPGcVhdckk5mAJsAnZeWqERfDtwVJfMDSt4qc4Ur8FnTudiDDpaiysJazx1xtb9LAUtP eAWKR5/g8OxVboy5Ojgh25HqJ4rZstsUKjI0BDxSmd9+UNz1ga6nhAyfrlcbXndkmexh OgaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-unsubscribe:list-id :precedence:subject:cc:message-id:date:to:from :arc-authentication-results; bh=rpwWwgfZASxY4NcZByNwsMZDJYFfPtTdlhwOMA5XUbU=; b=KioQC/3jFBjRqtvqDTdE1Jva0X/JxeUT1Ier5BUKPjoM6Ht740CuKmjyLIqON+SaWS +b65zz25rUXN5KXqrZApfW5Er/Oqn7s9nRWIXC1vMORKTwyxKHqvBTRWv38hBYaDfsHz NxGdCx0bLlfc1xPyfIEx9UyP39oByK7FvxO8HGuc7hQbhYjurv5eDKusxYNWiAUt7Fd0 FLDGPY2fkWgz1ErEtbONz08vNEF5fM3Wd4WeX0TbhvVS6iZ8uIMQXmdchtBAHepFRIlF r2/eLe/TMxhvc6AiSDLOsCvqzAiwVABclQWR3GdhrxQaRioqi7WOUS7nvSP1MOioTnYe ZhAw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org. [192.237.175.120]) by mx.google.com with ESMTPS id t80si4164633itb.18.2018.03.12.08.37.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Mar 2018 08:37:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1evPTe-0000pW-UD; Mon, 12 Mar 2018 15:35:06 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1evPTd-0000pQ-Kw for xen-devel@lists.xenproject.org; Mon, 12 Mar 2018 15:35:05 +0000 X-Inumbo-ID: eeb98858-260a-11e8-9728-bc764e045a96 Received: from foss.arm.com (unknown [217.140.101.70]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTP id eeb98858-260a-11e8-9728-bc764e045a96; Mon, 12 Mar 2018 16:35:03 +0100 (CET) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4CE3E1596; Mon, 12 Mar 2018 08:35:03 -0700 (PDT) Received: from e108454-lin.cambridge.arm.com (e108454-lin.cambridge.arm.com [10.1.206.53]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id EA3763F24A; Mon, 12 Mar 2018 08:35:01 -0700 (PDT) From: julien.grall@arm.com To: xen-devel@lists.xenproject.org Date: Mon, 12 Mar 2018 15:34:52 +0000 Message-Id: <20180312153452.24314-1-julien.grall@arm.com> X-Mailer: git-send-email 2.11.0 Cc: sstabellini@kernel.org, rcojocaru@bitdefender.com, andre.przywara@arm.com, Julien Grall , tamas@tklengyel.com, proskurin@sec.in.tum.de Subject: [Xen-devel] [PATCH v2] xen/arm: p2m: Prevent deadlock when using memaccess X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Julien Grall Commit 7d623b358a4 "arm/mem_access: Add long-descriptor based gpt" assumed the read-write lock can be taken recursively. However, this assumption is wrong and will lead to deadlock when the lock is contended. The read lock is taken recursively in the following case: 1) get_page_from_gva => Take the read lock (first read lock) => Call p2m_mem_access_check_and_get_page on failure when memaccess is enabled 2) p2m_mem_access_check_and_get_page => If hardware translation failed fallback to software lookup => Call guest_walk_tables 3) guest_walk_tables => Will use access_guest_memory_ipa to access stage-1 page-table 4) access_guest_memory_ipa => Because Arm does not have hardware instruction to only do stage-2 page-table, this is done in software. => Take the read lock (second read lock) To avoid the nested lock, rework the locking in get_page_from_gva and p2m_mem_access_check_and_get_page. The latter will now be called without the p2m lock. The new locking in p2m_mem_accces_check_and_get_page will not cover the translation of the VA to an IPA. This is fine because we can't promise that the stage-1 page-table have changed behind our back (they are under guest control). Modification in the stage-2 page-table can now happen, but I can't issue any potential issue here except with the break-before-make sequence used when updating page-table. gva_to_ipa may fail if the sequence is executed at the same on another CPU. In that case we would fallback in the software lookup path. Signed-off-by: Julien Grall Reviewed-by: Sergej Proskurin Reviewed-by: Stefano Stabellini --- This patch should be backported to Xen 4.10. There are other potential optimization that I am working on. Although, I don't think they are backport material. Changes in v2: - Update the commit message to explain where the lock is taken recursively. --- xen/arch/arm/mem_access.c | 8 ++++++-- xen/arch/arm/p2m.c | 4 ++-- xen/include/asm-arm/p2m.h | 4 ---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/xen/arch/arm/mem_access.c b/xen/arch/arm/mem_access.c index 0f2cbb81d3..11c2b03b7b 100644 --- a/xen/arch/arm/mem_access.c +++ b/xen/arch/arm/mem_access.c @@ -126,7 +126,7 @@ p2m_mem_access_check_and_get_page(vaddr_t gva, unsigned long flag, * is not mapped. */ if ( guest_walk_tables(v, gva, &ipa, &perms) < 0 ) - goto err; + return NULL; /* * Check permissions that are assumed by the caller. For instance in @@ -139,11 +139,13 @@ p2m_mem_access_check_and_get_page(vaddr_t gva, unsigned long flag, * test for execute permissions this check can be left out. */ if ( (flag & GV2M_WRITE) && !(perms & GV2M_WRITE) ) - goto err; + return NULL; } gfn = gaddr_to_gfn(ipa); + p2m_read_lock(p2m); + /* * We do this first as this is faster in the default case when no * permission is set on the page. @@ -216,6 +218,8 @@ p2m_mem_access_check_and_get_page(vaddr_t gva, unsigned long flag, page = NULL; err: + p2m_read_unlock(p2m); + return page; } diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c index 65e8b9c6ea..5de82aafe1 100644 --- a/xen/arch/arm/p2m.c +++ b/xen/arch/arm/p2m.c @@ -1449,11 +1449,11 @@ struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va, } err: + p2m_read_unlock(p2m); + if ( !page && p2m->mem_access_enabled ) page = p2m_mem_access_check_and_get_page(va, flags, v); - p2m_read_unlock(p2m); - return page; } diff --git a/xen/include/asm-arm/p2m.h b/xen/include/asm-arm/p2m.h index a0abc84ed8..45ef2cd58b 100644 --- a/xen/include/asm-arm/p2m.h +++ b/xen/include/asm-arm/p2m.h @@ -23,10 +23,6 @@ extern void memory_type_changed(struct domain *); struct p2m_domain { /* * Lock that protects updates to the p2m. - * - * Please note that we use this lock in a nested way by calling - * access_guest_memory_by_ipa in guest_walk_(sd|ld). This must be - * considered in the future implementation. */ rwlock_t lock;