@@ -748,6 +748,206 @@ static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
748748 return rc ;
749749}
750750
751+ #define GNTDEV_COPY_BATCH 24
752+
753+ struct gntdev_copy_batch {
754+ struct gnttab_copy ops [GNTDEV_COPY_BATCH ];
755+ struct page * pages [GNTDEV_COPY_BATCH ];
756+ s16 __user * status [GNTDEV_COPY_BATCH ];
757+ unsigned int nr_ops ;
758+ unsigned int nr_pages ;
759+ };
760+
761+ static int gntdev_get_page (struct gntdev_copy_batch * batch , void __user * virt ,
762+ bool writeable , unsigned long * gfn )
763+ {
764+ unsigned long addr = (unsigned long )virt ;
765+ struct page * page ;
766+ unsigned long xen_pfn ;
767+ int ret ;
768+
769+ ret = get_user_pages_fast (addr , 1 , writeable , & page );
770+ if (ret < 0 )
771+ return ret ;
772+
773+ batch -> pages [batch -> nr_pages ++ ] = page ;
774+
775+ xen_pfn = page_to_xen_pfn (page ) + XEN_PFN_DOWN (addr & ~PAGE_MASK );
776+ * gfn = pfn_to_gfn (xen_pfn );
777+
778+ return 0 ;
779+ }
780+
781+ static void gntdev_put_pages (struct gntdev_copy_batch * batch )
782+ {
783+ unsigned int i ;
784+
785+ for (i = 0 ; i < batch -> nr_pages ; i ++ )
786+ put_page (batch -> pages [i ]);
787+ batch -> nr_pages = 0 ;
788+ }
789+
790+ static int gntdev_copy (struct gntdev_copy_batch * batch )
791+ {
792+ unsigned int i ;
793+
794+ gnttab_batch_copy (batch -> ops , batch -> nr_ops );
795+ gntdev_put_pages (batch );
796+
797+ /*
798+ * For each completed op, update the status if the op failed
799+ * and all previous ops for the segment were successful.
800+ */
801+ for (i = 0 ; i < batch -> nr_ops ; i ++ ) {
802+ s16 status = batch -> ops [i ].status ;
803+ s16 old_status ;
804+
805+ if (status == GNTST_okay )
806+ continue ;
807+
808+ if (__get_user (old_status , batch -> status [i ]))
809+ return - EFAULT ;
810+
811+ if (old_status != GNTST_okay )
812+ continue ;
813+
814+ if (__put_user (status , batch -> status [i ]))
815+ return - EFAULT ;
816+ }
817+
818+ batch -> nr_ops = 0 ;
819+ return 0 ;
820+ }
821+
822+ static int gntdev_grant_copy_seg (struct gntdev_copy_batch * batch ,
823+ struct gntdev_grant_copy_segment * seg ,
824+ s16 __user * status )
825+ {
826+ uint16_t copied = 0 ;
827+
828+ /*
829+ * Disallow local -> local copies since there is only space in
830+ * batch->pages for one page per-op and this would be a very
831+ * expensive memcpy().
832+ */
833+ if (!(seg -> flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref )))
834+ return - EINVAL ;
835+
836+ /* Can't cross page if source/dest is a grant ref. */
837+ if (seg -> flags & GNTCOPY_source_gref ) {
838+ if (seg -> source .foreign .offset + seg -> len > XEN_PAGE_SIZE )
839+ return - EINVAL ;
840+ }
841+ if (seg -> flags & GNTCOPY_dest_gref ) {
842+ if (seg -> dest .foreign .offset + seg -> len > XEN_PAGE_SIZE )
843+ return - EINVAL ;
844+ }
845+
846+ if (put_user (GNTST_okay , status ))
847+ return - EFAULT ;
848+
849+ while (copied < seg -> len ) {
850+ struct gnttab_copy * op ;
851+ void __user * virt ;
852+ size_t len , off ;
853+ unsigned long gfn ;
854+ int ret ;
855+
856+ if (batch -> nr_ops >= GNTDEV_COPY_BATCH ) {
857+ ret = gntdev_copy (batch );
858+ if (ret < 0 )
859+ return ret ;
860+ }
861+
862+ len = seg -> len - copied ;
863+
864+ op = & batch -> ops [batch -> nr_ops ];
865+ op -> flags = 0 ;
866+
867+ if (seg -> flags & GNTCOPY_source_gref ) {
868+ op -> source .u .ref = seg -> source .foreign .ref ;
869+ op -> source .domid = seg -> source .foreign .domid ;
870+ op -> source .offset = seg -> source .foreign .offset + copied ;
871+ op -> flags |= GNTCOPY_source_gref ;
872+ } else {
873+ virt = seg -> source .virt + copied ;
874+ off = (unsigned long )virt & ~XEN_PAGE_MASK ;
875+ len = min (len , (size_t )XEN_PAGE_SIZE - off );
876+
877+ ret = gntdev_get_page (batch , virt , false, & gfn );
878+ if (ret < 0 )
879+ return ret ;
880+
881+ op -> source .u .gmfn = gfn ;
882+ op -> source .domid = DOMID_SELF ;
883+ op -> source .offset = off ;
884+ }
885+
886+ if (seg -> flags & GNTCOPY_dest_gref ) {
887+ op -> dest .u .ref = seg -> dest .foreign .ref ;
888+ op -> dest .domid = seg -> dest .foreign .domid ;
889+ op -> dest .offset = seg -> dest .foreign .offset + copied ;
890+ op -> flags |= GNTCOPY_dest_gref ;
891+ } else {
892+ virt = seg -> dest .virt + copied ;
893+ off = (unsigned long )virt & ~XEN_PAGE_MASK ;
894+ len = min (len , (size_t )XEN_PAGE_SIZE - off );
895+
896+ ret = gntdev_get_page (batch , virt , true, & gfn );
897+ if (ret < 0 )
898+ return ret ;
899+
900+ op -> dest .u .gmfn = gfn ;
901+ op -> dest .domid = DOMID_SELF ;
902+ op -> dest .offset = off ;
903+ }
904+
905+ op -> len = len ;
906+ copied += len ;
907+
908+ batch -> status [batch -> nr_ops ] = status ;
909+ batch -> nr_ops ++ ;
910+ }
911+
912+ return 0 ;
913+ }
914+
915+ static long gntdev_ioctl_grant_copy (struct gntdev_priv * priv , void __user * u )
916+ {
917+ struct ioctl_gntdev_grant_copy copy ;
918+ struct gntdev_copy_batch batch ;
919+ unsigned int i ;
920+ int ret = 0 ;
921+
922+ if (copy_from_user (& copy , u , sizeof (copy )))
923+ return - EFAULT ;
924+
925+ batch .nr_ops = 0 ;
926+ batch .nr_pages = 0 ;
927+
928+ for (i = 0 ; i < copy .count ; i ++ ) {
929+ struct gntdev_grant_copy_segment seg ;
930+
931+ if (copy_from_user (& seg , & copy .segments [i ], sizeof (seg ))) {
932+ ret = - EFAULT ;
933+ goto out ;
934+ }
935+
936+ ret = gntdev_grant_copy_seg (& batch , & seg , & copy .segments [i ].status );
937+ if (ret < 0 )
938+ goto out ;
939+
940+ cond_resched ();
941+ }
942+ if (batch .nr_ops )
943+ ret = gntdev_copy (& batch );
944+ return ret ;
945+
946+ out :
947+ gntdev_put_pages (& batch );
948+ return ret ;
949+ }
950+
751951static long gntdev_ioctl (struct file * flip ,
752952 unsigned int cmd , unsigned long arg )
753953{
@@ -767,6 +967,9 @@ static long gntdev_ioctl(struct file *flip,
767967 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY :
768968 return gntdev_ioctl_notify (priv , ptr );
769969
970+ case IOCTL_GNTDEV_GRANT_COPY :
971+ return gntdev_ioctl_grant_copy (priv , ptr );
972+
770973 default :
771974 pr_debug ("priv %p, unknown cmd %x\n" , priv , cmd );
772975 return - ENOIOCTLCMD ;
0 commit comments