linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: Liu Yi L <yi.l.liu@intel.com>
Cc: kwankhede@nvidia.com, kevin.tian@intel.com,
	baolu.lu@linux.intel.com, yi.y.sun@intel.com, joro@8bytes.org,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	yan.y.zhao@intel.com, shaopeng.he@intel.com,
	chenbo.xia@intel.com, jun.j.tian@intel.com
Subject: Re: [PATCH v2 08/13] vfio/pci: protect cap/ecap_perm bits alloc/free with atomic op
Date: Wed, 25 Sep 2019 20:36:20 -0600	[thread overview]
Message-ID: <20190925203620.301c66ca@x1.home> (raw)
In-Reply-To: <1567670370-4484-9-git-send-email-yi.l.liu@intel.com>

On Thu,  5 Sep 2019 15:59:25 +0800
Liu Yi L <yi.l.liu@intel.com> wrote:

> There is a case in which cap_perms and ecap_perms can be reallocated
> by different modules. e.g. the vfio-mdev-pci sample driver. To secure
> the initialization of cap_perms and ecap_perms, this patch adds an
> atomic variable to track the user of cap/ecap_perms bits. First caller
> of vfio_pci_init_perm_bits() will initialize the bits. While the last
> caller of vfio_pci_uninit_perm_bits() will free the bits.

Yes, but it still allows races; we're not really protecting the data.
If driver A begins freeing the shared data in the uninit path, driver B
could start allocating shared data in the init path and we're left with
either use after free issues or memory leaks.  Probably better to hold
a semaphore around the allocation/free and a non-atomic for reference
counting.  Thanks,

Alex
 
> Cc: Kevin Tian <kevin.tian@intel.com>
> Cc: Lu Baolu <baolu.lu@linux.intel.com>
> Suggested-by: Alex Williamson <alex.williamson@redhat.com>
> Signed-off-by: Liu Yi L <yi.l.liu@intel.com>
> ---
>  drivers/vfio/pci/vfio_pci_config.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> index f0891bd..1b3e6e5 100644
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
> @@ -992,11 +992,17 @@ static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
>  	return 0;
>  }
>  
> +/* Track the user number of the cap/ecap perm_bits */
> +atomic_t vfio_pci_perm_bits_users = ATOMIC_INIT(0);
> +
>  /*
>   * Initialize the shared permission tables
>   */
>  void vfio_pci_uninit_perm_bits(void)
>  {
> +	if (atomic_dec_return(&vfio_pci_perm_bits_users))
> +		return;
> +
>  	free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
>  
>  	free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
> @@ -1013,6 +1019,9 @@ int __init vfio_pci_init_perm_bits(void)
>  {
>  	int ret;
>  
> +	if (atomic_inc_return(&vfio_pci_perm_bits_users) != 1)
> +		return 0;
> +
>  	/* Basic config space */
>  	ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
>  


  reply	other threads:[~2019-09-26  2:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05  7:59 [PATCH v2 00/13] vfio_pci: wrap pci device as a mediated device Liu Yi L
2019-09-05  7:59 ` [PATCH v2 01/13] vfio_pci: move vfio_pci_is_vga/vfio_vga_disabled to header Liu Yi L
2019-09-05  7:59 ` [PATCH v2 02/13] vfio_pci: refine user config reference in vfio-pci module Liu Yi L
2019-09-26  2:36   ` Alex Williamson
2019-09-30 12:38     ` Liu, Yi L
2019-09-05  7:59 ` [PATCH v2 03/13] vfio_pci: refine vfio_pci_driver reference in vfio_pci.c Liu Yi L
2019-09-05  7:59 ` [PATCH v2 04/13] vfio_pci: make common functions be extern Liu Yi L
2019-09-05  7:59 ` [PATCH v2 05/13] vfio_pci: duplicate vfio_pci.c Liu Yi L
2019-09-05  7:59 ` [PATCH v2 06/13] vfio_pci: shrink vfio_pci_common.c Liu Yi L
2019-09-05  7:59 ` [PATCH v2 07/13] vfio_pci: shrink vfio_pci.c Liu Yi L
2019-09-05  7:59 ` [PATCH v2 08/13] vfio/pci: protect cap/ecap_perm bits alloc/free with atomic op Liu Yi L
2019-09-26  2:36   ` Alex Williamson [this message]
2019-09-30 12:38     ` Liu, Yi L
2019-09-05  7:59 ` [PATCH v2 09/13] samples: add vfio-mdev-pci driver Liu Yi L
2019-09-05  7:59 ` [PATCH v2 10/13] samples: refine " Liu Yi L
2019-09-26  2:36   ` Alex Williamson
2019-09-30 12:39     ` Liu, Yi L
2019-09-05  7:59 ` [PATCH v2 11/13] samples/vfio-mdev-pci: call vfio_add_group_dev() Liu Yi L
2019-09-26  2:36   ` Alex Williamson
2019-09-30 12:40     ` Liu, Yi L
2019-09-05  7:59 ` [PATCH v2 12/13] vfio/type1: use iommu_attach_group() for wrapping PF/VF as mdev Liu Yi L
2019-09-26  2:37   ` Alex Williamson
2019-09-05  7:59 ` [PATCH v2 13/13] vfio/type1: track iommu backed group attach Liu Yi L
2019-09-25  9:13 ` [PATCH v2 00/13] vfio_pci: wrap pci device as a mediated device Liu, Yi L

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190925203620.301c66ca@x1.home \
    --to=alex.williamson@redhat.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=chenbo.xia@intel.com \
    --cc=joro@8bytes.org \
    --cc=jun.j.tian@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shaopeng.he@intel.com \
    --cc=yan.y.zhao@intel.com \
    --cc=yi.l.liu@intel.com \
    --cc=yi.y.sun@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).