From patchwork Fri Feb 3 05:02:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Si-Wei Liu X-Patchwork-Id: 52305 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp648829wrn; Thu, 2 Feb 2023 21:04:13 -0800 (PST) X-Google-Smtp-Source: AK7set8Ue2ILjwLBHvKyxxdDY6T4WZOtfEh0Cbr1AmSpuve6TVJzY+ZngE//hOIdB/3IrUzT/F33 X-Received: by 2002:a17:906:7e42:b0:883:b1b4:e798 with SMTP id z2-20020a1709067e4200b00883b1b4e798mr8732355ejr.10.1675400653641; Thu, 02 Feb 2023 21:04:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675400653; cv=none; d=google.com; s=arc-20160816; b=IO3gJYoHX6wiox3lP01rMzlOgC0Od/rarZi8UWhXOBSm1TI63SutswuR+r3gS/OCCH MTaXbWLuTanGI32Ic2wqZR1sd5bduph5ib3aIQ+l9AZxCycDGKrg3FiE84DyQvxCA0an jdZLqhk6Y/hZBpSOjrb5NtH/LUCn8/SSEQB5HTono6m1FBw8r20Q18+fTmU9sPGMtHyr gelWcJZttUAKTEUlZDv33bDwBz9U3X1b3g1rZ+aDDKjrm97/fI/bNPxIKUSS3JvAWkaB sP+jHMsJjUKiGdr4M2DJ8LGpt9rXdiTiRHM1CoFziG7GhKxnK1Jh1cGA5xfs0eTjh6T6 CBFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=90PclZH5smGZrRUKyhlf4EA2JpBj5eesUZ1CnMBH5tI=; b=uYT5sz9LwhFVmmNGYf2/NY4W5xZN98wUgiwfUkMsP08a0hxJ4aK7lsuj+QVSNU+xk3 Rt0xLU2qq7Xmpnz3/4utqlxIQrkZErhPt0fnualRC4cdogPsMOVRJ2JcjP7owzcKUX37 NjLTNhufjhtxtI4EltAijXjBw2e1huj4p08PtdBG02xReXFSIv0rYh41dyhifyPOXebD VRF7ji3M24DjuF2TZFhdMMSvPb/CTMFwPFCFzI3ouslIsZtFczg3agLjIIJ8Wcnq9ALu 8jdsYpGHksnOUPPBS2Xx1Neak9c9JNCfRdXG+5AZ5GH+trnMsq79hdlXEqZV0KP7WMjK a9aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b=yicUr3AL; 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=oracle.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kb2-20020a170907924200b0087bd3430c13si1711710ejb.359.2023.02.02.21.03.48; Thu, 02 Feb 2023 21:04:13 -0800 (PST) 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=@oracle.com header.s=corp-2022-7-12 header.b=yicUr3AL; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231644AbjBCFCy (ORCPT + 99 others); Fri, 3 Feb 2023 00:02:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbjBCFCg (ORCPT ); Fri, 3 Feb 2023 00:02:36 -0500 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B154E74C2B for ; Thu, 2 Feb 2023 21:02:30 -0800 (PST) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3130OA8A005801; Fri, 3 Feb 2023 05:02:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2022-7-12; bh=90PclZH5smGZrRUKyhlf4EA2JpBj5eesUZ1CnMBH5tI=; b=yicUr3ALVdRQNK/1M8HYhVSra/F7w6MGWZCHgzRSiszChFeCBvbYExVek3rQHyHHQzff 0kvsu/EvyuibJOY6kBPEWPq0RfyxXH1vSXegyteQsCsecDRKApyvHJdU7EwnPVp40/bu xt47dM3uMKDXpURNUzRl/MEo5POy7Lra3DF4x4dhAnf1FaMKlacVTGUXZoITDSQNcfUh XZX6GY212Eou9LX4wbRZDyRRuelw/ZXtBW127QxdmhmI/XRk43cGYzl8Rn3+iJX5oLNS EwgY7B9roGRpUVkmNXjtQBNaKDZu/Itx/IDEMgkJmroMb6qdVQ6ziLDWgQDb8645Dzda YQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3nfkfe4wxg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 03 Feb 2023 05:02:26 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 31340RuP006053; Fri, 3 Feb 2023 05:02:25 GMT Received: from ban25x6uut24.us.oracle.com (ban25x6uut24.us.oracle.com [10.153.73.24]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3nct5a2tm8-5; Fri, 03 Feb 2023 05:02:25 +0000 From: Si-Wei Liu To: mst@redhat.com, jasowang@redhat.com, parav@nvidia.com, elic@nvidia.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/6] vdpa: validate device feature provisioning against supported class Date: Thu, 2 Feb 2023 21:02:01 -0800 Message-Id: <1675400523-12519-5-git-send-email-si-wei.liu@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1675400523-12519-1-git-send-email-si-wei.liu@oracle.com> References: <1675400523-12519-1-git-send-email-si-wei.liu@oracle.com> X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.122.1 definitions=2023-02-03_02,2023-02-02_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 suspectscore=0 mlxscore=0 spamscore=0 phishscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302030044 X-Proofpoint-GUID: cINSHcYYRUl5v3yBw4llRx1HaIZyOQZN X-Proofpoint-ORIG-GUID: cINSHcYYRUl5v3yBw4llRx1HaIZyOQZN X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756784915579491380?= X-GMAIL-MSGID: =?utf-8?q?1756784915579491380?= Today when device features are explicitly provisioned, the features user supplied may contain device class specific features that are not supported by the parent managment device. On the other hand, when parent managment device supports more than one class, the device features to provision may be ambiguous if none of the class specific attributes is provided at the same time. Validate these cases and prompt appropriate user errors accordingly. Signed-off-by: Si-Wei Liu --- drivers/vdpa/vdpa.c | 61 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index 1eba978..4aa2160 100644 --- a/drivers/vdpa/vdpa.c +++ b/drivers/vdpa/vdpa.c @@ -460,12 +460,30 @@ static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff *msg, const struct vdpa_mg return 0; } +static u64 vdpa_mgmtdev_get_classes(const struct vdpa_mgmt_dev *mdev, + unsigned int *nclasses) +{ + u64 supported_classes = 0; + unsigned int n = 0; + int i = 0; + + while (mdev->id_table[i].device) { + if (mdev->id_table[i].device <= 63) { + supported_classes |= BIT_ULL(mdev->id_table[i].device); + n++; + } + i++; + } + if (nclasses) + *nclasses = n; + + return supported_classes; +} + static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *msg, u32 portid, u32 seq, int flags) { - u64 supported_classes = 0; void *hdr; - int i = 0; int err; hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_MGMTDEV_NEW); @@ -475,14 +493,9 @@ static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *m if (err) goto msg_err; - while (mdev->id_table[i].device) { - if (mdev->id_table[i].device <= 63) - supported_classes |= BIT_ULL(mdev->id_table[i].device); - i++; - } - if (nla_put_u64_64bit(msg, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES, - supported_classes, VDPA_ATTR_UNSPEC)) { + vdpa_mgmtdev_get_classes(mdev, NULL), + VDPA_ATTR_UNSPEC)) { err = -EMSGSIZE; goto msg_err; } @@ -566,13 +579,25 @@ static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *i BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU) | \ BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP)) +/* + * Bitmask for all per-device features: feature bits VIRTIO_TRANSPORT_F_START + * through VIRTIO_TRANSPORT_F_END are unset, i.e. 0xfffffc000fffffff for + * all 64bit features. If the features are extended beyond 64 bits, or new + * "holes" are reserved for other type of features than per-device, this + * macro would have to be updated. + */ +#define VIRTIO_DEVICE_F_MASK (~0ULL << (VIRTIO_TRANSPORT_F_END + 1) | \ + ((1ULL << VIRTIO_TRANSPORT_F_START) - 1)) + static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *info) { struct vdpa_dev_set_config config = {}; struct nlattr **nl_attrs = info->attrs; struct vdpa_mgmt_dev *mdev; + unsigned int ncls = 0; const u8 *macaddr; const char *name; + u64 classes; int err = 0; if (!info->attrs[VDPA_ATTR_DEV_NAME]) @@ -649,6 +674,24 @@ static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *i goto err; } + classes = vdpa_mgmtdev_get_classes(mdev, &ncls); + if (config.mask & VDPA_DEV_NET_ATTRS_MASK && + !(classes & BIT_ULL(VIRTIO_ID_NET))) { + NL_SET_ERR_MSG_MOD(info->extack, + "Network class attributes provided on unsupported management device"); + err = -EINVAL; + goto err; + } + if (!(config.mask & VDPA_DEV_NET_ATTRS_MASK) && + config.mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES) && + classes & BIT_ULL(VIRTIO_ID_NET) && ncls > 1 && + config.device_features & VIRTIO_DEVICE_F_MASK) { + NL_SET_ERR_MSG_MOD(info->extack, + "Management device supports multi-class while device features specified are ambiguous"); + err = -EINVAL; + goto err; + } + err = mdev->ops->dev_add(mdev, name, &config); err: up_write(&vdpa_dev_lock);