From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Cooper Subject: Re: how to deal with copy_to_user returning non zero Date: Fri, 7 Nov 2014 10:44:42 +0000 Message-ID: <545CA29A.2040703@citrix.com> References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============9097860363784739315==" Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Anh Dinh , xen-devel@lists.xen.org List-Id: xen-devel@lists.xenproject.org --===============9097860363784739315== Content-Type: multipart/alternative; boundary="------------020604080304060105040807" --------------020604080304060105040807 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit On 07/11/14 10:38, Anh Dinh wrote: > I'm testing out a hypercall that takes in large data (many MB) from > the user space and simply copy the data back. > > For copying in, I call xmalloc_array() for about 4MB at a time and > call copy_from_user() successfully for the entire input. > > The problem is with copy_to_user() which returns non-zero values. I > tried to vary the size of the data being copy, but still unable to > copy the whole data back. > > What are the cause of copy_to_user() failure? I checked the source I > got quite lost at the code. > > How could I make sure all data is copy back? Surely it is not a memory > limitation problem, because I copy_from_user() succeeds for over 100MB. > copy_to_user() returns non-zero if it encounters a fault while moving data, generally a pagefault. > > ****** > long do_test_copy(long n, unsigned char *in, unsigned char *out){ This looks quite bogus. You should have XEN_GUEST_HANDLE() for a pointer into guest memory. A bare pointer like *in is completely wrong. ~Andrew > unsigned char **tmp; > long ret; > int m = n/(MAX_ALLOC); > tmp = xmalloc_array(unsigned char*,m); > > for (int i=0; i tmp[i] = xmalloc_array(unsigned char, MAX_ALLOC); > copy_from_user(tmp[i],in+i*MAX_ALLOC,MAX_ALLOC); > } > > for (int i=0; i if (tmp[i]){ > ret = > copy_to_user(out+i*MAX_ALLOC,tmp[i],MAX_ALLOC); > xfree(tmp[i]); > } > } > xfree(tmp); > return 0; > } > **** > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xen.org > http://lists.xen.org/xen-devel --------------020604080304060105040807 Content-Type: text/html; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit
On 07/11/14 10:38, Anh Dinh wrote:
I'm testing out a hypercall that takes in large data (many MB) from the user space and simply copy the data back. 

For copying in, I call xmalloc_array() for about 4MB at a time and call copy_from_user() successfully for the entire input. 

The problem is with copy_to_user() which returns non-zero values. I tried to vary the size of the data being copy, but still unable to copy the whole data back. 

What are the cause of copy_to_user() failure? I checked the source I got quite lost at the code. 

How could I make sure all data is copy back? Surely it is not a memory limitation problem, because I copy_from_user() succeeds for over 100MB. 


copy_to_user() returns non-zero if it encounters a fault while moving data, generally a pagefault.


******
long do_test_copy(long n, unsigned char *in, unsigned char *out){

This looks quite bogus.  You should have XEN_GUEST_HANDLE() for a pointer into guest memory.  A bare pointer like *in is completely wrong.

~Andrew

        unsigned char **tmp; 
        long ret;
        int m = n/(MAX_ALLOC); 
        tmp = xmalloc_array(unsigned char*,m); 

        for (int i=0; i<m; i++){
                tmp[i] = xmalloc_array(unsigned char, MAX_ALLOC); 
                copy_from_user(tmp[i],in+i*MAX_ALLOC,MAX_ALLOC); 
        }
        
        for (int i=0; i<m; i++){
                if (tmp[i]){
                        ret = copy_to_user(out+i*MAX_ALLOC,tmp[i],MAX_ALLOC);                 
                        xfree(tmp[i]); 
                }
        }
        xfree(tmp);
        return 0;       
}
****



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

--------------020604080304060105040807-- --===============9097860363784739315== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --===============9097860363784739315==--