if (!dev_data)
                return NULL;
 
+       spin_lock_init(&dev_data->lock);
        dev_data->devid = devid;
        ratelimit_default_init(&dev_data->rs);
 
 
        dev_data = get_dev_data(dev);
 
+       spin_lock(&dev_data->lock);
+
        ret = -EBUSY;
        if (dev_data->domain != NULL)
                goto out;
        domain_flush_complete(domain);
 
 out:
+       spin_unlock(&dev_data->lock);
+
        spin_unlock_irqrestore(&domain->lock, flags);
 
        return ret;
 
        spin_lock_irqsave(&domain->lock, flags);
 
+       spin_lock(&dev_data->lock);
+
        /*
         * First check if the device is still attached. It might already
         * be detached from its domain because the generic
        dev_data->ats.enabled = false;
 
 out:
+       spin_unlock(&dev_data->lock);
+
        spin_unlock_irqrestore(&domain->lock, flags);
 }
 
 
  * This struct contains device specific data for the IOMMU
  */
 struct iommu_dev_data {
+       /*Protect against attach/detach races */
+       spinlock_t lock;
+
        struct list_head list;            /* For domain->dev_list */
        struct llist_node dev_data_list;  /* For global dev_data_list */
        struct protection_domain *domain; /* Domain the device is bound to */