From patchwork Wed Jul 31 08:46:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067333 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 587F0186E for ; Wed, 31 Jul 2019 08:47:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 461A32886E for ; Wed, 31 Jul 2019 08:47:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A9D528872; Wed, 31 Jul 2019 08:47:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CC4692886E for ; Wed, 31 Jul 2019 08:47:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B213A8E0006; Wed, 31 Jul 2019 04:47:07 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AD1DA8E0001; Wed, 31 Jul 2019 04:47:07 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99A3C8E0006; Wed, 31 Jul 2019 04:47:07 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by kanga.kvack.org (Postfix) with ESMTP id 7AC638E0001 for ; Wed, 31 Jul 2019 04:47:07 -0400 (EDT) Received: by mail-qt1-f199.google.com with SMTP id l9so60734287qtu.12 for ; Wed, 31 Jul 2019 01:47:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=hXDPuh5Kn7pFxSlRRDrUdecWVXcCQZqbNwxxDsR27Vo=; b=dBF/dDIZc1r0fqU5+365l68QjCYmcN4AQs4zZKvuxiJtqRwSh3SZC6bvSl5GubiGrj NKKw6bQxLjanraXrCs8Ly0sO6a+CtD8D1oqmST1dmeDADVDih8ZO4LcD+gsHJs6zcneH XqrYFsa58TOgy17GrSiW8VfGxG92WmXzo/V5zeZU8sqf5+tz0T1oqWzUQkr62PaWTwnl JDSyd4/kqPaq2c6Ca5vBbqKXjga1wzZU8eQLzhSZJ/Lm0R9A6uLkVEYzA25meCx30iV5 yLwc+Cf8DbNt4Wk10RgVBvZkd+uYEJrlTkFGU8OTKFompmxelSgRiR7sPHGIDauxgLDD sWYA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAWUFAjs+qwQwnD+uXtigzdgwaKBM2/X7zetPwkVn9HbGS0o6Shk l95/8c05TSQ1gSbIcXxGsSy7h18s/S+MHAf6rAfecnO9Dv/86IakAFHk978dyaHxnRwkwhCcDcl qbtkOyEXoohfh/j/ZhHYjbI2eY2rUIRMhmUt5qqPR0qlSduAAuF0/9PEk7uFjre6Ztg== X-Received: by 2002:ac8:32c8:: with SMTP id a8mr82022522qtb.47.1564562827308; Wed, 31 Jul 2019 01:47:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqzfxo65+Qb30xaHZyi6RfF6+WWXvBwGu1khpW/vgAao3FrUCdXs/2KWrTf9z8cTizO+ZbiW X-Received: by 2002:ac8:32c8:: with SMTP id a8mr82022499qtb.47.1564562826748; Wed, 31 Jul 2019 01:47:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562826; cv=none; d=google.com; s=arc-20160816; b=elZ/jwGUGoHO+Q+Hzh/mqEY74XvseAckoUPUn2dfR7bvxxz056/j081r2D14WQRzHC ZKd28Yo2uLZ6vGKLqkQdmBrNBOF9HFkCLttswmYhQ47sdPt0lAsB2ODC1gbpMT6d7O2k aJpNjdh1bpvSfuSVvbvIRE5RX9FfS24byWvFCIqyPmZ6izQVjHOhFgNl5bCA9pH5phAg fTBSA25LoHMMiK/X8KhG/khOJGNknPzDTNUhA5JtOPQvl8wFGoktTAHOhy8qFw7ookgB h7Pjzv158ZXpslJykKbpqOc8IFtmuPGvIMzV5Y3RQ6m+zFhiu1CvyF2O1p/3PEDMDcrp U0YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=hXDPuh5Kn7pFxSlRRDrUdecWVXcCQZqbNwxxDsR27Vo=; b=mQGYQrqF3CP9a/6WFJ4ZdIntEO9hYtPTefby/gQSewIQCoZrXNSOh4LZh3WfxhwFl5 eeYBjYWbsjYoA9PUMPKP9BJXZgDFSpM+koRT50mmQObUMWaGfVuG3cdllO2w63R4Hv5J Ru1lSreWQnbZWKdS/kCfme1dc9XOYohJT9VfhZqJ/D8ayufZLOW0vExEi5gGmwoja4KL 9lQv2uONw5EJKVH1gpfUt6AiYccXOjNFmaoXsVhfvVO4MrdYiwjQTonjbwVoSwxbPY41 JlFiTprHg/+aepGscdHccI9V7Gna6HcAEWKFHGQlNwEKpsuayW2qRX4FXV7QRLGSBCzo DV/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id e12si39083476qve.144.2019.07.31.01.47.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:06 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 01C3C7FDE9; Wed, 31 Jul 2019 08:47:06 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 40E1C600CC; Wed, 31 Jul 2019 08:47:02 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 1/9] vhost: don't set uaddr for invalid address Date: Wed, 31 Jul 2019 04:46:47 -0400 Message-Id: <20190731084655.7024-2-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 31 Jul 2019 08:47:06 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP We should not setup uaddr for the invalid address, otherwise we may try to pin or prefetch mapping of wrong pages. Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 0536f8526359..488380a581dc 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2082,7 +2082,8 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d, } #if VHOST_ARCH_CAN_ACCEL_UACCESS - vhost_setup_vq_uaddr(vq); + if (r == 0) + vhost_setup_vq_uaddr(vq); if (d->mm) mmu_notifier_register(&d->mmu_notifier, d->mm); From patchwork Wed Jul 31 08:46:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067335 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F2E2B186E for ; Wed, 31 Jul 2019 08:47:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DEED02886E for ; Wed, 31 Jul 2019 08:47:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D2D2B28872; Wed, 31 Jul 2019 08:47:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 570142886E for ; Wed, 31 Jul 2019 08:47:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 444548E0007; Wed, 31 Jul 2019 04:47:11 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3F42A8E0001; Wed, 31 Jul 2019 04:47:11 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30B828E0007; Wed, 31 Jul 2019 04:47:11 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by kanga.kvack.org (Postfix) with ESMTP id 1153A8E0001 for ; Wed, 31 Jul 2019 04:47:11 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id t124so57530566qkh.3 for ; Wed, 31 Jul 2019 01:47:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=ObXb8s+P+33YOBHS71fbz/1szBcAnp4vJnFUmXXP1hc=; b=SPE3P9vVR88IQoXIuslwUm5a1wcVKOHa2h1AEEw3jf1JyBWFQ91BFPU2Ze7Efrz8PK IgM1vPk03d5/zBEk4KZFamBHqSNw6fIE+yTdg/eY15KdEkMI6iYnOGzCc8qW7zj0Kfk6 5ToMwwGSSuD3JA+5DiQQ2Q8iPTbG4u6MNJnX5ZGbvM3QDhoce/WjET+vfwDmsNWM6u9B wG3z7byXQLZO+uHvoAe8KsedNvx9obfLcVfnU1PMTrhkKjiIb6QR5EBVhYsnGGuJnozd 6OHFMCFVzlkj0NoVzoKFMZZyc5XSLuuyBlZalfG3IYJRQRk25JIPXKvECs00f38ToWwD G8gw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAVpndRlAHw9QaKAgkWenUo/kaxzqp1pmgVdV4Xj2Dh7FufTBggm XncKjAmzysRmg9GtpCrAkWe1vW9NIybSQ19ETxoAEX/U3TKaY1kR5R1/nXoPhyTSVrgg3LF8MU5 9CwxcpPciWJ8ZtHxTugkjD2t9xnpjCOR2nFIOAbIMv3/wJfMDmvbjbnr8XTL73DeqNQ== X-Received: by 2002:a37:9904:: with SMTP id b4mr76507965qke.159.1564562830866; Wed, 31 Jul 2019 01:47:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqx0CCIzwzvH9fcPHuUyen6xMwyJG70NQ8WO8hBKVou7c3j/mrg26/FiSs1k7JNHWFwdmacK X-Received: by 2002:a37:9904:: with SMTP id b4mr76507915qke.159.1564562829836; Wed, 31 Jul 2019 01:47:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562829; cv=none; d=google.com; s=arc-20160816; b=TFsXymezVvlW2l1rtMcMfir0ZssaNttjxpsRSVk66GiIosrk8JY3prtd7K7PDvBuEc 3DQzRXkfI2yQEnvfgfj/JdQ1khAknhP7amvX+4+Zk4Ci5vL57cH48k0k/VsrClTlNnqq Yx0fM1YOL2NtvqNEgoDWPEr1tvHgbXMVhviWudPjn0d7O0+2juDIosgBRPWsoW6Sa/Tx W/NRnpoBRnvr48i/0jRW+f5idSuoReyL5nBcmfKhwO0nnAWT+ipPUbROtXnIsUUE5LXz My7+5PWaXwuhobDzy/99L/1XnPzm4Sx2dRNXAPfe9SecBgyZUt/5AKDnBggR6xNkqfYz T1Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=ObXb8s+P+33YOBHS71fbz/1szBcAnp4vJnFUmXXP1hc=; b=RXhCbThZcPGLBcl8Jw4sSzJaOLHu0T71xmgtY/S/yKtMys708nujWwoZlxEJh1c63B 3ORbmigvOC9iDC05f4vEfCJUFEdvj2FQawi97yijjZttDAMhr7xrguPvQ+agEW0OhbgX a2OY/b1ZpZTEyH50PuWOc2WIlTi/+thM5WqIHZ9HWXuvYMgjtbGDqHyGq2/GoX9hDqHC jgw8NxkORfrDBhIUNU/vwRfUTNNHyWD+zrSDgprd+4lA0ggO+f+PStTxpi/CEd1DU2S7 YrVr/o6LtVWKC28wvVH3qYmOJ2QR6Fz8VlX+tbKbG2MHWAh8DwmirHdGBeMnRKzb43IT jIxA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id q20si38756108qke.380.2019.07.31.01.47.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:09 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2870D307CDFC; Wed, 31 Jul 2019 08:47:09 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A39C600CC; Wed, 31 Jul 2019 08:47:06 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 2/9] vhost: validate MMU notifier registration Date: Wed, 31 Jul 2019 04:46:48 -0400 Message-Id: <20190731084655.7024-3-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Wed, 31 Jul 2019 08:47:09 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The return value of mmu_notifier_register() is not checked in vhost_vring_set_num_addr(). This will cause an out of sync between mm and MMU notifier thus a double free. To solve this, introduce a boolean flag to track whether MMU notifier is registered and only do unregistering when it was true. Reported-and-tested-by: syzbot+e58112d71f77113ddb7b@syzkaller.appspotmail.com Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 19 +++++++++++++++---- drivers/vhost/vhost.h | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 488380a581dc..17f6abea192e 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -629,6 +629,7 @@ void vhost_dev_init(struct vhost_dev *dev, dev->iov_limit = iov_limit; dev->weight = weight; dev->byte_weight = byte_weight; + dev->has_notifier = false; init_llist_head(&dev->work_list); init_waitqueue_head(&dev->wait); INIT_LIST_HEAD(&dev->read_list); @@ -730,6 +731,7 @@ long vhost_dev_set_owner(struct vhost_dev *dev) if (err) goto err_mmu_notifier; #endif + dev->has_notifier = true; return 0; @@ -959,7 +961,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev) } if (dev->mm) { #if VHOST_ARCH_CAN_ACCEL_UACCESS - mmu_notifier_unregister(&dev->mmu_notifier, dev->mm); + if (dev->has_notifier) { + mmu_notifier_unregister(&dev->mmu_notifier, + dev->mm); + dev->has_notifier = false; + } #endif mmput(dev->mm); } @@ -2064,8 +2070,10 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d, /* Unregister MMU notifer to allow invalidation callback * can access vq->uaddrs[] without holding a lock. */ - if (d->mm) + if (d->has_notifier) { mmu_notifier_unregister(&d->mmu_notifier, d->mm); + d->has_notifier = false; + } vhost_uninit_vq_maps(vq); #endif @@ -2085,8 +2093,11 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d, if (r == 0) vhost_setup_vq_uaddr(vq); - if (d->mm) - mmu_notifier_register(&d->mmu_notifier, d->mm); + if (d->mm) { + r = mmu_notifier_register(&d->mmu_notifier, d->mm); + if (!r) + d->has_notifier = true; + } #endif mutex_unlock(&vq->mutex); diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 42a8c2a13ab1..a9a2a93857d2 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -214,6 +214,7 @@ struct vhost_dev { int iov_limit; int weight; int byte_weight; + bool has_notifier; }; bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len); From patchwork Wed Jul 31 08:46:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067337 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 88F0813A0 for ; Wed, 31 Jul 2019 08:47:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 75F212886E for ; Wed, 31 Jul 2019 08:47:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 69D2828872; Wed, 31 Jul 2019 08:47:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17D4B2886E for ; Wed, 31 Jul 2019 08:47:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2E978E0008; Wed, 31 Jul 2019 04:47:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id DE19B8E0001; Wed, 31 Jul 2019 04:47:13 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCE158E0008; Wed, 31 Jul 2019 04:47:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by kanga.kvack.org (Postfix) with ESMTP id B4B448E0001 for ; Wed, 31 Jul 2019 04:47:13 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id c207so57322370qkb.11 for ; Wed, 31 Jul 2019 01:47:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=DkgRyBFTw1NOJeXBoWug970nIMArpHVJngzRV71MoTc=; b=VhP/5+cZnrUBCicfOT/Tir2pz4Eb86GmFkvtDsLEdYQCbXnGlscikOi0gJYHbad/iz PuXruzSmY0eShbbOcR7UpVJNh5fuyhSqfqP9VvWBXeQ8ElhSOQTKryRALIKkoA/ytK1a T6pQCshjmDTCGLPfm5BXx7ANf4QhHk/ODWyOEXY/b1CI+RAUvOS/4kjRDmMdCGtAPL+m thsY2tyk8L8vsOjXI+Rqo1hWvCtmdlvsy4ajRMqdFM6hcxQ3X9+Zg8M+z/jYbXUosKNe 3WwV8BirSWByEJnfIxMB+SAyt68r3m50A3JoLZ9o6Lwq6MnClIF0uAbUXOyOdbLqg8qM 1zcA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAXt9oMkQLxt9I37JxO0ze2F1OzrHo8J6FkrXz3yRfdAsGSsve4A n9IpjTEWWjatQ+yALBlaelqKBKG6i5QicIbixZgowMlRJ1rkEZuGkaRtGdwhd3gmm2jNwIbjtZI w6k99Ep1IJftSfBLhACf8DoV6anbGD4RaX+XAGrJY0nf9lhvd0EVAwpdW7JtCLvdRKg== X-Received: by 2002:ac8:6b8d:: with SMTP id z13mr83197141qts.86.1564562833520; Wed, 31 Jul 2019 01:47:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqxgG8XKPC2RPn+I4Nnxu13CYb49Z90rzYi3Fz/BtvHW5jXGjJ2TxhkEmbslFil7tcyjmJ5e X-Received: by 2002:ac8:6b8d:: with SMTP id z13mr83197128qts.86.1564562833004; Wed, 31 Jul 2019 01:47:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562833; cv=none; d=google.com; s=arc-20160816; b=XaSaju4B5rTMw4+y2eA3r13X3C14vBVYMvXqMHqMSsgNHOGy6hiqEYvvwbVzZlgPoB hrZhkr18kPlQuOUBobY0qWnIMh8XCkK54knb6qqsjUAAxkttiRuZXijL4qxZAb3tm6ue 1ZpMUbI+fHHLQ54en83H/XYhlKcysEtxeI1so/+9uFEsX/Ip8qs07isoyOW2YWSOWjCg jCDxP2elc5x1+h+0yIGwwJMq9ODOnhfM2YW2mSHsSFz74JLE305aXaEVLHgzpf5vIEyG Y/jvMUSfOkzQTGZSJsHHZtCdRKw/kvsQnQ6O8P1eU8wcTqLVQsZ4du6Otq2Yeq+0mRUi 9/gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=DkgRyBFTw1NOJeXBoWug970nIMArpHVJngzRV71MoTc=; b=FhyaimjRYoCgZdDuGVvxCi4HOIYR+PG28QSdFexkjLdOIMnYCVaJJRTifkIcpJ3dDJ +lwDxrkpoNM3OtuAapW5L5Ubng30ziKN/fI4zm6+zlFyLmZp22wRfKv3N9ZRzTnZx6ZM MrYhXxJxSaIvxcmh9T7pR6N+lQPiNEA3bNmHzD1c5EtoA99Yl9KXxSEyVxuJIVZiZSpr FDvvTE6HTzCHiJ0+W/vgOsdKIhY74Q9lZRXVTEyB5msn+4WluHyhFr4h86340TPuJZg/ s138gxyk4Zezv4tw+jP1QnM+Doc3H2dBIq0JK6iZCEBLhIZvvFEAy4hwRPLpSVrLtna5 Kk0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id 128si20297612qkf.348.2019.07.31.01.47.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:12 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4E9347FDF4; Wed, 31 Jul 2019 08:47:12 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id A079A600CC; Wed, 31 Jul 2019 08:47:09 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 3/9] vhost: fix vhost map leak Date: Wed, 31 Jul 2019 04:46:49 -0400 Message-Id: <20190731084655.7024-4-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 31 Jul 2019 08:47:12 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP We don't free map during vhost_map_unprefetch(). This means it could be leaked. Fixing by free the map. Reported-by: Michael S. Tsirkin Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 17f6abea192e..2a3154976277 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -302,9 +302,7 @@ static void vhost_vq_meta_reset(struct vhost_dev *d) static void vhost_map_unprefetch(struct vhost_map *map) { kfree(map->pages); - map->pages = NULL; - map->npages = 0; - map->addr = NULL; + kfree(map); } static void vhost_uninit_vq_maps(struct vhost_virtqueue *vq) From patchwork Wed Jul 31 08:46:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067341 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E6AC314DB for ; Wed, 31 Jul 2019 08:47:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D3B6D2886E for ; Wed, 31 Jul 2019 08:47:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C839128872; Wed, 31 Jul 2019 08:47:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B7972886E for ; Wed, 31 Jul 2019 08:47:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 666878E0009; Wed, 31 Jul 2019 04:47:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 616628E0001; Wed, 31 Jul 2019 04:47:21 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 505648E0009; Wed, 31 Jul 2019 04:47:21 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by kanga.kvack.org (Postfix) with ESMTP id 2F2268E0001 for ; Wed, 31 Jul 2019 04:47:21 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id r200so57502069qke.19 for ; Wed, 31 Jul 2019 01:47:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=QiaTlTQOPqg5IgdYAWRREFpZ9TwRpPDQdSyP4YgHR84=; b=oOPAZjZdV/JnA3rnLnv5BASWPtRM9yPgxqi3fP1puMnfs6HphWsCv8W/Vq5VvtG/W9 3j1vFleU+UZhjfEGDIQvRmzQmTY9WPILICGQqT9kj1W+J1MEtVViB19uzYDvGC0X7Ot6 DCP+CDsHzZ5xizzbkWyrJNxOX2b/ItZhNebSLKotjLXS4VAQHNeEh94WAaiCpZKCnWPX JIeNUGxkDvb0vl5KDsa55BlAIVtogIOrAg6rQa8hg6tlZQa1mCThZkgwW1iseqhEdz5i Hj4kCXvmBnH/EKbz/7mTkTO8blDo6QYti0ng5XBjV2ZZAjHEDGbL1IjDr+e+htb6HCZz 4Ndg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAWVxwyXg32LEzxOsE38T887X4D6VQmE2yZ6N11CCyKzTjoD0Bzk CPR/1lgimUXO7nMJFe8p3EOlYg1JLUi4AeNqmWUF5MTq1dHzMhWWeDHQAF0vsreMmeDomBBk8BX mtUbdUzJ32BDiTuBB8aDDif3J9jgSros01Hw6e/Fh+ehegEp08I3KfO1kE975yB+1pQ== X-Received: by 2002:a05:620a:11ac:: with SMTP id c12mr78270811qkk.232.1564562840979; Wed, 31 Jul 2019 01:47:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZLxtcM2PjRYkGUxjn7L/vtTP0Ht8rOU8oHSJToA8Z+DXhNPNwUCrX4C0GrTxCleA89OnL X-Received: by 2002:a05:620a:11ac:: with SMTP id c12mr78270786qkk.232.1564562840337; Wed, 31 Jul 2019 01:47:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562840; cv=none; d=google.com; s=arc-20160816; b=HZMeW/eHgal1ZXAa4VaipYb1OvtjDAcOJY9lL4E5DSmY36ym436rkru00GEj2n0pLw rXnj0gL5hCJGVd3ziLUHRc0F98odW2HVqAJbj7jDwzK54kGbAQNfSkhEQi5lpOnLOnv4 5azTX4bq4y/GyWy3KrQCCuiIxHeaBXhCI8opGJMbwfjzjBoi8nVKm2C0jwIws4w/nytu tMe8Gvj1VUq7FVSKGGXdIeX1m/Qy8swKGgXG6b8qSuZ5zn6GyGbfI7EYCf2oqlqtXtQD x1MWVwZ0UjaJrNqnaSI0r+VH5wEKhWhVaqS5uvyGnketuQCbWfJmvOo2hCK6ap19mfYP S23g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=QiaTlTQOPqg5IgdYAWRREFpZ9TwRpPDQdSyP4YgHR84=; b=nraJyi7a/cOHEPiNigE4A39uYYCOjQZvp+IJwBtQyIGOz47kq8rqwZ7Ld11ImTJmd3 65t1PzpvvuUwyR1eI7dpjor7bfZxjJlVI4EcP8bUIOfh/CsyukMmVMPJiMgbN2p+6sEV qgEhzlQaK3mXPfCAV6I73jeR5M9CnBbFrhlFUNwv0byQ8N3mqzI78zvaiyL5TB9RZYkB Z1Ta7sNUUvK9M7+GjBqrZr8CduxenC4yuQ5Nhrsvp4pFYEu60PPd3FFGvwQSdTtMcjea Y9sqitwgpknJcAMlaIQyBUu75/Xq3++oPZxF5mwnWh6TDzX6lCxpIuYPjl5slHAs9XP/ sPWg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id j4si36984043qkm.152.2019.07.31.01.47.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:20 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9BF10300CA4E; Wed, 31 Jul 2019 08:47:19 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id E79D3600CC; Wed, 31 Jul 2019 08:47:12 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 4/9] vhost: reset invalidate_count in vhost_set_vring_num_addr() Date: Wed, 31 Jul 2019 04:46:50 -0400 Message-Id: <20190731084655.7024-5-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Wed, 31 Jul 2019 08:47:19 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The vhost_set_vring_num_addr() could be called in the middle of invalidate_range_start() and invalidate_range_end(). If we don't reset invalidate_count after the un-registering of MMU notifier, the invalidate_cont will run out of sync (e.g never reach zero). This will in fact disable the fast accessor path. Fixing by reset the count to zero. Reported-by: Michael S. Tsirkin Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 2a3154976277..2a7217c33668 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2073,6 +2073,10 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d, d->has_notifier = false; } + /* reset invalidate_count in case we are in the middle of + * invalidate_start() and invalidate_end(). + */ + vq->invalidate_count = 0; vhost_uninit_vq_maps(vq); #endif From patchwork Wed Jul 31 08:46:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067347 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7BA1714DB for ; Wed, 31 Jul 2019 08:47:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A7762886E for ; Wed, 31 Jul 2019 08:47:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5E98528872; Wed, 31 Jul 2019 08:47:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B5732886E for ; Wed, 31 Jul 2019 08:47:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D2AF8E000A; Wed, 31 Jul 2019 04:47:31 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 182D78E0001; Wed, 31 Jul 2019 04:47:31 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 098168E000A; Wed, 31 Jul 2019 04:47:31 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by kanga.kvack.org (Postfix) with ESMTP id DC1448E0001 for ; Wed, 31 Jul 2019 04:47:30 -0400 (EDT) Received: by mail-qk1-f198.google.com with SMTP id c1so57305508qkl.7 for ; Wed, 31 Jul 2019 01:47:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=JzO6kN8vx263crDqN9GLB46XC0hjzWSfPRcXHfgmC3U=; b=FShBdDSLLG5aiKYXDH1uzx8TUjxHbvOpccwFuZ6ieuqVqGhsdu0x4/uxz8kUZSLOIs tMd9OuT7Fqlv9ddzy42xuxDP9NFzRxOTOxyXWJUf/LIOtqd5Z6380vIfZH0cqRs8EVoR 2tOau1NFx7MWbxsgR+RcPd66oCuSD4Hc3aqba+OJGBl/ZvG+PFJLLZBtZwIXGnTszutA mB3W6apIeTf5H4aMHLSE4qjhMctzuVuQ5QDchvI/GjrbFTW26/amZoQx76/sWGffEbl4 9FNLZoUPdoqCNLGDtL1ygDvFVZFr7randWsXnvUdBw1uPgDNvm1stzpgqBQ/Al9INn7k OI0Q== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAWCif6rGHHColi9zZP7/BRWSMQMcDEUpCs6SkH9IGEQ/CJ5Hmfb 2YTJeyQOGYxirvp4cnv850V2wBlUTrI4aEW8vo0giGicehy5psyo0k9DrAY35GZtHc2sEN1nocf gehX/zcpX6sM1+P0Qbe87poPGNzvMY3FrtDFLF3ZDObwOAYILkbghgfg2ApjNitC/2A== X-Received: by 2002:ae9:dfc3:: with SMTP id t186mr76155753qkf.461.1564562850629; Wed, 31 Jul 2019 01:47:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqz9DrBVQbJBAg0uWc8qZTOTIo3JmHWsOcXoI184a5YAKV7QnHcTBIuirjjElPzu8ffOH78y X-Received: by 2002:ae9:dfc3:: with SMTP id t186mr76155718qkf.461.1564562849710; Wed, 31 Jul 2019 01:47:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562849; cv=none; d=google.com; s=arc-20160816; b=WzEwp6OoNZS+BM0Gog4HXjov8Tayo4YR/mK6UinGe0Zx2DxXCosWEXL+MjSwBKgFLo eT8FKScM78G2BfUJJgC5IdF42agBV68EFTIJDVV2ksXFTwPQwkeDMYSecvjeX6v2Y5zm wQYnali5YwusRFEsMCelLQBcV4X1BImXHtuEf0goFl1JJcYbJUsWA8Gl5npwElh3KECG Pa5Eoml7oR+7xBOs9JZXZURz6x6hL7xuZzV0jAsVS/apt63k9+iZo8CRufXMOuyQ7jWl fqorjzc0+Wr6ikjH+TTFUjzBPSx0HIpfYUFHHOkJIB2EWCRXIuo89doLO3aIBtLNhMUK pqTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=JzO6kN8vx263crDqN9GLB46XC0hjzWSfPRcXHfgmC3U=; b=EYQFy8cEm6cMZQ/bBJp+mTbsvghMGqX9Z+a3VdOmmAEDngXuPNgEXiZdvt4r+kiZPb 5NMWEysmu2L305hs1OQ8XO7xUSjSliPkzs5c/Uh4coRnQOtY+B1cPRYDXlCojMnnf3IU z6qGS3YWGXIZlmPrs/ugGuManoP2IWAiqOG9uy0jYBjG8CbtwrAIM/Jk4QFARfjfHCIr OSKmq34nN0K67fB/VYXJgBH3TyWasBEolSIuoHZzOSTGDgLrAuRnFqdbFtOGJ0+04cog PyG7vVCFIzcRWzUyzgAl898O22dfj/zlGDMhYMSGp1XnrlHt+fmLDm/HIepb21cR7iIv X4eA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id a13si224403qvh.92.2019.07.31.01.47.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:29 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EE70030833B0; Wed, 31 Jul 2019 08:47:28 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A4D6600CC; Wed, 31 Jul 2019 08:47:19 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 5/9] vhost: mark dirty pages during map uninit Date: Wed, 31 Jul 2019 04:46:51 -0400 Message-Id: <20190731084655.7024-6-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Wed, 31 Jul 2019 08:47:29 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP We don't mark dirty pages if the map was teared down outside MMU notifier. This will lead untracked dirty pages. Fixing by marking dirty pages during map uninit. Reported-by: Michael S. Tsirkin Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 2a7217c33668..c12cdadb0855 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -305,6 +305,18 @@ static void vhost_map_unprefetch(struct vhost_map *map) kfree(map); } +static void vhost_set_map_dirty(struct vhost_virtqueue *vq, + struct vhost_map *map, int index) +{ + struct vhost_uaddr *uaddr = &vq->uaddrs[index]; + int i; + + if (uaddr->write) { + for (i = 0; i < map->npages; i++) + set_page_dirty(map->pages[i]); + } +} + static void vhost_uninit_vq_maps(struct vhost_virtqueue *vq) { struct vhost_map *map[VHOST_NUM_ADDRS]; @@ -314,8 +326,10 @@ static void vhost_uninit_vq_maps(struct vhost_virtqueue *vq) for (i = 0; i < VHOST_NUM_ADDRS; i++) { map[i] = rcu_dereference_protected(vq->maps[i], lockdep_is_held(&vq->mmu_lock)); - if (map[i]) + if (map[i]) { + vhost_set_map_dirty(vq, map[i], i); rcu_assign_pointer(vq->maps[i], NULL); + } } spin_unlock(&vq->mmu_lock); @@ -353,7 +367,6 @@ static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, { struct vhost_uaddr *uaddr = &vq->uaddrs[index]; struct vhost_map *map; - int i; if (!vhost_map_range_overlap(uaddr, start, end)) return; @@ -364,10 +377,7 @@ static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, map = rcu_dereference_protected(vq->maps[index], lockdep_is_held(&vq->mmu_lock)); if (map) { - if (uaddr->write) { - for (i = 0; i < map->npages; i++) - set_page_dirty(map->pages[i]); - } + vhost_set_map_dirty(vq, map, index); rcu_assign_pointer(vq->maps[index], NULL); } spin_unlock(&vq->mmu_lock); From patchwork Wed Jul 31 08:46:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067349 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4FD3E13A0 for ; Wed, 31 Jul 2019 08:47:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E0592886E for ; Wed, 31 Jul 2019 08:47:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3285828872; Wed, 31 Jul 2019 08:47:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D26FE2886E for ; Wed, 31 Jul 2019 08:47:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB96F8E000B; Wed, 31 Jul 2019 04:47:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D8FF78E0001; Wed, 31 Jul 2019 04:47:37 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7FF18E000B; Wed, 31 Jul 2019 04:47:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by kanga.kvack.org (Postfix) with ESMTP id A97E68E0001 for ; Wed, 31 Jul 2019 04:47:37 -0400 (EDT) Received: by mail-qk1-f198.google.com with SMTP id v4so57329721qkj.10 for ; Wed, 31 Jul 2019 01:47:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=B1luxMJLkB4TGmpB4Z6oIauGJZZAFOoy1m4euQKOEm4=; b=YMfFMS84zmpKGh712lf2SvZZ9ffGnJdyn3oJtFyrNuOJPIyG11ElgHBtASBeJoLVV2 ZKMe80zf7wvNm9q9nM90dX9fbrx0SWQQXXbLcKHCkBC55qW+jAfYLYci5ScyVvpGXtSV 0swM1D2Zpju8TDHcpSmAIUGba+d85SVZYwmlbeMCYO89SrBbbXuQxMYy0AglegG8AY8G uNxLz89G/jZgcP0VJVYJXbEEYHpgwcvPDOrbW2Li1vBLg3jWHNAEgr+jncia+p+SpSq8 7AuhT21ahph1lW9QbF/7b4OT3r/74cS00NQPaGGdKmcdaIwB3RBkawGLqYl9pUkxb33B GS7A== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAWQ3A5txBLtW1TulXtyt0AfIMVQXOt1a9kein/lZYXVuRazMEKe yqD1/xJ3FtPxTlAhVHubto1nyL1SNQn1LDDTfhCvzsRv812wJecJYYbJK1kQqkAvXORPfUTiV73 nuMGkr4KktSnE5UDgcEgU1dP8S/wOvl3P4V+oFA2zqHLnPLalOkUYnPEuSgHoHy8RXA== X-Received: by 2002:ac8:750b:: with SMTP id u11mr83595988qtq.23.1564562857488; Wed, 31 Jul 2019 01:47:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqx5sWjiUd1C+sCC4V09L2A+3dCmClAe9xUEjA5CMYtnSFdGRzbqh9MQnw8b874OywyyDbuE X-Received: by 2002:ac8:750b:: with SMTP id u11mr83595961qtq.23.1564562856965; Wed, 31 Jul 2019 01:47:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562856; cv=none; d=google.com; s=arc-20160816; b=P4NEVPtf3pIOL5Fl5+puww5IP4amRRe0rFPVYJ7us1wF3ObAvy/MRknL/L2BJ3A2HW FczRyfikzWm5SQqcuH4C4Aa+AoQJLz2+QE/oBMawUocoRQT5UJFnjzn+WrjXiEkvLXWC 2whfFOX3/WbXfvsa0cFO2QVsMrAKq7/+tkIhvIDpL4+m91HkVpNNjTVmxNWfNsBeLumE hAVnXYiZ+tr1TPFHO/MLnkAEVe+HFtj2g18AKgpAJmzEEKzIqtYpPnaB6/XlZjbBRxVZ bDvSsCUhbDqlPJvVBlhwC/ZTsiw0Cvn0OGU1AxHrA/tFHdX/Yywsxj3tBiOHHW7AkgAE iqUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=B1luxMJLkB4TGmpB4Z6oIauGJZZAFOoy1m4euQKOEm4=; b=cWA5ySLKN3vBXEVdvM0aCqz27YNIguZWYJ1vXSI2iPb+nukn8qsJbWJ6122xkWSiMq +k4mpEdygPsuwZnW/OZTnwkNC1gUtd9km9KH8hfnpP91FPcLYefFvRrhHwYGgqJPrNj+ HK8UevPi/E4EHBdu9v23rCBBCM09Sfw41xhsDEfdBYWn0Ym/mUoPjSxwoVVlA1bxmtPR V1XM6H/H63yFIKg3YV/3pfAa2kFq7a6w5/7mAtgQHIuQCUy+a1dXRX2jxHIFK/tWeNAD QmGZ7XelLVLbuy/IKMYbAmiFesi9cSuo3EBZFXPPJ4m3Br/sEFvPwUkj3z8isBr4A8H5 3xbg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id w1si36331643qkd.138.2019.07.31.01.47.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:36 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 31CC531628E3; Wed, 31 Jul 2019 08:47:36 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 62DE06012E; Wed, 31 Jul 2019 08:47:29 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 6/9] vhost: don't do synchronize_rcu() in vhost_uninit_vq_maps() Date: Wed, 31 Jul 2019 04:46:52 -0400 Message-Id: <20190731084655.7024-7-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 31 Jul 2019 08:47:36 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP There's no need for RCU synchronization in vhost_uninit_vq_maps() since we've already serialized with readers (memory accessors). This also avoid the possible userspace DOS through ioctl() because of the possible high latency caused by synchronize_rcu(). Reported-by: Michael S. Tsirkin Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index c12cdadb0855..cfc11f9ed9c9 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -333,7 +333,9 @@ static void vhost_uninit_vq_maps(struct vhost_virtqueue *vq) } spin_unlock(&vq->mmu_lock); - synchronize_rcu(); + /* No need for synchronize_rcu() or kfree_rcu() since we are + * serialized with memory accessors (e.g vq mutex held). + */ for (i = 0; i < VHOST_NUM_ADDRS; i++) if (map[i]) From patchwork Wed Jul 31 08:46:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067353 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E8F9112C for ; Wed, 31 Jul 2019 08:47:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2B2342886F for ; Wed, 31 Jul 2019 08:47:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EBF628874; Wed, 31 Jul 2019 08:47:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 31B6E2886F for ; Wed, 31 Jul 2019 08:47:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F4848E000C; Wed, 31 Jul 2019 04:47:46 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3A5588E0001; Wed, 31 Jul 2019 04:47:46 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29FB78E000C; Wed, 31 Jul 2019 04:47:46 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by kanga.kvack.org (Postfix) with ESMTP id 085F18E0001 for ; Wed, 31 Jul 2019 04:47:46 -0400 (EDT) Received: by mail-qt1-f199.google.com with SMTP id f28so60968318qtg.2 for ; Wed, 31 Jul 2019 01:47:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=MJjEAWeRmGTtnyuYDhExoqlV/hpyLXzZwUX1r1E9si0=; b=o4jT5wwwkzQR1Hm37x8xARk9RzMdMS8KoBKewbmgs0BydwUqOG/IbIFskp95eHOURP qYPgU21eoGuPEUjRkD8oLtYDs6c3qWRSozrahcOJq6k1vIax4B0kdVapS7xuc4S8+87U 1K6udUM5UYPjilNp/H5qLi3xp/CAKXa7jYZpilQQ3yUj/oNAQ43rXayLDCFMmTAzPkoM OumDbUKzo2BnTf8Qcb7TgdGbnHK0jB2+Ixy7U8AzGuqxbCg561agQW1EfiNbkroCg/13 S15I93GTH/WrECRqISDGgm1fTpc2A3S8Y01nCKZidmDHycoSQrD9Cl5/cjUqyuJ2aYhh pj9w== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAWwt3p1d5fZ8kN+swbd5JgNx9tztEYMuGi7BpanGPqSP5fS0aYC vMEpWutcGRib2ejRrMNSolH6HWIpDZ4jz/3NMvtNJxoLh5dPpBvlyTDKoPADMv3LYNsBF7gT8qy vzIs+PDfZmueq+rp91ZKyoKzIyKwRdYSOE5HGPBbcsKIhpU18yZ2JH3seexMQtmAdGw== X-Received: by 2002:a37:47d1:: with SMTP id u200mr76519784qka.21.1564562865771; Wed, 31 Jul 2019 01:47:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqzK+Rik1LFHqsJqmqyx0qG/KREVZfcTKMA5KKM8ToXYWDtrtAvj8EpgMuiYdyJV1Qr2dNHW X-Received: by 2002:a37:47d1:: with SMTP id u200mr76519718qka.21.1564562864269; Wed, 31 Jul 2019 01:47:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562864; cv=none; d=google.com; s=arc-20160816; b=TvmB1ehMZXaRpfDIt+v9+O1CIXslkYr0L2e63m1B5KsMnCrG99aa34jabZWxi5rPP6 q5pTeUwpKwuhQg5ddYpk7Ifo9m8YZsWJG4l9ED0vA1B+PE4JMp9//q8Y6dhf74PhMusE B12soNXq2x8N5FTI8oPX4rGGEFafLD+iorKOQvVlUPrOp4k0LkA5SZxudkv939ywhDOw D5ql68RxBqVUZKW3uUP7Ltqe3V3JNrqsc0g9XMLBn26HVDdy+s7DxTplQ/T3HmYPFU0I hCyMHUJjH0IeWeQ7l/5p/wc+KPwMBb5wUuIV7SftxyDJZI9AKX03M/ainK7VqlLm1bD7 U/CQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=MJjEAWeRmGTtnyuYDhExoqlV/hpyLXzZwUX1r1E9si0=; b=L3fFx1RhhF2ecAf8wNAVyNMey9pjQstq9kqkpU7FI7Zk4lWwnV4GMH8YK4MVmwT4NR +UNVGZjZvSw8oo9hdJYFywtHmBkIfMwIacels2407YNTAoNeXcnKnH450mL5NEmdjNbQ +nHsM+u4gq1/kNsSCNtCKIsYSmdRbtUBrbUxZn0352m5ioJgvYyKbhh7mvgIBza6VJ3l w6BDrl6YXesGbIOJo/6GaTJdOwosQFT3DiFp7Itk4ZPLrBhMYABsDw2WkWR0VSSG7tTR vnDXw87XhzRwtdbZbMBMVXmNigEbhz3nS2GuAjRr05l2AYCIPdkS6y+wLezG2OJP/6cd 0R1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id i62si38853352qke.115.2019.07.31.01.47.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:44 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 73C3D356D2; Wed, 31 Jul 2019 08:47:43 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9CBDB600D1; Wed, 31 Jul 2019 08:47:36 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 7/9] vhost: do not use RCU to synchronize MMU notifier with worker Date: Wed, 31 Jul 2019 04:46:53 -0400 Message-Id: <20190731084655.7024-8-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 31 Jul 2019 08:47:43 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP We used to use RCU to synchronize MMU notifier with worker. This leads calling synchronize_rcu() in invalidate_range_start(). But on a busy system, there would be many factors that may slow down the synchronize_rcu() which makes it unsuitable to be called in MMU notifier. A solution is SRCU but its overhead is obvious with the expensive full memory barrier. Another choice is to use seqlock, but it doesn't provide a synchronization method between readers and writers. The last choice is to use vq mutex, but it need to deal with the worst case that MMU notifier must be blocked and wait for the finish of swap in. So this patch switches use a counter to track whether or not the map was used. The counter was increased when vq try to start or finish uses the map. This means, when it was even, we're sure there's no readers and MMU notifier is synchronized. When it was odd, it means there's a reader we need to wait it to be even again then we are synchronized. To avoid full memory barrier, store_release + load_acquire on the counter is used. Consider the read critical section is pretty small the synchronization should be done very fast. Note the patch lead about 3% PPS dropping. Reported-by: Michael S. Tsirkin Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 145 ++++++++++++++++++++++++++---------------- drivers/vhost/vhost.h | 7 +- 2 files changed, 94 insertions(+), 58 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index cfc11f9ed9c9..db2c81cb1e90 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -324,17 +324,16 @@ static void vhost_uninit_vq_maps(struct vhost_virtqueue *vq) spin_lock(&vq->mmu_lock); for (i = 0; i < VHOST_NUM_ADDRS; i++) { - map[i] = rcu_dereference_protected(vq->maps[i], - lockdep_is_held(&vq->mmu_lock)); + map[i] = vq->maps[i]; if (map[i]) { vhost_set_map_dirty(vq, map[i], i); - rcu_assign_pointer(vq->maps[i], NULL); + vq->maps[i] = NULL; } } spin_unlock(&vq->mmu_lock); - /* No need for synchronize_rcu() or kfree_rcu() since we are - * serialized with memory accessors (e.g vq mutex held). + /* No need for synchronization since we are serialized with + * memory accessors (e.g vq mutex held). */ for (i = 0; i < VHOST_NUM_ADDRS; i++) @@ -362,6 +361,44 @@ static bool vhost_map_range_overlap(struct vhost_uaddr *uaddr, return !(end < uaddr->uaddr || start > uaddr->uaddr - 1 + uaddr->size); } +static void inline vhost_vq_access_map_begin(struct vhost_virtqueue *vq) +{ + int ref = READ_ONCE(vq->ref); + + smp_store_release(&vq->ref, ref + 1); + /* Make sure ref counter is visible before accessing the map */ + smp_load_acquire(&vq->ref); +} + +static void inline vhost_vq_access_map_end(struct vhost_virtqueue *vq) +{ + int ref = READ_ONCE(vq->ref); + + /* Make sure vq access is done before increasing ref counter */ + smp_store_release(&vq->ref, ref + 1); +} + +static void inline vhost_vq_sync_access(struct vhost_virtqueue *vq) +{ + int ref; + + /* Make sure map change was done before checking ref counter */ + smp_mb(); + + ref = READ_ONCE(vq->ref); + if (ref & 0x1) { + /* When ref change, we are sure no reader can see + * previous map */ + while (READ_ONCE(vq->ref) == ref) { + set_current_state(TASK_RUNNING); + schedule(); + } + } + /* Make sure ref counter was checked before any other + * operations that was dene on map. */ + smp_mb(); +} + static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, int index, unsigned long start, @@ -376,16 +413,15 @@ static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, spin_lock(&vq->mmu_lock); ++vq->invalidate_count; - map = rcu_dereference_protected(vq->maps[index], - lockdep_is_held(&vq->mmu_lock)); + map = vq->maps[index]; if (map) { vhost_set_map_dirty(vq, map, index); - rcu_assign_pointer(vq->maps[index], NULL); + vq->maps[index] = NULL; } spin_unlock(&vq->mmu_lock); if (map) { - synchronize_rcu(); + vhost_vq_sync_access(vq); vhost_map_unprefetch(map); } } @@ -457,7 +493,7 @@ static void vhost_init_maps(struct vhost_dev *dev) for (i = 0; i < dev->nvqs; ++i) { vq = dev->vqs[i]; for (j = 0; j < VHOST_NUM_ADDRS; j++) - RCU_INIT_POINTER(vq->maps[j], NULL); + vq->maps[j] = NULL; } } #endif @@ -655,6 +691,7 @@ void vhost_dev_init(struct vhost_dev *dev, vq->indirect = NULL; vq->heads = NULL; vq->dev = dev; + vq->ref = 0; mutex_init(&vq->mutex); spin_lock_init(&vq->mmu_lock); vhost_vq_reset(dev, vq); @@ -921,7 +958,7 @@ static int vhost_map_prefetch(struct vhost_virtqueue *vq, map->npages = npages; map->pages = pages; - rcu_assign_pointer(vq->maps[index], map); + vq->maps[index] = map; /* No need for a synchronize_rcu(). This function should be * called by dev->worker so we are serialized with all * readers. @@ -1216,18 +1253,18 @@ static inline int vhost_put_avail_event(struct vhost_virtqueue *vq) struct vring_used *used; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_USED]); + map = vq->maps[VHOST_ADDR_USED]; if (likely(map)) { used = map->addr; *((__virtio16 *)&used->ring[vq->num]) = cpu_to_vhost16(vq, vq->avail_idx); - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1245,18 +1282,18 @@ static inline int vhost_put_used(struct vhost_virtqueue *vq, size_t size; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_USED]); + map = vq->maps[VHOST_ADDR_USED]; if (likely(map)) { used = map->addr; size = count * sizeof(*head); memcpy(used->ring + idx, head, size); - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1272,17 +1309,17 @@ static inline int vhost_put_used_flags(struct vhost_virtqueue *vq) struct vring_used *used; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_USED]); + map = vq->maps[VHOST_ADDR_USED]; if (likely(map)) { used = map->addr; used->flags = cpu_to_vhost16(vq, vq->used_flags); - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1298,17 +1335,17 @@ static inline int vhost_put_used_idx(struct vhost_virtqueue *vq) struct vring_used *used; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_USED]); + map = vq->maps[VHOST_ADDR_USED]; if (likely(map)) { used = map->addr; used->idx = cpu_to_vhost16(vq, vq->last_used_idx); - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1362,17 +1399,17 @@ static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq, struct vring_avail *avail; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]); + map = vq->maps[VHOST_ADDR_AVAIL]; if (likely(map)) { avail = map->addr; *idx = avail->idx; - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1387,17 +1424,17 @@ static inline int vhost_get_avail_head(struct vhost_virtqueue *vq, struct vring_avail *avail; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]); + map = vq->maps[VHOST_ADDR_AVAIL]; if (likely(map)) { avail = map->addr; *head = avail->ring[idx & (vq->num - 1)]; - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1413,17 +1450,17 @@ static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq, struct vring_avail *avail; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]); + map = vq->maps[VHOST_ADDR_AVAIL]; if (likely(map)) { avail = map->addr; *flags = avail->flags; - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1438,15 +1475,15 @@ static inline int vhost_get_used_event(struct vhost_virtqueue *vq, struct vring_avail *avail; if (!vq->iotlb) { - rcu_read_lock(); - map = rcu_dereference(vq->maps[VHOST_ADDR_AVAIL]); + vhost_vq_access_map_begin(vq); + map = vq->maps[VHOST_ADDR_AVAIL]; if (likely(map)) { avail = map->addr; *event = (__virtio16)avail->ring[vq->num]; - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1461,17 +1498,17 @@ static inline int vhost_get_used_idx(struct vhost_virtqueue *vq, struct vring_used *used; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_USED]); + map = vq->maps[VHOST_ADDR_USED]; if (likely(map)) { used = map->addr; *idx = used->idx; - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1486,17 +1523,17 @@ static inline int vhost_get_desc(struct vhost_virtqueue *vq, struct vring_desc *d; if (!vq->iotlb) { - rcu_read_lock(); + vhost_vq_access_map_begin(vq); - map = rcu_dereference(vq->maps[VHOST_ADDR_DESC]); + map = vq->maps[VHOST_ADDR_DESC]; if (likely(map)) { d = map->addr; *desc = *(d + idx); - rcu_read_unlock(); + vhost_vq_access_map_end(vq); return 0; } - rcu_read_unlock(); + vhost_vq_access_map_end(vq); } #endif @@ -1843,13 +1880,11 @@ static bool iotlb_access_ok(struct vhost_virtqueue *vq, #if VHOST_ARCH_CAN_ACCEL_UACCESS static void vhost_vq_map_prefetch(struct vhost_virtqueue *vq) { - struct vhost_map __rcu *map; + struct vhost_map *map; int i; for (i = 0; i < VHOST_NUM_ADDRS; i++) { - rcu_read_lock(); - map = rcu_dereference(vq->maps[i]); - rcu_read_unlock(); + map = vq->maps[i]; if (unlikely(!map)) vhost_map_prefetch(vq, i); } diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index a9a2a93857d2..f9e9558a529d 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -115,16 +115,17 @@ struct vhost_virtqueue { #if VHOST_ARCH_CAN_ACCEL_UACCESS /* Read by memory accessors, modified by meta data * prefetching, MMU notifier and vring ioctl(). - * Synchonrized through mmu_lock (writers) and RCU (writers - * and readers). + * Synchonrized through mmu_lock (writers) and ref counters, + * see vhost_vq_access_map_begin()/vhost_vq_access_map_end(). */ - struct vhost_map __rcu *maps[VHOST_NUM_ADDRS]; + struct vhost_map *maps[VHOST_NUM_ADDRS]; /* Read by MMU notifier, modified by vring ioctl(), * synchronized through MMU notifier * registering/unregistering. */ struct vhost_uaddr uaddrs[VHOST_NUM_ADDRS]; #endif + int ref; const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS]; struct file *kick; From patchwork Wed Jul 31 08:46:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067359 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1BF7B112C for ; Wed, 31 Jul 2019 08:47:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 084A42886E for ; Wed, 31 Jul 2019 08:47:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F097128872; Wed, 31 Jul 2019 08:47:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F4002886E for ; Wed, 31 Jul 2019 08:47:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A84258E000D; Wed, 31 Jul 2019 04:47:54 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A34DD8E0001; Wed, 31 Jul 2019 04:47:54 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94BAB8E000D; Wed, 31 Jul 2019 04:47:54 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by kanga.kvack.org (Postfix) with ESMTP id 76E638E0001 for ; Wed, 31 Jul 2019 04:47:54 -0400 (EDT) Received: by mail-qt1-f199.google.com with SMTP id g30so60784244qtm.17 for ; Wed, 31 Jul 2019 01:47:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=4wTbOdmhrqXSYLdOwV661kuyaF4EI1X+WzfX+kFYoYc=; b=fHTQcWB144K/DR+ZmX36XlFTXSSpfq4/XDyAbayL/SwSrMlTRMXNilqv4OU11CPJqp ShQTExDqCQa4T6EJ37bmpD2HLEXdET/8k/2gCXWwcqj3fx+w7LjvTXv6PDnkGnwq4cAD bUjfKhcfnuY6ef6Ihe3X6WPAd6l/McaRvsD7QuxFtQcslPJZGjO3x1iW9SyBbxDVOLfg lmMjud3WOhiBzRCFk1wzKDWu+KABz++jJijMXsnFORqd8P+VedBtD1efve6nZG5ATVmK qtbBlM1OZfRsKUaM+wH7UBbMn//n6tln0muEIWHyfSKIN6UHmxlAvxEnPjK/X1kPsitK mB8A== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAXdKjlRIAFvUjYHnhMrZVmFBB6y5v26lrH5HdvTPrJmdH5t7qPx 5sN5RW6bSc9mX6gMMQnRo5cebfntW/CRBzjxf3SCl0WRr1s114goged1MIZOcSdBWECH9SyIgS0 I/pcoC7AD0gsVnxPW5V5Gk+sTX6qGVjHRlZIbxUf5X+8aEy3JsJqn5DwasXyaSFAinA== X-Received: by 2002:a0c:d2da:: with SMTP id x26mr88105839qvh.51.1564562874288; Wed, 31 Jul 2019 01:47:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqyi0DuvC2rSPAe0PjcOgZWzqI4Z1oW4MOa912ShoJS2ZU9afVYcanIP0ASvmDV2ULODhFwJ X-Received: by 2002:a0c:d2da:: with SMTP id x26mr88105818qvh.51.1564562873821; Wed, 31 Jul 2019 01:47:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562873; cv=none; d=google.com; s=arc-20160816; b=y+704TY+QTAd8iE58Ooej36c54Vei3SW7m7l7LzB+l8euPeiLx4NNuw6fVIYwo0+Wz ywznrbELu4rsSQeS+HRLPftr+ZYjpEFDYuSE9h/VdtGBOKhfxv9yUqP2eOP8Uvl6iHNZ EGxVtBKaiIpxORcmS/3zoHKJuC2+3Qeups6huMj9vF2pk9pJtbRMwEsX/6DbG+SQ2pO8 T9Ga2FUEaigggzv3Qpuf8/5OEubeeBdO+t7n+5CCRmCJK+QouSX5H1b2XL4xmuo9oiu3 6rBPJs6ZnIVjs3LNNMVPOZTsUfT6Ro2EdK5O29sTcajvNdmW4C2dxUZ3F/QYwElkc1Ry /pFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=4wTbOdmhrqXSYLdOwV661kuyaF4EI1X+WzfX+kFYoYc=; b=Ov1fwYwc0CgrXsVnRnfnnqfUo7U5UGs9qF85+dotF0+2VPn12ryOKP8iLm8w3cgp/h 1ehGrweM4jy2sfUkOgu80O5yPAVfVz3EmV6OiffNqHdMneeX0NgmT/18TMdt6FYN0GXb rMqMfdfs0QYRoMqSUsDVlosfsQnzxS9AGbJHy012xK8k2T4HukbfSFZElVcrEmaZ9+cC FnlVZdNBVAW6kMFBf6BAw9NKx+CktTLl8wTYH8KB8gbCZKvuZpxi6iairw3GyAsfZcO/ z5wC9pIY54dKHOg2HZRO7ecQcBill43rnaa47uSNEaKMSmVyI4fcSF2otD2Ij7rxPdCr skvA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id q20si38756847qke.380.2019.07.31.01.47.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:47:53 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0D5CC30860A7; Wed, 31 Jul 2019 08:47:53 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 40899600F8; Wed, 31 Jul 2019 08:47:43 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 8/9] vhost: correctly set dirty pages in MMU notifiers callback Date: Wed, 31 Jul 2019 04:46:54 -0400 Message-Id: <20190731084655.7024-9-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Wed, 31 Jul 2019 08:47:53 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP We need make sure there's no reference on the map before trying to mark set dirty pages. Reported-by: Michael S. Tsirkin Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index db2c81cb1e90..fc2da8a0c671 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -414,14 +414,13 @@ static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, ++vq->invalidate_count; map = vq->maps[index]; - if (map) { - vhost_set_map_dirty(vq, map, index); + if (map) vq->maps[index] = NULL; - } spin_unlock(&vq->mmu_lock); if (map) { vhost_vq_sync_access(vq); + vhost_set_map_dirty(vq, map, index); vhost_map_unprefetch(map); } } From patchwork Wed Jul 31 08:46:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 11067363 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C903F13A0 for ; Wed, 31 Jul 2019 08:48:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6E422886E for ; Wed, 31 Jul 2019 08:48:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AB09828874; Wed, 31 Jul 2019 08:48:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3ABB02886E for ; Wed, 31 Jul 2019 08:48:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F72D8E000E; Wed, 31 Jul 2019 04:48:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3A4F28E0001; Wed, 31 Jul 2019 04:48:02 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 292F28E000E; Wed, 31 Jul 2019 04:48:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by kanga.kvack.org (Postfix) with ESMTP id 082B08E0001 for ; Wed, 31 Jul 2019 04:48:02 -0400 (EDT) Received: by mail-qk1-f198.google.com with SMTP id c79so57555239qkg.13 for ; Wed, 31 Jul 2019 01:48:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=f3Dgp0rISQbLBy1W6JCGeOGgAgYgofB8EtL0G3nJn5I=; b=AtQn+Cch0tO9iPi50leQ7r7UDcO/JFPefH+vWWE35WX8Xm3YQc2Eq51GvZECBdPAe0 GQySMBC6hdmm+fgIbVrTHSKjN3UrKfLNCTEN4+hk09xjtfp1Cnh4KtRZCoi7JPjfjDE1 vAfm8WYwnHgVuH62iTa89m5VG7sxHy3PZz+8E/9xTq+2tLDuOcs08nrAUsfZU8n6nb5E feOeBw2IR8N13ShY/S6kQmLzcFYIkaAD0mREL+nbr60f3GhWfsM6lKFUtEbCmgF77rG2 AhK/jArUdoyE07uok+polhdoH7Ov8Vlgmp4kfxkceVRJEeOR2TEh67XJv7CKtGz9RPco WEgw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: APjAAAUhPOQyWJHBpaLHDSJ0zAEVLc7XPPzJfxkQdL00+tza7a3OKVHN CfOk4mcrxiOxzxkl/RTJGBdhYNSUqaN3gbiPDBlYgGKoYGIfdo03g2Tc8aEit8Eqj0IOmiYKV2h by0a2+Wm/7hIJBSAaelcINGpssV1CpPRfr4VWVByG276iYMRNBD1WoRac+zBKJQb0Kg== X-Received: by 2002:a37:9c88:: with SMTP id f130mr78368388qke.494.1564562881816; Wed, 31 Jul 2019 01:48:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqyGK2cyQkvPdQdwqSd3LRO1ZL9IxZHbxzlsF51O/uFpzqb8zYKrcCypYJQUJvg+K73sRsXQ X-Received: by 2002:a37:9c88:: with SMTP id f130mr78368358qke.494.1564562881220; Wed, 31 Jul 2019 01:48:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564562881; cv=none; d=google.com; s=arc-20160816; b=zsdA0/6B2DuzCbTp6o2CdEcZjW02G1HsdvL8QewYJM24WTZji6GpTrsD66VmG3NAnd Rj1JpW2iDiMlmpTf/SYOeTEs/rRuu++Uihbz/F5uCC9UdiEX94Iv9gENK/CaX3vyIj4f ydAHfwAwv2woyydO/lzfaK/6XuTQQsK3HpWOSErsIi/Aj3IVchvidDyhxHFQy8qTUcEF E+8znb5X7+rRmucQiXZoLiVnych2P6kkAKHPatmOjyW5nxI9OgVnKE/26lEHWg+cOwDh 9+4JW99pZdSYFh5j2T1z0YJqEIfr8ahpTeenIe0Pu+915ClYFNZrC9v71aJcJdVsD2aA Tphw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=f3Dgp0rISQbLBy1W6JCGeOGgAgYgofB8EtL0G3nJn5I=; b=vpN2XHB7eQ4Obd7IWBWLHUk2PHynYMU20SI/o/JS/q+pQOLk0vSBauUIHDT/KRfpLL JNfKedLXLG+0DNoDTPyw+eDWXo8qEWPcGYzumaHCbSNqiTJ++VC9ia0HBhl+QzyWMe7J JQsTX85KJJSlDMEahZ1v3f1nhdI3icirjGfE+6bQBjIQXLEF5tO8TunOL8jWblvnvwfF XD8athKHta+m0c7kVJ0vF1eMrlET0M1XjfUbVUVGPYAK6sKq6hspn/lYBgaqxm5aUD7o wPLtmMnBAEtO/SDu25VueU9XZvmKc+JY5u+KvNIi1xWk4BEADBWpcXozxNdbrsub3YyF xX/A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id m56si9779577qtk.70.2019.07.31.01.48.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 01:48:01 -0700 (PDT) Received-SPF: pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jasowang@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=jasowang@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5159130C134A; Wed, 31 Jul 2019 08:48:00 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7AA3F600D1; Wed, 31 Jul 2019 08:47:53 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, jgg@ziepe.ca Subject: [PATCH V2 9/9] vhost: do not return -EAGIAN for non blocking invalidation too early Date: Wed, 31 Jul 2019 04:46:55 -0400 Message-Id: <20190731084655.7024-10-jasowang@redhat.com> In-Reply-To: <20190731084655.7024-1-jasowang@redhat.com> References: <20190731084655.7024-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Wed, 31 Jul 2019 08:48:00 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Instead of returning -EAGAIN unconditionally, we'd better do that only we're sure the range is overlapped with the metadata area. Reported-by: Jason Gunthorpe Fixes: 7f466032dc9e ("vhost: access vq metadata through kernel virtual address") Signed-off-by: Jason Wang --- drivers/vhost/vhost.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index fc2da8a0c671..96c6aeb1871f 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -399,16 +399,19 @@ static void inline vhost_vq_sync_access(struct vhost_virtqueue *vq) smp_mb(); } -static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, - int index, - unsigned long start, - unsigned long end) +static int vhost_invalidate_vq_start(struct vhost_virtqueue *vq, + int index, + unsigned long start, + unsigned long end, + bool blockable) { struct vhost_uaddr *uaddr = &vq->uaddrs[index]; struct vhost_map *map; if (!vhost_map_range_overlap(uaddr, start, end)) - return; + return 0; + else if (!blockable) + return -EAGAIN; spin_lock(&vq->mmu_lock); ++vq->invalidate_count; @@ -423,6 +426,8 @@ static void vhost_invalidate_vq_start(struct vhost_virtqueue *vq, vhost_set_map_dirty(vq, map, index); vhost_map_unprefetch(map); } + + return 0; } static void vhost_invalidate_vq_end(struct vhost_virtqueue *vq, @@ -443,18 +448,19 @@ static int vhost_invalidate_range_start(struct mmu_notifier *mn, { struct vhost_dev *dev = container_of(mn, struct vhost_dev, mmu_notifier); - int i, j; - - if (!mmu_notifier_range_blockable(range)) - return -EAGAIN; + bool blockable = mmu_notifier_range_blockable(range); + int i, j, ret; for (i = 0; i < dev->nvqs; i++) { struct vhost_virtqueue *vq = dev->vqs[i]; - for (j = 0; j < VHOST_NUM_ADDRS; j++) - vhost_invalidate_vq_start(vq, j, - range->start, - range->end); + for (j = 0; j < VHOST_NUM_ADDRS; j++) { + ret = vhost_invalidate_vq_start(vq, j, + range->start, + range->end, blockable); + if (ret) + return ret; + } } return 0;