From patchwork Thu Sep 26 09:46:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xia, Hongyan" X-Patchwork-Id: 11162203 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5F1351709 for ; Thu, 26 Sep 2019 09:51:51 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3B4D8222C2 for ; Thu, 26 Sep 2019 09:51:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="qF96qfJD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3B4D8222C2 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=none 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.89) (envelope-from ) id 1iDQQQ-0001Zp-1g; Thu, 26 Sep 2019 09:51:02 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDQQL-0001PD-89 for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 09:50:57 +0000 X-Inumbo-ID: 2330c38e-e043-11e9-964c-12813bfff9fa Received: from smtp-fw-2101.amazon.com (unknown [72.21.196.25]) by localhost (Halon) with ESMTPS id 2330c38e-e043-11e9-964c-12813bfff9fa; Thu, 26 Sep 2019 09:50:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1569491456; x=1601027456; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=LDyYzg3DfaKc88izlDhVAvNjYE+nmpb1QGtOx4rbMSo=; b=qF96qfJDRqXBVLsGGGM3wjJkN+crY7W0hSndoOcyn62xPf8PVR49pqYC Wkv/ebd9eKFsSv38T/Ko5BbxlGyR2ODCW+c8p1fURJC1j6nHswWfJnKz0 47vVQsEYehnV/1VRjbQfihsO5nCqxV8GU+vGpUkzqliMWu8+bxPhy/Iyc 8=; X-IronPort-AV: E=Sophos;i="5.64,551,1559520000"; d="scan'208";a="753354562" Received: from iad6-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1e-57e1d233.us-east-1.amazon.com) ([10.124.125.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 26 Sep 2019 09:50:56 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-57e1d233.us-east-1.amazon.com (Postfix) with ESMTPS id 7F35A1417B8; Thu, 26 Sep 2019 09:50:54 +0000 (UTC) Received: from EX13D28EUB002.ant.amazon.com (10.43.166.97) by EX13MTAUEA001.ant.amazon.com (10.43.61.82) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 26 Sep 2019 09:50:17 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D28EUB002.ant.amazon.com (10.43.166.97) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 26 Sep 2019 09:50:16 +0000 Received: from u9d785c4ba99158.ant.amazon.com (10.125.106.58) by mail-relay.amazon.com (10.43.160.118) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 26 Sep 2019 09:50:13 +0000 From: To: Date: Thu, 26 Sep 2019 10:46:46 +0100 Message-ID: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [RFC PATCH 83/84] x86/pmap: rewrite logic for locking. X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , =?utf-8?q?Roger_Pau_Monn?= =?utf-8?q?=C3=A9?= , Wei Liu , Jan Beulich , Hongyan Xia Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Hongyan Xia Due to the limited PMAP entries, another pCPU is allowed to use PMAP only when the current pCPU has unmapped all mappings. Signed-off-by: Hongyan Xia --- xen/arch/x86/domain_page.c | 4 ---- xen/arch/x86/pmap.c | 48 ++++++++++++++++++++++++++++++++------ xen/include/asm-x86/pmap.h | 2 -- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 782dd0650c..189ca41a1d 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -171,9 +171,7 @@ out: return (void *)MAPCACHE_VIRT_START + pfn_to_paddr(idx); pmap: - pmap_lock(); ret = pmap_map(mfn); - pmap_unlock(); flush_tlb_one_local(ret); return ret; } @@ -188,9 +186,7 @@ void unmap_domain_page(const void *ptr) if ( va >= FIXADDR_START && va < FIXADDR_TOP ) { - pmap_lock(); pmap_unmap((void *)ptr); - pmap_unlock(); return; } diff --git a/xen/arch/x86/pmap.c b/xen/arch/x86/pmap.c index 93104d0b86..af7438cbe4 100644 --- a/xen/arch/x86/pmap.c +++ b/xen/arch/x86/pmap.c @@ -15,18 +15,44 @@ * used anywhere else other than the stated purpose above. */ -static DEFINE_SPINLOCK(lock); +static DEFINE_SPINLOCK(lock_cpu); /* Bitmap to track which slot is used */ static unsigned long inuse; -void pmap_lock(void) +static unsigned int lock_cpu_id = ~0; +static unsigned int lock_count; + +/* + * Only one pCPU is allowed to use PMAP entries. Another pCPU can use PMAP only + * when the current pCPU has unmapped all entries. + */ +static void pmap_cpu_up(void) { - spin_lock(&lock); + int ret = -1; + unsigned int cpu_id = smp_processor_id(); + + do + { + while ( cpu_id != lock_cpu_id && lock_count != 0 ) + ; + spin_lock(&lock_cpu); + if ( cpu_id == lock_cpu_id || lock_count == 0 ) + { + lock_cpu_id = cpu_id; + lock_count++; + ret = 0; + } + spin_unlock(&lock_cpu); + } while ( ret == -1 ); } -void pmap_unlock(void) +static void pmap_cpu_down(void) { - spin_unlock(&lock); + spin_lock(&lock_cpu); + ASSERT(smp_processor_id() == lock_cpu_id); + ASSERT(lock_count); + lock_count--; + spin_unlock(&lock_cpu); } void *pmap_map(mfn_t mfn) @@ -37,7 +63,13 @@ void *pmap_map(mfn_t mfn) l1_pgentry_t *pl1e; ASSERT(!in_irq()); - ASSERT(spin_is_locked(&lock)); + + /* + * This semaphore-like locking means only one pCPU is allowed, which also + * suggests PMAP should only be used to bootstrap other structures. Any + * general purpose use of PMAP is a mistake. + */ + pmap_cpu_up(); idx = find_first_zero_bit(&inuse, NUM_FIX_PMAP); if ( idx == NUM_FIX_PMAP ) @@ -63,13 +95,15 @@ void pmap_unmap(void *p) ASSERT(!in_irq()); ASSERT(slot >= FIX_PMAP_BEGIN && slot <= FIX_PMAP_END); - ASSERT(spin_is_locked(&lock)); + idx = slot - FIX_PMAP_BEGIN; __clear_bit(idx, &inuse); pl1e = &l1_fixmap[L1_PAGETABLE_ENTRIES - 1 - slot]; l1e_write_atomic(pl1e, l1e_from_mfn(_mfn(0), 0)); + + pmap_cpu_down(); } static void __maybe_unused build_assertions(void) diff --git a/xen/include/asm-x86/pmap.h b/xen/include/asm-x86/pmap.h index 34d4f2bb38..790cd71fb3 100644 --- a/xen/include/asm-x86/pmap.h +++ b/xen/include/asm-x86/pmap.h @@ -4,8 +4,6 @@ /* Large enough for mapping 5 levels of page tables with some headroom */ #define NUM_FIX_PMAP 8 -void pmap_lock(void); -void pmap_unlock(void); void *pmap_map(mfn_t mfn); void pmap_unmap(void *p);