From patchwork Thu Sep 28 17:32:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13403355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 77C4DE732FE for ; Thu, 28 Sep 2023 17:32:57 +0000 (UTC) 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:Message-ID: References:Mime-Version: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=zrw7WR0VMagVAEB6QWMpZn67XQifwJA/Q9OARF1x0yo=; b=NkQYPBdUehYuYsWv1p84ddowr/ 86XQhTqkcX0tZ6i/QtreKU592z/lkBhtyg6NJC+9r+flV/qLpEgo0NinLfF1JYhOSilY4TlMQOWg+ DJjA2zHbmMy9C8FiepvYr3YMgRXC4xWV3NKrJdncgxqSDpilozvJt2JbwmFhBCCs8sTXIKqqvogfN A3lksYiP7FoZoGWxyCLASf448PErhiVfwUzW3lRvxqs7yaf8L+NeZaCgy7uEY5qEDfLStIFawos/F CsWkwtl1/q++4FE9ROuZ0KP3TRPJdcJSzzsuLCIFLLbE6j7GrP5F/2hOBNZZm2vfhMkbY17jEx6YJ Ngw9f0iw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qlusH-006Qvm-1Z; Thu, 28 Sep 2023 17:32:29 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qlus6-006QsX-2Y for linux-arm-kernel@lists.infradead.org; Thu, 28 Sep 2023 17:32:20 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a21c283542so30123847b3.3 for ; Thu, 28 Sep 2023 10:32:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695922335; x=1696527135; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0OrolHEhppSNqHfijRmLo8eiKiIO9HbxQQbiWJh4bXs=; b=O4ZLuVHu8cUFovWTRbzq7gej7AU5t+EJYDfFW2KYKWNLAJqmOQgYjPh+pVpy/vDbZt GFNp575JBBXunho/kTM+maLsywMuwUMiDtMuXO3byj9C1RuB2ugjqzG7ogCol++HMki3 gCnjDZC9WK+Nh942dI1E9IZTl6hi13bdlAV9D90Z5lyhJbGSOLgxSkL2NcufgQZuKO2G s7Wr+w6uZ7Ogtohl8kWnwCTiKH0dL6q5VKR+uC/+gVJ7MtHL1Mg2L+GDyOuMM0bsE19c KmmQ6/HlB3IQYU1uXMUr+UdWxL/rpFduSv/naWsXWwPvBtVnv55vI516cIElMlEIJmtQ 4URw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695922335; x=1696527135; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0OrolHEhppSNqHfijRmLo8eiKiIO9HbxQQbiWJh4bXs=; b=NQvXPS5zdhRz8fQachfyHmkJ2Ta5VOZs11qm9rYkesBEIqXWR1+vPmQbRP+ppeCIpj 3s4GFI0BboIHdDSRGcRKPJa0e970fts34LsbsINi9IpIf/B9h7oIbRSPT68vETctqwO7 MBoUZPix6VK0weOrxrDdi8rKY8AjSdRTSIjzVIZ6inlQhvCI6t/YcsVqrEC63SRqtCNl 70gyUIA3aacZxPZCTosBMB5kHslD2PT3RkNyE/OGxC8cdldIQ2fXmKXCcsB6461Z/JSA 8i8JOe5eGW4xZdXsYbj6R3BjonJLB4YNdX2u3dctvf1bBDFnVXDTiQO8YXtiwW0Zcgxo 47IA== X-Gm-Message-State: AOJu0YzJ3ZHd7+nGYoca2ym8uNHWiLk3/sc0ULpPKw4/X5bMoKA6A6cC krMjyLIW/b652sS0d044xKOkMUL5g99qLxhr X-Google-Smtp-Source: AGHT+IF/Fa0Mh0oBH5WiyhZSx/am6vHeFH1SjUosji1bp+q9vgUSK9otdQ/l0ZJOkJ52EUgq+NqoKxRy+7XU3Ds2 X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a81:7e07:0:b0:59b:c811:a709 with SMTP id o7-20020a817e07000000b0059bc811a709mr29487ywn.0.1695922335176; Thu, 28 Sep 2023 10:32:15 -0700 (PDT) Date: Thu, 28 Sep 2023 18:32:03 +0100 In-Reply-To: <20230928173205.2826598-1-vdonnefort@google.com> Mime-Version: 1.0 References: <20230928173205.2826598-1-vdonnefort@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230928173205.2826598-2-vdonnefort@google.com> Subject: [PATCH v2 1/2] KVM: arm64: Do not transfer page refcount for THP adjustment From: Vincent Donnefort To: maz@kernel.org, oliver.upton@linux.dev Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, will@kernel.org, willy@infradead.org, Vincent Donnefort X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230928_103218_982477_F43307EC X-CRM114-Status: GOOD ( 12.69 ) 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 GUP affects a refcount common to all pages forming the THP. There is therefore no need to move the refcount from a tail to the head page. Under the hood it decrements and increments the same counter. Signed-off-by: Vincent Donnefort Reviewed-by: Gavin Shan diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 587a104f66c3..de5e5148ef5d 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1295,28 +1295,8 @@ transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot, if (sz < PMD_SIZE) return PAGE_SIZE; - /* - * The address we faulted on is backed by a transparent huge - * page. However, because we map the compound huge page and - * not the individual tail page, we need to transfer the - * refcount to the head page. We have to be careful that the - * THP doesn't start to split while we are adjusting the - * refcounts. - * - * We are sure this doesn't happen, because mmu_invalidate_retry - * was successful and we are holding the mmu_lock, so if this - * THP is trying to split, it will be blocked in the mmu - * notifier before touching any of the pages, specifically - * before being able to call __split_huge_page_refcount(). - * - * We can therefore safely transfer the refcount from PG_tail - * to PG_head and switch the pfn from a tail page to the head - * page accordingly. - */ *ipap &= PMD_MASK; - kvm_release_pfn_clean(pfn); pfn &= ~(PTRS_PER_PMD - 1); - get_page(pfn_to_page(pfn)); *pfnp = pfn; return PMD_SIZE; From patchwork Thu Sep 28 17:32:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13403356 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id EBB35E732FD for ; Thu, 28 Sep 2023 17:32:57 +0000 (UTC) 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:Message-ID: References:Mime-Version: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=uCk3A6HxrLRKsrUn6oMg4cBBf3TxREuMEEH8yAYK/fk=; b=3s7txUR7ZOWmzrd16DxP3/0Rbt afpF4xkQh8yWZYYTsODktiaJpfE4zSSejAarSmjCIRjESk5E5y3rgENFmjFzUeVS9RrbFR5ZdsfBf uu+C8z0Y5oliP2lFcxct3vl8HbIxV61DoKBj02Wz7x57IDhNDZXlf6ylX0OtUORa/FkvZJxN+D1MC HRWpexzJDCRF+lZDJFeZ60pl2hoEbjAoZkqGNGgCNNFVvwlz5UAr1CbHRzecbrSZRW2GZbp9OrfEt bG1StMZaLx+PUpHRC6JEC1qVnAG/5kx1UFkG2Npy3tqi1njNnhjKOGFLM9o+FTzntA3iHWAJXskBt NO7uyssA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qlusI-006Qw4-0K; Thu, 28 Sep 2023 17:32:30 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qlus7-006QtI-0L for linux-arm-kernel@lists.infradead.org; Thu, 28 Sep 2023 17:32:21 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d8486b5e780so20273566276.0 for ; Thu, 28 Sep 2023 10:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695922338; x=1696527138; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oE1bPfUJTQsQ/yIbfzwaE5cLdZ9DfBf0dADmXwJMZog=; b=EYrWsULarV4EqtOwb3Sg1EbcUGudys3R6sycTkaJgv3McVo/mQiYdOlbz081HvAxUQ 2nDBCF073qyS16gVnlz23rlCnwG4+c9AkXmPTckYkWScoVI0+aLK6Wv/MFAspxrfxCvB D0dN+RZF+66CXCVT61yn7oeRUIOMe4AqZ4zMJr1ZqUDtLZYjUQOAkx4wJ/kE5ifIdSIC EDwJHJVtLbTc5JLoVTd6Xf3wlsu1cw487NrSv2r7X2uQf48+AnZa9Lx5qwJhDtP/TC0d xRVTyFm1atxM//7EJdqr3UqpIfulHcSQdjGp2yFHBM6Fq5/VuFrqgOA6+Q1HQ193za4D 7skg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695922338; x=1696527138; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oE1bPfUJTQsQ/yIbfzwaE5cLdZ9DfBf0dADmXwJMZog=; b=daN2lI5iCiaDtAMC8PN5nzszYxHlSDCM1GRmkYifmJgkqy+LqVBfRvAYwrD45KgRJM tfom6rithXqnN5cpHMo9dwUSPbTMM9qfzal1xxIbPl4/G+czqBoAdgV7pxoiyQGKLws/ 7dAgkYVsakAvb/OGrsTRSdhdr6a4S/Sk4BxsTZDEHdQwJCr9f5xZpgu6XuSSfRYTgOGo xaSjxXzcf4vyYZGXA2gTaaZbe+gizE43sCTt2vYq0gAdaFMXAbPoSqOGebynSkm5plst JkQ23OPJG12/kX8ztxcFHYw9Y9CAGT4Tc655uyzwEnGNiJTNLT4Ir8v7/fV1NBhlvf8H M/0A== X-Gm-Message-State: AOJu0YxlKch8xOeqs2DXy3ueKOt1o+11cPb23ndM2cTIpxcqZ63XITtJ QGz1hmKjddvVchpsKZkHfzGelcr55qRTFYNh X-Google-Smtp-Source: AGHT+IEYD/Bsn2ww5i0Im0lm2faKv5QwH7fKM71MswQZo1QvGz692BVrUSJZXvIpRoyOP952GIplH8aBbdhhmTza X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a25:408a:0:b0:d77:f4f5:9e4 with SMTP id n132-20020a25408a000000b00d77f4f509e4mr23919yba.2.1695922337778; Thu, 28 Sep 2023 10:32:17 -0700 (PDT) Date: Thu, 28 Sep 2023 18:32:04 +0100 In-Reply-To: <20230928173205.2826598-1-vdonnefort@google.com> Mime-Version: 1.0 References: <20230928173205.2826598-1-vdonnefort@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230928173205.2826598-3-vdonnefort@google.com> Subject: [PATCH v2 2/2] KVM: arm64: Use folio for THP adjustment From: Vincent Donnefort To: maz@kernel.org, oliver.upton@linux.dev Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, will@kernel.org, willy@infradead.org, Vincent Donnefort X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230928_103219_151431_A29A8865 X-CRM114-Status: GOOD ( 15.09 ) 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 Since commit cb196ee1ef39 ("mm/huge_memory: convert do_huge_pmd_anonymous_page() to use vma_alloc_folio()"), transparent huge pages use folios. It enables us to check efficiently if a page is mapped by a block simply looking at the folio size. This is saving a page table walk. It is safe to read the folio in this path. We've just increased its refcount (GUP from __gfn_to_pfn_memslot()). This will prevent attempts of splitting the huge page. Signed-off-by: Vincent Donnefort Reviewed-by: Gavin Shan diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index de5e5148ef5d..69fcbcc7aca5 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -791,51 +791,6 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, return 0; } -static struct kvm_pgtable_mm_ops kvm_user_mm_ops = { - /* We shouldn't need any other callback to walk the PT */ - .phys_to_virt = kvm_host_va, -}; - -static int get_user_mapping_size(struct kvm *kvm, u64 addr) -{ - struct kvm_pgtable pgt = { - .pgd = (kvm_pteref_t)kvm->mm->pgd, - .ia_bits = vabits_actual, - .start_level = (KVM_PGTABLE_MAX_LEVELS - - CONFIG_PGTABLE_LEVELS), - .mm_ops = &kvm_user_mm_ops, - }; - unsigned long flags; - kvm_pte_t pte = 0; /* Keep GCC quiet... */ - u32 level = ~0; - int ret; - - /* - * Disable IRQs so that we hazard against a concurrent - * teardown of the userspace page tables (which relies on - * IPI-ing threads). - */ - local_irq_save(flags); - ret = kvm_pgtable_get_leaf(&pgt, addr, &pte, &level); - local_irq_restore(flags); - - if (ret) - return ret; - - /* - * Not seeing an error, but not updating level? Something went - * deeply wrong... - */ - if (WARN_ON(level >= KVM_PGTABLE_MAX_LEVELS)) - return -EFAULT; - - /* Oops, the userspace PTs are gone... Replay the fault */ - if (!kvm_pte_valid(pte)) - return -EAGAIN; - - return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(level)); -} - static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { .zalloc_page = stage2_memcache_zalloc_page, .zalloc_pages_exact = kvm_s2_zalloc_pages_exact, @@ -1274,7 +1229,7 @@ static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot, * * Returns the size of the mapping. */ -static long +static unsigned long transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot, unsigned long hva, kvm_pfn_t *pfnp, phys_addr_t *ipap) @@ -1287,10 +1242,7 @@ transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot, * block map is contained within the memslot. */ if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE)) { - int sz = get_user_mapping_size(kvm, hva); - - if (sz < 0) - return sz; + size_t sz = folio_size(pfn_folio(pfn)); if (sz < PMD_SIZE) return PAGE_SIZE; @@ -1385,7 +1337,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); unsigned long fault_level = kvm_vcpu_trap_get_fault_level(vcpu); - long vma_pagesize, fault_granule; + unsigned long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; @@ -1530,11 +1482,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, vma_pagesize = transparent_hugepage_adjust(kvm, memslot, hva, &pfn, &fault_ipa); - - if (vma_pagesize < 0) { - ret = vma_pagesize; - goto out_unlock; - } } if (fault_status != ESR_ELx_FSC_PERM && !device && kvm_has_mte(kvm)) {