Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2761891rdh; Wed, 27 Sep 2023 11:53:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuYqPRiJ+leFp0vtuQPVzFyOaFaH9NDEI6R5/k1unsW4NC9dAgjBjmvzfYZPf5BK6w3IhH X-Received: by 2002:a05:6a00:2da7:b0:68e:2478:d6c9 with SMTP id fb39-20020a056a002da700b0068e2478d6c9mr3240434pfb.2.1695840803377; Wed, 27 Sep 2023 11:53:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695840803; cv=none; d=google.com; s=arc-20160816; b=dy3IZDDPxrbx0inYple7OgoWTOStBefAwWssI/G7CkTUQAVcAYR7QDPJGGtYGjZ0Am u0+XnUeb6Mb2EDDonhp6+El2SgBEvAUPYtBgfsevpo13vrcSt67jDP7d4m4Zn9CMB4Jl ZQKZnj9q3WvUdvCM0CSkUYU9JvZkwzpJOU8qEOf6taPQl7zMtHoD0y7VNINqeHEJbPLW jItVolxhsYxJ7iHEe+RXFG3Gx1QHVAk37Nq1P/0ZZnuDEi/s4rIm6rIxhs/rD4YJ3fMZ 5/s5LU4ajWPwplgdRtEjdJfYlhef4YMPe7d2YfAgSimKF/KutOzhCHXfmOuL6TKdYkHz tCAw== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=GyAXU0uE1EdeS0YBedvAe9EDOHGgB3p/3Ue6UiY2GCE=; fh=IoyuW1MdpAH/ckqYJDA59MiNzAXOSnj+fQjbVLQI568=; b=cdvrdvthXac+egUjNpkSovk+Xmu1McSreMxhMuWzcwQQzb3ewTHi15izwJ/PNeHAZP bSjUYZ7iTJ+s3L3V5uA1kgsPWmLbUby4t9F0HXYQeZFHRnrODtvWMLEe6qNnNcK385SZ IfYabyGsIEw6wSPpKHNClQ1iepioij1FzRp9gYqulrhXygqkWztG6K7gGDBaCenOPXU5 EOUNEJ7mppD2JqAYz88RO2uoh4x6HDGdKsNDEWi5OxS4o8Y9Hzvqhayml7ULyGJ/1HNn rrbJlVTcvYi1Mc6IlgaFDEovWCEOSC87yDvOYUITQkjEGED9Xa2fpfcfVCtwDhhtuhWp FsYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=132Uw3jf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id s22-20020a056a00179600b00690d4b91636si17903724pfg.229.2023.09.27.11.53.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 11:53:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=132Uw3jf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id E3208802DF31; Wed, 27 Sep 2023 01:35:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230294AbjI0IfF (ORCPT + 99 others); Wed, 27 Sep 2023 04:35:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230421AbjI0Iei (ORCPT ); Wed, 27 Sep 2023 04:34:38 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 458F11B3; Wed, 27 Sep 2023 01:33:54 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12E5CC433C7; Wed, 27 Sep 2023 08:33:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1695803633; bh=I5DZTuEJvTywxXc0qMvlpl6ebHjK8ISFhOGKsz7oZgk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=132Uw3jfwPEJVezfW+Ns78q1UcSiSTw9uJW4qYSDHVitzTUG01Mju4TknFo/4xRAd /3ztEnzyxLxFZ6/Zuozy30RmREj5YmdU7GxGWv70kzUN8BOtFkhYBDbUgS8n/FuajJ MTS49kXLOMUoQQ+edrzW2VHszhz2/Z9c8ZKwFnao= Date: Wed, 27 Sep 2023 10:33:50 +0200 From: Greg KH To: Wei Liu Cc: Nuno Das Neves , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, patches@lists.linux.dev, mikelley@microsoft.com, kys@microsoft.com, haiyangz@microsoft.com, decui@microsoft.com, apais@linux.microsoft.com, Tianyu.Lan@microsoft.com, ssengar@linux.microsoft.com, mukeshrathor@microsoft.com, stanislav.kinsburskiy@gmail.com, jinankjain@linux.microsoft.com, vkuznets@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, will@kernel.org, catalin.marinas@arm.com Subject: Re: [PATCH v3 15/15] Drivers: hv: Add modules to expose /dev/mshv to VMMs running on Hyper-V Message-ID: <2023092757-cupbearer-cancel-b314@gregkh> References: <2023092342-staunch-chafe-1598@gregkh> <2023092630-masculine-clinic-19b6@gregkh> <2023092614-tummy-dwelling-7063@gregkh> <2023092646-version-series-a7b5@gregkh> <05119cbc-155d-47c5-ab21-e6a08eba5dc4@linux.microsoft.com> <2023092737-daily-humility-f01c@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 27 Sep 2023 01:35:32 -0700 (PDT) On Wed, Sep 27, 2023 at 08:04:42AM +0000, Wei Liu wrote: > On Wed, Sep 27, 2023 at 08:01:01AM +0200, Greg KH wrote: > [...] > > > > > If we're working with real devices like network cards or graphics cards > > > > > I would agree -- it is easy to imagine that we have several cards of the > > > > > same model in the system -- but in real world there won't be two > > > > > hypervisor instances running on the same hardware. > > > > > > > > > > We can stash the struct device inside some private data fields, but that > > > > > doesn't change the fact that we're still having one instance of the > > > > > structure. Is this what you want? Or do you have something else in mind? > > > > > > > > You have a real device, it's how userspace interacts with your > > > > subsystem. Please use that, it is dynamically created and handled and > > > > is the correct representation here. > > > > > > > > > > Are you referring to the struct device we get from calling > > > misc_register? > > > > Yes. > > > > We know about this, please see below. And we plan to use this. > > > > How would you suggest we get a reference to that device via e.g. open() > > > or ioctl() without keeping a global reference to it? > > > > You explicitly have it in your open() and ioctl() call, you never need a > > global reference for it the kernel gives it to you! > > > > This is what I don't follow. > > Nuno and I discussed this today offline. We looked at the code before > and looked again today (well, yesterday now). > > Here are the two functions: > > int vfs_open(const struct path *path, struct file *file) > long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) > > Or, if we provide an open function in our file_operations struct, we get > an additional struct inode pointer. > > int (*open) (struct inode *, struct file *); > > Neither struct file nor struct inode contains a reference to struct device. > > Then in vfs.rst, there is a section about open: > > ``open`` > called by the VFS when an inode should be opened. When the VFS > opens a file, it creates a new "struct file". It then calls the > open method for the newly allocated file structure. You might > think that the open method really belongs in "struct > inode_operations", and you may be right. I think it's done the > way it is because it makes filesystems simpler to implement. > The open() method is a good place to initialize the > "private_data" member in the file structure if you want to point > to a device structure > > So, the driver is supposed to stash a pointer to struct device in > private_data. That's what I alluded to in my previous reply. The core > driver framework or the VFS doesn't give us a reference to struct > device. We have to do it ourselves. Please read Linux Device Drivers, 3rd edition, chapter 3, for how to do this properly. The book is free online. Also look at the zillion in-kernel example drivers that use the misc device api, container_of() is your friend... > We can do that for sure, but the struct device we stash into > private_data is going to be the one that is returned from misc_register, > which at the same time is already stashed inside a static variable in > our driver by our own code (Note that this is a pervasive pattern in the > kernel). Again, don't make this static, there's no requirement to do so. But even if you do, sure, use it this way, you have a device. But I would strongly discourage you from having a static variable, there is no need to do this at all, and no one else should do so either. thanks, greg k-h