linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josef Bacik <josef@redhat.com>
To: Vitaly Mayatskikh <v.mayatskih@gmail.com>
Cc: Josef Bacik <josef@redhat.com>,
	sandeen@redhat.com, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] Introduce check_readable_bytes()
Date: Thu, 14 May 2009 13:40:09 -0400	[thread overview]
Message-ID: <20090514173858.GA10059@dhcp231-156.rdu.redhat.com> (raw)
In-Reply-To: <1242317939-15392-2-git-send-email-v.mayatskih@gmail.com>

On Thu, May 14, 2009 at 06:19:00PM +0200, Vitaly Mayatskikh wrote:
> This routine acts almost as fault_in_pages_readable(), but returns
> accessible amount of bytes instead of plain OK/FAIL, so callers
> may know how many data can be proceeded without GPF.
> 
> Signed-off-by: Vitaly Mayatskikh <v.mayatskih@gmail.com>
> ---
>  include/linux/pagemap.h |   35 +++++++++++++++++++++++++++++++++++
>  1 files changed, 35 insertions(+), 0 deletions(-)
> 
> diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
> index 34da523..f931308 100644
> --- a/include/linux/pagemap.h
> +++ b/include/linux/pagemap.h
> @@ -439,6 +439,41 @@ static inline int fault_in_pages_readable(const char __user *uaddr, int size)
>  	return ret;
>  }
>  
> +/*
> + * check_readable_bytes - check if given amount of bytes can be read
> + * at given address.
> + * @uaddr: address to check
> + * @size: size to check
> + *
> + * Returns 0 when @size is 0, -EFAULT when @uaddr points to
> + * unaccessible region, or count of accessible bytes.
> + */
> +static inline int check_readable_bytes(const char __user *uaddr, int size)
> +{
> +	volatile char c;
> +	int ret = 0;
> +	long page_begin = (unsigned long)uaddr & PAGE_MASK;
> +	long page_end = ((unsigned long)uaddr + size) & PAGE_MASK;
> +	long ptr = page_begin;
> +

Firstly page_begin/page_end/ptr should all be unsigned long.  Secondly page_end
should be uaddr+size-1.

> +	if (unlikely(size == 0))
> +		goto out;
> +
> +	while (!ret && ptr <= page_end) {
> +		ret = __get_user(c, (const char __user*)ptr);
> +		if (!ret)
> +			ptr += PAGE_SIZE;
> +	}
> +
> +	if (likely(!ret))
> +		ret = size;
> +	else
> +		ret = (ptr == page_begin) ?
> +			-EFAULT : (const char __user *)ptr - uaddr;

This isn't quite right either, we do some pointer math, and return that and its
casted as an int?  That seems like a bad idea in general.  Thanks,

Josef

  reply	other threads:[~2009-05-14 17:41 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-14 16:18 [PATCH 0/2] sys_write() should write all valid data Vitaly Mayatskikh
2009-05-14 16:19 ` [PATCH 1/2] Introduce check_readable_bytes() Vitaly Mayatskikh
2009-05-14 17:40   ` Josef Bacik [this message]
2009-05-14 17:57     ` Vitaly Mayatskikh
2009-05-14 16:19 ` [PATCH 2/2] Perform check in iov_iter_fault_in_readable() by check_readable_bytes() Vitaly Mayatskikh
2009-05-15  6:56   ` Andi Kleen
2009-05-15  7:56     ` Vitaly Mayatskikh
2009-05-15  9:38       ` Andi Kleen
2009-05-15 11:56         ` Vitaly Mayatskikh
2009-05-15 12:19           ` Andi Kleen
2009-05-15 13:43         ` Jamie Lokier
2009-05-15 14:01           ` Andi Kleen
2009-05-15 14:37             ` Jamie Lokier
2009-05-18  8:31           ` Alan Cox
2009-05-18  9:48             ` Jamie Lokier
2009-05-18 10:03               ` Alan Cox
2009-05-18 10:16               ` Andi Kleen
2009-05-19  8:55   ` Pavel Machek
2009-05-14 18:02 ` [PATCH 0/2] sys_write() should write all valid data Josef Bacik
2009-05-14 18:48   ` Vitaly Mayatskikh
2009-05-14 19:05     ` Josef Bacik
2009-05-15  6:52 ` Andi Kleen

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=20090514173858.GA10059@dhcp231-156.rdu.redhat.com \
    --to=josef@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sandeen@redhat.com \
    --cc=v.mayatskih@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).