scsi: Use iovec_import() instead of import_iovec().
Subsystems (7)
PATCH 5/9 scsi: Use iovec_import() instead of import_iovec().
iovec_import() has a safer calling convention than import_iovec().
--- block/scsi_ioctl.c | 14 ++++++++------ drivers/scsi/sg.c | 14 +++++++------- 2 files changed, 15 insertions(+), 13 deletions(-)
block/scsi_ioctl.c +8 -6
@@ -331,20 +331,22 @@ static int sg_io(struct request_queue *q, struct gendisk *bd_disk, ret =0 ;if (hdr->iovec_count) {struct iov_iter i; -struct iovec *iov =NULL ; +struct iovec *iov;#ifdef CONFIG_COMPAT if (in_compat_syscall ()) - ret =compat_import_iovec (rq_data_dir (rq), + iov =compat_iovec_import (rq_data_dir (rq), hdr->dxferp, hdr->iovec_count, -0 , &iov, &i); +NULL , &i); else#endif - ret =import_iovec (rq_data_dir (rq), + iov =iovec_import (rq_data_dir (rq), hdr->dxferp, hdr->iovec_count, -0 , &iov, &i); -if (ret <0 ) +NULL , &i); +if (IS_ERR (iov)) { + ret =PTR_ERR (iov); goto out_free_cdb; + }/* SG_IO howto says that the shorter of the two wins */ iov_iter_truncate (&i, hdr->dxfer_len);
drivers/scsi/sg.c +7 -7
@@ -1817,19 +1817,19 @@ sg_start_req(Sg_request *srp, unsigned char *cmd) }if (iov_count) { -struct iovec *iov =NULL ; +struct iovec *iov;struct iov_iter i;#ifdef CONFIG_COMPAT if (in_compat_syscall ()) - res =compat_import_iovec (rw, hp->dxferp, iov_count, -0 , &iov, &i); + iov =compat_iovec_import (rw, hp->dxferp, iov_count, +NULL , &i); else#endif - res =import_iovec (rw, hp->dxferp, iov_count, -0 , &iov, &i); -if (res <0 ) -return res; + iov =iovec_import (rw, hp->dxferp, iov_count, +NULL , &i); +if (IS_ERR (iov)) +return PTR_ERR (iov);iov_iter_truncate (&i, hp->dxfer_len);if (!iov_iter_count (&i)) {
Signature · 5 lines
2.25.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
So looking at the various callers I'm not sure this API is the best. If we want to do something fancy I'd hide the struct iovec instances entirely with something like:
struct iov_storage {
struct iovec stack[UIO_FASTIOV], *vec;
}int iov_iter_import_iovec(struct iov_iter *iter, struct iov_storage *s,
const struct iovec __user *vec, unsigned long nr_segs, int type);
and then add a new helper to free the thing if needed:
void iov_iter_release_iovec(struct iov_storage *s)
{
if (s->vec != s->stack)
kfree(s->vec);
}From: Christoph Hellwig
Quoted 14 lines
Sent: 21 September 2020 15:22
So looking at the various callers I'm not sure this API is the best. If we want to do something fancy I'd hide the struct iovec instances entirely with something like:
struct iov_storage { struct iovec stack[UIO_FASTIOV], *vec; }int iov_iter_import_iovec(struct
iov_iter*iter, structiov_storage*s,const struct iovec __user *vec, unsigned long nr_segs, int type);
Quoted 7 lines
and then add a new helper to free the thing if needed:
void
iov_iter_release_iovec(struct iov_storage *s){ if (s->vec != s->stack) kfree(s->vec); }
I didn't think of going that far.
There are 2 call sites (in scsi) that don't pass the cache.
Given that the 'buffer to free' address probably needs to be spilled to stack forcing in into an on-stack structure that is already passed by address is probably a good idea.
The iov_iter_release_iovec() should be static inline and just:
if (s->vec) kfree(s->vec);
You want the test because 99.99% of the time it will be NULL. The kernel iov[] to use is iter.iov not part of the cache.
That will be a bigger change on the io_uring code. (The patch I didn't write.)
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
From: Christoph Hellwig
Quoted 14 lines
Sent: 21 September 2020 15:22
So looking at the various callers I'm not sure this API is the best. If we want to do something fancy I'd hide the struct iovec instances entirely with something like:
struct iov_storage { struct iovec stack[UIO_FASTIOV], *vec; }int iov_iter_import_iovec(struct
iov_iter*iter, structiov_storage*s,const struct iovec __user *vec, unsigned long nr_segs, int type);
Quoted 7 lines
and then add a new helper to free the thing if needed:
void
iov_iter_release_iovec(struct iov_storage *s){ if (s->vec != s->stack) kfree(s->vec); }
I've been looking at this code again now most of the pending changes are in Linus's tree.
I was actually looking at going one stage further.
The 'iov_iter' is always allocated with the 'iov_storage' *above). Usually both are on the callers stack - possibly in different functions.
So add:
struct iovec_iter {
struct iov_iter iter;
struct iovec to_free;
struct iovec stack[UIO_FASTIOV];
};int __iovec_import(struct iovec_iter *, const struct iovec __user *vec,
unsigned long nr_segs, int type, bool compat);
And a 'clean' function to do kfree(iovec->to_free);
This reduces the complexity of most of the callers.
I started doing the changes, but got in a mess in io_uring.c (as usual). I think I've got a patch pending (in my brain) to simplify the io_uring code.
The plan is to add:
if (iter->iov != xxx->to_free) iter->iov = xxx->stack;
Prior to every use of the iter.
This fixes up anything that got broken by a memcpy() of the fields. The tidyup code is then always kfree(xxx->to_free).
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)