Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp812767lqb; Wed, 29 May 2024 11:10:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW6e6LEUK5/c9VOu8XDdU28t210nyhlocKvCZxlRsmymcmvSH+tWmPBp47/Yy2JhhyLPT6iSeSl6jHx/J0RDz7/0B6uhJ4qrUW2elpyVg== X-Google-Smtp-Source: AGHT+IEOoXbRVAfMqrQsRxAQ/761O0PWSaJCAE8pwLPKOJPJ7co05kapZMhNi46NhDMoTelYnY0z X-Received: by 2002:a17:902:e5ca:b0:1f4:8e4a:b77a with SMTP id d9443c01a7336-1f48e4aba21mr104515065ad.18.1717006243966; Wed, 29 May 2024 11:10:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717006243; cv=pass; d=google.com; s=arc-20160816; b=xL4r2tAM9lzsivGtfiIDaOcjW1QatChH3nmYiB6vwNA1Zdl2TfYGbsNJGm6zwl7NWQ MRWApPey3o2ixJfYEs6xYmKA4yprds3NCXgN+0V2tD4chfzb4mKHATT3esZIKuUDZTQp rBSCwfEjNwIq5hAFT7pdMSSXaE4S9/DaXZ5JtGnXAvVRLeO5b5dmHzqhiTaxSteW2dRE mUgyVOnqeSsLi5mdtnenF5G1quQULMi9leaGMwZtbQi33UJW7JiQjNHjl3lIQURUEtjw kbgP6OvqiuyDsfKrBDSx+SURwYyGzSOLPf+Mdiyt8sZ1VGuKqmrYkiwFm3mRKr05bxwR 49gQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=K/D/1FA4Uw1YrzG6+cN1+7larQcduYYAcSD+7tGLFpU=; fh=aXXCK72DH+5V9UUd9YzNgx/z5xMx2d5caRpOm63Q31w=; b=toMZD1IDMV/Lt8E0Ay4nRC5nSEoJjGk33TjovIud3Zk+l4QZTsRQx08PCfismOf0Tw EwrZkv/MkRJqZxGyIAmIjclnZUS7VmzL0Ess8gA0Vx7J1zRJGCivB8n/L/oC6ymKf3a0 QJdUyJNeQrvhJcRseTWUGiOC0yoQy0xTMXSr6jXnHojD5i/FXgTYBKwj1qBuLjMrSwVu E++cch/izrGJS9UPq4LVRWi0+9GaohypoppxmJ5F9S/EmHK3UW1uqOj79qer+tCq11EM T/PGK1N1WkpcFAOyOM7MSMSSrZGyMWvicMq2ZwLtsq3bJmgwttkgOKEg3yw+vAop8Poi SvWw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fv1rpczK; arc=pass (i=1 spf=pass spfdomain=flex--jthoughton.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-194551-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194551-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f44c9bb4d2si103242455ad.479.2024.05.29.11.10.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 11:10:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-194551-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fv1rpczK; arc=pass (i=1 spf=pass spfdomain=flex--jthoughton.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-194551-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194551-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 13C51B24351 for ; Wed, 29 May 2024 18:07:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E36201C8FB9; Wed, 29 May 2024 18:05:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fv1rpczK" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D3021C2333 for ; Wed, 29 May 2024 18:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717005927; cv=none; b=t7+B65i56iolZO9gsbUBjSE35zmgPR0Z0TWv4D14qVem/JVTPyn0dQti3SojaA3orSZrzzMDlSwQQv0R36yqVvSgMIZPjArT2CPwpO74yEiU4+jpWs77ETskpW8TqtFHjTEQpsuwjITEA1tsHzVB7TSGFG+kUBDIEIuU4LyW3PE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717005927; c=relaxed/simple; bh=LSBrwS6oby84HQacrqjfwftAjZHXaXKkGHYxsFQ9F2c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SR+VMrFNAMwRzuecPnDKeYXOgy1EUu02Lrui92nx7tIZuV9HBs8Elp5cjD4rHii87JMmUBgwUeNfvk1pf8Xw8HsKoIGntGBTVRNp67rY9hBCe3SLzJBT7/zsxPC9lx2ek2f/e2wDIAA0XkBG7JueTtpS60ohRYTmh4bqNrt3DPM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fv1rpczK; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62a2aec8521so35470397b3.0 for ; Wed, 29 May 2024 11:05:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717005923; x=1717610723; darn=vger.kernel.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=K/D/1FA4Uw1YrzG6+cN1+7larQcduYYAcSD+7tGLFpU=; b=fv1rpczKcA5ByZYD6R4NmNMHfp/hM+ObdEYFCkFS0SOaHquNKIIX/G/yJs938lKOrj 2tJM/u2M4txUnAC/JoMCE/p0co7svHeBiCuFljJGmx+l6RCK0jIqSo3gsj6iystns/lj iEElzBm6r7KHFXZ8UYD6odjgI4WPgtp7NeoSUrZX0TBfMaRKo2zYHhkLGDYLTPm1Dvux Si5Ckc2bNemwbCLUI7YcK0q3rWsUN+tiOapE4czgQppgqeUI8D9ofEwCzaWliTFEd+Hb eyzKGTvP7Lzn4tIUkI39cIj+SFCZlC+EydZdr1IjcDBTlCpscCPF2r3oY/QSa6Th/k+c hUqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717005923; x=1717610723; 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=K/D/1FA4Uw1YrzG6+cN1+7larQcduYYAcSD+7tGLFpU=; b=fyTzN77BVLtiod5iT+bJOPmx7LiGOFzTDs2wzBumAI81Vkn0tph+c9CLXhFSrlt6rF JPSeY3yjKzzjElFgiIG3wNBadL+gCr+xvExTA/x80wBWbPqEo9UJiyjdjnK06qrKck5e +hEzdPua3Rt1UbxiHrUCOoa37txmJbgevOIJWigj+Ni8ldkMNsbV1kDPoHrr9GTPgm8j IZXnWCqI7TAPbokyq/V+su5EW9cjY2Yszrky+DHqF01UEW7DRNCpjO+BIv7sFpD/MDBE jIK7RhaqXMTuiH27N73E1+yG64luwQOojh1zdMjijr984MaGM9DuU/oqwJFnszQYik/u UZ7A== X-Forwarded-Encrypted: i=1; AJvYcCXTnI2XEHwk4pj3qUKiXlOuE3kMxoGfLBT+ABIHz9CoQie39t+NpuxznyaXmXREv/VIzN1GfT37zU/i3drHgJINhbA1T41m2WPZiAaG X-Gm-Message-State: AOJu0YxyrQI9pnoEGKYOThGg2VX9oo+NCGHJbuRkKHawtRRBUxSkyZ/d UFhJD6dOnko0Qc86GSI2sE+aLChlHAPiXEGkeEy9fJu96DvKXWn7vwIuaoY/7hagwF9IiWd1Yi1 3G3KgGNP2xjkDSNZA8w== X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:6010:b0:61a:e7f7:a4cc with SMTP id 00721157ae682-62a08dd7631mr39296497b3.4.1717005922981; Wed, 29 May 2024 11:05:22 -0700 (PDT) Date: Wed, 29 May 2024 18:05:09 +0000 In-Reply-To: <20240529180510.2295118-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240529180510.2295118-1-jthoughton@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Message-ID: <20240529180510.2295118-7-jthoughton@google.com> Subject: [PATCH v4 6/7] KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn From: James Houghton To: Andrew Morton , Paolo Bonzini Cc: Albert Ou , Ankit Agrawal , Anup Patel , Atish Patra , Axel Rasmussen , Bibo Mao , Catalin Marinas , David Matlack , David Rientjes , Huacai Chen , James Houghton , James Morse , Jonathan Corbet , Marc Zyngier , Michael Ellerman , Nicholas Piggin , Oliver Upton , Palmer Dabbelt , Paul Walmsley , Raghavendra Rao Ananta , Ryan Roberts , Sean Christopherson , Shaoqin Huang , Shuah Khan , Suzuki K Poulose , Tianrui Zhao , Will Deacon , Yu Zhao , Zenghui Yu , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Replace the MMU write locks for read locks. Grabbing the read lock instead of the write lock is safe because the only requirement we have is that the stage-2 page tables do not get deallocated while we are walking them. The stage2_age_walker() callback is safe to race with itself; update the comment to reflect the synchronization change. Signed-off-by: James Houghton --- arch/arm64/kvm/hyp/pgtable.c | 9 ++++----- arch/arm64/kvm/mmu.c | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 9e2bbee77491..eabb07c66a07 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1319,10 +1319,8 @@ static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, data->young = true; /* - * stage2_age_walker() is always called while holding the MMU lock for - * write, so this will always succeed. Nonetheless, this deliberately - * follows the race detection pattern of the other stage-2 walkers in - * case the locking mechanics of the MMU notifiers is ever changed. + * This walk may not be exclusive; the PTE is permitted to change + * from under us. */ if (data->mkold && !stage2_try_set_pte(ctx, new)) return -EAGAIN; @@ -1345,7 +1343,8 @@ bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, struct kvm_pgtable_walker walker = { .cb = stage2_age_walker, .arg = &data, - .flags = KVM_PGTABLE_WALK_LEAF, + .flags = KVM_PGTABLE_WALK_LEAF | + KVM_PGTABLE_WALK_SHARED, }; WARN_ON(kvm_pgtable_walk(pgt, addr, size, &walker)); diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 8337009dde77..40e7427462a7 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1775,7 +1775,7 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) u64 size = (range->end - range->start) << PAGE_SHIFT; bool young = false; - write_lock(&kvm->mmu_lock); + read_lock(&kvm->mmu_lock); if (!kvm->arch.mmu.pgt) goto out; @@ -1785,7 +1785,7 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) size, true); out: - write_unlock(&kvm->mmu_lock); + read_unlock(&kvm->mmu_lock); return young; } @@ -1794,7 +1794,7 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) u64 size = (range->end - range->start) << PAGE_SHIFT; bool young = false; - write_lock(&kvm->mmu_lock); + read_lock(&kvm->mmu_lock); if (!kvm->arch.mmu.pgt) goto out; @@ -1804,7 +1804,7 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) size, false); out: - write_unlock(&kvm->mmu_lock); + read_unlock(&kvm->mmu_lock); return young; } -- 2.45.1.288.g0e0cd299f1-goog