lib/scatterlist: Fix offset type in sg_alloc_table_from_pages
Subsystems (5)
Tvrtko Ursulin
maintainerPATCH 1/4 lib/scatterlist: Fix offset type in sg_alloc_table_from_pages
From: Tvrtko Ursulin <[email protected]>
Scatterlist entries have an unsigned int for the offset so correct the sg_alloc_table_from_pages function accordingly.
Since these are offsets withing a page, unsigned int is wide enough.
Also converts callers which were using unsigned long locally with the lower_32_bits annotation to make it explicitly clear what is happening.
v2: Use offset_in_page. (Chris Wilson)
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 4 ++-- drivers/rapidio/devices/rio_mport_cdev.c | 4 ++-- include/linux/scatterlist.h | 2 +- lib/scatterlist.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -2
@@ -478,7 +478,7 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, {struct vb2_dc_buf *buf;struct frame_vector *vec; -unsigned long offset; +unsigned int offset;int n_pages, i;int ret =0 ;struct sg_table *sgt;
@@ -506,7 +506,7 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, buf->dev = dev; buf->dma_dir = dma_dir; - offset = vaddr & ~PAGE_MASK ; + offset =lower_32_bits (offset_in_page (vaddr)); vec =vb2_create_framevec (vaddr, size, dma_dir ==DMA_FROM_DEVICE );if (IS_ERR (vec)) { ret =PTR_ERR (vec);
drivers/rapidio/devices/rio_mport_cdev.c +2 -2
@@ -876,10 +876,10 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode,* offset within the internal buffer specified by handle parameter. */ if (xfer->loc_addr) { -unsigned long offset; +unsigned int offset;long pinned; - offset = (unsigned long )(uintptr_t )xfer->loc_addr & ~PAGE_MASK ; + offset =lower_32_bits (offset_in_page (xfer->loc_addr)); nr_pages =PAGE_ALIGN (xfer->length + offset) >>PAGE_SHIFT ; page_list = kmalloc_array(nr_pages,
include/linux/scatterlist.h +1 -1
@@ -263,7 +263,7 @@ int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,int sg_alloc_table (struct sg_table *,unsigned int ,gfp_t );int sg_alloc_table_from_pages (struct sg_table *sgt,struct page **pages,unsigned int n_pages, -unsigned long offset,unsigned long size, +unsigned int offset,unsigned long size,gfp_t gfp_mask); size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
lib/scatterlist.c +1 -1
@@ -391,7 +391,7 @@ EXPORT_SYMBOL(sg_alloc_table);*/ int sg_alloc_table_from_pages (struct sg_table *sgt,struct page **pages,unsigned int n_pages, -unsigned long offset,unsigned long size, +unsigned int offset,unsigned long size,gfp_t gfp_mask) {unsigned int chunks;
Signature · 1 line
2.7.4
Tvrtko Ursulin
maintainerPATCH 2/4 lib/scatterlist: Avoid potential scatterlist entry overflow
From: Tvrtko Ursulin <[email protected]>
Since the scatterlist length field is an unsigned int, make sure that sg_alloc_table_from_pages does not overflow it while coallescing pages to a single entry.
v2: Drop reference to future use. Use UINT_MAX.
v3: max_segment must be page aligned.
--- lib/scatterlist.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
lib/scatterlist.c +19 -6
@@ -394,7 +394,8 @@ int sg_alloc_table_from_pages(struct sg_table *sgt, unsigned int offset, unsigned long size, gfp_t gfp_mask) { - unsigned int chunks; + const unsigned int max_segment = rounddown(UINT_MAX, PAGE_SIZE); +unsigned int seg_len, chunks;unsigned int i;unsigned int cur_page;int ret;
@@ -402,9 +403,16 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* compute number of contiguous chunks */ chunks =1 ; -for (i =1 ; i < n_pages; ++i) -if (page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) + seg_len =PAGE_SIZE ; +for (i =1 ; i < n_pages; ++i) { +if (seg_len >= max_segment || +page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) { ++chunks; + seg_len =PAGE_SIZE ; + }else { + seg_len +=PAGE_SIZE ; + } + } ret =sg_alloc_table (sgt, chunks, gfp_mask);if (unlikely (ret))
@@ -413,17 +421,22 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* merging chunks and putting them into the scatterlist */ cur_page =0 ;for_each_sg (sgt->sgl, s, sgt->orig_nents, i) { -unsigned long chunk_size; +unsigned int chunk_size;unsigned int j;/* look for the end of the current chunk */ + seg_len =PAGE_SIZE ;for (j = cur_page +1 ; j < n_pages; ++j) -if (page_to_pfn (pages[j]) != +if (seg_len >= max_segment || +page_to_pfn (pages[j]) !=page_to_pfn (pages[j -1 ]) +1 )break ; +else + seg_len +=PAGE_SIZE ; chunk_size = ((j - cur_page) <<PAGE_SHIFT ) - offset; -sg_set_page (s, pages[cur_page],min (size, chunk_size), offset); +sg_set_page (s, pages[cur_page], + min_t(unsigned long, size, chunk_size), offset); size -= chunk_size; offset =0 ; cur_page = j;
Signature · 6 lines
2.7.4 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Tvrtko Ursulin
maintainerPATCH v4 1/1 lib/scatterlist: Avoid potential scatterlist entry overflow
From: Tvrtko Ursulin <[email protected]>
Since the scatterlist length field is an unsigned int, make sure that sg_alloc_table_from_pages does not overflow it while coallescing pages to a single entry.
v2: Drop reference to future use. Use UINT_MAX.
v3: max_segment must be page aligned.
v4: Do not rely on compiler to optimise out the rounddown.
(Joonas Lahtinen)
--- include/linux/scatterlist.h | 6 ++++++ lib/scatterlist.c | 25 +++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-)
include/linux/scatterlist.h +6 -0
@@ -21,6 +21,12 @@ struct scatterlist { };/* +* Since the above length field is an unsigned int, below we define the maximum +* lenght in bytes that can be stored in one scatterlist entry. +*/ +#define SCATTERLIST_MAX_SEGMENT (0xfffff000) + +/* * These macros should be used after a dma_map_sg call has been done * to get bus addresses of each of the SG entries and their lengths. * You should only work with the number of sg entries dma_map_sg
lib/scatterlist.c +19 -6
@@ -394,7 +394,8 @@ int sg_alloc_table_from_pages(struct sg_table *sgt, unsigned int offset, unsigned long size, gfp_t gfp_mask) { - unsigned int chunks; + const unsigned int max_segment = SCATTERLIST_MAX_SEGMENT; +unsigned int seg_len, chunks;unsigned int i;unsigned int cur_page;int ret;
@@ -402,9 +403,16 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* compute number of contiguous chunks */ chunks =1 ; -for (i =1 ; i < n_pages; ++i) -if (page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) + seg_len =PAGE_SIZE ; +for (i =1 ; i < n_pages; ++i) { +if (seg_len >= max_segment || +page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) { ++chunks; + seg_len =PAGE_SIZE ; + }else { + seg_len +=PAGE_SIZE ; + } + } ret =sg_alloc_table (sgt, chunks, gfp_mask);if (unlikely (ret))
@@ -413,17 +421,22 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* merging chunks and putting them into the scatterlist */ cur_page =0 ;for_each_sg (sgt->sgl, s, sgt->orig_nents, i) { -unsigned long chunk_size; +unsigned int chunk_size;unsigned int j;/* look for the end of the current chunk */ + seg_len =PAGE_SIZE ;for (j = cur_page +1 ; j < n_pages; ++j) -if (page_to_pfn (pages[j]) != +if (seg_len >= max_segment || +page_to_pfn (pages[j]) !=page_to_pfn (pages[j -1 ]) +1 )break ; +else + seg_len +=PAGE_SIZE ; chunk_size = ((j - cur_page) <<PAGE_SHIFT ) - offset; -sg_set_page (s, pages[cur_page],min (size, chunk_size), offset); +sg_set_page (s, pages[cur_page], + min_t(unsigned long, size, chunk_size), offset); size -= chunk_size; offset =0 ; cur_page = j;
Signature · 1 line
2.7.4
Re: [PATCH v4] lib/scatterlist: Avoid potential scatterlist entry overflow
Quoted 8 lines · Tvrtko Ursulin
Since the scatterlist length field is an unsigned int, make sure that
sg_alloc_table_from_pagesdoes not overflow it while coallescing pages to a single entry./*
+ * Since the above length field is an unsigned int, below we define the maximum
+ * lenght in bytes that can be stored in one scatterlist entry.
length
Quoted 2 lines
+ */
+#defineSCATTERLIST_MAX_SEGMENT(0xfffff000)
Shouldn't be calculated from PAGE_SIZE (PAGE bits, etc)?
Quoted 9 lines
lib/scatterlist.c +0
-2@@ -402,9 +403,16 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* compute number of contiguous chunks */ chunks =1 ; -for (i =1 ; i < n_pages; ++i) -if (page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 )
lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 415
Quoted 10 lines
lib/scatterlist.c +9
-0+ seg_len =PAGE_SIZE ; +for (i =1 ; i < n_pages; ++i) { +if (seg_len >= max_segment || +page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) { ++chunks; + seg_len =PAGE_SIZE ; + }else { + seg_len +=PAGE_SIZE ; + } + }
Wouldn't be following looks more readable?
seg_len = 0;
// Are compilers so stupid doing calculation per iteration in for-conditional? // for (i = 0; i + 1 < n_pages; i++) ?
for (i = 1; i < n_pages; ++i) {
seg_len += PAGE_SIZE;
if (seg_len >= max_segment || page_to_pfn(pages[i]) !=
page_to_pfn(pages[i - 1]) + 1) {
++chunks;
seg_len = PAGE_SIZE; }
}
Perhaps while() or do-while() will increase readability even more, but I didn't check.
lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 435
Quoted 10 lines
lib/scatterlist.c +5
-1/* look for the end of the current chunk */ + seg_len =PAGE_SIZE ;for (j = cur_page +1 ; j < n_pages; ++j) -if (page_to_pfn (pages[j]) != +if (seg_len >= max_segment || +page_to_pfn (pages[j]) !=page_to_pfn (pages[j -1 ]) +1 )break ; +else + seg_len +=PAGE_SIZE ;
Something similar here (didn't you get warning from checkpath about curly braces?).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [Intel-gfx] [PATCH v4] lib/scatterlist: Avoid potential scatterlist entry overflow
Hi,
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Tvrtko Ursulin
Since the scatterlist length field is an unsigned int, make sure that
sg_alloc_table_from_pagesdoes not overflow it while coallescing pages to a single entry./*
+ * Since the above length field is an unsigned int, below we define the maximum
+ * lenght in bytes that can be stored in one scatterlist entry.
Quoted 6 lines
length
Quoted 2 lines
+ */
+#defineSCATTERLIST_MAX_SEGMENT(0xfffff000)Shouldn't be calculated from
PAGE_SIZE(PAGE bits, etc)?
Yep, and at the same time I would potentially change the name to be consistent with the other defines in the file as Joonas suggested. Something like:
#define SG_MAX_SEGMENT (UINT_MAX & PAGE_MASK)
But would need a better name since SG_MAX_SEGMENT*S* already exists and means something else. If we can't come up with a better name then leave it as SCATTERLIST_MAX_SEGMENT?
Quoted 22 lines
Quoted 9 lines
lib/scatterlist.c +0
-2@@ -402,9 +403,16 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* compute number of contiguous chunks */ chunks =1 ; -for (i =1 ; i < n_pages; ++i) -if (page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 )lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 415
Quoted 10 lines
lib/scatterlist.c +9
-0+ seg_len =PAGE_SIZE ; +for (i =1 ; i < n_pages; ++i) { +if (seg_len >= max_segment || +page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) { ++chunks; + seg_len =PAGE_SIZE ; + }else { + seg_len +=PAGE_SIZE ; + } + }Wouldn't be following looks more readable?
Quoted 3 lines
seg_len= 0;
// Are compilers so stupid doing calculation per iteration in for-conditional? // for (i = 0; i + 1 <n_pages; i++) ?
I didn't get what you meant here?
Quoted 8 lines
for (i = 1; i <
n_pages; ++i) {
seg_len+=PAGE_SIZE;
if (seg_len>=max_segment||page_to_pfn(pages[i])!=page_to_pfn(pages[i - 1]) + 1) { ++chunks; seg_len = PAGE_SIZE;}
}
Tried it in my unit tester but it doesn't work for all scenarios, guess there is a subtle bug somewhere. I don't find it that unreadable so would prefer to leave it since it works.
Quoted 14 lines
Perhaps
while()or do-while() will increase readability even more, but I didn't check.lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 435
Quoted 10 lines
lib/scatterlist.c +5
-1/* look for the end of the current chunk */ + seg_len =PAGE_SIZE ;for (j = cur_page +1 ; j < n_pages; ++j) -if (page_to_pfn (pages[j]) != +if (seg_len >= max_segment || +page_to_pfn (pages[j]) !=page_to_pfn (pages[j -1 ]) +1 )break ; +else + seg_len +=PAGE_SIZE ;
Quoted 2 lines
Something similar here (didn't you get warning from checkpath about curly braces?).
It didn't, but agreed that I should have added them. Will do.
Regards,
Tvrtko
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [Intel-gfx] [PATCH v4] lib/scatterlist: Avoid potential scatterlist entry overflow
Quoted 22 lines
Quoted 22 lines
Quoted 8 lines
lib/scatterlist.c +1
-2@@ -402,9 +403,16 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* compute number of contiguous chunks */ chunks =1 ; -for (i =1 ; i < n_pages; ++i) -if (page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) + 1) + seg_len =PAGE_SIZE ;lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 417
Quoted 10 lines
lib/scatterlist.c +8
-0+for (i =1 ; i < n_pages; ++i) { +if (seg_len >= max_segment || +page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) { ++chunks; + seg_len =PAGE_SIZE ; + }else { + seg_len +=PAGE_SIZE ; + } + }Wouldn't be following looks more readable?
Quoted 7 lines
Quoted 4 lines
seg_len= 0;
// Are compilers so stupid doing calculation per iteration in for-conditional?
// for (i = 0; i + 1 <n_pages; i++) ?I didn't get what you meant here?
Why do we start from 1? I see here two micro (?) optimizations:
1) starting from 1 on believe that compiler dumb enough to every time do a calculation in condition;
2) ++i instead of i++, but this is just matter of style, it's not a c++.
Quoted 10 lines
Quoted 8 lines
for (i = 1; i <
n_pages; ++i) {
seg_len+=PAGE_SIZE;
if (seg_len>=max_segment||page_to_pfn(pages[i])!=page_to_pfn(pages[i - 1]) + 1) { ++chunks; seg_len = PAGE_SIZE;}
}
Quoted 3 lines
Tried it in my unit tester but it doesn't work for all scenarios, guess there is a subtle bug somewhere. I don't find it that unreadable so would prefer to leave it since it works.
Last seems has to beseg_len = 0;
Signature · 6 lines
With Best Regards, Andy Shevchenko _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [Intel-gfx] [PATCH v4] lib/scatterlist: Avoid potential scatterlist entry overflow
Quoted 28 lines · Andy Shevchenko
Quoted 28 lines
Quoted 26 lines
Quoted 8 lines
lib/scatterlist.c +1
-2@@ -402,9 +403,16 @@ int sg_alloc_table_from_pages(struct sg_table *sgt,/* compute number of contiguous chunks */ chunks =1 ; -for (i =1 ; i < n_pages; ++i) -if (page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) + 1) + seg_len =PAGE_SIZE ;lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 417
Quoted 10 lines
lib/scatterlist.c +8
-0+for (i =1 ; i < n_pages; ++i) { +if (seg_len >= max_segment || +page_to_pfn (pages[i]) !=page_to_pfn (pages[i -1 ]) +1 ) { ++chunks; + seg_len =PAGE_SIZE ; + }else { + seg_len +=PAGE_SIZE ; + } + }Wouldn't be following looks more readable?
seg_len= 0;
// Are compilers so stupid doing calculation per iteration in for-conditional?
// for (i = 0; i + 1 <n_pages; i++) ?
Quoted 5 lines
Quoted 1 line
I didn't get what you meant here?
Why do we start from 1? I see here two micro (?) optimizations:
1) starting from 1 on believe that compiler dumb enough to every time do a calculation in condition;
The existing code starts from 1 because the pfn condition looks up page i - 1. I don't feel there is a need to change that as well.
Quoted 1 line
2) ++i instead of i++, but this is just matter of style, it's not a c++.
Note that I haven't changed the existing code in this respect. I am happy to change it though.
Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
for (i = 1; i <
n_pages; ++i) {
seg_len+=PAGE_SIZE;
if (seg_len>=max_segment||page_to_pfn(pages[i])!=page_to_pfn(pages[i - 1]) + 1) { ++chunks; seg_len = PAGE_SIZE;}
}
Quoted 6 lines
Quoted 3 lines
Tried it in my unit tester but it doesn't work for all scenarios, guess there is a subtle bug somewhere. I don't find it that unreadable so would prefer to leave it since it works.
Last seems has to be
seg_len= 0;
Oh right, of course. Your suggestion generates a tiny bit smaller binary so I am happy to change that as well. I'll resend the patch hopefully today.
Regards,
Tvrtko
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Re: [Intel-gfx] [PATCH v4] lib/scatterlist: Avoid potential scatterlist entry overflow
Quoted 13 lines · Tvrtko Ursulin
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
lib/scatterlist.c int sg_alloc_table_from_pages(struct sg_table *sgt, · line 407
Quoted 2 lines
lib/scatterlist.c +1
-1-for (i =1 ; i < n_pages; ++i) +for (i =1 ; i < n_pages; ++i) {// Are compilers so stupid doing calculation per iteration in for-conditional?
// for (i = 0; i + 1 <n_pages; i++) ?Quoted 5 lines
Quoted 1 line
I didn't get what you meant here?
Why do we start from 1? I see here two micro (?) optimizations:
1) starting from 1 on believe that compiler dumb enough to every time do a calculation in condition;
Quoted 7 lines
The existing code starts from 1 because the pfn condition looks up page i -
1. I don't feel there is a need to change that as well.Quoted 1 line
2) ++i instead of i++, but this is just matter of style, it's not a c++.
Note that I haven't changed the existing code in this respect. I am happy to change it though.
Yes, this is another story. Just a side note to existing code, indeed.
Signature · 6 lines
With Best Regards, Andy Shevchenko _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Tvrtko Ursulin
maintainerPATCH 3/4 lib/scatterlist: Introduce and export __sg_alloc_table_from_pages
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Drivers like i915 benefit from being able to control the maxium size of the sg coallesced segment while building the scatter- gather list.
Introduce and export the __sg_alloc_table_from_pages function which will allow it that control.
v2: Reorder parameters. (Chris Wilson)
v3: Fix incomplete reordering in v2.
v4: max_segment needs to be page aligned.
--- include/linux/scatterlist.h | 11 ++++++--- lib/scatterlist.c | 59 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 17 deletions(-)
include/linux/scatterlist.h +7 -4
@@ -261,10 +261,13 @@ void sg_free_table(struct sg_table *);int __sg_alloc_table (struct sg_table *,unsigned int ,unsigned int ,struct scatterlist *,gfp_t ,sg_alloc_fn *);int sg_alloc_table (struct sg_table *,unsigned int ,gfp_t ); -int sg_alloc_table_from_pages (struct sg_table *sgt, -struct page **pages,unsigned int n_pages, -unsigned int offset,unsigned long size, -gfp_t gfp_mask); +int __sg_alloc_table_from_pages (struct sg_table *sgt,struct page **pages, +unsigned int n_pages,unsigned int offset, +unsigned long size,unsigned int max_segment, +gfp_t gfp_mask); +int sg_alloc_table_from_pages (struct sg_table *sgt,struct page **pages, +unsigned int n_pages,unsigned int offset, +unsigned long size,gfp_t gfp_mask);size_t sg_copy_buffer (struct scatterlist *sgl,unsigned int nents,void *buf,size_t buflen,off_t skip,bool to_buffer);
lib/scatterlist.c +46 -13
84 lines not on this page · raw message
Signature · 6 lines
2.7.4 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Tvrtko Ursulin
maintainerPATCH 4/4 drm/i915: Use __sg_alloc_table_from_pages for userptr allocations
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
With the addition of __sg_alloc_table_from_pages we can control the maximum coallescing size and eliminate a separate path for allocating backing store here.
Similar to 871dfbd67d4e ("drm/i915: Allow compaction upto SWIOTLB max segment size") this enables more compact sg lists to be created and so has a beneficial effect on workloads with many and/or large objects of this class.
v2:
* Rename helper to i915_sg_segment_size and fix swiotlb override.
* Commit message update.
v3:
* Actually include the swiotlb override fix.
v4:
* Regroup parameters a bit. (Chris Wilson)
v5:
* Rebase for swiotlb_max_segment.
* Add DMA map failure handling as in abb0deacb5a6 ("drm/i915: Fallback to single PAGE_SIZE segments for DMA remapping").
--- drivers/gpu/drm/i915/i915_drv.h | 10 +++++ drivers/gpu/drm/i915/i915_gem.c | 6 +-- drivers/gpu/drm/i915/i915_gem_userptr.c | 79 ++++++++++++--------------------- 3 files changed, 40 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +10 -0
@@ -2594,6 +2594,16 @@ static inline struct scatterlist *__sg_next(struct scatterlist *sg) (((__iter).curr += PAGE_SIZE) < (__iter).max) || \ ((__iter) = __sgt_iter(__sg_next((__iter).sgp), false), 0)) +static inline unsigned int i915_sg_segment_size (void ) +{ +unsigned int size =swiotlb_max_segment (); + +if (size ==0 ) + size =UINT_MAX ; + +return rounddown (size,PAGE_SIZE ); +} +static inline const struct intel_device_info *intel_info (const struct drm_i915_private *dev_priv) {
drivers/gpu/drm/i915/i915_gem.c +1 -5
@@ -2248,7 +2248,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)struct sgt_iter sgt_iter;struct page *page;unsigned long last_pfn =0 ;/* suppress gcc warning */ -unsigned int max_segment; +unsigned int max_segment =i915_sg_segment_size ();int ret;gfp_t gfp;
@@ -2259,10 +2259,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)GEM_BUG_ON (obj->base.read_domains &I915_GEM_GPU_DOMAINS );GEM_BUG_ON (obj->base.write_domain &I915_GEM_GPU_DOMAINS ); - max_segment =swiotlb_max_segment (); -if (!max_segment) - max_segment =rounddown (UINT_MAX ,PAGE_SIZE ); - st =kmalloc (sizeof (*st),GFP_KERNEL );if (st ==NULL )return ERR_PTR (-ENOMEM );
drivers/gpu/drm/i915/i915_gem_userptr.c +29 -50
107 lines not on this page · raw message
Signature · 6 lines
2.7.4 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Tvrtko Ursulin
maintainerPATCH v6 1/1 drm/i915: Use __sg_alloc_table_from_pages for userptr allocations
From: Tvrtko Ursulin <[email protected]>
With the addition of __sg_alloc_table_from_pages we can control the maximum coallescing size and eliminate a separate path for allocating backing store here.
Similar to 871dfbd67d4e ("drm/i915: Allow compaction upto SWIOTLB max segment size") this enables more compact sg lists to be created and so has a beneficial effect on workloads with many and/or large objects of this class.
v2:
* Rename helper to i915_sg_segment_size and fix swiotlb override.
* Commit message update.
v3:
* Actually include the swiotlb override fix.
v4:
* Regroup parameters a bit. (Chris Wilson)
v5:
* Rebase for swiotlb_max_segment.
* Add DMA map failure handling as in abb0deacb5a6 ("drm/i915: Fallback to single PAGE_SIZE segments for DMA remapping").
v6: Handle swiotlb_max_segment() returning 1. (Joonas Lahtinen)
--- drivers/gpu/drm/i915/i915_drv.h | 15 +++++++ drivers/gpu/drm/i915/i915_gem.c | 6 +-- drivers/gpu/drm/i915/i915_gem_userptr.c | 79 ++++++++++++--------------------- 3 files changed, 45 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +15 -0
@@ -2598,6 +2598,21 @@ static inline struct scatterlist *__sg_next(struct scatterlist *sg) (((__iter).curr += PAGE_SIZE) < (__iter).max) || \ ((__iter) = __sgt_iter(__sg_next((__iter).sgp), false), 0)) +static inline unsigned int i915_sg_segment_size (void ) +{ +unsigned int size =swiotlb_max_segment (); + +if (size ==0 ) +return SCATTERLIST_MAX_SEGMENT ; + + size =rounddown (size,PAGE_SIZE ); +/* swiotlb_max_segment_size can return 1 byte when it means one page. */ +if (size <PAGE_SIZE ) + size =PAGE_SIZE ; + +return size; +} +static inline const struct intel_device_info *intel_info (const struct drm_i915_private *dev_priv) {
drivers/gpu/drm/i915/i915_gem.c +1 -5
@@ -2255,7 +2255,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)struct sgt_iter sgt_iter;struct page *page;unsigned long last_pfn =0 ;/* suppress gcc warning */ -unsigned int max_segment; +unsigned int max_segment =i915_sg_segment_size ();int ret;gfp_t gfp;
@@ -2266,10 +2266,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)GEM_BUG_ON (obj->base.read_domains &I915_GEM_GPU_DOMAINS );GEM_BUG_ON (obj->base.write_domain &I915_GEM_GPU_DOMAINS ); - max_segment =swiotlb_max_segment (); -if (!max_segment) - max_segment =rounddown (UINT_MAX ,PAGE_SIZE ); - st =kmalloc (sizeof (*st),GFP_KERNEL );if (st ==NULL )return ERR_PTR (-ENOMEM );
drivers/gpu/drm/i915/i915_gem_userptr.c +29 -50
107 lines not on this page · raw message
Signature · 1 line
2.7.4
kbuild test robot
build botRe: [Intel-gfx] [PATCH v6] drm/i915: Use __sg_alloc_table_from_pages for userptr allocations
reproduce:
Hi Tvrtko,
[auto build test ERROR on drm-intel/for-linux-next]
[also build test ERROR on next-20170111]
[cannot apply to v4.10-rc3]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Tvrtko-Ursulin/drm-i915-Use-__sg_alloc_table_from_pages-for-userptr-allocations/20170113-004619 base: git://anongit.freedesktop.org/drm-intel for-linux-next config: x86_64-rhel (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce: # save the attached .config to linux build tree make ARCH=x86_64
All errors (new ones prefixed by >>):
In file included from drivers/gpu/drm/i915/i915_drv.c:48:0:
drivers/gpu/drm/i915/i915_drv.h: In function 'i915_sg_segment_size':
>> drivers/gpu/drm/i915/i915_drv.h:2605:10: error: 'SCATTERLIST_MAX_SEGMENT' undeclared (first use in this function)
return SCATTERLIST_MAX_SEGMENT;
^~~~~~~~~~~~~~~~~~~~~~~ drivers/gpu/drm/i915/i915_drv.h:2605:10: note: each undeclared identifier is reported only once for each function it appears in
--
In file included from drivers/gpu/drm/i915/i915_gem_userptr.c:27:0:
drivers/gpu/drm/i915/i915_drv.h: In function 'i915_sg_segment_size':
>> drivers/gpu/drm/i915/i915_drv.h:2605:10: error: 'SCATTERLIST_MAX_SEGMENT' undeclared (first use in this function)
return SCATTERLIST_MAX_SEGMENT;
^~~~~~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/i915/i915_drv.h:2605:10: note: each undeclared identifier is reported only once for each function it appears in
drivers/gpu/drm/i915/i915_gem_userptr.c: In function '__i915_gem_userptr_alloc_pages':
>> drivers/gpu/drm/i915/i915_gem_userptr.c:406:8: error: implicit declaration of function '__sg_alloc_table_from_pages' [-Werror=implicit-function-declaration] ret = __sg_alloc_table_from_pages(st, pvec, num_pages,
^~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
vim +/SCATTERLIST_MAX_SEGMENT +2605 drivers/gpu/drm/i915/i915_drv.h
2599
2600 static inline unsigned int i915_sg_segment_size(void)
2601 {
2602 unsigned int size = swiotlb_max_segment();
2603
2604 if (size == 0)
> 2605 return SCATTERLIST_MAX_SEGMENT;
2606
2607 size = rounddown(size, PAGE_SIZE);
2608 /* swiotlb_max_segment_size can return 1 byte when it means one page. */
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Patchwork
CI✓ Fi.CI.BAT: success for series starting with [1/4] lib/scatterlist: Fix offset type in sg_alloc_table_from_pages
In reply to Tvrtko Ursulin== Series Details ==
== Series Details ==
Series: series starting with [1/4] lib/scatterlist: Fix offset type in sg_alloc_table_from_pages
URL : https://patchwork.freedesktop.org/series/17816/
State : success
== Summary ==
Series 17816v1 Series without cover letter
https://patchwork.freedesktop.org/api/1.0/series/17816/revisions/1/mbox/
fi-bdw-5557u total:246 pass:232 dwarn:0 dfail:0 fail:0 skip:14 fi-bsw-n3050 total:246 pass:207 dwarn:0 dfail:0 fail:0 skip:39 fi-bxt-j4205 total:246 pass:224 dwarn:0 dfail:0 fail:0 skip:22 fi-bxt-t5700 total:82 pass:69 dwarn:0 dfail:0 fail:0 skip:12 fi-byt-j1900 total:246 pass:219 dwarn:0 dfail:0 fail:0 skip:27 fi-byt-n2820 total:246 pass:215 dwarn:0 dfail:0 fail:0 skip:31 fi-hsw-4770 total:246 pass:227 dwarn:0 dfail:0 fail:0 skip:19 fi-hsw-4770r total:246 pass:227 dwarn:0 dfail:0 fail:0 skip:19 fi-ivb-3520m total:246 pass:225 dwarn:0 dfail:0 fail:0 skip:21 fi-ivb-3770 total:246 pass:225 dwarn:0 dfail:0 fail:0 skip:21 fi-kbl-7500u total:246 pass:225 dwarn:0 dfail:0 fail:0 skip:21 fi-skl-6260u total:246 pass:233 dwarn:0 dfail:0 fail:0 skip:13 fi-skl-6700hq total:246 pass:226 dwarn:0 dfail:0 fail:0 skip:20 fi-skl-6700k total:246 pass:222 dwarn:3 dfail:0 fail:0 skip:21 fi-skl-6770hq total:246 pass:233 dwarn:0 dfail:0 fail:0 skip:13 fi-snb-2520m total:246 pass:215 dwarn:0 dfail:0 fail:0 skip:31 fi-snb-2600 total:246 pass:214 dwarn:0 dfail:0 fail:0 skip:32
abf5260be6dda4ade94e8edf66e133260083f29b drm-tip: 2017y-01m-10d-23h-42m-21s UTC integration manifest 91daf71e drm/i915: Use __sg_alloc_table_from_pages for userptr allocations
653fe3c lib/scatterlist: Introduce and export __sg_alloc_table_from_pages
9fee41b lib/scatterlist: Avoid potential scatterlist entry overflow
354e525 lib/scatterlist: Fix offset type in sg_alloc_table_from_pages
== Logs ==
For more details see: https://intel-gfx-ci.01.org/CI/Patchwork_3473/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Tvrtko Ursulin
maintainerPATCH v5 1/1 lib/scatterlist: Introduce and export __sg_alloc_table_from_pages
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Drivers like i915 benefit from being able to control the maxium size of the sg coallesced segment while building the scatter- gather list.
Introduce and export the __sg_alloc_table_from_pages function which will allow it that control.
v2: Reorder parameters. (Chris Wilson)
v3: Fix incomplete reordering in v2.
v4: max_segment needs to be page aligned.
v5: Rebase.
--- include/linux/scatterlist.h | 11 +++++---- lib/scatterlist.c | 58 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 17 deletions(-)
include/linux/scatterlist.h +7 -4
@@ -267,10 +267,13 @@ void sg_free_table(struct sg_table *);int __sg_alloc_table (struct sg_table *,unsigned int ,unsigned int ,struct scatterlist *,gfp_t ,sg_alloc_fn *);int sg_alloc_table (struct sg_table *,unsigned int ,gfp_t ); -int sg_alloc_table_from_pages (struct sg_table *sgt, -struct page **pages,unsigned int n_pages, -unsigned int offset,unsigned long size, -gfp_t gfp_mask); +int __sg_alloc_table_from_pages (struct sg_table *sgt,struct page **pages, +unsigned int n_pages,unsigned int offset, +unsigned long size,unsigned int max_segment, +gfp_t gfp_mask); +int sg_alloc_table_from_pages (struct sg_table *sgt,struct page **pages, +unsigned int n_pages,unsigned int offset, +unsigned long size,gfp_t gfp_mask);size_t sg_copy_buffer (struct scatterlist *sgl,unsigned int nents,void *buf,size_t buflen,off_t skip,bool to_buffer);
lib/scatterlist.c +45 -13
83 lines not on this page · raw message
Signature · 6 lines
2.7.4 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx