linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: "Liam R. Howlett" <Liam.Howlett@oracle.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	 linux-fsdevel@vger.kernel.org, brauner@kernel.org,
	viro@zeniv.linux.org.uk,  akpm@linux-foundation.org,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	 gregkh@linuxfoundation.org, linux-mm@kvack.org,
	surenb@google.com,  rppt@kernel.org, adobriyan@gmail.com
Subject: Re: [PATCH v5 2/6] fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps
Date: Wed, 26 Jun 2024 09:37:52 -0700	[thread overview]
Message-ID: <CAEf4BzZ9G9aek7u7Tr=307h8eK4v98LGZVsm5Eqz_4jpsEaiag@mail.gmail.com> (raw)
In-Reply-To: <dqa4q42iy3yyzagm2fdvxqdlwbn5pc7uf5gizbdsvrsbcjglpo@s67nv7kam5a7>

On Tue, Jun 25, 2024 at 7:42 PM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
>
> * Andrii Nakryiko <andrii@kernel.org> [240618 18:45]:
> ...
>
> > +
> > +static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
> > +{
> > +     struct procmap_query karg;
> > +     struct vm_area_struct *vma;
> > +     struct mm_struct *mm;
> > +     const char *name = NULL;
> > +     char *name_buf = NULL;
> > +     __u64 usize;
> > +     int err;
> > +
> > +     if (copy_from_user(&usize, (void __user *)uarg, sizeof(usize)))
> > +             return -EFAULT;
> > +     /* argument struct can never be that large, reject abuse */
> > +     if (usize > PAGE_SIZE)
> > +             return -E2BIG;
> > +     /* argument struct should have at least query_flags and query_addr fields */
> > +     if (usize < offsetofend(struct procmap_query, query_addr))
> > +             return -EINVAL;
> > +     err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
> > +     if (err)
> > +             return err;
> > +
> > +     /* reject unknown flags */
> > +     if (karg.query_flags & ~PROCMAP_QUERY_VALID_FLAGS_MASK)
> > +             return -EINVAL;
> > +     /* either both buffer address and size are set, or both should be zero */
> > +     if (!!karg.vma_name_size != !!karg.vma_name_addr)
> > +             return -EINVAL;
> > +
> > +     mm = priv->mm;
> > +     if (!mm || !mmget_not_zero(mm))
> > +             return -ESRCH;
> > +
> > +     err = query_vma_setup(mm);
> > +     if (err) {
> > +             mmput(mm);
> > +             return err;
> > +     }
> > +
> > +     vma = query_matching_vma(mm, karg.query_addr, karg.query_flags);
> > +     if (IS_ERR(vma)) {
> > +             err = PTR_ERR(vma);
> > +             vma = NULL;
> > +             goto out;
> > +     }
> > +
> > +     karg.vma_start = vma->vm_start;
> > +     karg.vma_end = vma->vm_end;
> > +
> > +     karg.vma_flags = 0;
> > +     if (vma->vm_flags & VM_READ)
> > +             karg.vma_flags |= PROCMAP_QUERY_VMA_READABLE;
> > +     if (vma->vm_flags & VM_WRITE)
> > +             karg.vma_flags |= PROCMAP_QUERY_VMA_WRITABLE;
> > +     if (vma->vm_flags & VM_EXEC)
> > +             karg.vma_flags |= PROCMAP_QUERY_VMA_EXECUTABLE;
> > +     if (vma->vm_flags & VM_MAYSHARE)
> > +             karg.vma_flags |= PROCMAP_QUERY_VMA_SHARED;
> > +
> > +     karg.vma_page_size = vma_kernel_pagesize(vma);
> > +
> ...
>
> > +/*
> > + * Input/output argument structured passed into ioctl() call. It can be used
> > + * to query a set of VMAs (Virtual Memory Areas) of a process.
> > + *
> > + * Each field can be one of three kinds, marked in a short comment to the
> > + * right of the field:
> > + *   - "in", input argument, user has to provide this value, kernel doesn't modify it;
> > + *   - "out", output argument, kernel sets this field with VMA data;
> > + *   - "in/out", input and output argument; user provides initial value (used
> > + *     to specify maximum allowable buffer size), and kernel sets it to actual
> > + *     amount of data written (or zero, if there is no data).
> > + *
> > + * If matching VMA is found (according to criterias specified by
> > + * query_addr/query_flags, all the out fields are filled out, and ioctl()
> > + * returns 0. If there is no matching VMA, -ENOENT will be returned.
> > + * In case of any other error, negative error code other than -ENOENT is
> > + * returned.
> > + *
> > + * Most of the data is similar to the one returned as text in /proc/<pid>/maps
> > + * file, but procmap_query provides more querying flexibility. There are no
> > + * consistency guarantees between subsequent ioctl() calls, but data returned
> > + * for matched VMA is self-consistent.
> > + */
> > +struct procmap_query {
> > +     /* Query struct size, for backwards/forward compatibility */
> > +     __u64 size;
> > +     /*
> > +      * Query flags, a combination of enum procmap_query_flags values.
> > +      * Defines query filtering and behavior, see enum procmap_query_flags.
> > +      *
> > +      * Input argument, provided by user. Kernel doesn't modify it.
> > +      */
> > +     __u64 query_flags;              /* in */
> > +     /*
> > +      * Query address. By default, VMA that covers this address will
> > +      * be looked up. PROCMAP_QUERY_* flags above modify this default
> > +      * behavior further.
> > +      *
> > +      * Input argument, provided by user. Kernel doesn't modify it.
> > +      */
> > +     __u64 query_addr;               /* in */
> > +     /* VMA starting (inclusive) and ending (exclusive) address, if VMA is found. */
> > +     __u64 vma_start;                /* out */
> > +     __u64 vma_end;                  /* out */
> > +     /* VMA permissions flags. A combination of PROCMAP_QUERY_VMA_* flags. */
> > +     __u64 vma_flags;                /* out */
> > +     /* VMA backing page size granularity. */
> > +     __u32 vma_page_size;            /* out */
>
> The vma_kernel_pagesize() returns an unsigned long.  We could
> potentially be truncating the returned value (although probably not
> today?).  This is from the vm_operations_struct pagesize, which also
> returns an unsigned long.  Could we switch this to __u64?
>

Yep, fair point. It seemed excessive to use u64, but I guess nothing
prevents us (in the future) from having humongous pages with >4GB
sizes. I'll update to __u64.

I'll wait for a few more hours just in case I get some other feedback,
and will rebase and post a new revision later today, thanks.


> ...
>
> Thanks,
> Liam


  reply	other threads:[~2024-06-26 16:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-18 22:45 [PATCH v5 0/6] ioctl()-based API to query VMAs from /proc/<pid>/maps Andrii Nakryiko
2024-06-18 22:45 ` [PATCH v5 1/6] fs/procfs: extract logic for getting VMA name constituents Andrii Nakryiko
2024-06-18 22:45 ` [PATCH v5 2/6] fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps Andrii Nakryiko
2024-06-26  2:42   ` Liam R. Howlett
2024-06-26 16:37     ` Andrii Nakryiko [this message]
2024-06-18 22:45 ` [PATCH v5 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API Andrii Nakryiko
2024-06-19 10:14   ` Alexey Dobriyan
2024-06-20 18:50     ` Andrii Nakryiko
2024-06-18 22:45 ` [PATCH v5 4/6] docs/procfs: call out ioctl()-based PROCMAP_QUERY command existence Andrii Nakryiko
2024-06-18 22:45 ` [PATCH v5 5/6] tools: sync uapi/linux/fs.h header into tools subdir Andrii Nakryiko
2024-06-18 22:45 ` [PATCH v5 6/6] selftests/proc: add PROCMAP_QUERY ioctl tests Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEf4BzZ9G9aek7u7Tr=307h8eK4v98LGZVsm5Eqz_4jpsEaiag@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rppt@kernel.org \
    --cc=surenb@google.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox