Skip to content

Commit

Permalink
bpf: Add bpf_page_to_pfn helper
Browse files Browse the repository at this point in the history
In CRIU, we need to be able to determine whether the page pinned by
io_uring is still present in the same range in the process VMA.
/proc/<pid>/pagemap gives us the PFN, hence using this helper we can
establish this mapping easily from the iterator side.

It is a simple wrapper over the in-kernel page_to_pfn helper, and
ensures the passed in pointer is a struct page PTR_TO_BTF_ID. This is
obtained from the bvec of io_uring_ubuf for the CRIU usecase.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
  • Loading branch information
kkdwivedi authored and intel-lab-lkp committed Nov 16, 2021
1 parent 1081a1c commit 567c9b6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions fs/io_uring.c
Expand Up @@ -11295,6 +11295,23 @@ static struct bpf_iter_reg io_uring_buf_reg_info = {
.seq_info = &bpf_io_uring_buf_seq_info,
};

BPF_CALL_1(bpf_page_to_pfn, struct page *, page)
{
/* PTR_TO_BTF_ID can be NULL */
if (!page)
return U64_MAX;
return page_to_pfn(page);
}

BTF_ID_LIST_SINGLE(btf_page_to_pfn_ids, struct, page)

const struct bpf_func_proto bpf_page_to_pfn_proto = {
.func = bpf_page_to_pfn,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_BTF_ID,
.arg1_btf_id = &btf_page_to_pfn_ids[0],
};

static int __init io_uring_iter_init(void)
{
io_uring_buf_reg_info.ctx_arg_info[0].btf_id = btf_io_uring_ids[0];
Expand Down
1 change: 1 addition & 0 deletions include/linux/bpf.h
Expand Up @@ -2166,6 +2166,7 @@ extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
extern const struct bpf_func_proto bpf_kallsyms_lookup_name_proto;
extern const struct bpf_func_proto bpf_find_vma_proto;
extern const struct bpf_func_proto bpf_page_to_pfn_proto;

const struct bpf_func_proto *tracing_prog_func_proto(
enum bpf_func_id func_id, const struct bpf_prog *prog);
Expand Down
9 changes: 9 additions & 0 deletions include/uapi/linux/bpf.h
Expand Up @@ -4960,6 +4960,14 @@ union bpf_attr {
* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*.
* **-EBUSY** if failed to try lock mmap_lock.
* **-EINVAL** for invalid **flags**.
*
* long bpf_page_to_pfn(struct page *page)
* Description
* Obtain the page frame number (PFN) for the given *struct page*
* pointer.
* Return
* Page Frame Number corresponding to the page pointed to by the
* *struct page* pointer, or U64_MAX if pointer is NULL.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
Expand Down Expand Up @@ -5143,6 +5151,7 @@ union bpf_attr {
FN(skc_to_unix_sock), \
FN(kallsyms_lookup_name), \
FN(find_vma), \
FN(page_to_pfn), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down
2 changes: 2 additions & 0 deletions kernel/trace/bpf_trace.c
Expand Up @@ -1212,6 +1212,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_find_vma_proto;
case BPF_FUNC_trace_vprintk:
return bpf_get_trace_vprintk_proto();
case BPF_FUNC_page_to_pfn:
return &bpf_page_to_pfn_proto;
default:
return bpf_base_func_proto(func_id);
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/bpf_doc.py
Expand Up @@ -549,6 +549,7 @@ def __init__(self, parser):
'struct socket',
'struct file',
'struct bpf_timer',
'struct page',
]
known_types = {
'...',
Expand Down Expand Up @@ -598,6 +599,7 @@ def __init__(self, parser):
'struct socket',
'struct file',
'struct bpf_timer',
'struct page',
}
mapped_types = {
'u8': '__u8',
Expand Down
9 changes: 9 additions & 0 deletions tools/include/uapi/linux/bpf.h
Expand Up @@ -4960,6 +4960,14 @@ union bpf_attr {
* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*.
* **-EBUSY** if failed to try lock mmap_lock.
* **-EINVAL** for invalid **flags**.
*
* long bpf_page_to_pfn(struct page *page)
* Description
* Obtain the page frame number (PFN) for the given *struct page*
* pointer.
* Return
* Page Frame Number corresponding to the page pointed to by the
* *struct page* pointer, or U64_MAX if pointer is NULL.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
Expand Down Expand Up @@ -5143,6 +5151,7 @@ union bpf_attr {
FN(skc_to_unix_sock), \
FN(kallsyms_lookup_name), \
FN(find_vma), \
FN(page_to_pfn), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down

0 comments on commit 567c9b6

Please sign in to comment.