cifs: Fix collect_sample() to handle any iterator type
Subsystems (3)
PATCH 1/1 cifs: Fix collect_sample() to handle any iterator type
collect_sample() is used to gather samples of the data in a Write op for analysis to try and determine if the compression algorithm is likely to achieve anything more quickly than actually running the compression algorithm.
However, collect_sample() assumes that the data it is going to be sampling is stored in an ITER_XARRAY-type iterator (which it now should never be) and doesn't actually check that it is before accessing the underlying xarray directly.
Fix this by replacing the code with a loop that just uses the standard iterator functions to sample every other 2KiB block, skipping the intervening ones. It's not quite the same as the previous algorithm as it doesn't necessarily align to the pages within an ordinary write from the pagecache.
Note that the btrfs code from which this was derived samples the inode's pagecache directly rather than the iterator - but that doesn't necessarily work for network filesystems if O_DIRECT is in operation.
--- fs/smb/client/compress.c | 71 +++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 50 deletions(-)
fs/smb/client/compress.c +21 -50
@@ -155,58 +155,29 @@ static int cmp_bkt(const void *_a, const void *_b) }/* -* TODO: -* Support other iter types, if required. -* Only ITER_XARRAY is supported for now. +* Collect some 2K samples with 2K gaps between. */ -static int collect_sample (const struct iov_iter *iter,ssize_t max,u8 *sample) +static int collect_sample (const struct iov_iter *source,ssize_t max,u8 *sample) { -struct folio *folios[16 ], *folio; -unsigned int nr, i, j, npages; -loff_t start = iter->xarray_start + iter->iov_offset; -pgoff_t last, index = start /PAGE_SIZE ; -size_t len, off, foff; -void *p; -int s =0 ; - - last = (start + max -1 ) /PAGE_SIZE ; -do { - nr =xa_extract (iter->xarray, (void **)folios, index, last,ARRAY_SIZE (folios), -XA_PRESENT ); -if (nr ==0 ) -return -EIO ; - -for (i =0 ; i < nr; i++) { - folio = folios[i]; - npages =folio_nr_pages (folio); - foff = start -folio_pos (folio); - off = foff %PAGE_SIZE ; - -for (j = foff /PAGE_SIZE ; j < npages; j++) { -size_t len2; - - len =min_t (size_t, max,PAGE_SIZE - off); - len2 =min_t (size_t, len,SZ_2K ); - - p =kmap_local_page (folio_page (folio, j)); -memcpy (&sample[s], p, len2); -kunmap_local (p); - - s += len2; - -if (len2 <SZ_2K || s >= max -SZ_2K ) -return s; - - max -= len; -if (max <=0 ) -return s; - - start += len; - off =0 ; - index++; - } - } - }while (nr ==ARRAY_SIZE (folios)); +struct iov_iter iter = *source; +size_t s =0 ; + +while (iov_iter_count (&iter) >=SZ_2K ) { +size_t part =umin (umin (iov_iter_count (&iter),SZ_2K ), max); +size_t n; + + n =copy_from_iter (sample + s, part, &iter); +if (n != part) +return -EFAULT ; + + s += n; + max -= n; + +if (iov_iter_count (&iter) <PAGE_SIZE -SZ_2K ) +break ; + +iov_iter_advance (&iter,SZ_2K ); + }return s; }
Quoted 29 lines · David Howells
collect_sample()is used to gather samples of the data in a Write op for analysis to try and determine if the compression algorithm is likely to achieve anything more quickly than actually running the compression algorithm.However,
collect_sample()assumes that the data it is going to be sampling is stored in an ITER_XARRAY-type iterator (which it now should never be) and doesn't actually check that it is before accessing the underlying xarray directly.Fix this by replacing the code with a loop that just uses the standard iterator functions to sample every other 2KiB block, skipping the intervening ones. It's not quite the same as the previous algorithm as it doesn't necessarily align to the pages within an ordinary write from the pagecache.
Note that the btrfs code from which this was derived samples the inode's pagecache directly rather than the iterator - but that doesn't necessarily work for network filesystems if
O_DIRECTis in operation.Fixes 94ae8c3fee94 ("smb: client: compress: LZ77 code improvements cleanup")Signed-off-by David Howells <[email protected]>cc Steve French <[email protected]>cc Enzo Matsumiya <[email protected]>cc Paulo Alcantara <[email protected]>cc Shyam Prasad N <[email protected]>cc Tom Talpey <[email protected]>
Quoted 3 lines
--- fs/smb/client/compress.c | 71 +++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 50 deletions(-)