Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5198979ybl; Tue, 10 Dec 2019 02:08:31 -0800 (PST) X-Google-Smtp-Source: APXvYqxPEZ3MApEzoeGMdw7xf0k2lkfeHT+uGrF3rH18gVp/GrsMt8ihrtbaH4CAb6SwciR9pcYd X-Received: by 2002:a54:4895:: with SMTP id r21mr3419748oic.107.1575972511029; Tue, 10 Dec 2019 02:08:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575972511; cv=none; d=google.com; s=arc-20160816; b=y8wH3QUrYN5nfwu07hG8ZbJMExuIn0gAlv0n3kL3y/OenXSSeHrg69iL0ZapXkB/E+ Slg9c6MLta4th/5tAwcCaA93YLipJ8a+yN+iUzBw+SsOgyw6ylQ6GXo+Q2vfV2e3F4l5 dHjOI4WOx7GVknti2xDISSiDM87VF2Wvg+0noUJArWy7DB9XycvH2FB5v7xJ40HMLDxD fF1W3PDbd+D6JX0Ns8n3z7o9x5u3iXmGThtZhsacTJ81Bmtoo1waTWcL8VT+wCHZux+C xSfP6AqP7eJgEQx6bjhXQlD741dIsVX6a8b9a81Aigi+pa09NkEaxY0kNcOg4pMwO6gj NcAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=CMYeQ7tZmLdKNEh9TlqLYvgk+bkQDsij/88OR5a5q7A=; b=YlFPg6JQ9qVaYyRWrJFGVeTTpfWd/A3oK9IPp7WpdPuaotqhLK7pxW3XxyCP/W2KfR sP1sx9lYKfvklAE8agH3V/kqCOeOKbHab/FKplrm3RC8OUMgkr3QOsGu335DfDU5ZtDt hxUh23Z8+v+rr2qZ9RUpji6PsUoyASQrpl1iI7oUw+tRblmejqvb8c7JyGXk91mn0Uy0 dpm5jYeuR56BMRP8LhtcpvL4/kPYSzHXNkvfFuvY5mCKgmjHCh+Dd3D77u697pWotJv9 B+yUDN8O2C8Rfp2bpzC38ITjzf7cTya330WQNxNduT5A1A2IHo1xEDYnqHycLnmyDGYy W3ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PAuSgPlB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y14si1538013otk.237.2019.12.10.02.08.18; Tue, 10 Dec 2019 02:08:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PAuSgPlB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727335AbfLJKHg (ORCPT + 99 others); Tue, 10 Dec 2019 05:07:36 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:23100 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727242AbfLJKHf (ORCPT ); Tue, 10 Dec 2019 05:07:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575972454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CMYeQ7tZmLdKNEh9TlqLYvgk+bkQDsij/88OR5a5q7A=; b=PAuSgPlBzYyQzdxfASObE7ixrEzXK1NOOVgZ8hXCFJ40pddQxjMWUTQvPl+Tss4Xzikiq5 fp0ozpyWG/qwTBxztbrFhO9mygxZqqwtsQOWX6OTg+erIRKOmh88xyo2JU42LM52XTNdVP TCpWUWEo9BLNZGQtwzONN1APWJwJzjw= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-149-HY5R-pzdOjuqK2IHM923VQ-1; Tue, 10 Dec 2019 05:07:33 -0500 Received: by mail-wm1-f72.google.com with SMTP id o135so453104wme.2 for ; Tue, 10 Dec 2019 02:07:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=CMYeQ7tZmLdKNEh9TlqLYvgk+bkQDsij/88OR5a5q7A=; b=hZHYGXlorgEvwt+wr0lsT/jMLNk5MHor3BIfrrRe7p4ADmOEzwOzgzeHvbUfE82cvI IRoLtp7WX/YNJjrhEG5y07BASirMAcLlXmVpGj8XS9OpZ0i4WF46ibvKksEpXvrlotG9 Lcxjy7tr1yfObtD8Z5Z6zqhWdPrat2MA+3T6VAjv7xewAEn62BbZiG+rVWMsYK6PXX+H gST1glAKcYWvyabtkjEz09whaizNtYO6kbGwTIktAjSBPpkkg5llS7oMTD0PGufF74gj CiBhOk923e8PHFoOr+tPI4miBBz/USaMuKUert5g+UL93oT6di/7/1/zPBY95IdGk3rW hEhg== X-Gm-Message-State: APjAAAVysvGNLOcJtESmSfvW7XFURB0pScVmXvCwl4Q2sNDALqDzcg2q mIAZb4IfqZc8ocuJjrudr/xZnh2DfitFl2lJ1eIwRsLxjkMmB4+0CE52gn34FbbNO49kThMb2Kf LRlb3Q/Wd+0ejqMn8U5Pv98D6 X-Received: by 2002:a7b:c386:: with SMTP id s6mr4078407wmj.105.1575972452037; Tue, 10 Dec 2019 02:07:32 -0800 (PST) X-Received: by 2002:a7b:c386:: with SMTP id s6mr4078380wmj.105.1575972451765; Tue, 10 Dec 2019 02:07:31 -0800 (PST) Received: from ?IPv6:2001:b07:6468:f312:e9bb:92e9:fcc3:7ba9? ([2001:b07:6468:f312:e9bb:92e9:fcc3:7ba9]) by smtp.gmail.com with ESMTPSA id m187sm2534576wmm.16.2019.12.10.02.07.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Dec 2019 02:07:31 -0800 (PST) Subject: Re: [PATCH RFC 04/15] KVM: Implement ring-based dirty memory tracking To: Peter Xu Cc: Sean Christopherson , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, "Dr . David Alan Gilbert" , Vitaly Kuznetsov References: <20191129213505.18472-1-peterx@redhat.com> <20191129213505.18472-5-peterx@redhat.com> <20191202201036.GJ4063@linux.intel.com> <20191202211640.GF31681@xz-x1> <20191202215049.GB8120@linux.intel.com> <20191203184600.GB19877@linux.intel.com> <374f18f1-0592-9b70-adbb-0a72cc77d426@redhat.com> <20191209215400.GA3352@xz-x1> From: Paolo Bonzini Message-ID: Date: Tue, 10 Dec 2019 11:07:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 In-Reply-To: <20191209215400.GA3352@xz-x1> Content-Language: en-US X-MC-Unique: HY5R-pzdOjuqK2IHM923VQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/12/19 22:54, Peter Xu wrote: > Just until recently I noticed that actually kvm_get_running_vcpu() has > a real benefit in that it gives a very solid result on whether we're > with the vcpu context, even more accurate than when we pass vcpu > pointers around (because sometimes we just passed the kvm pointer > along the stack even if we're with a vcpu context, just like what we > did with mark_page_dirty_in_slot). Right, that's the point. > I'm thinking whether I can start > to use this information in the next post on solving an issue I > encountered with the waitqueue. > > Current waitqueue is still problematic in that it could wait even with > the mmu lock held when with vcpu context. I think the idea of the soft limit is that the waiting just cannot happen. That is, the number of dirtied pages _outside_ the guest (guest accesses are taken care of by PML, and are subtracted from the soft limit) cannot exceed hard_limit - (soft_limit + pml_size). > The issue is KVM_RESET_DIRTY_RINGS needs the mmu lock to manipulate > the write bits, while it's the only interface to also wake up the > dirty ring sleepers. They could dead lock like this: > > main thread vcpu thread > =========== =========== > kvm page fault > mark_page_dirty_in_slot > mmu lock taken > mark dirty, ring full > queue on waitqueue > (with mmu lock) > KVM_RESET_DIRTY_RINGS > take mmu lock <------------ deadlock here > reset ring gfns > wakeup dirty ring sleepers > > And if we see if the mark_page_dirty_in_slot() is not with a vcpu > context (e.g. kvm_mmu_page_fault) but with an ioctl context (those > cases we'll use per-vm dirty ring) then it's probably fine. > > My planned solution: > > - When kvm_get_running_vcpu() != NULL, we postpone the waitqueue waits > until we finished handling this page fault, probably in somewhere > around vcpu_enter_guest, so that we can do wait_event() after the > mmu lock released I think this can cause a race: vCPU 1 vCPU 2 host --------------------------------------------------------------- mark page dirty write to page treat page as not dirty add page to ring where vCPU 2 skips the clean-page slow path entirely. Paolo