License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 21:07:57 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-01-29 20:53:40 +07:00
|
|
|
/*
|
|
|
|
* Functions related to mapping data to requests
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
2017-02-09 00:51:37 +07:00
|
|
|
#include <linux/sched/task_stack.h>
|
2008-01-29 20:53:40 +07:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/blkdev.h>
|
2015-01-18 22:16:31 +07:00
|
|
|
#include <linux/uio.h>
|
2008-01-29 20:53:40 +07:00
|
|
|
|
|
|
|
#include "blk.h"
|
|
|
|
|
2020-03-28 00:48:37 +07:00
|
|
|
struct bio_map_data {
|
|
|
|
int is_our_pages;
|
|
|
|
struct iov_iter iter;
|
|
|
|
struct iovec iov[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
|
|
|
|
gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
struct bio_map_data *bmd;
|
|
|
|
|
|
|
|
if (data->nr_segs > UIO_MAXIOV)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
|
|
|
|
if (!bmd)
|
|
|
|
return NULL;
|
|
|
|
memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
|
|
|
|
bmd->iter = *data;
|
|
|
|
bmd->iter.iov = bmd->iov;
|
|
|
|
return bmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_copy_from_iter - copy all pages from iov_iter to bio
|
|
|
|
* @bio: The &struct bio which describes the I/O as destination
|
|
|
|
* @iter: iov_iter as source
|
|
|
|
*
|
|
|
|
* Copy all pages from iov_iter to bio.
|
|
|
|
* Returns 0 on success, or error on failure.
|
|
|
|
*/
|
|
|
|
static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter)
|
|
|
|
{
|
|
|
|
struct bio_vec *bvec;
|
|
|
|
struct bvec_iter_all iter_all;
|
|
|
|
|
|
|
|
bio_for_each_segment_all(bvec, bio, iter_all) {
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = copy_page_from_iter(bvec->bv_page,
|
|
|
|
bvec->bv_offset,
|
|
|
|
bvec->bv_len,
|
|
|
|
iter);
|
|
|
|
|
|
|
|
if (!iov_iter_count(iter))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ret < bvec->bv_len)
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_copy_to_iter - copy all pages from bio to iov_iter
|
|
|
|
* @bio: The &struct bio which describes the I/O as source
|
|
|
|
* @iter: iov_iter as destination
|
|
|
|
*
|
|
|
|
* Copy all pages from bio to iov_iter.
|
|
|
|
* Returns 0 on success, or error on failure.
|
|
|
|
*/
|
|
|
|
static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
|
|
|
|
{
|
|
|
|
struct bio_vec *bvec;
|
|
|
|
struct bvec_iter_all iter_all;
|
|
|
|
|
|
|
|
bio_for_each_segment_all(bvec, bio, iter_all) {
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = copy_page_to_iter(bvec->bv_page,
|
|
|
|
bvec->bv_offset,
|
|
|
|
bvec->bv_len,
|
|
|
|
&iter);
|
|
|
|
|
|
|
|
if (!iov_iter_count(&iter))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ret < bvec->bv_len)
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_uncopy_user - finish previously mapped bio
|
|
|
|
* @bio: bio being terminated
|
|
|
|
*
|
|
|
|
* Free pages allocated from bio_copy_user_iov() and write back data
|
|
|
|
* to user space in case of a read.
|
|
|
|
*/
|
|
|
|
static int bio_uncopy_user(struct bio *bio)
|
|
|
|
{
|
|
|
|
struct bio_map_data *bmd = bio->bi_private;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
|
|
|
|
/*
|
|
|
|
* if we're in a workqueue, the request is orphaned, so
|
|
|
|
* don't copy into a random user address space, just free
|
|
|
|
* and return -EINTR so user space doesn't expect any data.
|
|
|
|
*/
|
|
|
|
if (!current->mm)
|
|
|
|
ret = -EINTR;
|
|
|
|
else if (bio_data_dir(bio) == READ)
|
|
|
|
ret = bio_copy_to_iter(bio, bmd->iter);
|
|
|
|
if (bmd->is_our_pages)
|
|
|
|
bio_free_pages(bio);
|
|
|
|
}
|
|
|
|
kfree(bmd);
|
|
|
|
bio_put(bio);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_copy_user_iov - copy user data to bio
|
|
|
|
* @q: destination block queue
|
|
|
|
* @map_data: pointer to the rq_map_data holding pages (if necessary)
|
|
|
|
* @iter: iovec iterator
|
|
|
|
* @gfp_mask: memory allocation flags
|
|
|
|
*
|
|
|
|
* Prepares and returns a bio for indirect user io, bouncing data
|
|
|
|
* to/from kernel pages as necessary. Must be paired with
|
|
|
|
* call bio_uncopy_user() on io completion.
|
|
|
|
*/
|
|
|
|
static struct bio *bio_copy_user_iov(struct request_queue *q,
|
|
|
|
struct rq_map_data *map_data, struct iov_iter *iter,
|
|
|
|
gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
struct bio_map_data *bmd;
|
|
|
|
struct page *page;
|
|
|
|
struct bio *bio;
|
|
|
|
int i = 0, ret;
|
|
|
|
int nr_pages;
|
|
|
|
unsigned int len = iter->count;
|
|
|
|
unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
|
|
|
|
|
|
|
|
bmd = bio_alloc_map_data(iter, gfp_mask);
|
|
|
|
if (!bmd)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to do a deep copy of the iov_iter including the iovecs.
|
|
|
|
* The caller provided iov might point to an on-stack or otherwise
|
|
|
|
* shortlived one.
|
|
|
|
*/
|
|
|
|
bmd->is_our_pages = map_data ? 0 : 1;
|
|
|
|
|
|
|
|
nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
|
|
|
|
if (nr_pages > BIO_MAX_PAGES)
|
|
|
|
nr_pages = BIO_MAX_PAGES;
|
|
|
|
|
|
|
|
ret = -ENOMEM;
|
|
|
|
bio = bio_kmalloc(gfp_mask, nr_pages);
|
|
|
|
if (!bio)
|
|
|
|
goto out_bmd;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (map_data) {
|
|
|
|
nr_pages = 1 << map_data->page_order;
|
|
|
|
i = map_data->offset / PAGE_SIZE;
|
|
|
|
}
|
|
|
|
while (len) {
|
|
|
|
unsigned int bytes = PAGE_SIZE;
|
|
|
|
|
|
|
|
bytes -= offset;
|
|
|
|
|
|
|
|
if (bytes > len)
|
|
|
|
bytes = len;
|
|
|
|
|
|
|
|
if (map_data) {
|
|
|
|
if (i == map_data->nr_entries * nr_pages) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
page = map_data->pages[i / nr_pages];
|
|
|
|
page += (i % nr_pages);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
page = alloc_page(q->bounce_gfp | gfp_mask);
|
|
|
|
if (!page) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) {
|
|
|
|
if (!map_data)
|
|
|
|
__free_page(page);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= bytes;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (map_data)
|
|
|
|
map_data->offset += bio->bi_iter.bi_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* success
|
|
|
|
*/
|
|
|
|
if ((iov_iter_rw(iter) == WRITE &&
|
|
|
|
(!map_data || !map_data->null_mapped)) ||
|
|
|
|
(map_data && map_data->from_user)) {
|
|
|
|
ret = bio_copy_from_iter(bio, iter);
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
} else {
|
|
|
|
if (bmd->is_our_pages)
|
|
|
|
zero_fill_bio(bio);
|
|
|
|
iov_iter_advance(iter, bio->bi_iter.bi_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bio->bi_private = bmd;
|
|
|
|
if (map_data && map_data->null_mapped)
|
|
|
|
bio_set_flag(bio, BIO_NULL_MAPPED);
|
|
|
|
return bio;
|
|
|
|
cleanup:
|
|
|
|
if (!map_data)
|
|
|
|
bio_free_pages(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
out_bmd:
|
|
|
|
kfree(bmd);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_map_user_iov - map user iovec into bio
|
|
|
|
* @q: the struct request_queue for the bio
|
|
|
|
* @iter: iovec iterator
|
|
|
|
* @gfp_mask: memory allocation flags
|
|
|
|
*
|
|
|
|
* Map the user space address into a bio suitable for io to a block
|
|
|
|
* device. Returns an error pointer in case of error.
|
|
|
|
*/
|
|
|
|
static struct bio *bio_map_user_iov(struct request_queue *q,
|
|
|
|
struct iov_iter *iter, gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
struct bio *bio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!iov_iter_count(iter))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES));
|
|
|
|
if (!bio)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
while (iov_iter_count(iter)) {
|
|
|
|
struct page **pages;
|
|
|
|
ssize_t bytes;
|
|
|
|
size_t offs, added = 0;
|
|
|
|
int npages;
|
|
|
|
|
|
|
|
bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs);
|
|
|
|
if (unlikely(bytes <= 0)) {
|
|
|
|
ret = bytes ? bytes : -EFAULT;
|
|
|
|
goto out_unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
|
|
|
|
|
|
|
|
if (unlikely(offs & queue_dma_alignment(q))) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
j = 0;
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < npages; j++) {
|
|
|
|
struct page *page = pages[j];
|
|
|
|
unsigned int n = PAGE_SIZE - offs;
|
|
|
|
bool same_page = false;
|
|
|
|
|
|
|
|
if (n > bytes)
|
|
|
|
n = bytes;
|
|
|
|
|
|
|
|
if (!__bio_add_pc_page(q, bio, page, n, offs,
|
|
|
|
&same_page)) {
|
|
|
|
if (same_page)
|
|
|
|
put_page(page);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
added += n;
|
|
|
|
bytes -= n;
|
|
|
|
offs = 0;
|
|
|
|
}
|
|
|
|
iov_iter_advance(iter, added);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* release the pages we didn't map into the bio, if any
|
|
|
|
*/
|
|
|
|
while (j < npages)
|
|
|
|
put_page(pages[j++]);
|
|
|
|
kvfree(pages);
|
|
|
|
/* couldn't stuff something into bio? */
|
|
|
|
if (bytes)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio_set_flag(bio, BIO_USER_MAPPED);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subtle -- if bio_map_user_iov() ended up bouncing a bio,
|
|
|
|
* it would normally disappear when its bi_end_io is run.
|
|
|
|
* however, we need it for the unmap, so grab an extra
|
|
|
|
* reference to it
|
|
|
|
*/
|
|
|
|
bio_get(bio);
|
|
|
|
return bio;
|
|
|
|
|
|
|
|
out_unmap:
|
|
|
|
bio_release_pages(bio, false);
|
|
|
|
bio_put(bio);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_unmap_user - unmap a bio
|
|
|
|
* @bio: the bio being unmapped
|
|
|
|
*
|
|
|
|
* Unmap a bio previously mapped by bio_map_user_iov(). Must be called from
|
|
|
|
* process context.
|
|
|
|
*
|
|
|
|
* bio_unmap_user() may sleep.
|
|
|
|
*/
|
|
|
|
static void bio_unmap_user(struct bio *bio)
|
|
|
|
{
|
|
|
|
bio_release_pages(bio, bio_data_dir(bio) == READ);
|
|
|
|
bio_put(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bio_invalidate_vmalloc_pages(struct bio *bio)
|
|
|
|
{
|
|
|
|
#ifdef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
|
|
|
|
if (bio->bi_private && !op_is_write(bio_op(bio))) {
|
|
|
|
unsigned long i, len = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < bio->bi_vcnt; i++)
|
|
|
|
len += bio->bi_io_vec[i].bv_len;
|
|
|
|
invalidate_kernel_vmap_range(bio->bi_private, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bio_map_kern_endio(struct bio *bio)
|
|
|
|
{
|
|
|
|
bio_invalidate_vmalloc_pages(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_map_kern - map kernel address into bio
|
|
|
|
* @q: the struct request_queue for the bio
|
|
|
|
* @data: pointer to buffer to map
|
|
|
|
* @len: length in bytes
|
|
|
|
* @gfp_mask: allocation flags for bio allocation
|
|
|
|
*
|
|
|
|
* Map the kernel address into a bio suitable for io to a block
|
|
|
|
* device. Returns an error pointer in case of error.
|
|
|
|
*/
|
|
|
|
static struct bio *bio_map_kern(struct request_queue *q, void *data,
|
|
|
|
unsigned int len, gfp_t gfp_mask)
|
|
|
|
{
|
|
|
|
unsigned long kaddr = (unsigned long)data;
|
|
|
|
unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
|
|
unsigned long start = kaddr >> PAGE_SHIFT;
|
|
|
|
const int nr_pages = end - start;
|
|
|
|
bool is_vmalloc = is_vmalloc_addr(data);
|
|
|
|
struct page *page;
|
|
|
|
int offset, i;
|
|
|
|
struct bio *bio;
|
|
|
|
|
|
|
|
bio = bio_kmalloc(gfp_mask, nr_pages);
|
|
|
|
if (!bio)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
if (is_vmalloc) {
|
|
|
|
flush_kernel_vmap_range(data, len);
|
|
|
|
bio->bi_private = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = offset_in_page(kaddr);
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
unsigned int bytes = PAGE_SIZE - offset;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (bytes > len)
|
|
|
|
bytes = len;
|
|
|
|
|
|
|
|
if (!is_vmalloc)
|
|
|
|
page = virt_to_page(data);
|
|
|
|
else
|
|
|
|
page = vmalloc_to_page(data);
|
|
|
|
if (bio_add_pc_page(q, bio, page, bytes,
|
|
|
|
offset) < bytes) {
|
|
|
|
/* we don't support partial mappings */
|
|
|
|
bio_put(bio);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
data += bytes;
|
|
|
|
len -= bytes;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio->bi_end_io = bio_map_kern_endio;
|
|
|
|
return bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bio_copy_kern_endio(struct bio *bio)
|
|
|
|
{
|
|
|
|
bio_free_pages(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bio_copy_kern_endio_read(struct bio *bio)
|
|
|
|
{
|
|
|
|
char *p = bio->bi_private;
|
|
|
|
struct bio_vec *bvec;
|
|
|
|
struct bvec_iter_all iter_all;
|
|
|
|
|
|
|
|
bio_for_each_segment_all(bvec, bio, iter_all) {
|
|
|
|
memcpy(p, page_address(bvec->bv_page), bvec->bv_len);
|
|
|
|
p += bvec->bv_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio_copy_kern_endio(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bio_copy_kern - copy kernel address into bio
|
|
|
|
* @q: the struct request_queue for the bio
|
|
|
|
* @data: pointer to buffer to copy
|
|
|
|
* @len: length in bytes
|
|
|
|
* @gfp_mask: allocation flags for bio and page allocation
|
|
|
|
* @reading: data direction is READ
|
|
|
|
*
|
|
|
|
* copy the kernel address into a bio suitable for io to a block
|
|
|
|
* device. Returns an error pointer in case of error.
|
|
|
|
*/
|
|
|
|
static struct bio *bio_copy_kern(struct request_queue *q, void *data,
|
|
|
|
unsigned int len, gfp_t gfp_mask, int reading)
|
|
|
|
{
|
|
|
|
unsigned long kaddr = (unsigned long)data;
|
|
|
|
unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
|
|
unsigned long start = kaddr >> PAGE_SHIFT;
|
|
|
|
struct bio *bio;
|
|
|
|
void *p = data;
|
|
|
|
int nr_pages = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overflow, abort
|
|
|
|
*/
|
|
|
|
if (end < start)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
nr_pages = end - start;
|
|
|
|
bio = bio_kmalloc(gfp_mask, nr_pages);
|
|
|
|
if (!bio)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
struct page *page;
|
|
|
|
unsigned int bytes = PAGE_SIZE;
|
|
|
|
|
|
|
|
if (bytes > len)
|
|
|
|
bytes = len;
|
|
|
|
|
|
|
|
page = alloc_page(q->bounce_gfp | gfp_mask);
|
|
|
|
if (!page)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!reading)
|
|
|
|
memcpy(page_address(page), p, bytes);
|
|
|
|
|
|
|
|
if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
|
|
|
|
break;
|
|
|
|
|
|
|
|
len -= bytes;
|
|
|
|
p += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reading) {
|
|
|
|
bio->bi_end_io = bio_copy_kern_endio_read;
|
|
|
|
bio->bi_private = data;
|
|
|
|
} else {
|
|
|
|
bio->bi_end_io = bio_copy_kern_endio;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bio;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
bio_free_pages(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:31:51 +07:00
|
|
|
/*
|
2017-12-18 14:40:44 +07:00
|
|
|
* Append a bio to a passthrough request. Only works if the bio can be merged
|
|
|
|
* into the request based on the driver constraints.
|
2016-07-19 16:31:51 +07:00
|
|
|
*/
|
2017-12-18 14:40:44 +07:00
|
|
|
int blk_rq_append_bio(struct request *rq, struct bio **bio)
|
2008-01-29 20:53:40 +07:00
|
|
|
{
|
2017-12-18 14:40:44 +07:00
|
|
|
struct bio *orig_bio = *bio;
|
2019-06-06 17:29:01 +07:00
|
|
|
struct bvec_iter iter;
|
|
|
|
struct bio_vec bv;
|
|
|
|
unsigned int nr_segs = 0;
|
2017-12-18 14:40:44 +07:00
|
|
|
|
|
|
|
blk_queue_bounce(rq->q, bio);
|
2017-06-28 01:13:21 +07:00
|
|
|
|
2019-06-06 17:29:01 +07:00
|
|
|
bio_for_each_bvec(bv, *bio, iter)
|
|
|
|
nr_segs++;
|
|
|
|
|
2016-07-19 16:31:51 +07:00
|
|
|
if (!rq->bio) {
|
2019-06-06 17:29:01 +07:00
|
|
|
blk_rq_bio_prep(rq, *bio, nr_segs);
|
2016-07-19 16:31:51 +07:00
|
|
|
} else {
|
2019-06-06 17:29:01 +07:00
|
|
|
if (!ll_back_merge_fn(rq, *bio, nr_segs)) {
|
2017-12-18 14:40:44 +07:00
|
|
|
if (orig_bio != *bio) {
|
|
|
|
bio_put(*bio);
|
|
|
|
*bio = orig_bio;
|
|
|
|
}
|
2016-07-19 16:31:51 +07:00
|
|
|
return -EINVAL;
|
2017-12-18 14:40:44 +07:00
|
|
|
}
|
2016-07-19 16:31:51 +07:00
|
|
|
|
2017-12-18 14:40:44 +07:00
|
|
|
rq->biotail->bi_next = *bio;
|
|
|
|
rq->biotail = *bio;
|
|
|
|
rq->__data_len += (*bio)->bi_iter.bi_size;
|
2008-01-29 20:53:40 +07:00
|
|
|
}
|
2016-07-19 16:31:51 +07:00
|
|
|
|
2008-01-29 20:53:40 +07:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-19 16:31:51 +07:00
|
|
|
EXPORT_SYMBOL(blk_rq_append_bio);
|
2008-01-29 20:53:40 +07:00
|
|
|
|
|
|
|
static int __blk_rq_unmap_user(struct bio *bio)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (bio) {
|
|
|
|
if (bio_flagged(bio, BIO_USER_MAPPED))
|
|
|
|
bio_unmap_user(bio);
|
|
|
|
else
|
|
|
|
ret = bio_uncopy_user(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-03 00:07:14 +07:00
|
|
|
static int __blk_rq_map_user_iov(struct request *rq,
|
|
|
|
struct rq_map_data *map_data, struct iov_iter *iter,
|
|
|
|
gfp_t gfp_mask, bool copy)
|
|
|
|
{
|
|
|
|
struct request_queue *q = rq->q;
|
|
|
|
struct bio *bio, *orig_bio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (copy)
|
|
|
|
bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
|
|
|
|
else
|
|
|
|
bio = bio_map_user_iov(q, iter, gfp_mask);
|
|
|
|
|
|
|
|
if (IS_ERR(bio))
|
|
|
|
return PTR_ERR(bio);
|
|
|
|
|
2017-01-31 22:57:31 +07:00
|
|
|
bio->bi_opf &= ~REQ_OP_MASK;
|
|
|
|
bio->bi_opf |= req_op(rq);
|
|
|
|
|
2016-03-03 00:07:14 +07:00
|
|
|
orig_bio = bio;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We link the bounce buffer in and could have to traverse it
|
|
|
|
* later so we have to get a ref to prevent it from being freed
|
|
|
|
*/
|
2017-12-18 14:40:44 +07:00
|
|
|
ret = blk_rq_append_bio(rq, &bio);
|
2016-03-03 00:07:14 +07:00
|
|
|
if (ret) {
|
|
|
|
__blk_rq_unmap_user(orig_bio);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-18 14:40:44 +07:00
|
|
|
bio_get(bio);
|
2016-03-03 00:07:14 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-29 20:53:40 +07:00
|
|
|
/**
|
2017-01-31 22:57:31 +07:00
|
|
|
* blk_rq_map_user_iov - map user data to a request, for passthrough requests
|
2008-01-29 20:53:40 +07:00
|
|
|
* @q: request queue where request should be inserted
|
|
|
|
* @rq: request to map data to
|
2008-08-28 14:17:06 +07:00
|
|
|
* @map_data: pointer to the rq_map_data holding pages (if necessary)
|
2015-01-18 22:16:31 +07:00
|
|
|
* @iter: iovec iterator
|
2008-08-28 14:17:05 +07:00
|
|
|
* @gfp_mask: memory allocation flags
|
2008-01-29 20:53:40 +07:00
|
|
|
*
|
|
|
|
* Description:
|
2008-08-20 01:13:11 +07:00
|
|
|
* Data will be mapped directly for zero copy I/O, if possible. Otherwise
|
2008-01-29 20:53:40 +07:00
|
|
|
* a kernel bounce buffer is used.
|
|
|
|
*
|
2008-08-20 01:13:11 +07:00
|
|
|
* A matching blk_rq_unmap_user() must be issued at the end of I/O, while
|
2008-01-29 20:53:40 +07:00
|
|
|
* still in process context.
|
|
|
|
*
|
|
|
|
* Note: The mapped bio may need to be bounced through blk_queue_bounce()
|
|
|
|
* before being submitted to the device, as pages mapped may be out of
|
|
|
|
* reach. It's the callers responsibility to make sure this happens. The
|
|
|
|
* original bio must be passed back in to blk_rq_unmap_user() for proper
|
|
|
|
* unmapping.
|
|
|
|
*/
|
|
|
|
int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
|
2015-01-18 22:16:31 +07:00
|
|
|
struct rq_map_data *map_data,
|
|
|
|
const struct iov_iter *iter, gfp_t gfp_mask)
|
2008-01-29 20:53:40 +07:00
|
|
|
{
|
2016-04-09 06:05:19 +07:00
|
|
|
bool copy = false;
|
|
|
|
unsigned long align = q->dma_pad_mask | queue_dma_alignment(q);
|
2016-03-03 00:07:14 +07:00
|
|
|
struct bio *bio = NULL;
|
|
|
|
struct iov_iter i;
|
2018-01-15 05:00:48 +07:00
|
|
|
int ret = -EINVAL;
|
2008-01-29 20:53:40 +07:00
|
|
|
|
2016-12-07 07:18:14 +07:00
|
|
|
if (!iter_is_iovec(iter))
|
|
|
|
goto fail;
|
|
|
|
|
2016-04-09 06:05:19 +07:00
|
|
|
if (map_data)
|
|
|
|
copy = true;
|
|
|
|
else if (iov_iter_alignment(iter) & align)
|
|
|
|
copy = true;
|
|
|
|
else if (queue_virt_boundary(q))
|
|
|
|
copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter);
|
2008-04-11 17:56:51 +07:00
|
|
|
|
2016-03-03 00:07:14 +07:00
|
|
|
i = *iter;
|
|
|
|
do {
|
|
|
|
ret =__blk_rq_map_user_iov(rq, map_data, &i, gfp_mask, copy);
|
|
|
|
if (ret)
|
|
|
|
goto unmap_rq;
|
|
|
|
if (!bio)
|
|
|
|
bio = rq->bio;
|
|
|
|
} while (iov_iter_count(&i));
|
2008-01-29 20:53:40 +07:00
|
|
|
|
2008-04-11 17:56:52 +07:00
|
|
|
if (!bio_flagged(bio, BIO_USER_MAPPED))
|
2016-10-20 20:12:13 +07:00
|
|
|
rq->rq_flags |= RQF_COPY_USER;
|
2008-01-29 20:53:40 +07:00
|
|
|
return 0;
|
2016-03-03 00:07:14 +07:00
|
|
|
|
|
|
|
unmap_rq:
|
2019-12-18 15:44:04 +07:00
|
|
|
blk_rq_unmap_user(bio);
|
2016-12-07 07:18:14 +07:00
|
|
|
fail:
|
2016-03-03 00:07:14 +07:00
|
|
|
rq->bio = NULL;
|
2018-01-15 05:00:48 +07:00
|
|
|
return ret;
|
2008-01-29 20:53:40 +07:00
|
|
|
}
|
2008-08-28 14:17:06 +07:00
|
|
|
EXPORT_SYMBOL(blk_rq_map_user_iov);
|
2008-01-29 20:53:40 +07:00
|
|
|
|
2015-01-18 22:16:29 +07:00
|
|
|
int blk_rq_map_user(struct request_queue *q, struct request *rq,
|
|
|
|
struct rq_map_data *map_data, void __user *ubuf,
|
|
|
|
unsigned long len, gfp_t gfp_mask)
|
|
|
|
{
|
2015-01-18 22:16:31 +07:00
|
|
|
struct iovec iov;
|
|
|
|
struct iov_iter i;
|
2015-03-22 07:06:04 +07:00
|
|
|
int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
|
2015-01-18 22:16:29 +07:00
|
|
|
|
2015-03-22 07:06:04 +07:00
|
|
|
if (unlikely(ret < 0))
|
|
|
|
return ret;
|
2015-01-18 22:16:29 +07:00
|
|
|
|
2015-01-18 22:16:31 +07:00
|
|
|
return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
|
2015-01-18 22:16:29 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(blk_rq_map_user);
|
|
|
|
|
2008-01-29 20:53:40 +07:00
|
|
|
/**
|
|
|
|
* blk_rq_unmap_user - unmap a request with user data
|
|
|
|
* @bio: start of bio list
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unmap a rq previously mapped by blk_rq_map_user(). The caller must
|
|
|
|
* supply the original rq->bio from the blk_rq_map_user() return, since
|
2008-08-20 01:13:11 +07:00
|
|
|
* the I/O completion may have changed rq->bio.
|
2008-01-29 20:53:40 +07:00
|
|
|
*/
|
|
|
|
int blk_rq_unmap_user(struct bio *bio)
|
|
|
|
{
|
|
|
|
struct bio *mapped_bio;
|
|
|
|
int ret = 0, ret2;
|
|
|
|
|
|
|
|
while (bio) {
|
|
|
|
mapped_bio = bio;
|
|
|
|
if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
|
|
|
|
mapped_bio = bio->bi_private;
|
|
|
|
|
|
|
|
ret2 = __blk_rq_unmap_user(mapped_bio);
|
|
|
|
if (ret2 && !ret)
|
|
|
|
ret = ret2;
|
|
|
|
|
|
|
|
mapped_bio = bio;
|
|
|
|
bio = bio->bi_next;
|
|
|
|
bio_put(mapped_bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(blk_rq_unmap_user);
|
|
|
|
|
|
|
|
/**
|
2017-01-31 22:57:31 +07:00
|
|
|
* blk_rq_map_kern - map kernel data to a request, for passthrough requests
|
2008-01-29 20:53:40 +07:00
|
|
|
* @q: request queue where request should be inserted
|
|
|
|
* @rq: request to fill
|
|
|
|
* @kbuf: the kernel buffer
|
|
|
|
* @len: length of user data
|
|
|
|
* @gfp_mask: memory allocation flags
|
2008-04-25 17:47:50 +07:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Data will be mapped directly if possible. Otherwise a bounce
|
2014-02-18 20:54:36 +07:00
|
|
|
* buffer is used. Can be called multiple times to append multiple
|
2009-05-17 22:55:18 +07:00
|
|
|
* buffers.
|
2008-01-29 20:53:40 +07:00
|
|
|
*/
|
|
|
|
int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
|
|
|
|
unsigned int len, gfp_t gfp_mask)
|
|
|
|
{
|
2008-04-25 17:47:50 +07:00
|
|
|
int reading = rq_data_dir(rq) == READ;
|
2010-09-15 18:08:27 +07:00
|
|
|
unsigned long addr = (unsigned long) kbuf;
|
2008-04-25 17:47:50 +07:00
|
|
|
int do_copy = 0;
|
2017-12-18 14:40:44 +07:00
|
|
|
struct bio *bio, *orig_bio;
|
2009-05-17 22:55:18 +07:00
|
|
|
int ret;
|
2008-01-29 20:53:40 +07:00
|
|
|
|
2009-05-23 04:17:50 +07:00
|
|
|
if (len > (queue_max_hw_sectors(q) << 9))
|
2008-01-29 20:53:40 +07:00
|
|
|
return -EINVAL;
|
|
|
|
if (!len || !kbuf)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-09-15 18:08:27 +07:00
|
|
|
do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
|
2008-04-25 17:47:50 +07:00
|
|
|
if (do_copy)
|
|
|
|
bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
|
|
|
|
else
|
|
|
|
bio = bio_map_kern(q, kbuf, len, gfp_mask);
|
|
|
|
|
2008-01-29 20:53:40 +07:00
|
|
|
if (IS_ERR(bio))
|
|
|
|
return PTR_ERR(bio);
|
|
|
|
|
2017-01-31 22:57:31 +07:00
|
|
|
bio->bi_opf &= ~REQ_OP_MASK;
|
|
|
|
bio->bi_opf |= req_op(rq);
|
2008-01-29 20:53:40 +07:00
|
|
|
|
2008-04-25 17:47:50 +07:00
|
|
|
if (do_copy)
|
2016-10-20 20:12:13 +07:00
|
|
|
rq->rq_flags |= RQF_COPY_USER;
|
2008-04-25 17:47:50 +07:00
|
|
|
|
2017-12-18 14:40:44 +07:00
|
|
|
orig_bio = bio;
|
|
|
|
ret = blk_rq_append_bio(rq, &bio);
|
2009-05-17 22:55:18 +07:00
|
|
|
if (unlikely(ret)) {
|
|
|
|
/* request is too big */
|
2017-12-18 14:40:44 +07:00
|
|
|
bio_put(orig_bio);
|
2009-05-17 22:55:18 +07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-29 20:53:40 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(blk_rq_map_kern);
|