From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 92E4C8821 for ; Sat, 25 Jan 2025 00:50:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737766221; cv=none; b=MqZiAq/t1JydJ4gXxiXicqmSNvCdh6OxYK9IsoWN3syD9lm5PyZqVVJXrofcloPf5f6+lerTEj8GF/JmIPXQV9FGwnE3aX/XIfQK+A28go7+EdVgoRmf1+C/a/4/Xu1uCEUYD0wfBpV6P56HEMB65oz5JkI58VEY5qSscGrp5Bw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737766221; c=relaxed/simple; bh=oEGupVNDTZ1TZfaKV7p2mr3SO5fE4Coa0z47OYyQvys=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=hRsjm4HJIsGeJna5F1YhmhDLMzY3A98JFkRtaO2EjhLkCPr1GceHlm4V1MglQL86TMroazzCoLZCkP7tjOWXV2A1hEwgCb8f4znPxQhcR1rtsgX62C8/5Jl57a1ai/j/xNCgWisrsOK7J5Iio7RyDeK3ck9dEDX1kVh/78Wv/1A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ND3Mfqdi; arc=none smtp.client-ip=209.85.214.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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ND3Mfqdi" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-216728b170cso51580895ad.2 for ; Fri, 24 Jan 2025 16:50:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737766219; x=1738371019; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=0i8PhjRzp2KlZn/OgBC0R5Tc8Tj8apF3Z4WwS7zlbko=; b=ND3MfqdiUQCbvGduVtzI4P49OLl9p7zrgxTTILMVzV2j8C9d8+Uj9z7VFgxWtxRgX0 dgYKoqhF6icIQZcX8T6LBFdNLA0CWxWmweCWaQaxFeeioIQobaRrgwVIdhL+vVbLeYgR ZB3XOWV2/E+EisZRpD6DYg0V5Us49YaNIkFzTOv8g9FkQE9vQ+MfkHNwPhoKPo3D4e9j pYFJoE3s7eUz0ITliOL15xCYmMlhtsgTE8mTj4bAXlOTK/VVZCRgASqEpyIWHKKVePze Ok1dFm6VQWT2bImLWxEkZobFyy1hcMOMpvdM05udp0iGr+8lKzzB2cPkKoUKBldhz2R9 cmIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737766219; x=1738371019; h=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=0i8PhjRzp2KlZn/OgBC0R5Tc8Tj8apF3Z4WwS7zlbko=; b=CQ9KSLdWZ61kvPiw45g0l6qXD8eG+0lXPn7CGKiDMHRCLkTCNS5ZB255cZpHmR2hg8 qtzq2PWl+qL248LKwQHZsVJJhDsija7C1iWlfIQVFQOsiBNFx+80JyW4mRqb27q74OTj tamRXvryTvSUYJGgAHccse79/3qJEiUaUK0Yoyp+z23aX9/v5FCRgqNMJQAklbFY8rFR WItOpvfvmFXR9M5FqPAH58LXWgWJQ42mRwbfITtv/8/Ylzk19MuuGKJ2100R7SEF8FIt dVVUY1bPWcu3BJYzPVY5Pswzef5W/+PpIZmcf9CDTv7dyEZf8IeLF7DV5r+XhRVpZ2uk tKFA== X-Forwarded-Encrypted: i=1; AJvYcCWCx1SzyHGfJ7pQVMCj1G7rjSpSNonWkVpdvqHfXe5Dfq1GDURuCFfVTQPQJyS73qx1DHvoQpKYJ+kOkxQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzF6G7BokIy6o6tpkPqO5ORCWolBfmoeExZHCaCwUF8m7vRF/l0 LIfw0IvkYn4UYK8t1tZy+hzyKfEDWdrBnYteH1D7+KTzPOr4bBHgkhYnO03+3ej6TbNZ39bvODe HBw== X-Google-Smtp-Source: AGHT+IESQ7fgogXW/Au/e5Du8hywDj5Be3G+Eu0iSGzrsRlqChwX2lYnxbj77S5/1Wo9bC1eghKSlPn1yTU= X-Received: from pfbcj8.prod.google.com ([2002:a05:6a00:2988:b0:72d:8b6a:d16c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:734e:b0:1e0:d848:9e83 with SMTP id adf61e73a8af0-1eb214e526cmr50355103637.25.1737766218822; Fri, 24 Jan 2025 16:50:18 -0800 (PST) Date: Fri, 24 Jan 2025 16:50:17 -0800 In-Reply-To: <20250124234623.3609069-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250124234623.3609069-1-seanjc@google.com> Message-ID: Subject: Re: [PATCH] KVM: x86/mmu: Ensure NX huge page recovery thread is alive before waking From: Sean Christopherson To: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Keith Busch Content-Type: text/plain; charset="us-ascii" On Fri, Jan 24, 2025, Sean Christopherson wrote: > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index a45ae60e84ab..74c20dbb92da 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -7120,6 +7120,19 @@ static void mmu_destroy_caches(void) > kmem_cache_destroy(mmu_page_header_cache); > } > > +static void kvm_wake_nx_recovery_thread(struct kvm *kvm) > +{ > + /* > + * The NX recovery thread is spawned on-demand at the first KVM_RUN and > + * may not be valid even though the VM is globally visible. Do nothing, > + * as such a VM can't have any possible NX huge pages. > + */ > + struct vhost_task *nx_thread = READ_ONCE(kvm->arch.nx_huge_page_recovery_thread); > + > + if (nx_thread) > + vhost_task_wake(nx_thread); As mentioned in the original thread[*], I belatedly realized there's a race with this approach. If vhost_task_start() completes and kvm_nx_huge_page_recovery_worker() runs before a parameter change, but the parameter change runs before the WRITE_ONCE(), then the worker will run with stale params and could end up sleeping for far longer than userspace wants. I assume we could address that by taking kvm->arch.nx_once.mutex in this helper instead of using the lockless approach. I don't think that would lead to any deadlocks? [*] https://lore.kernel.org/all/Z5QsBXJ7rkJFDtmK@google.com