From patchwork Fri Apr 26 17:21:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamas K Lengyel X-Patchwork-Id: 10919479 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 00E6514C0 for ; Fri, 26 Apr 2019 17:23:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D7931288D1 for ; Fri, 26 Apr 2019 17:23:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CBDBF28E5C; Fri, 26 Apr 2019 17:23:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A4EF7288D1 for ; Fri, 26 Apr 2019 17:23:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hK4Xi-0004Mn-1V; Fri, 26 Apr 2019 17:21:46 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hK4Xg-0004Mi-VK for xen-devel@lists.xenproject.org; Fri, 26 Apr 2019 17:21:44 +0000 X-Inumbo-ID: c3205a37-6847-11e9-843c-bc764e045a96 Received: from mail-io1-f67.google.com (unknown [209.85.166.67]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id c3205a37-6847-11e9-843c-bc764e045a96; Fri, 26 Apr 2019 17:21:43 +0000 (UTC) Received: by mail-io1-f67.google.com with SMTP id c3so3562259iok.6 for ; Fri, 26 Apr 2019 10:21:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=A9UWGu9rBj7DOnwO13kr7TG5xm4sj9fXYKV4wmxsbgI=; b=LzXIkE9kCEhWjhUkPlPFunUS0e5unWKrPNJHfZk6blm7ddPpmmizvngqwSZAugG7F9 90iliLeMXhtPZ9yi5JsCI087eZ3IhF6sEhy/4Olnk/m+B6FL5Ems21X7JQqVni/0fRQW hDaf/mqYYIn0QVZ/O8FWqPqdEAgfqJCFNx42zR23z7Uuys8GBfrgnzTCjlDR3zwLCYfn Pj3VEWKhRYvcRIrqP2CcjtPsS6XfMqIac88nJCibo8kY3jwTuaGuCxlgM3MBf8h1IKcr GEbXzH5es7IJAnXjTwDrrfR/AjkLR+R0fh5cxDQH9F/In26HY2g8TkzJvtanicug2wyH q6QA== X-Gm-Message-State: APjAAAXEpdgu41YUIPaQkIt/M2bydb7DOJSImtwuN1kmj1IpLiWTuMJL +0+So1Xr0pFpBB437BGw9la2fBuX X-Google-Smtp-Source: APXvYqzI16HRAYQLJ9msmZvsW1tGKUQSLET3a3DJxQzmq3J0YLPlh3SgEYHKchTvemJef6HkJwft6Q== X-Received: by 2002:a5d:9d84:: with SMTP id 4mr13325746ion.124.1556299303029; Fri, 26 Apr 2019 10:21:43 -0700 (PDT) Received: from localhost.localdomain (c-71-205-12-124.hsd1.co.comcast.net. [71.205.12.124]) by smtp.gmail.com with ESMTPSA id u16sm3706808iol.66.2019.04.26.10.21.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Apr 2019 10:21:41 -0700 (PDT) From: Tamas K Lengyel To: xen-devel@lists.xenproject.org Date: Fri, 26 Apr 2019 11:21:35 -0600 Message-Id: <20190426172138.14669-1-tamas@tklengyel.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v3 1/4] x86/mem_sharing: reorder when pages are unlocked and released X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Tamas K Lengyel , Wei Liu , George Dunlap , Andrew Cooper , Jan Beulich , Roger Pau Monne Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Calling _put_page_type while also holding the page_lock for that page can cause a deadlock. Signed-off-by: Tamas K Lengyel Cc: Jan Beulich Cc: Andrew Cooper Cc: George Dunlap Cc: Wei Liu Cc: Roger Pau Monne --- v3: simplified patch by keeping the additional references already in-place --- xen/arch/x86/mm/mem_sharing.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/mm/mem_sharing.c b/xen/arch/x86/mm/mem_sharing.c index dfc279d371..e2f74ac770 100644 --- a/xen/arch/x86/mm/mem_sharing.c +++ b/xen/arch/x86/mm/mem_sharing.c @@ -648,10 +648,6 @@ static int page_make_private(struct domain *d, struct page_info *page) return -EBUSY; } - /* We can only change the type if count is one */ - /* Because we are locking pages individually, we need to drop - * the lock here, while the page is typed. We cannot risk the - * race of page_unlock and then put_page_type. */ expected_type = (PGT_shared_page | PGT_validated | PGT_locked | 2); if ( page->u.inuse.type_info != expected_type ) { @@ -660,12 +656,12 @@ static int page_make_private(struct domain *d, struct page_info *page) return -EEXIST; } - /* Drop the final typecount */ - put_page_and_type(page); - /* Now that we've dropped the type, we can unlock */ mem_sharing_page_unlock(page); + /* Drop the final typecount */ + put_page_and_type(page); + /* Change the owner */ ASSERT(page_get_owner(page) == dom_cow); page_set_owner(page, d); @@ -900,6 +896,7 @@ static int share_pages(struct domain *sd, gfn_t sgfn, shr_handle_t sh, p2m_type_t smfn_type, cmfn_type; struct two_gfns tg; struct rmap_iterator ri; + unsigned long put_count = 0; get_two_gfns(sd, sgfn, &smfn_type, NULL, &smfn, cd, cgfn, &cmfn_type, NULL, &cmfn, 0, &tg); @@ -984,7 +981,7 @@ static int share_pages(struct domain *sd, gfn_t sgfn, shr_handle_t sh, * Don't change the type of rmap for the client page. */ rmap_del(gfn, cpage, 0); rmap_add(gfn, spage); - put_page_and_type(cpage); + put_count++; d = get_domain_by_id(gfn->domain); BUG_ON(!d); BUG_ON(set_shared_p2m_entry(d, gfn->gfn, smfn)); @@ -999,6 +996,10 @@ static int share_pages(struct domain *sd, gfn_t sgfn, shr_handle_t sh, mem_sharing_page_unlock(secondpg); mem_sharing_page_unlock(firstpg); + BUG_ON(!put_count); + while ( put_count-- ) + put_page_and_type(cpage); + /* Free the client page */ if(test_and_clear_bit(_PGC_allocated, &cpage->count_info)) put_page(cpage); @@ -1167,8 +1168,8 @@ int __mem_sharing_unshare_page(struct domain *d, { if ( !last_gfn ) mem_sharing_gfn_destroy(page, d, gfn_info); - put_page_and_type(page); mem_sharing_page_unlock(page); + put_page_and_type(page); if ( last_gfn ) { if ( !get_page(page, dom_cow) )