Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7534398rwb; Wed, 23 Nov 2022 07:39:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf7S1bfB0GNQ2cj7bvHx2axzKB+SR4K+zAPdYsFQjzqoNaHoDGzoxLwCzvrVwmLmETBYgqIU X-Received: by 2002:a63:ff17:0:b0:470:8f8d:eb51 with SMTP id k23-20020a63ff17000000b004708f8deb51mr12783880pgi.69.1669217995215; Wed, 23 Nov 2022 07:39:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1669217995; cv=pass; d=google.com; s=arc-20160816; b=tZijOfDWxiqWorN02+3sp9+Iz7c1KAGBoilYBATgZh3923B7hj1gWvnZzurE1W5dgZ jlVAjDix0I0DbNro3D8J9KjyXuwaUxPbJ6sn0I4JFbuLyIc/qIzWVPUhrNoKO80jGWJa jCGWFw5SnZQiNmu0BZMuI936JE5HVKhyOev4yGPG0z3fXb+7rvGhaTUXMHuJuK6J3NBI /pAujeXbItYcgxfz3fju3RDqgpxbUry8Mu63aZmWRL8psypXYJ89rNMlyWL52fp3fjpy PMrvvNyBuaOitmbtTvNmT82ppmr4s2Ii3bX6idqTYBCd8ZuS4TzMjbUxKV/iWHZSzhLw okKg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=VtYx2dsehvqKOTIqH+aLUlreoNGJsh0MhM83FypMTpA=; b=Kl9JdZNoJh4BPX57GTfGo5B5nViRHe+cqE/VbfAU2m6F3eVfFob13aTt/dl6hc8FpT QZZuxy4lzWe9kt7ukO3une2dDQYP6MqmWB0pzU0E32zjFyEbKTk7VJp2g0puigd+I5X1 65UJbUoh4MR/KhrqsSk6GCJc/NLG7e8NPadQp752GRGofwVaSgqx6DAXVHDcz0aSDVel WmKLyF8XvRR0/gb/F94IXXu1a0K9SA0bOR/eLH4azz2C4MAYyb3H/vwJOKKIQr5+i4Vq hd5gLGB1hZV9d809dMUHYcN40MHpfKTuG3BlfrCJkKFQWPySo32SglJq6iVNpeXGbyGW 33Nw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b="GHj91/ui"; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=outlook.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x1-20020a623101000000b00562a956d979si14700681pfx.67.2022.11.23.07.39.43; Wed, 23 Nov 2022 07:39:55 -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=@outlook.com header.s=selector1 header.b="GHj91/ui"; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=outlook.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237556AbiKWPdU (ORCPT + 89 others); Wed, 23 Nov 2022 10:33:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238580AbiKWPdK (ORCPT ); Wed, 23 Nov 2022 10:33:10 -0500 Received: from JPN01-OS0-obe.outbound.protection.outlook.com (mail-os0jpn01olkn2038.outbound.protection.outlook.com [40.92.98.38]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B292027DD3 for ; Wed, 23 Nov 2022 07:33:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N66sk8YHeEj8YE01tzIOywzP+wRhr9ErETIV458x5blYog9CmXa3vgyaM9T0O6XowS7ogKuqyKydOiRlEcpH72HRdAuHFiXRs0oMRdWpKsgkQXgLrg4Ejoryr4za6sm26Kd3mCFE2L1HUrVisy1AOZ8oq6zn6BYv83Dis/1krSRA8EUTlYUgJi/aDZeMhHoVWCHEvLcMjx8XHKyhang7mfkhqe1YWP+QbcM0R7wuCLv9FKYA2XAZv7OdKfyc6Hf4/yp/9wltGXqsAdyvH3JOU1ttWjUE8BhbZwGYP6ia5lkpl3sWnpUB+JOkSwNbG9DVRziTij5hIkO0FF7UY05Qug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VtYx2dsehvqKOTIqH+aLUlreoNGJsh0MhM83FypMTpA=; b=bTyrcM0ggGaUS8yeg/gqEQCOHRl+//1GGvsmg8Dj8ENei2p9RNLcD3L6Jx+FbZhHrruGepbvh7snYJoddxZFD0WFNTRElvova12AvRJJiMgx0DDK3x3t8H4UW/SJfHYZEfeKCfbuwvjs1fFajjKSGHpNhamFA9/xoHuSJ+tSAbY/gZIR36Ncq2rSNNTBrM1yXV96mTnzQ3K25srdixkkHw9I0CPM7MHhDHfeaYYNYyqFd/jpMZfIz7Y0EbYKAMcqhUkSw3O9/XB+L6rAmykg64s1rS76EJZ3ajxM5lUZw+iMndGF4NHyRS7VCdo/S3JvIlfj+741wdU3HVThmeki/w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VtYx2dsehvqKOTIqH+aLUlreoNGJsh0MhM83FypMTpA=; b=GHj91/uiyJkmWIHfzOUyVvTfvjb1TJXeEOG1i1TAS2UDwNEErHf+DEHXk2KRFq7OrcU3uCDIhUJN1iOkdF0lEsjsdc9z5zfip91sokKzKDDzZxeftShA1Sv5JdhoXP9U67d4OAOOZNEAEZO5Pw3RJCTyhKJivzZa5KnDKZu6qrE7YTjQit0ORTTGY60pADnPQA/f1tBspV7+KR0vrNTdJfWOcUH002VNglVGVnPlzH6tDTYk6QdjtEKF2Fy/hwAN+QFgIaJeVc7GEoxeUL+K+OouegLN0m0z+lbOB0ymkowKy/oBInYfyaSLTBLRrav5X7YSzZGJXmmqIWHdEwD00Q== Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) by TYCP286MB2382.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:188::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5857.18; Wed, 23 Nov 2022 15:33:05 +0000 Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::ff96:9cb6:e047:c605]) by TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::ff96:9cb6:e047:c605%4]) with mapi id 15.20.5834.015; Wed, 23 Nov 2022 15:33:05 +0000 From: Dawei Li To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Dawei Li Subject: [PATCH] virtio: sanity check on callback of virtio drivers Date: Wed, 23 Nov 2022 23:32:27 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TMN: [QZCILqdR4ZhvN9kKXoXXD1cXeci6AiVS] X-ClientProxiedBy: TYAPR04CA0011.apcprd04.prod.outlook.com (2603:1096:404:15::23) To TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) X-Microsoft-Original-Message-ID: <20221123153227.627460-1-set_pte_at@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYCP286MB2323:EE_|TYCP286MB2382:EE_ X-MS-Office365-Filtering-Correlation-Id: cb987214-7016-4782-65b1-08dacd680406 X-MS-Exchange-SLBlob-MailProps: 0wLWl8rLpvsNNDSScp0ukZGcW9c5unyOC174Q5NUXaIe/Qnq3KwSn/xK7gUE0fPFVbqvN7AsSstn31NHfqcuR+6KIJgpdW7QvoEW0XSnttJYqd7stlgpkHbWRusZTDG4IG20clkN8roj8dhzkDjyF+7LaQwqoxbKovxAMAmFqnx/IBlXLRHFKsANj6iKKISEqNYbBM4R+5EwcmgIAz8ayrbWE+4fW61wZrJ6ifEEln3OyMoaaRrjit6SZoXeaN0aPfKmDz0LRTG/ufmVu4TMnlOxw7DvFDEJY5CCXPq4pw7NHM50ps9QBdOe8x9JEm1hPJTZCho7/RVsvOHgNwO3FxiLCC3X/Xq04/Cnhih+WQODEwD95rCcB73tjAQxaEHElbwWlOVXT45jyxae4EmMU1XEYEltbJLYx567xp8lzoCDiyWnPn17hnsMXSJt41miX+VZm3tVzUziLrHwUtKsdJ2+mnVcMmLYBnlaaHVLRHu4ZrQPfrR6GvLSYyKcXHfkGrvIeknJPKDxfqU7VFZjgAi3L8HqSgIY9CRgwFBHDHQKD77mdt3VtI9tRmOJKtH6jaJa5DCh1P+IzOKOIapzUfl7rZe5K8Eg0gr00L0wgr9q7CU2hNO6wgtAfT5suYKkqSnSr2Qy5a8nckqzV4KDAXCDCTs70uNuxynodWuHGJPKdnXpjxnaLxxkNtg+evehidxmJVcgJvpfPYvTiykqJPPMcx6KB97tmnD2pjlGRuJw7uXQB4z5iWG2afCk66ekS2bgR/L+03Y= X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vyYDK1w5abFZIXwe0DwD4GQVCAI9QR/a+oOKcChdSucU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?us-ascii?Q?ueUEzYKyOGnRqxqFG30dUZQSrR5uDUZ2YzDJcRqRcqUXU3eN6HL6f9GQwNE5?= =?us-ascii?Q?2Zy/Ns4/LEbHwXsjmmctVfLxtcK++XouwzvVlz4hfw=3D=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cb987214-7016-4782-65b1-08dacd680406 X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Nov 2022 15:33:05.6354 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYCP286MB2382 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS 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 This commit includes changes below: 1 Since register_virtio_driver doesn't force probe & remove to be mandatory callback, so it's caller's job to make the sanity check before invocation. 2 Replace sprintf with sysfs_emit or its variants for their built-in PAGE_SIZE awareness. Signed-off-by: Dawei Li --- drivers/virtio/virtio.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 828ced060742..e391a8dff333 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -15,7 +15,7 @@ static ssize_t device_show(struct device *_d, struct device_attribute *attr, char *buf) { struct virtio_device *dev = dev_to_virtio(_d); - return sprintf(buf, "0x%04x\n", dev->id.device); + return sysfs_emit(buf, "0x%04x\n", dev->id.device); } static DEVICE_ATTR_RO(device); @@ -23,7 +23,7 @@ static ssize_t vendor_show(struct device *_d, struct device_attribute *attr, char *buf) { struct virtio_device *dev = dev_to_virtio(_d); - return sprintf(buf, "0x%04x\n", dev->id.vendor); + return sysfs_emit(buf, "0x%04x\n", dev->id.vendor); } static DEVICE_ATTR_RO(vendor); @@ -31,7 +31,7 @@ static ssize_t status_show(struct device *_d, struct device_attribute *attr, char *buf) { struct virtio_device *dev = dev_to_virtio(_d); - return sprintf(buf, "0x%08x\n", dev->config->get_status(dev)); + return sysfs_emit(buf, "0x%08x\n", dev->config->get_status(dev)); } static DEVICE_ATTR_RO(status); @@ -39,7 +39,7 @@ static ssize_t modalias_show(struct device *_d, struct device_attribute *attr, char *buf) { struct virtio_device *dev = dev_to_virtio(_d); - return sprintf(buf, "virtio:d%08Xv%08X\n", + return sysfs_emit(buf, "virtio:d%08Xv%08X\n", dev->id.device, dev->id.vendor); } static DEVICE_ATTR_RO(modalias); @@ -54,9 +54,9 @@ static ssize_t features_show(struct device *_d, /* We actually represent this as a bitstring, as it could be * arbitrary length in future. */ for (i = 0; i < sizeof(dev->features)*8; i++) - len += sprintf(buf+len, "%c", + len += sysfs_emit_at(buf, len, "%c", __virtio_test_bit(dev, i) ? '1' : '0'); - len += sprintf(buf+len, "\n"); + len += sysfs_emit_at(buf, len, "\n"); return len; } static DEVICE_ATTR_RO(features); @@ -302,9 +302,11 @@ static int virtio_dev_probe(struct device *_d) if (err) goto err; - err = drv->probe(dev); - if (err) - goto err; + if (drv->probe) { + err = drv->probe(dev); + if (err) + goto err; + } /* If probe didn't do it, mark device DRIVER_OK ourselves. */ if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) @@ -329,7 +331,8 @@ static void virtio_dev_remove(struct device *_d) virtio_config_disable(dev); - drv->remove(dev); + if (drv->remove) + drv->remove(dev); /* Driver should have reset device. */ WARN_ON_ONCE(dev->config->get_status(dev)); -- 2.25.1