Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2385685ybk; Sun, 17 May 2020 20:00:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz+6S+yKyikC3jkGZ+N2mVRjfK19zqBoHZBa54a2wqT+8FcZhRaI0IwezxMcpUskZ4QpzAP X-Received: by 2002:a50:f311:: with SMTP id p17mr11595624edm.284.1589770846783; Sun, 17 May 2020 20:00:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589770846; cv=none; d=google.com; s=arc-20160816; b=kw3h+3wY1vfuGtJlIP5hfgNm1kKGQF5FaB3/QKGSgfSz9haXU9s32LWbjCvN/2J54T CyCasfY62KYR8SJCwj0NaJwzak/T910UKKeuD2HXA0TN4pv011Tx7DDjYTDNeBXRjcay M8Rvnn+z2UH8ivS76+czktSQdTKWgPAryId7/hSu2kj/ScEvYGA7zXo5JjIZ6ZnJa/ic YvEakqezok1KgoCfphusYNlzJV3ZRNsrbrGvitleh27deynyOCKNBqtyRl5YwytWw+F7 DUABPiZgHmh1j1novLxr0NB9ERfGytKsz37lSedwAcMex+zUp5a3pipSQcKANM0uoI/u W0vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr:ironport-sdr; bh=YUlWgqhWL1cX8VbzOd1smdmYFGcTJN0d6Lntx/Te1xA=; b=J6rMb07wwedZlCZ8OQSNpcCZenkp2jPkxdt5tzHTAeGnECReD8smS1bxrP0R9Yan4P hscUfDaud6uPMMMSSFBKsZoF9UJmSXh2qb/RUVgpiPaswQfWBuaixo4OmqAF9I/GpUwc fqZHf80WYKyBObqPDBGK0qwGKYw86tLL8Yo60tJUyWx+epKYs8+cxQgeAemos+Jg+kC0 38pHjeFVpKUVCAEC/Rv9FNUXpHgo8vSfKlLYo9t4jDPTitezWUqT/T3VUbbqZZ+VF0XS kQsdVJS/nvkKlrXY2xVFPI9TXUzkH0RJxJI1b4vUe6Istr/FLWFZ9tpz2bw34U03MvyG ETRg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id nz19si5550744ejb.495.2020.05.17.20.00.23; Sun, 17 May 2020 20:00:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726945AbgERCzX (ORCPT + 99 others); Sun, 17 May 2020 22:55:23 -0400 Received: from mga18.intel.com ([134.134.136.126]:29280 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726639AbgERCzW (ORCPT ); Sun, 17 May 2020 22:55:22 -0400 IronPort-SDR: jEwfjgTW73c42okrEuuT7PBqz32zu76m2cohSCWJFZRDuINEgQgzmNLC3eCO3UCr4XP+cKmxfp iWq+Gw3begWw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2020 19:55:21 -0700 IronPort-SDR: OglWgLH78bA92/WGFbFfRAjAgOZ3+ncDSkAz2DVB2civwg7YxMXX52i0cJOEpn49OAm94bmAL6 sKi6ocpyuOWA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,405,1583222400"; d="scan'208";a="411104064" Received: from joy-optiplex-7040.sh.intel.com ([10.239.13.16]) by orsmga004.jf.intel.com with ESMTP; 17 May 2020 19:55:18 -0700 From: Yan Zhao To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: alex.williamson@redhat.com, cohuck@redhat.com, zhenyuw@linux.intel.com, zhi.a.wang@intel.com, kevin.tian@intel.com, shaopeng.he@intel.com, yi.l.liu@intel.com, xin.zeng@intel.com, hang.yuan@intel.com, Yan Zhao Subject: [RFC PATCH v4 02/10] vfio/pci: macros to generate module_init and module_exit for vendor modules Date: Sun, 17 May 2020 22:45:10 -0400 Message-Id: <20200518024510.14115-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200518024202.13996-1-yan.y.zhao@intel.com> References: <20200518024202.13996-1-yan.y.zhao@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org vendor modules call macro module_vfio_pci_register_vendor_handler to generate module_init and module_exit. It is necessary to ensure that vendor modules always call vfio_pci_register_vendor_driver() on driver loading and vfio_pci_unregister_vendor_driver on driver unloading, because (1) at compiling time, there's only a dependency of vendor modules on vfio_pci. (2) at runtime, - vendor modules add refs of vfio_pci on a successful calling of vfio_pci_register_vendor_driver() and deref of vfio_pci on a successful calling of vfio_pci_unregister_vendor_driver(). - vfio_pci only adds refs of vendor module on a successful probe of vendor driver. vfio_pci derefs vendor module when unbinding from a device. So, after vfio_pci is unbound from a device, the vendor module to that device is free to get unloaded. However, if that vendor module does not call vfio_pci_unregister_vendor_driver() in its module_exit, vfio_pci may hold a stale pointer to vendor module. Cc: Kevin Tian Suggested-by: Alex Williamson Signed-off-by: Yan Zhao --- include/linux/vfio.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 3e53deb012b6..f3746608c2d9 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -223,4 +223,31 @@ struct vfio_pci_vendor_driver_ops { }; int __vfio_pci_register_vendor_driver(struct vfio_pci_vendor_driver_ops *ops); void vfio_pci_unregister_vendor_driver(struct vfio_device_ops *device_ops); + +#define vfio_pci_register_vendor_driver(__name, __probe, __remove, \ + __device_ops) \ +static struct vfio_pci_vendor_driver_ops __ops ## _node = { \ + .owner = THIS_MODULE, \ + .name = __name, \ + .probe = __probe, \ + .remove = __remove, \ + .device_ops = __device_ops, \ +}; \ +__vfio_pci_register_vendor_driver(&__ops ## _node) + +#define module_vfio_pci_register_vendor_handler(name, probe, remove, \ + device_ops) \ +static int __init device_ops ## _module_init(void) \ +{ \ + vfio_pci_register_vendor_driver(name, probe, remove, \ + device_ops); \ + return 0; \ +}; \ +static void __exit device_ops ## _module_exit(void) \ +{ \ + vfio_pci_unregister_vendor_driver(device_ops); \ +}; \ +module_init(device_ops ## _module_init); \ +module_exit(device_ops ## _module_exit) + #endif /* VFIO_H */ -- 2.17.1