Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp311687pxm; Wed, 2 Mar 2022 16:05:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJy1EnpHH/Oyns18Bpe+bOBiDi7QFtFS30WyJEuA44hFvpyCINlu3ov7nEkZ5MI3uhgr6ODq X-Received: by 2002:a63:f555:0:b0:373:9ad0:ca08 with SMTP id e21-20020a63f555000000b003739ad0ca08mr27929157pgk.315.1646265950910; Wed, 02 Mar 2022 16:05:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646265950; cv=none; d=google.com; s=arc-20160816; b=GPYT1FANCmWiYyGBuUqAai3QdHR9QG6IJjl4sTJe7ciaeiOdwpfgOcHxngs3m6bC2X E+SVQerD/q0d045l9ApsnejIUtucABurJATOZ11WIvCzpDlBgr8elxTkLISYT2P/TMfa gVXZ8FH8tF0fWFNfSXKAi7a5mxjerLakXA3heOnsaTgp/af3ZwvmXAArQXa/LUR4IemN NU8yCPBlshz/nLubXmPeAuNWv/2APr4Fsd+RNpGn0MBICNhFJ14y10e58OaCSpMRcPVE +fKMEf+wK4r59uRlfmkqpGlNWpRWO9nxwvTT+VKrTYmSLd81lph9LHBz6ivbUhTeiVWZ Euww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=9E1Gw4+rs3O1SDqvrlAIeVXWi4IR/tSpwpoKpax7Rfo=; b=P9geHtfmvJKP0AHTozhhm8N9ilMMZ/hVK56MbY4JXXEQQnmtLiFZrBLuVorERe6PGD uqsitPr0WbptGBbYf512iXX9Jq0sqwGdoMcSPrbYANxwt85qZha1Bu7az3wyEsrFsUTM myYorRDJc4lNOY4NU6riCmiJS8HZfAKScpyx4CRmVN9RnnrhLuMqmiB4xTw7X82r7qvG loZMA7TMEJbKF6uW+dfUS7aBF8Uo/sQlslaPaHDYfuv9JriRO2v9I2KubVvvJSGt7OgO 5fm31tXqE5Ka2y1Cl/l2ZC+Xbc+2bosqc6JU6jupo0ZXZbeTsVKmhJtkNXXMWKOzKo8W ECXA== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id bv24-20020a17090af19800b001bd0ee9487csi377951pjb.74.2022.03.02.16.05.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 16:05:50 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 727C01AA4B0; Wed, 2 Mar 2022 15:22:48 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233361AbiCBUf2 (ORCPT + 99 others); Wed, 2 Mar 2022 15:35:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231263AbiCBUf2 (ORCPT ); Wed, 2 Mar 2022 15:35:28 -0500 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BA6D5AA73; Wed, 2 Mar 2022 12:34:44 -0800 (PST) Received: by mail-wr1-f49.google.com with SMTP id m6so4601304wrr.10; Wed, 02 Mar 2022 12:34:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=9E1Gw4+rs3O1SDqvrlAIeVXWi4IR/tSpwpoKpax7Rfo=; b=i1GS/q5DuakYgNxrzDPFiAlgjHvyTjPS5+aDE36hZaMxjvIBQIiCn49VKe+arJT3Yh /QA8n5tAqAn+Wiu3VwLxCXMA7mlKfHXhFrxUCao1ZaUltOFYjdJIpwoMICWx11F+1qPG X2kUQGfYFFTBHqfoxmxoL21PuMTUJp7i+gSrmwtd5SQuBTOQ46rnKmmnROy6DqQK/03t zGG7D9ckenKAh4Jq8IqlrziPGMs0mXNysmhF4jFeWeeb5nEVVlQDZXcpZkGPh/xkJBBj A2ms1pMPEXG+0D5YEb9a8d/66nmStINsAzrC4re4RoqDqQeurbJrJa/pmpM5a74LcfPa bxQQ== X-Gm-Message-State: AOAM532WjQnUaZbQKZPVzwnk0lBvGFVnKb9ij8zkCvhbaMYWxD31SD/0 GMfglv9Wq+vdUoquuxvLp0c= X-Received: by 2002:a5d:68c6:0:b0:1ed:e119:23e6 with SMTP id p6-20020a5d68c6000000b001ede11923e6mr24063358wrw.654.1646253282698; Wed, 02 Mar 2022 12:34:42 -0800 (PST) Received: from liuwe-devbox-debian-v2 ([51.145.34.42]) by smtp.gmail.com with ESMTPSA id l12-20020a5d6d8c000000b001efd2c071dbsm55423wrs.20.2022.03.02.12.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 12:34:42 -0800 (PST) Date: Wed, 2 Mar 2022 20:34:40 +0000 From: Wei Liu To: Iouri Tarassov Cc: Wei Liu , Greg KH , kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, spronovo@microsoft.com, spronovo@linux.microsoft.com Subject: Re: [PATCH v3 02/30] drivers: hv: dxgkrnl: Driver initialization and loading Message-ID: <20220302203440.inzt6z6mxhs56wab@liuwe-devbox-debian-v2> References: <719fe06b7cbe9ac12fa4a729e810e3383ab421c1.1646163378.git.iourit@linux.microsoft.com> <739cf89e71ff72436d7ca3f846881dfb45d07a6a.1646163378.git.iourit@linux.microsoft.com> <20220301222321.yradz24nuyhzh7om@liuwe-devbox-debian-v2> <20220302115334.wemdkznokszlzcpe@liuwe-devbox-debian-v2> <6ac1dd87-3c78-66ca-c526-d1f6cf253400@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6ac1dd87-3c78-66ca-c526-d1f6cf253400@linux.microsoft.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Wed, Mar 02, 2022 at 10:49:15AM -0800, Iouri Tarassov wrote: > On 3/2/2022 3:53 AM, Wei Liu wrote: > > On Wed, Mar 02, 2022 at 08:53:15AM +0100, Greg KH wrote: > > > On Tue, Mar 01, 2022 at 10:23:21PM +0000, Wei Liu wrote: > > > > > > +struct dxgglobal *dxgglobal; > > > > > > > > > > No, make this per-device, NEVER have a single device for your driver. > > > > > The Linux driver model makes it harder to do it this way than to do it > > > > > correctly. Do it correctly please and have no global structures like > > > > > this. > > > > > > > > > > > > > This may not be as big an issue as you thought. The device discovery is > > > > still done via the normal VMBus probing routine. For all intents and > > > > purposes the dxgglobal structure can be broken down into per device > > > > fields and a global structure which contains the protocol versioning > > > > information -- my understanding is there will always be a global > > > > structure to hold information related to the backend, regardless of how > > > > many devices there are. > > > > > > Then that is wrong and needs to be fixed. Drivers should almost never > > > have any global data, that is not how Linux drivers work. What happens > > > when you get a second device in your system for this? Major rework > > > would have to happen and the code will break. Handle that all now as it > > > takes less work to make this per-device than it does to have a global > > > variable. > > > > > > > It is perhaps easier to draw parallel from an existing driver. I feel > > like we're talking past each other. > > > > Let's look at drivers/iommu/intel/iommu.c. There are a bunch of lists > > like `static LIST_HEAD(dmar_rmrr_units)`. During the probing phase, new > > units will be added to the list. I this the current code is following > > this model. dxgglobal fulfills the role of a list. > > > > Setting aside the question of whether it makes sense to keep a copy of > > the per-VM state in each device instance, I can see the code be changed > > to: > > > > struct mutex device_mutex; /* split out from dxgglobal */ > > static LIST_HEAD(dxglist); > > > > /* Rename struct dxgglobal to struct dxgstate */ > > struct dxgstate { > > struct list_head dxglist; /* link for dxglist */ > > /* ... original fields sans device_mutex */ > > } > > > > /* > > * Provide a bunch of helpers manipulate the list. Called in probe / > > * remove etc. > > */ > > struct dxgstate *find_dxgstate(...); > > void remove_dxgstate(...); > > int add_dxgstate(...); > > > > This model is well understood and used in tree. It is just that it > > doesn't provide much value in doing this now since the list will only > > contain one element. I hope that you're not saying we cannot even use a > > per-module pointer to quickly get the data structure we want to use, > > right? > > > > Are you suggesting Iouri use dev_set_drvdata to stash the dxgstate > > into the device object? I think that can be done too. > > > > The code can be changed as: > > > > /* Rename struct dxgglobal to dxgstate and remove unneeded fields */ > > struct dxgstate { ... }; > > > > static int dxg_probe_vmbus(...) { > > > > /* probe successfully */ > > > > struct dxgstate *state = kmalloc(...); > > /* Fill in dxgstate with information from backend */ > > > > /* hdev->dev is the device object from the core driver framework */ > > dev_set_drvdata(&hdev->dev, state); > > } > > > > static int dxg_remove_vmbus(...) { > > /* Normal stuff here ...*/ > > > > struct dxgstate *state = dev_get_drvdata(...); > > dev_set_drvdata(..., NULL); > > kfree(state); > > } > > > > /* In all other functions */ > > void do_things(...) { > > struct dxgstate *state = dev_get_drvdata(...); > > > > /* Use state in place of where dxgglobal was needed */ > > > > } > > > > Iouri, notice this doesn't change anything regarding how userspace is > > designed. This is about how kernel organises its data. > > > > I hope what I wrote above can bring our understanding closer. > > > > Thanks, > > Wei. > > > I can certainly remove dxgglobal and keep the? pointer to the global > state in the device object. > No, no more global pointer needed. You just call dev_drv_setdata in the place that you assign to the global pointer. > This will require passing of the global pointer to all functions, which > need to access it. > And in the place you need the global pointer, call dev_drv_getdata. > > Maybe my understanding of the Greg's suggestion was not correct. I > thought the suggestion was > > to have multiple /dev/dxgN devices (one per virtual compute device). > This would change how the user mode > No. You still have only one /dev/dxg here. Wei.