drm_lease_held() only answers whether a given object is part of a lease,
and the answer can change from one call to the next. Code that needs to
look at several objects in a row has no way to tell whether the lease it
has been checking against is still around by the time it is done.

Add a helper reporting whether a lease has been revoked, so that such
code can bail out instead of acting on a lease that went away.

Signed-off-by: Maxime Ripard <[email protected]>
---
 drivers/gpu/drm/drm_lease.c | 30 ++++++++++++++++++++++++++++++
 include/drm/drm_auth.h      | 11 +++++++++++
 include/drm/drm_lease.h     |  2 ++
 3 files changed, 43 insertions(+)

diff --git a/drivers/gpu/drm/drm_lease.c b/drivers/gpu/drm/drm_lease.c
index 5d2cf724cbd7..f2370a9a18fc 100644
--- a/drivers/gpu/drm/drm_lease.c
+++ b/drivers/gpu/drm/drm_lease.c
@@ -145,10 +145,38 @@ bool drm_lease_held(struct drm_file *file_priv, int id)
 out:
        drm_master_put(&master);
        return ret;
 }
 
+/*
+ * Checks whether the file is a lessee whose lease has been revoked. A
+ * lease is always revoked as a whole, but empty leases can also be
+ * created on purpose, so the revocation is tracked explicitly in
+ * drm_master rather than inferred from an empty lease.
+ */
+bool drm_lease_revoked(struct drm_file *file_priv)
+{
+       struct drm_master *master;
+       bool ret = false;
+
+       if (!file_priv)
+               return false;
+
+       master = drm_file_get_master(file_priv);
+       if (!master)
+               return false;
+
+       if (master->lessor) {
+               mutex_lock(&master->dev->mode_config.idr_mutex);
+               ret = master->lease_revoked;
+               mutex_unlock(&master->dev->mode_config.idr_mutex);
+       }
+
+       drm_master_put(&master);
+       return ret;
+}
+
 /*
  * Given a bitmask of crtcs to check, reconstructs a crtc mask based on the
  * crtcs which are visible through the specified file.
  */
 uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
@@ -315,10 +343,12 @@ static void _drm_lease_revoke(struct drm_master *top)
 
                /* Evacuate the lease */
                idr_for_each_entry(&master->leases, entry, object)
                        idr_remove(&master->leases, object);
 
+               master->lease_revoked = true;
+
                /* Depth-first list walk */
 
                /* Down */
                if (!list_empty(&master->lessees)) {
                        master = list_first_entry(&master->lessees, struct 
drm_master, lessee_list);
diff --git a/include/drm/drm_auth.h b/include/drm/drm_auth.h
index 50131383ed81..b1962f197b05 100644
--- a/include/drm/drm_auth.h
+++ b/include/drm/drm_auth.h
@@ -118,10 +118,21 @@ struct drm_master {
         * Objects are leased all together in drm_lease_create(), and are
         * removed all together when the lease is revoked.
         */
        struct idr leases;
 
+       /**
+        * @lease_revoked:
+        *
+        * Whether the lease of this drm_master has been revoked. Since empty
+        * leases can be created on purpose, an empty &leases doesn't imply a
+        * revocation, so it has to be tracked explicitly. Never cleared once
+        * set: a revoked master cannot be leased anything again. Protected by
+        * &drm_device.mode_config's &drm_mode_config.idr_mutex.
+        */
+       bool lease_revoked;
+
        /**
         * @lessee_idr:
         *
         * All lessees under this owner (only used where @lessor is NULL).
         * Protected by &drm_device.mode_config's &drm_mode_config.idr_mutex.
diff --git a/include/drm/drm_lease.h b/include/drm/drm_lease.h
index 53545b4ca9ef..03f7ea36c37c 100644
--- a/include/drm/drm_lease.h
+++ b/include/drm/drm_lease.h
@@ -18,10 +18,12 @@ void drm_lease_destroy(struct drm_master *lessee);
 
 bool drm_lease_held(struct drm_file *file_priv, int id);
 
 bool _drm_lease_held(struct drm_file *file_priv, int id);
 
+bool drm_lease_revoked(struct drm_file *file_priv);
+
 void drm_lease_revoke(struct drm_master *master);
 
 uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs);
 
 int drm_mode_create_lease_ioctl(struct drm_device *dev,

-- 
2.56.0

Reply via email to