Re: [PATCH v2] drm/i915: Fix page cleanup on DMA remap failure
On Wed, Jan 22, 2025 at 10:07 PM Srinivas, Vidya wrote: > > Hello Brian, Many thanks for the fix. I am adding my tested-by. > Tested-by: Vidya Srinivas Thanks for testing Vidya. Can we get a maintainer to take a look? > > > > -Original Message- > > From: Brian Geffon > > Sent: 16 January 2025 21:24 > > To: intel-...@lists.freedesktop.org > > Cc: Wilson, Chris P ; Saarinen, Jani > > ; Mistat, Tomasz ; > > Srinivas, Vidya ; ville.syrj...@linux.intel.com; > > jani.nik...@linux.intel.com; linux-ker...@vger.kernel.org; dri- > > de...@lists.freedesktop.org; Joonas Lahtinen > > ; Brian Geffon ; > > sta...@vger.kernel.org; Tomasz Figa > > Subject: [PATCH v2] drm/i915: Fix page cleanup on DMA remap failure > > > > When converting to folios the cleanup path of shmem_get_pages() was > > missed. When a DMA remap fails and the max segment size is greater than > > PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment size. > > The cleanup code isn't properly using the folio apis and as a result isn't > > handling compound pages correctly. > > > > v1 -> v2: > > (Ville) Fixed locations where we were not clearing mapping unevictable. > > > > Cc: sta...@vger.kernel.org > > Cc: Ville Syrjala > > Cc: Vidya Srinivas > > Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 > > Link: https://lore.kernel.org/lkml/20250116135636.410164-1- > > bgef...@google.com/ > > Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a > > folio_batch") > > Signed-off-by: Brian Geffon > > Suggested-by: Tomasz Figa > > --- > > drivers/gpu/drm/i915/gem/i915_gem_object.h | 3 +-- > > drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 23 +- > > drivers/gpu/drm/i915/gem/i915_gem_ttm.c| 7 --- > > 3 files changed, 14 insertions(+), 19 deletions(-) > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.h > > b/drivers/gpu/drm/i915/gem/i915_gem_object.h > > index 3dc61cbd2e11..0f122a12d4a5 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_object.h > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.h > > @@ -843,8 +843,7 @@ int shmem_sg_alloc_table(struct drm_i915_private > > *i915, struct sg_table *st, > >size_t size, struct intel_memory_region *mr, > >struct address_space *mapping, > >unsigned int max_segment); > > -void shmem_sg_free_table(struct sg_table *st, struct address_space > > *mapping, > > - bool dirty, bool backup); > > +void shmem_sg_free_table(struct sg_table *st, bool dirty, bool backup); > > void __shmem_writeback(size_t size, struct address_space *mapping); > > > > #ifdef CONFIG_MMU_NOTIFIER > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > index fe69f2c8527d..b320d9dfd6d3 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > @@ -29,16 +29,13 @@ static void check_release_folio_batch(struct > > folio_batch *fbatch) > > cond_resched(); > > } > > > > -void shmem_sg_free_table(struct sg_table *st, struct address_space > > *mapping, > > - bool dirty, bool backup) > > +void shmem_sg_free_table(struct sg_table *st, bool dirty, bool backup) > > { > > struct sgt_iter sgt_iter; > > struct folio_batch fbatch; > > struct folio *last = NULL; > > struct page *page; > > > > - mapping_clear_unevictable(mapping); > > - > > folio_batch_init(&fbatch); > > for_each_sgt_page(page, sgt_iter, st) { > > struct folio *folio = page_folio(page); @@ -180,10 +177,10 > > @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table > > *st, > > return 0; > > err_sg: > > sg_mark_end(sg); > > + mapping_clear_unevictable(mapping); > > if (sg != st->sgl) { > > - shmem_sg_free_table(st, mapping, false, false); > > + shmem_sg_free_table(st, false, false); > > } else { > > - mapping_clear_unevictable(mapping); > > sg_free_table(st); > > } > > > > @@ -209,8 +206,6 @@ static int shmem_get_pages(struct > > drm_i915_gem_object *obj) > > struct address_space *mapping = obj->base.filp->f_mapping; > > unsigned int m
Re: [PATCH v2] drm/i915: Fix page cleanup on DMA remap failure
On Mon, Jan 27, 2025 at 1:47 PM Ville Syrjälä wrote: > > On Thu, Jan 16, 2025 at 10:53:40AM -0500, Brian Geffon wrote: > > When converting to folios the cleanup path of shmem_get_pages() was > > missed. When a DMA remap fails and the max segment size is greater than > > PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment > > size. The cleanup code isn't properly using the folio apis and as a > > result isn't handling compound pages correctly. > > > > v1 -> v2: > > (Ville) Fixed locations where we were not clearing mapping unevictable. > > > > Cc: sta...@vger.kernel.org > > Cc: Ville Syrjala > > Cc: Vidya Srinivas > > Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 > > Link: > > https://lore.kernel.org/lkml/20250116135636.410164-1-bgef...@google.com/ > > Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a > > folio_batch") > > Signed-off-by: Brian Geffon > > Suggested-by: Tomasz Figa > > --- > > drivers/gpu/drm/i915/gem/i915_gem_object.h | 3 +-- > > drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 23 +- > > drivers/gpu/drm/i915/gem/i915_gem_ttm.c| 7 --- > > 3 files changed, 14 insertions(+), 19 deletions(-) > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.h > > b/drivers/gpu/drm/i915/gem/i915_gem_object.h > > index 3dc61cbd2e11..0f122a12d4a5 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_object.h > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.h > > @@ -843,8 +843,7 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, > > struct sg_table *st, > >size_t size, struct intel_memory_region *mr, > >struct address_space *mapping, > >unsigned int max_segment); > > -void shmem_sg_free_table(struct sg_table *st, struct address_space > > *mapping, > > - bool dirty, bool backup); > > +void shmem_sg_free_table(struct sg_table *st, bool dirty, bool backup); > > void __shmem_writeback(size_t size, struct address_space *mapping); > > > > #ifdef CONFIG_MMU_NOTIFIER > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > index fe69f2c8527d..b320d9dfd6d3 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > @@ -29,16 +29,13 @@ static void check_release_folio_batch(struct > > folio_batch *fbatch) > > cond_resched(); > > } > > > > -void shmem_sg_free_table(struct sg_table *st, struct address_space > > *mapping, > > - bool dirty, bool backup) > > +void shmem_sg_free_table(struct sg_table *st, bool dirty, bool backup) > > This still makes the alloc vs. free completely asymmetric. > This is not what we want because it just makes it very easy > to make it mistake in the caller. > > I think the correct fix is to simply call the current > shmem_sg_free_table() from the now broken failure path. > mapping_{set,clear}_unevictable() just seems to be some > bit operation so AFAICS the slight ping-pong should be > inconsequential. Ok, I guess that's true, it'll be set unevictable again in the call to shmem_sg_alloc_table() after it jumps back to rebuild_st. I'll send a v3 which should then become a 1 line change. > > > { > > struct sgt_iter sgt_iter; > > struct folio_batch fbatch; > > struct folio *last = NULL; > > struct page *page; > > > > - mapping_clear_unevictable(mapping); > > - > > folio_batch_init(&fbatch); > > for_each_sgt_page(page, sgt_iter, st) { > > struct folio *folio = page_folio(page); > > @@ -180,10 +177,10 @@ int shmem_sg_alloc_table(struct drm_i915_private > > *i915, struct sg_table *st, > > return 0; > > err_sg: > > sg_mark_end(sg); > > + mapping_clear_unevictable(mapping); > > if (sg != st->sgl) { > > - shmem_sg_free_table(st, mapping, false, false); > > + shmem_sg_free_table(st, false, false); > > } else { > > - mapping_clear_unevictable(mapping); > > sg_free_table(st); > > } > > > > @@ -209,8 +206,6 @@ static int shmem_get_pages(struct drm_i915_gem_object > > *obj) > > struct address_space *mapping = obj->base.filp->f_mapping; > > unsigned int max_segment = i915_sg_segment_size(i915-&
[PATCH v3] drm/i915: Fix page cleanup on DMA remap failure
When converting to folios the cleanup path of shmem_get_pages() was missed. When a DMA remap fails and the max segment size is greater than PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment size. The cleanup code isn't properly using the folio apis and as a result isn't handling compound pages correctly. v2 -> v3: (Ville) Just use shmem_sg_free_table() as-is in the failure path of shmem_get_pages(). shmem_sg_free_table() will clear mapping unevictable but it will be reset when it retries in shmem_sg_alloc_table(). v1 -> v2: (Ville) Fixed locations where we were not clearing mapping unevictable. Cc: sta...@vger.kernel.org Cc: Ville Syrjala Cc: Vidya Srinivas Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 Link: https://lore.kernel.org/lkml/20250116135636.410164-1-bgef...@google.com/ Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a folio_batch") Signed-off-by: Brian Geffon Suggested-by: Tomasz Figa --- drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 6 +- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c index fe69f2c8527d..ae3343c81a64 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -209,8 +209,6 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) struct address_space *mapping = obj->base.filp->f_mapping; unsigned int max_segment = i915_sg_segment_size(i915->drm.dev); struct sg_table *st; - struct sgt_iter sgt_iter; - struct page *page; int ret; /* @@ -239,9 +237,7 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) * for PAGE_SIZE chunks instead may be helpful. */ if (max_segment > PAGE_SIZE) { - for_each_sgt_page(page, sgt_iter, st) - put_page(page); - sg_free_table(st); + shmem_sg_free_table(st, mapping, false, false); kfree(st); max_segment = PAGE_SIZE; -- 2.48.1.262.g85cc9f2d1e-goog
[PATCH v2] drm/i915: Fix page cleanup on DMA remap failure
When converting to folios the cleanup path of shmem_get_pages() was missed. When a DMA remap fails and the max segment size is greater than PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment size. The cleanup code isn't properly using the folio apis and as a result isn't handling compound pages correctly. v1 -> v2: (Ville) Fixed locations where we were not clearing mapping unevictable. Cc: sta...@vger.kernel.org Cc: Ville Syrjala Cc: Vidya Srinivas Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 Link: https://lore.kernel.org/lkml/20250116135636.410164-1-bgef...@google.com/ Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a folio_batch") Signed-off-by: Brian Geffon Suggested-by: Tomasz Figa --- drivers/gpu/drm/i915/gem/i915_gem_object.h | 3 +-- drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 23 +- drivers/gpu/drm/i915/gem/i915_gem_ttm.c| 7 --- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.h b/drivers/gpu/drm/i915/gem/i915_gem_object.h index 3dc61cbd2e11..0f122a12d4a5 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_object.h +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.h @@ -843,8 +843,7 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st, size_t size, struct intel_memory_region *mr, struct address_space *mapping, unsigned int max_segment); -void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping, -bool dirty, bool backup); +void shmem_sg_free_table(struct sg_table *st, bool dirty, bool backup); void __shmem_writeback(size_t size, struct address_space *mapping); #ifdef CONFIG_MMU_NOTIFIER diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c index fe69f2c8527d..b320d9dfd6d3 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -29,16 +29,13 @@ static void check_release_folio_batch(struct folio_batch *fbatch) cond_resched(); } -void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping, -bool dirty, bool backup) +void shmem_sg_free_table(struct sg_table *st, bool dirty, bool backup) { struct sgt_iter sgt_iter; struct folio_batch fbatch; struct folio *last = NULL; struct page *page; - mapping_clear_unevictable(mapping); - folio_batch_init(&fbatch); for_each_sgt_page(page, sgt_iter, st) { struct folio *folio = page_folio(page); @@ -180,10 +177,10 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st, return 0; err_sg: sg_mark_end(sg); + mapping_clear_unevictable(mapping); if (sg != st->sgl) { - shmem_sg_free_table(st, mapping, false, false); + shmem_sg_free_table(st, false, false); } else { - mapping_clear_unevictable(mapping); sg_free_table(st); } @@ -209,8 +206,6 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) struct address_space *mapping = obj->base.filp->f_mapping; unsigned int max_segment = i915_sg_segment_size(i915->drm.dev); struct sg_table *st; - struct sgt_iter sgt_iter; - struct page *page; int ret; /* @@ -239,9 +234,8 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) * for PAGE_SIZE chunks instead may be helpful. */ if (max_segment > PAGE_SIZE) { - for_each_sgt_page(page, sgt_iter, st) - put_page(page); - sg_free_table(st); + /* Leave the mapping unevictable while we retry */ + shmem_sg_free_table(st, false, false); kfree(st); max_segment = PAGE_SIZE; @@ -265,7 +259,8 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) return 0; err_pages: - shmem_sg_free_table(st, mapping, false, false); + mapping_clear_unevictable(mapping); + shmem_sg_free_table(st, false, false); /* * shmemfs first checks if there is enough memory to allocate the page * and reports ENOSPC should there be insufficient, along with the usual @@ -402,8 +397,8 @@ void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_ if (i915_gem_object_needs_bit17_swizzle(obj)) i915_gem_object_save_bit_17_swizzle(obj, pages); - shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping, - obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED); + mapping_clear_unevictable(file_inode(obj->base.
Re: [PATCH] drm/i915: Fix page cleanup on DMA remap failure
On Thu, Jan 16, 2025 at 9:24 AM Ville Syrjälä wrote: > > On Thu, Jan 16, 2025 at 08:56:36AM -0500, Brian Geffon wrote: > > When converting to folios the cleanup path of shmem_get_pages() was > > missed. When a DMA remap fails and the max segment size is greater than > > PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment > > size. The cleanup code isn't properly using the folio apis and as a > > result isn't handling compound pages correctly. > > > > Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 > > Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a > > folio_batch") > > Signed-off-by: Brian Geffon > > Suggested-by: Tomasz Figa > > --- > > drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 13 + > > 1 file changed, 5 insertions(+), 8 deletions(-) > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > index fe69f2c8527d..02ddab5bf5c0 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > @@ -37,8 +37,6 @@ void shmem_sg_free_table(struct sg_table *st, struct > > address_space *mapping, > > struct folio *last = NULL; > > struct page *page; > > > > - mapping_clear_unevictable(mapping); > > - > > The assymmetry here between the alloc vs. free is a bit annoying. > Maybe we can just keep this here? If you want, I think this can also be fixed by something like the following I believe. Ultimately we don't want to put page on non-head pages in a compound page. What do you think? If you like this better I can test and mail a v2. diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c index fe69f2c8527d..b79cd396e878 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -239,8 +239,14 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) * for PAGE_SIZE chunks instead may be helpful. */ if (max_segment > PAGE_SIZE) { - for_each_sgt_page(page, sgt_iter, st) + struct folio *last = NULL; + for_each_sgt_page(page, sgt_iter, st) { + struct folio *folio = page_folio(page); + if (folio == last) + continue; + last = folio; put_page(page); + } sg_free_table(st); kfree(st); -- 2.48.0.rc2.279.g1de40edade-goog > > Or if avoiding the ping-pong actually mattes in the gtt prepare > error case, then maybe we should rename this guy into > __shmem_sg_free_table() without the mapping_clear_unevictable() > and wrap it in a higher level shmem_sg_free_table() that does > everything? > > > folio_batch_init(&fbatch); > > for_each_sgt_page(page, sgt_iter, st) { > > struct folio *folio = page_folio(page); > > @@ -180,10 +178,10 @@ int shmem_sg_alloc_table(struct drm_i915_private > > *i915, struct sg_table *st, > > return 0; > > err_sg: > > sg_mark_end(sg); > > + mapping_clear_unevictable(mapping); > > if (sg != st->sgl) { > > shmem_sg_free_table(st, mapping, false, false); > > } else { > > - mapping_clear_unevictable(mapping); > > sg_free_table(st); > > } > > > > @@ -209,8 +207,6 @@ static int shmem_get_pages(struct drm_i915_gem_object > > *obj) > > struct address_space *mapping = obj->base.filp->f_mapping; > > unsigned int max_segment = i915_sg_segment_size(i915->drm.dev); > > struct sg_table *st; > > - struct sgt_iter sgt_iter; > > - struct page *page; > > int ret; > > > > /* > > @@ -239,9 +235,8 @@ static int shmem_get_pages(struct drm_i915_gem_object > > *obj) > >* for PAGE_SIZE chunks instead may be helpful. > >*/ > > if (max_segment > PAGE_SIZE) { > > - for_each_sgt_page(page, sgt_iter, st) > > - put_page(page); > > - sg_free_table(st); > > + /* Leave the mapping unevictable while we retry */ > > + shmem_sg_free_table(st, mapping, false, false); > > kfree(st); > > > >
Re: [PATCH] drm/i915: Fix page cleanup on DMA remap failure
On Thu, Jan 16, 2025 at 9:24 AM Ville Syrjälä wrote: > > On Thu, Jan 16, 2025 at 08:56:36AM -0500, Brian Geffon wrote: > > When converting to folios the cleanup path of shmem_get_pages() was > > missed. When a DMA remap fails and the max segment size is greater than > > PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment > > size. The cleanup code isn't properly using the folio apis and as a > > result isn't handling compound pages correctly. > > > > Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 > > Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a > > folio_batch") > > Signed-off-by: Brian Geffon > > Suggested-by: Tomasz Figa > > --- > > drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 13 + > > 1 file changed, 5 insertions(+), 8 deletions(-) > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > index fe69f2c8527d..02ddab5bf5c0 100644 > > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > @@ -37,8 +37,6 @@ void shmem_sg_free_table(struct sg_table *st, struct > > address_space *mapping, > > struct folio *last = NULL; > > struct page *page; > > > > - mapping_clear_unevictable(mapping); > > - > > The assymmetry here between the alloc vs. free is a bit annoying. > Maybe we can just keep this here? My thought on this was that freeing the sg table is orthogonal to making the mapping evictable, and shmem_sg_free_table() can be simplified to not even take the mapping as a parameter. > > Or if avoiding the ping-pong actually mattes in the gtt prepare > error case, then maybe we should rename this guy into > __shmem_sg_free_table() without the mapping_clear_unevictable() > and wrap it in a higher level shmem_sg_free_table() that does > everything? > > > folio_batch_init(&fbatch); > > for_each_sgt_page(page, sgt_iter, st) { > > struct folio *folio = page_folio(page); > > @@ -180,10 +178,10 @@ int shmem_sg_alloc_table(struct drm_i915_private > > *i915, struct sg_table *st, > > return 0; > > err_sg: > > sg_mark_end(sg); > > + mapping_clear_unevictable(mapping); > > if (sg != st->sgl) { > > shmem_sg_free_table(st, mapping, false, false); > > } else { > > - mapping_clear_unevictable(mapping); > > sg_free_table(st); > > } > > > > @@ -209,8 +207,6 @@ static int shmem_get_pages(struct drm_i915_gem_object > > *obj) > > struct address_space *mapping = obj->base.filp->f_mapping; > > unsigned int max_segment = i915_sg_segment_size(i915->drm.dev); > > struct sg_table *st; > > - struct sgt_iter sgt_iter; > > - struct page *page; > > int ret; > > > > /* > > @@ -239,9 +235,8 @@ static int shmem_get_pages(struct drm_i915_gem_object > > *obj) > >* for PAGE_SIZE chunks instead may be helpful. > >*/ > > if (max_segment > PAGE_SIZE) { > > - for_each_sgt_page(page, sgt_iter, st) > > - put_page(page); > > - sg_free_table(st); > > + /* Leave the mapping unevictable while we retry */ > > + shmem_sg_free_table(st, mapping, false, false); > > kfree(st); > > > > max_segment = PAGE_SIZE; > > @@ -265,6 +260,7 @@ static int shmem_get_pages(struct drm_i915_gem_object > > *obj) > > return 0; > > > > err_pages: > > + mapping_clear_unevictable(mapping); > > shmem_sg_free_table(st, mapping, false, false); > > /* > >* shmemfs first checks if there is enough memory to allocate the page > > @@ -402,6 +398,7 @@ void i915_gem_object_put_pages_shmem(struct > > drm_i915_gem_object *obj, struct sg_ > > if (i915_gem_object_needs_bit17_swizzle(obj)) > > i915_gem_object_save_bit_17_swizzle(obj, pages); > > > > + mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); > > shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping, > > obj->mm.dirty, obj->mm.madv == > > I915_MADV_WILLNEED); > > kfree(pages); > > -- > > 2.48.0.rc2.279.g1de40edade-goog > > -- > Ville Syrjälä > Intel
Re: [PATCH] drm/i915: Fix page cleanup on DMA remap failure
On Thu, Jan 16, 2025 at 9:38 AM Ville Syrjälä wrote: > > On Thu, Jan 16, 2025 at 04:24:26PM +0200, Ville Syrjälä wrote: > > On Thu, Jan 16, 2025 at 08:56:36AM -0500, Brian Geffon wrote: > > > When converting to folios the cleanup path of shmem_get_pages() was > > > missed. When a DMA remap fails and the max segment size is greater than > > > PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment > > > size. The cleanup code isn't properly using the folio apis and as a > > > result isn't handling compound pages correctly. > > > > > > Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 > > > Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a > > > folio_batch") > > > Signed-off-by: Brian Geffon > > > Suggested-by: Tomasz Figa > > > --- > > > drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 13 + > > > 1 file changed, 5 insertions(+), 8 deletions(-) > > > > > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > > b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > > index fe69f2c8527d..02ddab5bf5c0 100644 > > > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > > > @@ -37,8 +37,6 @@ void shmem_sg_free_table(struct sg_table *st, struct > > > address_space *mapping, > > > struct folio *last = NULL; > > > struct page *page; > > > > > > - mapping_clear_unevictable(mapping); > > > - > > > > The assymmetry here between the alloc vs. free is a bit annoying. > > Maybe we can just keep this here? > > Actually, I guess it's a bit more than just annoying since > shmem_sg_free_table() is actually used from outside i915_gem_shmem.c > as well. You're correct, this was a bad oversight on my part. This patch is going to require a v2 regardless, how do you feel about just changing shmem_sg_free_table() to not accept a mapping given that managing the mapping is really orthogonal to cleaning up the sg table. > > > > > Or if avoiding the ping-pong actually mattes in the gtt prepare > > error case, then maybe we should rename this guy into > > __shmem_sg_free_table() without the mapping_clear_unevictable() > > and wrap it in a higher level shmem_sg_free_table() that does > > everything? > > > > > folio_batch_init(&fbatch); > > > for_each_sgt_page(page, sgt_iter, st) { > > > struct folio *folio = page_folio(page); > > > @@ -180,10 +178,10 @@ int shmem_sg_alloc_table(struct drm_i915_private > > > *i915, struct sg_table *st, > > > return 0; > > > err_sg: > > > sg_mark_end(sg); > > > + mapping_clear_unevictable(mapping); > > > if (sg != st->sgl) { > > > shmem_sg_free_table(st, mapping, false, false); > > > } else { > > > - mapping_clear_unevictable(mapping); > > > sg_free_table(st); > > > } > > > > > > @@ -209,8 +207,6 @@ static int shmem_get_pages(struct drm_i915_gem_object > > > *obj) > > > struct address_space *mapping = obj->base.filp->f_mapping; > > > unsigned int max_segment = i915_sg_segment_size(i915->drm.dev); > > > struct sg_table *st; > > > - struct sgt_iter sgt_iter; > > > - struct page *page; > > > int ret; > > > > > > /* > > > @@ -239,9 +235,8 @@ static int shmem_get_pages(struct drm_i915_gem_object > > > *obj) > > > * for PAGE_SIZE chunks instead may be helpful. > > > */ > > > if (max_segment > PAGE_SIZE) { > > > - for_each_sgt_page(page, sgt_iter, st) > > > - put_page(page); > > > - sg_free_table(st); > > > + /* Leave the mapping unevictable while we retry */ > > > + shmem_sg_free_table(st, mapping, false, false); > > > kfree(st); > > > > > > max_segment = PAGE_SIZE; > > > @@ -265,6 +260,7 @@ static int shmem_get_pages(struct drm_i915_gem_object > > > *obj) > > > return 0; > > > > > > err_pages: > > > + mapping_clear_unevictable(mapping); > > > shmem_sg_free_table(st, mapping, false, false); > > > /* > > > * shmemfs first checks if there is enough memory to allocate the page > > > @@ -402,6 +398,7 @@ void i915_gem_object_put_pages_shmem(struct > > > drm_i915_gem_object *obj, struct sg_ > > > if (i915_gem_object_needs_bit17_swizzle(obj)) > > > i915_gem_object_save_bit_17_swizzle(obj, pages); > > > > > > + mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); > > > shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping, > > > obj->mm.dirty, obj->mm.madv == > > > I915_MADV_WILLNEED); > > > kfree(pages); > > > -- > > > 2.48.0.rc2.279.g1de40edade-goog > > > > -- > > Ville Syrjälä > > Intel > > -- > Ville Syrjälä > Intel
[PATCH] drm/i915: Fix page cleanup on DMA remap failure
When converting to folios the cleanup path of shmem_get_pages() was missed. When a DMA remap fails and the max segment size is greater than PAGE_SIZE it will attempt to retry the remap with a PAGE_SIZEd segment size. The cleanup code isn't properly using the folio apis and as a result isn't handling compound pages correctly. Link: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/13487 Fixes: 0b62af28f249 ("i915: convert shmem_sg_free_table() to use a folio_batch") Signed-off-by: Brian Geffon Suggested-by: Tomasz Figa --- drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 13 + 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c index fe69f2c8527d..02ddab5bf5c0 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -37,8 +37,6 @@ void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping, struct folio *last = NULL; struct page *page; - mapping_clear_unevictable(mapping); - folio_batch_init(&fbatch); for_each_sgt_page(page, sgt_iter, st) { struct folio *folio = page_folio(page); @@ -180,10 +178,10 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st, return 0; err_sg: sg_mark_end(sg); + mapping_clear_unevictable(mapping); if (sg != st->sgl) { shmem_sg_free_table(st, mapping, false, false); } else { - mapping_clear_unevictable(mapping); sg_free_table(st); } @@ -209,8 +207,6 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) struct address_space *mapping = obj->base.filp->f_mapping; unsigned int max_segment = i915_sg_segment_size(i915->drm.dev); struct sg_table *st; - struct sgt_iter sgt_iter; - struct page *page; int ret; /* @@ -239,9 +235,8 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) * for PAGE_SIZE chunks instead may be helpful. */ if (max_segment > PAGE_SIZE) { - for_each_sgt_page(page, sgt_iter, st) - put_page(page); - sg_free_table(st); + /* Leave the mapping unevictable while we retry */ + shmem_sg_free_table(st, mapping, false, false); kfree(st); max_segment = PAGE_SIZE; @@ -265,6 +260,7 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj) return 0; err_pages: + mapping_clear_unevictable(mapping); shmem_sg_free_table(st, mapping, false, false); /* * shmemfs first checks if there is enough memory to allocate the page @@ -402,6 +398,7 @@ void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_ if (i915_gem_object_needs_bit17_swizzle(obj)) i915_gem_object_save_bit_17_swizzle(obj, pages); + mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping, obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED); kfree(pages); -- 2.48.0.rc2.279.g1de40edade-goog