From patchwork Wed Jun 2 09:43:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12293567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B360C47083 for ; Wed, 2 Jun 2021 09:46:21 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 35560613BF for ; Wed, 2 Jun 2021 09:46:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 35560613BF Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=4UY0YCbgkLqjVZxXJsbXsejJe5xu/IGdhMR6AuFsHQ8=; b=VjeplsVawJyKKk4kWrel5iBpB1 UxqkXYC0Q1L3lcjU89S0++d/yukz2SYGgB0LzeZV475gJjkieCp7RAQanj7ayhWs8/tVc1HK+EHv4 Q2iMhz8W0sMGxsTaD7QvSFckPjuZsztahcVyoJW0r6R4fUNIFLxhdzjbE/6hwrR1ebNizRGR5PG1U MhHbSRS6e6+CXebRJZpAUL/7VGI9gSu7Y7sqqCIz675Au45tvy+wMEQQ28GUKA/dBd22IV/GogBCl PMl0pkkc9QohcvRFUgQqLPfNcF13kdSG1j3B56oZXNryLv+0Ws4EZE57jvSRFUFFNLP3G9dSV06fg YR2eLngA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1loNQD-0032E6-Hl; Wed, 02 Jun 2021 09:44:21 +0000 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1loNPq-00327J-C9 for linux-arm-kernel@lists.infradead.org; Wed, 02 Jun 2021 09:43:59 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id w14-20020a056214012eb02901f3a4388530so1256632qvs.17 for ; Wed, 02 Jun 2021 02:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=iZUQk8fIlBvRNxDOUvgxujyUIWimZaPXfaVXd2+o6PY=; b=VLEyklUT17eTyvASVE7D7VJJlhe5fAE1d9RgPZBqibyIBB6t1KdUSjsMDHUTzawsYb kX7dTP9UCIcEy4saEDT8A1ACHMYxFaTwbasSzUy9XUR/Y2LxVJc9AKyM8VvhpSZRZ3NL MxZ1AY2eP2/bBpheP6r59nKkqlZ/ZGdBOqYGu8KXHl7lSkugmxTEnm+8Wu68jUWSVkJL 0xrNW3CgzLjbiR2UH+ZZSedoZtMHG+8aASS7O0TbNhnJBXFav94x+bAlSa4Vz6cWhaK6 egWJUSMlQpeb1mS5RVmlTwu/ULC5Aw0INHSbGNgFhYW0WNgXxCvaRJ0qlrTHoPcNIdkK Jcnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=iZUQk8fIlBvRNxDOUvgxujyUIWimZaPXfaVXd2+o6PY=; b=GslDwBKTp7PDIfDc2FpkfcsolLNP0ehNb7rfeDiLN+7srhadghZ465+MWS92imMBNZ AGo9CV0nRSqxVQgYLbrBfC14a/0+JJSqMM7SbJZvMLaIMjrEuZGb3YXZWj3IL5Ip7fkv 49Ct9pnDxOulsJsgTX2+7boRx1+NhuG2h+mhzW77HmRFbbUcRTLWDOjRZ6XKjIxdXq8P m7ArnDB+uQB8GgXU7cFXPNvTJWYejeBHrL6nt/wchxbYhAH5g/sGSlqawy6+2FxuKdgx EAdvFeBOp1t87Jp7x0mLUc656hrCh+9y+eAfRQDJ4ji9ye4c/qSJUYTzPm0bZqXYAkLm ZS6Q== X-Gm-Message-State: AOAM532OGBAu4gIHJIENgjLPwh+mhfF2ULL1GoSN/sLU58koJBgNH3MW b05X2Y3n9jlwsdALl71+6flKjjVfWGJX X-Google-Smtp-Source: ABdhPJw5u97rh4yWHjSnTxNGWaGWtM7mSfMb+qJ6Mxm9lPPMCbPzQl92O3It3Tn7cZ0dBOcajFXTAP9JCzwj X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a05:6214:c88:: with SMTP id r8mr6671320qvr.58.1622627036015; Wed, 02 Jun 2021 02:43:56 -0700 (PDT) Date: Wed, 2 Jun 2021 09:43:42 +0000 In-Reply-To: <20210602094347.3730846-1-qperret@google.com> Message-Id: <20210602094347.3730846-3-qperret@google.com> Mime-Version: 1.0 References: <20210602094347.3730846-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.rc0.204.g9fa02ecfa5-goog Subject: [PATCH v2 2/7] KVM: arm64: Use refcount at hyp to check page availability From: Quentin Perret To: maz@kernel.org, will@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, catalin.marinas@arm.com, suzuki.poulose@arm.com Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kernel-team@android.com, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210602_024358_451192_0F8093AC X-CRM114-Status: GOOD ( 18.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The hyp buddy allocator currently checks the struct hyp_page list node to see if a page is available for allocation or not when trying to coalesce memory. Now that decrementing the refcount and attaching to the buddy tree is done in the same critical section, we can rely on the refcount of the buddy page to be in sync, which allows to replace the list node check by a refcount check. This will ease removing the list node from struct hyp_page later on. Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/page_alloc.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/page_alloc.c b/arch/arm64/kvm/hyp/nvhe/page_alloc.c index d666f4789e31..2602577daa00 100644 --- a/arch/arm64/kvm/hyp/nvhe/page_alloc.c +++ b/arch/arm64/kvm/hyp/nvhe/page_alloc.c @@ -55,7 +55,7 @@ static struct hyp_page *__find_buddy_avail(struct hyp_pool *pool, { struct hyp_page *buddy = __find_buddy_nocheck(pool, p, order); - if (!buddy || buddy->order != order || list_empty(&buddy->node)) + if (!buddy || buddy->order != order || buddy->refcount) return NULL; return buddy; @@ -133,6 +133,12 @@ static inline void hyp_set_page_refcounted(struct hyp_page *p) p->refcount = 1; } +static void __hyp_put_page(struct hyp_pool *pool, struct hyp_page *p) +{ + if (hyp_page_ref_dec_and_test(p)) + __hyp_attach_page(pool, p); +} + /* * Changes to the buddy tree and page refcounts must be done with the hyp_pool * lock held. If a refcount change requires an update to the buddy tree (e.g. @@ -146,8 +152,7 @@ void hyp_put_page(void *addr) struct hyp_pool *pool = hyp_page_to_pool(p); hyp_spin_lock(&pool->lock); - if (hyp_page_ref_dec_and_test(p)) - __hyp_attach_page(pool, p); + __hyp_put_page(pool, p); hyp_spin_unlock(&pool->lock); } @@ -202,15 +207,16 @@ int hyp_pool_init(struct hyp_pool *pool, u64 pfn, unsigned int nr_pages, /* Init the vmemmap portion */ p = hyp_phys_to_page(phys); - memset(p, 0, sizeof(*p) * nr_pages); for (i = 0; i < nr_pages; i++) { p[i].pool = pool; + p[i].order = 0; INIT_LIST_HEAD(&p[i].node); + hyp_set_page_refcounted(&p[i]); } /* Attach the unused pages to the buddy tree */ for (i = reserved_pages; i < nr_pages; i++) - __hyp_attach_page(pool, &p[i]); + __hyp_put_page(pool, &p[i]); return 0; }