Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2595310pxb; Mon, 19 Apr 2021 09:10:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyF7StwfMaRPhelZF83CZfhAcqCXCEoAl0ByU0qUg8o4L3oj7ivTdeqNzzAgBW0Q8Pjv7Vk X-Received: by 2002:a17:906:f41:: with SMTP id h1mr13037854ejj.399.1618848637573; Mon, 19 Apr 2021 09:10:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618848637; cv=none; d=google.com; s=arc-20160816; b=l5qr7vWjsJs/akL2pZQnBocew6+ONhwsg/eDz1HhkGZSZ0f5Sil3EM5VoB5KKS6BBO MVm0qTRXw1jBRS1Ev9cjG7MakSuzY4GuMqYFFyCMBKU3SOGVjd6nU/ETLxR81iZU85lP rp/GKbkFttY1twrT1lGbNglDbzE4moXYbrftQC5RkjrEkjJa/jy+8UErT8/aE2u0P+AR okpczs2JECKr1mBT/45yay3JWdeTiUIxjXmoitpzWsdG8esapvp9Mr3k4iB/CGm4vvjI rUNml77F2bPBB2T9sY01eayZQNBNU0e4R8OS6Q0jLq5b7v8vICz7adMVjpskcDJ7HzYy BKkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=hANO9FC/Eqy7x+FqUY+Eg+GK+A5D7OA8qqUpVhz6pBc=; b=fiRdDaN5SWfQcAvw20JTbKo/YO5rxR5cgmPqR3RBy/D1KL7ls8gHI/2XMofG1B/suJ ioJ49QnIlqWBPtjEV8WOEjAFNAyrpkfroqX1lNotylC3f/bg/Tk9eOvmgzmwE0Hdl4lE TXFLvxSoQn/H2e2K1fkS4OCl2XCd1aw8CeNLDA+4nXZXr0ahBhVPDZvd75ReG3dPUEK0 p8IAihBTtuuvhQiY44ZG6yfE5Cz3XT1ZzhpDh46Non7zOmE6tKu2FhYbpfnyH6kP9xr5 gLsqKS1DOhiKZZd9Sa8mpCB8BIBJL8uxi8Cu6LXFAA67yocF1FKcD8xl5XwP5sWOKxM0 Yoew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kdJeLy95; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y11si12576900edd.539.2021.04.19.09.10.13; Mon, 19 Apr 2021 09:10:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kdJeLy95; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238693AbhDSPJh (ORCPT + 99 others); Mon, 19 Apr 2021 11:09:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232081AbhDSPJg (ORCPT ); Mon, 19 Apr 2021 11:09:36 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52887C06174A for ; Mon, 19 Apr 2021 08:09:06 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id m11so23380687pfc.11 for ; Mon, 19 Apr 2021 08:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=hANO9FC/Eqy7x+FqUY+Eg+GK+A5D7OA8qqUpVhz6pBc=; b=kdJeLy959fNOuKxiroQcVOpawxEafqWiFZUUkpHihGfEQxevL2br8jYADTI99Hlkt0 pxi48YwShSMIGYOPYh19SRcKw0/qtKjHOQV5NZHG+GX3fdSH9rc/Ouut1LmJAMWfESyZ nSsqU7qb1XMvGLjaeScqMEfymOKRe3oFN5h9wviXivc2hQBA5q2ZPRZBFb9g2WqmQVK1 G+Wz9+BBVFhBKwoL+EhblfYM+PcGh0+DJoIKjPWLd5d26fRiyKa1RFPaq72urIp3Mljh Jdv5YUWT0YTiUMer7shX5JYUHOCJczBMI1/0L4KbuH5uuYKPtWl6XvJtdLw4ASYwNnxq i1Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=hANO9FC/Eqy7x+FqUY+Eg+GK+A5D7OA8qqUpVhz6pBc=; b=a5x4cnPrpD/zJoOP0Mv7PQWT4uLHtkAqYzkKHMHCGFCU9UHs09Hh3KGsmRx3uuA2gk +PUNeoI6u+pRa835JzEfCxvW/KmR16kYSt1QvKxUtWJWMeRqI6B0AvnJHul0KwsRF5Ga SoxKvDl9OEi8eVby/rihaafrwWNfGzrOmcrxvWOIGfyiFUwBhILGqbGg0vxiosuhUXDh wR0k6k3NMgCYWYPmLvnbORcxCygM5KERL1x8Lh3R4GJW7dSZI8mBtJc4Gybf6yb4gw4U a3dZDP4kuOnUE5d6rwycRyrQuLreBSRxDSMDn7xGvSKiVs+Ds5vx7781bIkZw7bUz11m j/ng== X-Gm-Message-State: AOAM531EbXmY/p1Vi3y5nqWVeApsBIbcdm5XjsxBTMDykMdwEp/07Rb0 r28k8o78hddvoarFMCeWVCt/8A== X-Received: by 2002:a05:6a00:1687:b029:253:f417:4dba with SMTP id k7-20020a056a001687b0290253f4174dbamr20329863pfc.5.1618844945636; Mon, 19 Apr 2021 08:09:05 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id gt22sm14457236pjb.7.2021.04.19.08.09.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 08:09:05 -0700 (PDT) Date: Mon, 19 Apr 2021 15:09:01 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: Wanpeng Li , Marc Zyngier , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm , LKML , Ben Gardon Subject: Re: [PATCH v2 09/10] KVM: Don't take mmu_lock for range invalidation unless necessary Message-ID: References: <20210402005658.3024832-1-seanjc@google.com> <20210402005658.3024832-10-seanjc@google.com> <2a7670e4-94c0-9f35-74de-a7d5b1504ced@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2a7670e4-94c0-9f35-74de-a7d5b1504ced@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 19, 2021, Paolo Bonzini wrote: > On 19/04/21 10:49, Wanpeng Li wrote: > > I saw this splatting: > > > > ====================================================== > > WARNING: possible circular locking dependency detected > > 5.12.0-rc3+ #6 Tainted: G OE > > ------------------------------------------------------ > > qemu-system-x86/3069 is trying to acquire lock: > > ffffffff9c775ca0 (mmu_notifier_invalidate_range_start){+.+.}-{0:0}, > > at: __mmu_notifier_invalidate_range_end+0x5/0x190 > > > > but task is already holding lock: > > ffffaff7410a9160 (&kvm->mmu_notifier_slots_lock){.+.+}-{3:3}, at: > > kvm_mmu_notifier_invalidate_range_start+0x36d/0x4f0 [kvm] > > I guess it is possible to open-code the wait using a readers count and a > spinlock (see patch after signature). This allows including the > rcu_assign_pointer in the same critical section that checks the number > of readers. Also on the plus side, the init_rwsem() is replaced by > slightly nicer code. Ugh, the count approach is nearly identical to Ben's original code. Using a rwsem seemed so clever :-/ > IIUC this could be extended to non-sleeping invalidations too, but I > am not really sure about that. Yes, that should be fine. > There are some issues with the patch though: > > - I am not sure if this should be a raw spin lock to avoid the same issue > on PREEMPT_RT kernel. That said the critical section is so tiny that using > a raw spin lock may make sense anyway If using spinlock_t is problematic, wouldn't mmu_lock already be an issue? Or am I misunderstanding your concern? > - this loses the rwsem fairness. On the other hand, mm/mmu_notifier.c's > own interval-tree-based filter is also using a similar mechanism that is > likewise not fair, so it should be okay. The one concern I had with an unfair mechanism of this nature is that, in theory, the memslot update could be blocked indefinitely. > Any opinions? For now I placed the change below in kvm/queue, but I'm > leaning towards delaying this optimization to the next merge window. I think delaying it makes sense. > @@ -1333,9 +1351,22 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, > WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS); > slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; > - down_write(&kvm->mmu_notifier_slots_lock); > + /* > + * This cannot be an rwsem because the MMU notifier must not run > + * inside the critical section. A sleeping rwsem cannot exclude > + * that. How on earth did you decipher that from the splat? I stared at it for a good five minutes and was completely befuddled. > + */ > + spin_lock(&kvm->mn_invalidate_lock); > + prepare_to_rcuwait(&kvm->mn_memslots_update_rcuwait); > + while (kvm->mn_active_invalidate_count) { > + set_current_state(TASK_UNINTERRUPTIBLE); > + spin_unlock(&kvm->mn_invalidate_lock); > + schedule(); > + spin_lock(&kvm->mn_invalidate_lock); > + } > + finish_rcuwait(&kvm->mn_memslots_update_rcuwait); > rcu_assign_pointer(kvm->memslots[as_id], slots); > - up_write(&kvm->mmu_notifier_slots_lock); > + spin_unlock(&kvm->mn_invalidate_lock); > synchronize_srcu_expedited(&kvm->srcu); >