From patchwork Mon Feb 6 23:12:02 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: 53555 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2516414wrn; Mon, 6 Feb 2023 15:15:00 -0800 (PST) X-Google-Smtp-Source: AK7set8Cx5h7J25LW1qiZZQG7KRe5VF0hEtXAJQqQBB5SiPJxKIm9lgtCtjY1//CxOQL5ZoP1dTZ X-Received: by 2002:a50:c05d:0:b0:49d:34ae:c7aa with SMTP id u29-20020a50c05d000000b0049d34aec7aamr1354730edd.29.1675725300649; Mon, 06 Feb 2023 15:15:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675725300; cv=none; d=google.com; s=arc-20160816; b=kd4q5/ALSyZEtDKugIVQEs3tFTgnHJxxrRz8MvvPPfmU3+FirTETTSjTcfw4FyWL5i Um9sjr2tfeomHJefJaKQioAigL+Rd52qXyyYAby5xVtt46b8ZvNmOTnTbecxhcc2HSfk cUW0oSfQvuaSGghlqB+3lgenPFuqMdye/WsfrI0y5g0O0nyYJr+64Mz1qlv0aLf+YdJF U/+s6dy32uk+rfMDpYSX3Hk6dRRdF0FqFoZWcouWJ8D8AmRwPIE6q+UHW9H0wUiCPrIK lZcqyW/441T9yGhJzibFL2ze2lv2tSPVREahvhEYnGB0686eh4Pnp0xIBIMePc3xpsw+ WlIw== 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=d3JugFvzxOjps33xev2euAm7g4qNmWk7oY4Vz08lzVM=; b=uJLG7mJtMyT9TBe3uRS493Csp5bJIFkYn12HxK4vqa9s8pyS8Vz0DsR+kN/7FD8PM/ 1ipJmzYNtA7ViYhLhJAXfZnF887ri27o9CX4Gq2nXWfZHiG1FkOP7FI2/FJ59UIqIz3P LnqxYXzDfGuOBBObhvikJpoNZV+EtqvFfBUN2tHuoaf0cbJqrbhnyqp6OatecaDVdD1I WkpaydAqKkkUcWQBYisvC8S3rg5Jt2eaXlBc2fJuXqo1szILEjdN47lPXtCBP7NtVKu1 zAY02fEyGzLRJLpP+JlbKWkjdALSjOqrLwmXW1JgxldavkqzsJLPJWw6/yobQ2Lu5BJV 3z0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b=ZTGXVzJO; 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 fg14-20020a056402548e00b004a0db7e4383si12730651edb.394.2023.02.06.15.14.15; Mon, 06 Feb 2023 15:15:00 -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=ZTGXVzJO; 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 S229939AbjBFXMk (ORCPT + 99 others); Mon, 6 Feb 2023 18:12:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbjBFXMe (ORCPT ); Mon, 6 Feb 2023 18:12:34 -0500 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 218F831E07 for ; Mon, 6 Feb 2023 15:12:33 -0800 (PST) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 316KDrhf003911; Mon, 6 Feb 2023 23:12:28 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=d3JugFvzxOjps33xev2euAm7g4qNmWk7oY4Vz08lzVM=; b=ZTGXVzJO4bSE/0v+jxfEf9XTuSzyyotNX2Yr1sT8ZUDVbokDhqLrYXLrLP+OYRPVXQ3Q O/CJGi2I2Q6qU8plhsB2VPWkS3lrRBZVZRKzS1RAHVSVgcVadQZmAl++AGTP0Gzhxtdf 0pl444B7XsndMHsWr/nZWKSP0ruzEUkZuhnQ22nb+MBsTdmxRVJaKf7vNP29KPRkxf+U IcxiE5EoiIkiwA7ZSsuEf1fG7tKaZjv8VnjhCED5yeqAuHI9iUgHu1GC1X6ToRs6DjW3 +NFGkS4Ri+ITH9CiGs5Ll0Ex0qsHhD+fEEXD0W8v3zTEmXKZDHZtwDdICP+5GOQwhPZh 0A== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3nheytv8br-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 06 Feb 2023 23:12:28 +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 316LpWgt021033; Mon, 6 Feb 2023 23:12:27 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 3nhdt4sj6f-5; Mon, 06 Feb 2023 23:12:27 +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 RESENT v4 4/6] vdpa: validate device feature provisioning against supported class Date: Mon, 6 Feb 2023 15:12:02 -0800 Message-Id: <1675725124-7375-5-git-send-email-si-wei.liu@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1675725124-7375-1-git-send-email-si-wei.liu@oracle.com> References: <1675725124-7375-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-06_07,2023-02-06_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 bulkscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 adultscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302060202 X-Proofpoint-GUID: KZV65-0zqrh8RBL-x8u4yUftGOYLTUxQ X-Proofpoint-ORIG-GUID: KZV65-0zqrh8RBL-x8u4yUftGOYLTUxQ 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?1757125332760110671?= X-GMAIL-MSGID: =?utf-8?q?1757125332760110671?= Today when device features are explicitly provisioned, the features user supplied may contain device class specific features that are not supported by the parent management device. On the other hand, when parent management 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 | 59 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index 1eba978..8da5120 100644 --- a/drivers/vdpa/vdpa.c +++ b/drivers/vdpa/vdpa.c @@ -460,12 +460,28 @@ 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; + + for (int i = 0; mdev->id_table[i].device; i++) { + if (mdev->id_table[i].device > 63) + continue; + supported_classes |= BIT_ULL(mdev->id_table[i].device); + n++; + } + 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 +491,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 +577,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 +672,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);