Skip to contentSkip to conversation

drm/nouveau: gk20a: Turn instmem lock into mutex

PATCH In review Thierry Reding 3 participants 4 messages Jan 30, 2017 – Feb 24, 2017 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/nouveau: gk20a: Turn instmem lock into mutex

From: Thierry Reding <[email protected]>

The gk20a implementation of instance memory uses vmap()/vunmap() to map memory regions into the kernel's virtual address space. These functions may sleep, so protecting them by a spin lock is not safe. This triggers a warning if the DEBUG_ATOMIC_SLEEP Kconfig option is enabled. Fix this by using a mutex instead.

---
 drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +8 -11
index a6a7fa0d7679..7f5244d57d2f 100644
@@ -94,7 +94,7 @@ struct gk20a_instmem {
 	struct nvkm_instmem base;
 
 	/* protects vaddr_* and gk20a_instobj::vaddr* */
-	spinlock_t lock;
+	struct mutex lock;
 
 	/* CPU mappings LRU */
 	unsigned int vaddr_use;
@@ -184,11 +184,10 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
 	struct gk20a_instmem *imem = node->base.imem;
 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
 	const u64 size = nvkm_memory_size(memory);
-	unsigned long flags;
 
 	nvkm_ltc_flush(ltc);
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	if (node->base.vaddr) {
 		if (!node->use_cpt) {
@@ -216,7 +215,7 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
 
 out:
 	node->use_cpt++;
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	return node->base.vaddr;
 }
@@ -239,9 +238,8 @@ gk20a_instobj_release_iommu(struct nvkm_memory *memory)
 	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
 	struct gk20a_instmem *imem = node->base.imem;
 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
-	unsigned long flags;
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	/* we should at least have one user to release... */
 	if (WARN_ON(node->use_cpt == 0))
@@ -252,7 +250,7 @@ gk20a_instobj_release_iommu(struct nvkm_memory *memory)
 		list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
 
 out:
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	wmb();
 	nvkm_ltc_invalidate(ltc);
@@ -306,19 +304,18 @@ gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
 	struct gk20a_instmem *imem = node->base.imem;
 	struct device *dev = imem->base.subdev.device->dev;
 	struct nvkm_mm_node *r;
-	unsigned long flags;
 	int i;
 
 	if (unlikely(list_empty(&node->base.mem.regions)))
 		goto out;
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	/* vaddr has already been recycled */
 	if (node->base.vaddr)
 		gk20a_instobj_iommu_recycle_vaddr(node);
 
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	r = list_first_entry(&node->base.mem.regions, struct nvkm_mm_node,
 			     rl_entry);
@@ -580,7 +577,7 @@ gk20a_instmem_new(struct nvkm_device *device, int index,
 	if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
 		return -ENOMEM;
 	nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
-	spin_lock_init(&imem->lock);
+	mutex_init(&imem->lock);
 	*pimem = &imem->base;
 
 	/* do not allow more than 1MB of CPU-mapped instmem */
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 8 lines · Thierry Reding

From: Thierry Reding <[email protected]>

The gk20a implementation of instance memory uses vmap()/vunmap() to map memory regions into the kernel's virtual address space. These functions may sleep, so protecting them by a spin lock is not safe. This triggers a warning if the DEBUG_ATOMIC_SLEEP Kconfig option is enabled. Fix this by using a mutex instead.

Quoted 4 lines
---
 drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

Alex, could you take a look at this?

Thanks,
Thierry

Quoted 89 lines
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +8 -11
index a6a7fa0d7679..7f5244d57d2f 100644
@@ -94,7 +94,7 @@ struct gk20a_instmem {
 	struct nvkm_instmem base;
 
 	/* protects vaddr_* and gk20a_instobj::vaddr* */
-	spinlock_t lock;
+	struct mutex lock;
 
 	/* CPU mappings LRU */
 	unsigned int vaddr_use;
@@ -184,11 +184,10 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
 	struct gk20a_instmem *imem = node->base.imem;
 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
 	const u64 size = nvkm_memory_size(memory);
-	unsigned long flags;
 
 	nvkm_ltc_flush(ltc);
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	if (node->base.vaddr) {
 		if (!node->use_cpt) {
@@ -216,7 +215,7 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
 
 out:
 	node->use_cpt++;
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	return node->base.vaddr;
 }
@@ -239,9 +238,8 @@ gk20a_instobj_release_iommu(struct nvkm_memory *memory)
 	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
 	struct gk20a_instmem *imem = node->base.imem;
 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
-	unsigned long flags;
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	/* we should at least have one user to release... */
 	if (WARN_ON(node->use_cpt == 0))
@@ -252,7 +250,7 @@ gk20a_instobj_release_iommu(struct nvkm_memory *memory)
 		list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
 
 out:
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	wmb();
 	nvkm_ltc_invalidate(ltc);
@@ -306,19 +304,18 @@ gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
 	struct gk20a_instmem *imem = node->base.imem;
 	struct device *dev = imem->base.subdev.device->dev;
 	struct nvkm_mm_node *r;
-	unsigned long flags;
 	int i;
 
 	if (unlikely(list_empty(&node->base.mem.regions)))
 		goto out;
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	/* vaddr has already been recycled */
 	if (node->base.vaddr)
 		gk20a_instobj_iommu_recycle_vaddr(node);
 
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	r = list_first_entry(&node->base.mem.regions, struct nvkm_mm_node,
 			     rl_entry);
@@ -580,7 +577,7 @@ gk20a_instmem_new(struct nvkm_device *device, int index,
 	if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
 		return -ENOMEM;
 	nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
-	spin_lock_init(&imem->lock);
+	mutex_init(&imem->lock);
 	*pimem = &imem->base;
 
 	/* do not allow more than 1MB of CPU-mapped instmem */
Signature · 1 line
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Reviewed-by Tested-by
#2 lore raw
Quoted 11 lines · Thierry Reding

* PGP Signed by an unknown key

Quoted 8 lines · Thierry Reding

From: Thierry Reding <[email protected]>

The gk20a implementation of instance memory uses vmap()/vunmap() to map memory regions into the kernel's virtual address space. These functions may sleep, so protecting them by a spin lock is not safe. This triggers a warning if the DEBUG_ATOMIC_SLEEP Kconfig option is enabled. Fix this by using a mutex instead.

Quoted 6 lines
Quoted 4 lines
---
 drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

Alex, could you take a look at this?

Sorry! Yes, using a mutex here should be safe since vmap() can sleep anyway. And I don't think this code can ever be reached in atomic context (Ben can confirm that last point). Tested this patch and it seems to work like a charm.

Quoted 89 lines

Thanks,
Thierry

Quoted 85 lines
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +8 -11
index a6a7fa0d7679..7f5244d57d2f 100644
@@ -94,7 +94,7 @@ struct gk20a_instmem {
 	struct nvkm_instmem base;
 
 	/* protects vaddr_* and gk20a_instobj::vaddr* */
-	spinlock_t lock;
+	struct mutex lock;
 
 	/* CPU mappings LRU */
 	unsigned int vaddr_use;
@@ -184,11 +184,10 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
 	struct gk20a_instmem *imem = node->base.imem;
 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
 	const u64 size = nvkm_memory_size(memory);
-	unsigned long flags;
 
 	nvkm_ltc_flush(ltc);
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	if (node->base.vaddr) {
 		if (!node->use_cpt) {
@@ -216,7 +215,7 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
 
 out:
 	node->use_cpt++;
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	return node->base.vaddr;
 }
@@ -239,9 +238,8 @@ gk20a_instobj_release_iommu(struct nvkm_memory *memory)
 	struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
 	struct gk20a_instmem *imem = node->base.imem;
 	struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
-	unsigned long flags;
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	/* we should at least have one user to release... */
 	if (WARN_ON(node->use_cpt == 0))
@@ -252,7 +250,7 @@ gk20a_instobj_release_iommu(struct nvkm_memory *memory)
 		list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
 
 out:
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	wmb();
 	nvkm_ltc_invalidate(ltc);
@@ -306,19 +304,18 @@ gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
 	struct gk20a_instmem *imem = node->base.imem;
 	struct device *dev = imem->base.subdev.device->dev;
 	struct nvkm_mm_node *r;
-	unsigned long flags;
 	int i;
 
 	if (unlikely(list_empty(&node->base.mem.regions)))
 		goto out;
 
-	spin_lock_irqsave(&imem->lock, flags);
+	mutex_lock(&imem->lock);
 
 	/* vaddr has already been recycled */
 	if (node->base.vaddr)
 		gk20a_instobj_iommu_recycle_vaddr(node);
 
-	spin_unlock_irqrestore(&imem->lock, flags);
+	mutex_unlock(&imem->lock);
 
 	r = list_first_entry(&node->base.mem.regions, struct nvkm_mm_node,
 			     rl_entry);
@@ -580,7 +577,7 @@ gk20a_instmem_new(struct nvkm_device *device, int index,
 	if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
 		return -ENOMEM;
 	nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
-	spin_lock_init(&imem->lock);
+	mutex_init(&imem->lock);
 	*pimem = &imem->base;
 
Quoted 8 lines
Quoted 4 lines
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +0 -1
 	/* do not allow more than 1MB of CPU-mapped instmem */
--

2.11.0

* Unknown Key
* 0x7F3EB3A1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 6febf110-a04a-da21-20b4-638bde1eaf36-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org

#4 lore raw
Quoted 18 lines · Alexandre Courbot
Quoted 17 lines · Thierry Reding

* PGP Signed by an unknown key

Quoted 13 lines · Thierry Reding

From: Thierry Reding <[email protected]>

The gk20a implementation of instance memory uses vmap()/vunmap() to map memory regions into the kernel's virtual address space. These functions may sleep, so protecting them by a spin lock is not safe. This triggers a warning if the DEBUG_ATOMIC_SLEEP Kconfig option is enabled. Fix this by using a mutex instead.

---
 drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c | 19
++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
Quoted 6 lines
Quoted 1 line

Alex, could you take a look at this?

Sorry! Yes, using a mutex here should be safe since vmap() can sleep anyway. And I don't think this code can ever be reached in atomic context (Ben can confirm that last point). Tested this patch and it seems to work like a charm.

That should be true on this chipset. Though, if we ever need to touch the grctx or something in response to an interrupt (iirc nvgpu does this, maybe?), this could change. Granted, we probably should switch to threaded interrupts at some point anyway.

As it currently stands, it should be fine.

Ben.

Quoted 109 lines
Quoted 96 lines

Thanks,
Thierry

Quoted 92 lines
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +2 -3
index a6a7fa0d7679..7f5244d57d2f 100644
@@ -94,7 +94,7 @@ struct gk20a_instmem {
     struct nvkm_instmem base;
 
     /* protects vaddr_* and gk20a_instobj::vaddr* */
-    spinlock_t lock;
+    struct mutex lock;
 
     /* CPU mappings LRU */
     unsigned int vaddr_use;
@@ -184,11 +184,10 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory
 *memory)
     struct gk20a_instmem *imem = node->base.imem;
     struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
     const u64 size = nvkm_memory_size(memory);
-    unsigned long flags;
 
     nvkm_ltc_flush(ltc);
 
-    spin_lock_irqsave(&imem->lock, flags);
+    mutex_lock(&imem->lock);
 
     if (node->base.vaddr) {
         if (!node->use_cpt) {
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +1 -1
@@ -216,7 +215,7 @@ gk20a_instobj_acquire_iommu(struct nvkm_memory
 *memory)
 
 out:
     node->use_cpt++;
-    spin_unlock_irqrestore(&imem->lock, flags);
+    mutex_unlock(&imem->lock);
 
     return node->base.vaddr;

}

drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +1 -2
@@ -239,9 +238,8 @@ gk20a_instobj_release_iommu(struct nvkm_memory
 *memory)
     struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
     struct gk20a_instmem *imem = node->base.imem;
     struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
-    unsigned long flags;
 
-    spin_lock_irqsave(&imem->lock, flags);
+    mutex_lock(&imem->lock);
 
     /* we should at least have one user to release... */
     if (WARN_ON(node->use_cpt == 0))
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +1 -1
@@ -252,7 +250,7 @@ gk20a_instobj_release_iommu(struct nvkm_memory
 *memory)
         list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
 
 out:
-    spin_unlock_irqrestore(&imem->lock, flags);
+    mutex_unlock(&imem->lock);
 
     wmb();
     nvkm_ltc_invalidate(ltc);
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +2 -3
@@ -306,19 +304,18 @@ gk20a_instobj_dtor_iommu(struct nvkm_memory
 *memory)
     struct gk20a_instmem *imem = node->base.imem;
     struct device *dev = imem->base.subdev.device->dev;
     struct nvkm_mm_node *r;
-    unsigned long flags;
     int i;
 
     if (unlikely(list_empty(&node->base.mem.regions)))
         goto out;
 
-    spin_lock_irqsave(&imem->lock, flags);
+    mutex_lock(&imem->lock);
 
     /* vaddr has already been recycled */
     if (node->base.vaddr)
         gk20a_instobj_iommu_recycle_vaddr(node);
 
-    spin_unlock_irqrestore(&imem->lock, flags);
+    mutex_unlock(&imem->lock);
 
     r = list_first_entry(&node->base.mem.regions, struct nvkm_mm_node,
                  rl_entry);
drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c +1 -1
@@ -580,7 +577,7 @@ gk20a_instmem_new(struct nvkm_device *device, int
 index,
     if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
         return -ENOMEM;
     nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
-    spin_lock_init(&imem->lock);
+    mutex_init(&imem->lock);
     *pimem = &imem->base;
 
Quoted 8 lines
Quoted 4 lines
     /* do not allow more than 1MB of CPU-mapped instmem */
Signature · 1 line
2.11.0

* Unknown Key
* 0x7F3EB3A1

Quoted 4 lines

_______________________________________________
Nouveau mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau