Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1609177pxp; Mon, 21 Mar 2022 00:18:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtNXKikiE/v2pTXhFsyBo5axlzTmhNJuUEjzDinbj1H/z+Os90/hhAG0KUYtOsdfRcfihO X-Received: by 2002:a17:907:6e17:b0:6da:83a3:c27a with SMTP id sd23-20020a1709076e1700b006da83a3c27amr19174747ejc.415.1647847101037; Mon, 21 Mar 2022 00:18:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647847101; cv=none; d=google.com; s=arc-20160816; b=Idic/CJistKO277DDGiWYsKJzhHml7j8s6Te7sdbEIQUKCDSJbH4jS3vqmFGH7V7hg Or5jw0/3v/HwahnH1H85culTLUklnQFBrd9l26Ff+rDwmuGmI8o3J+/5j6fWd+YXZcPi +zuwVy13W8EUpexSbaETz+D6jpCoVh2qxf1L7MUTbi1/gJwmQFHHbUeesdqWNuWkLMsL SX3inRI36CLPmIG3ZZKHBYLTUTYeh/59o+bh6JkRbw2wFgtu5r0UWyAWVKBwBUj6ShaB YmF01oTszTITd6ZKycRj+o1ZHsM8JL9LklwR12Unl32tfDmQG47s7u5Wks1gFOWVuClV BT+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=IiAGK8MJyYDtNfKFlzVpmQAJqIr42Om/VU2/PvLTG0Y=; b=iGxzQ+kyRzy5JrrgNgxl8IYuCyASoG/ok1E44uVVSKxz9hSn3B+FXgJL6hJmIfdo+w twZij0PVF5CSRe26yZMJBsBeyT69lVX1S43Bp3vrRqpLcfn1B3SPq3EY8/Zd+nJWPIe0 VxBFlDJBeuQXuAKFPLDw20tL2VkrcxSzjumZu3a+28qY8sPXtlX3cwBmY9TwJ8FU5v1v ywwmmfLQTUvkvlr3Qd73HDI0r+6ZpZpAhDgcb4smajsv8YKae6IMRkEi3+08twhT1e57 uqEc30hnku6TW1MPMkgjatRBhuU5eTIEvB6BYXeWW/VqPgKts3BS2lCWF/rKEhAplKjw Zw3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BZOUuYL2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dr18-20020a170907721200b006dfa09bbb70si7054876ejc.451.2022.03.21.00.17.56; Mon, 21 Mar 2022 00:18:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BZOUuYL2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1344509AbiCUGGJ (ORCPT + 99 others); Mon, 21 Mar 2022 02:06:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238522AbiCUGGH (ORCPT ); Mon, 21 Mar 2022 02:06:07 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6012F167CA for ; Sun, 20 Mar 2022 23:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647842682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=IiAGK8MJyYDtNfKFlzVpmQAJqIr42Om/VU2/PvLTG0Y=; b=BZOUuYL2kI9766LTX6r+XylaS3kRPKN8slUF8u4+VISBA5cXWfee0mUiR+bn9z3oX5wfb+ NTtX+jqed+tW6CtyEXMT3ebgykad8eT4fXAzh2lHSlvagHtoyDsSgpWKt36Vf+zuA86IWd qLn4Ax8nTlsw2OqPIaizj/lAVdpOu1s= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-459-DXGk5XR-OZCnQpdP67PjOw-1; Mon, 21 Mar 2022 02:04:39 -0400 X-MC-Unique: DXGk5XR-OZCnQpdP67PjOw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0248E3C02B73; Mon, 21 Mar 2022 06:04:38 +0000 (UTC) Received: from localhost.localdomain (ovpn-14-31.pek2.redhat.com [10.72.14.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id BA0632166B2D; Mon, 21 Mar 2022 06:04:32 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: elic@nvidia.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] vdpa: mlx5: prevent cvq work from hogging CPU Date: Mon, 21 Mar 2022 14:04:28 +0800 Message-Id: <20220321060429.10457-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A userspace triggerable infinite loop could happen in mlx5_cvq_kick_handler() if userspace keeps sending a huge amount of cvq requests. Fixing this by introducing a quota and re-queue the work if we're out of the budget. While at it, using a per device workqueue to avoid on demand memory allocation for cvq. Fixes: 5262912ef3cfc ("vdpa/mlx5: Add support for control VQ and MAC setting") Signed-off-by: Jason Wang --- drivers/vdpa/mlx5/net/mlx5_vnet.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c index d0f91078600e..d5a6fb3f9c41 100644 --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c @@ -163,6 +163,7 @@ struct mlx5_vdpa_net { u32 cur_num_vqs; struct notifier_block nb; struct vdpa_callback config_cb; + struct mlx5_vdpa_wq_ent cvq_ent; }; static void free_resources(struct mlx5_vdpa_net *ndev); @@ -1600,6 +1601,8 @@ static virtio_net_ctrl_ack handle_ctrl_mq(struct mlx5_vdpa_dev *mvdev, u8 cmd) return status; } +#define MLX5_CVQ_BUDGET 16 + static void mlx5_cvq_kick_handler(struct work_struct *work) { virtio_net_ctrl_ack status = VIRTIO_NET_ERR; @@ -1609,17 +1612,17 @@ static void mlx5_cvq_kick_handler(struct work_struct *work) struct mlx5_control_vq *cvq; struct mlx5_vdpa_net *ndev; size_t read, write; - int err; + int err, n = 0; wqent = container_of(work, struct mlx5_vdpa_wq_ent, work); mvdev = wqent->mvdev; ndev = to_mlx5_vdpa_ndev(mvdev); cvq = &mvdev->cvq; if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ))) - goto out; + return; if (!cvq->ready) - goto out; + return; while (true) { err = vringh_getdesc_iotlb(&cvq->vring, &cvq->riov, &cvq->wiov, &cvq->head, @@ -1653,9 +1656,13 @@ static void mlx5_cvq_kick_handler(struct work_struct *work) if (vringh_need_notify_iotlb(&cvq->vring)) vringh_notify(&cvq->vring); + + n++; + if (n > MLX5_CVQ_BUDGET) { + queue_work(mvdev->wq, &wqent->work); + break; + } } -out: - kfree(wqent); } static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx) @@ -1663,7 +1670,6 @@ static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx) struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); struct mlx5_vdpa_virtqueue *mvq; - struct mlx5_vdpa_wq_ent *wqent; if (!is_index_valid(mvdev, idx)) return; @@ -1672,13 +1678,7 @@ static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx) if (!mvdev->cvq.ready) return; - wqent = kzalloc(sizeof(*wqent), GFP_ATOMIC); - if (!wqent) - return; - - wqent->mvdev = mvdev; - INIT_WORK(&wqent->work, mlx5_cvq_kick_handler); - queue_work(mvdev->wq, &wqent->work); + queue_work(mvdev->wq, &ndev->cvq_ent.work); return; } @@ -2668,6 +2668,8 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name, if (err) goto err_mr; + ndev->cvq_ent.mvdev = mvdev; + INIT_WORK(&ndev->cvq_ent.work, mlx5_cvq_kick_handler); mvdev->wq = create_singlethread_workqueue("mlx5_vdpa_wq"); if (!mvdev->wq) { err = -ENOMEM; -- 2.18.1