From: "Jan Beulich" Subject: [PATCH v2] x86/HVM: avoid pointer wraparound in bufioreq handling Date: Thu, 18 Jun 2015 14:14:46 +0100 Message-ID: <5582E0660200007800086A27@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2D19F756.2__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Z5Zec-0007UR-3o for xen-devel@lists.xenproject.org; Thu, 18 Jun 2015 13:14:50 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser , Stefano Stabellini , Andrew Cooper , Ian Jackson , Tim Deegan , Ian Campbell , Wei Liu List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part2D19F756.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The number of slots per page being 511 (i.e. not a power of two) means that the (32-bit) read and write indexes going beyond 2^32 will likely disturb operation. Extend I/O req server creation so the caller can indicate that it is using suitable atomic accesses where needed (not all accesses to the two pointers really need to be atomic), allowing the hypervisor to atomically canonicalize both pointers when both have gone through at least one cycle. Signed-off-by: Jan Beulich Acked-by: Ian Campbell --- v2: Limit canonicalization loop to IOREQ_BUFFER_SLOT_NUM iterations. Adjust xc_hvm_create_ioreq_server() documentation. --- a/tools/libxc/include/xenctrl.h +++ b/tools/libxc/include/xenctrl.h @@ -1933,7 +1933,8 @@ int xc_get_hvm_param(xc_interface *handl * * @parm xch a handle to an open hypervisor interface. * @parm domid the domain id to be serviced - * @parm handle_bufioreq should the IOREQ Server handle buffered = requests? + * @parm handle_bufioreq how should the IOREQ Server handle buffered = requests + * (HVM_IOREQSRV_BUFIOREQ_*)? * @parm id pointer to an ioservid_t to receive the IOREQ Server id. * @return 0 on success, -1 on failure. */ --- a/tools/libxc/xc_domain.c +++ b/tools/libxc/xc_domain.c @@ -1411,7 +1411,7 @@ int xc_hvm_create_ioreq_server(xc_interf hypercall.arg[1] =3D HYPERCALL_BUFFER_AS_ARG(arg); =20 arg->domid =3D domid; - arg->handle_bufioreq =3D !!handle_bufioreq; + arg->handle_bufioreq =3D handle_bufioreq; =20 rc =3D do_xen_hypercall(xch, &hypercall); =20 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -921,7 +921,7 @@ static void hvm_ioreq_server_disable(str =20 static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, struct = domain *d, domid_t domid, bool_t is_default, - bool_t handle_bufioreq, ioservid_t id) + int bufioreq_handling, ioservid_t id) { struct vcpu *v; int rc; @@ -938,7 +938,11 @@ static int hvm_ioreq_server_init(struct=20 if ( rc ) return rc; =20 - rc =3D hvm_ioreq_server_setup_pages(s, is_default, handle_bufioreq); + if ( bufioreq_handling =3D=3D HVM_IOREQSRV_BUFIOREQ_ATOMIC ) + s->bufioreq_atomic =3D 1; + + rc =3D hvm_ioreq_server_setup_pages( + s, is_default, bufioreq_handling !=3D HVM_IOREQSRV_BUFIOREQ_O= FF); if ( rc ) goto fail_map; =20 @@ -997,12 +1001,15 @@ static ioservid_t next_ioservid(struct d } =20 static int hvm_create_ioreq_server(struct domain *d, domid_t domid, - bool_t is_default, bool_t handle_bufior= eq, + bool_t is_default, int bufioreq_handlin= g, ioservid_t *id) { struct hvm_ioreq_server *s; int rc; =20 + if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC ) + return -EINVAL; + rc =3D -ENOMEM; s =3D xzalloc(struct hvm_ioreq_server); if ( !s ) @@ -1015,7 +1022,7 @@ static int hvm_create_ioreq_server(struc if ( is_default && d->arch.hvm_domain.default_ioreq_server !=3D NULL = ) goto fail2; =20 - rc =3D hvm_ioreq_server_init(s, d, domid, is_default, handle_bufioreq,= + rc =3D hvm_ioreq_server_init(s, d, domid, is_default, bufioreq_handlin= g, next_ioservid(d)); if ( rc ) goto fail3; @@ -2560,7 +2567,7 @@ int hvm_buffered_io_send(ioreq_t *p) =20 spin_lock(&s->bufioreq_lock); =20 - if ( (pg->write_pointer - pg->read_pointer) >=3D + if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=3D (IOREQ_BUFFER_SLOT_NUM - qw) ) { /* The queue is full: send the iopacket through the normal path. = */ @@ -2568,17 +2575,29 @@ int hvm_buffered_io_send(ioreq_t *p) return 0; } =20 - pg->buf_ioreq[pg->write_pointer % IOREQ_BUFFER_SLOT_NUM] =3D bp; + pg->buf_ioreq[pg->ptrs.write_pointer % IOREQ_BUFFER_SLOT_NUM] =3D bp; =20 if ( qw ) { bp.data =3D p->data >> 32; - pg->buf_ioreq[(pg->write_pointer+1) % IOREQ_BUFFER_SLOT_NUM] =3D = bp; + pg->buf_ioreq[(pg->ptrs.write_pointer+1) % IOREQ_BUFFER_SLOT_NUM] = =3D bp; } =20 /* Make the ioreq_t visible /before/ write_pointer. */ wmb(); - pg->write_pointer +=3D qw ? 2 : 1; + pg->ptrs.write_pointer +=3D qw ? 2 : 1; + + /* Canonicalize read/write pointers to prevent their overflow. */ + while ( s->bufioreq_atomic && qw++ < IOREQ_BUFFER_SLOT_NUM && + pg->ptrs.read_pointer >=3D IOREQ_BUFFER_SLOT_NUM ) + { + union bufioreq_pointers old =3D pg->ptrs, new; + unsigned int n =3D old.read_pointer / IOREQ_BUFFER_SLOT_NUM; + + new.read_pointer =3D old.read_pointer - n * IOREQ_BUFFER_SLOT_NUM;= + new.write_pointer =3D old.write_pointer - n * IOREQ_BUFFER_SLOT_NU= M; + cmpxchg(&pg->ptrs.full, old.full, new.full); + } =20 notify_via_xen_event_channel(d, s->bufioreq_evtchn); spin_unlock(&s->bufioreq_lock); @@ -5446,7 +5465,7 @@ static int hvmop_create_ioreq_server( goto out; =20 rc =3D hvm_create_ioreq_server(d, curr_d->domain_id, 0, - !!op.handle_bufioreq, &op.id); + op.handle_bufioreq, &op.id); if ( rc !=3D 0 ) goto out; =20 @@ -5928,7 +5947,8 @@ static int hvmop_get_param( =20 /* May need to create server. */ domid =3D d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN]; - rc =3D hvm_create_ioreq_server(d, domid, 1, 1, NULL); + rc =3D hvm_create_ioreq_server(d, domid, 1, + HVM_IOREQSRV_BUFIOREQ_LEGACY, NULL); if ( rc !=3D 0 && rc !=3D -EEXIST ) goto out; } --- a/xen/include/asm-x86/hvm/domain.h +++ b/xen/include/asm-x86/hvm/domain.h @@ -70,6 +70,7 @@ struct hvm_ioreq_server { evtchn_port_t bufioreq_evtchn; struct rangeset *range[NR_IO_RANGE_TYPES]; bool_t enabled; + bool_t bufioreq_atomic; }; =20 struct hvm_domain { --- a/xen/include/public/hvm/hvm_op.h +++ b/xen/include/public/hvm/hvm_op.h @@ -266,6 +266,13 @@ typedef uint16_t ioservid_t; #define HVMOP_create_ioreq_server 17 struct xen_hvm_create_ioreq_server { domid_t domid; /* IN - domain to be serviced */ +#define HVM_IOREQSRV_BUFIOREQ_OFF 0 +#define HVM_IOREQSRV_BUFIOREQ_LEGACY 1 +/* + * Use this when read_pointer gets updated atomically and + * the pointer pair gets read atomically: + */ +#define HVM_IOREQSRV_BUFIOREQ_ATOMIC 2 uint8_t handle_bufioreq; /* IN - should server handle buffered ioreqs = */ ioservid_t id; /* OUT - server id */ }; --- a/xen/include/public/hvm/ioreq.h +++ b/xen/include/public/hvm/ioreq.h @@ -83,8 +83,17 @@ typedef struct buf_ioreq buf_ioreq_t; =20 #define IOREQ_BUFFER_SLOT_NUM 511 /* 8 bytes each, plus 2 4-byte = indexes */ struct buffered_iopage { - unsigned int read_pointer; - unsigned int write_pointer; +#ifdef __XEN__ + union bufioreq_pointers { + struct { +#endif + uint32_t read_pointer; + uint32_t write_pointer; +#ifdef __XEN__ + }; + uint64_t full; + } ptrs; +#endif buf_ioreq_t buf_ioreq[IOREQ_BUFFER_SLOT_NUM]; }; /* NB. Size of this structure must be no greater than one page. */ typedef struct buffered_iopage buffered_iopage_t; --=__Part2D19F756.2__= Content-Type: text/plain; name="x86-HVM-bufioreq-atomic.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-bufioreq-atomic.patch" x86/HVM: avoid pointer wraparound in bufioreq handling=0A=0AThe number of = slots per page being 511 (i.e. not a power of two) means=0Athat the = (32-bit) read and write indexes going beyond 2^32 will likely=0Adisturb = operation. Extend I/O req server creation so the caller can=0Aindicate = that it is using suitable atomic accesses where needed (not=0Aall accesses = to the two pointers really need to be atomic), allowing=0Athe hypervisor = to atomically canonicalize both pointers when both have=0Agone through at = least one cycle.=0A=0ASigned-off-by: Jan Beulich =0AAcke= d-by: Ian Campbell =0A---=0Av2: Limit canonicaliza= tion loop to IOREQ_BUFFER_SLOT_NUM iterations.=0A Adjust xc_hvm_create_i= oreq_server() documentation.=0A=0A--- a/tools/libxc/include/xenctrl.h=0A+++= b/tools/libxc/include/xenctrl.h=0A@@ -1933,7 +1933,8 @@ int xc_get_hvm_par= am(xc_interface *handl=0A *=0A * @parm xch a handle to an open hypervisor= interface.=0A * @parm domid the domain id to be serviced=0A- * @parm = handle_bufioreq should the IOREQ Server handle buffered requests?=0A+ * = @parm handle_bufioreq how should the IOREQ Server handle buffered = requests=0A+ * (HVM_IOREQSRV_BUFIOREQ_*)?=0A * = @parm id pointer to an ioservid_t to receive the IOREQ Server id.=0A * = @return 0 on success, -1 on failure.=0A */=0A--- a/tools/libxc/xc_domain.c= =0A+++ b/tools/libxc/xc_domain.c=0A@@ -1411,7 +1411,7 @@ int xc_hvm_create_= ioreq_server(xc_interf=0A hypercall.arg[1] =3D HYPERCALL_BUFFER_AS_ARG(= arg);=0A =0A arg->domid =3D domid;=0A- arg->handle_bufioreq =3D = !!handle_bufioreq;=0A+ arg->handle_bufioreq =3D handle_bufioreq;=0A =0A = rc =3D do_xen_hypercall(xch, &hypercall);=0A =0A--- a/xen/arch/x86/hvm/= hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c=0A@@ -921,7 +921,7 @@ static void = hvm_ioreq_server_disable(str=0A =0A static int hvm_ioreq_server_init(struct= hvm_ioreq_server *s, struct domain *d,=0A = domid_t domid, bool_t is_default,=0A- = bool_t handle_bufioreq, ioservid_t id)=0A+ = int bufioreq_handling, ioservid_t id)=0A {=0A struct vcpu *v;=0A = int rc;=0A@@ -938,7 +938,11 @@ static int hvm_ioreq_server_init(struct =0A = if ( rc )=0A return rc;=0A =0A- rc =3D hvm_ioreq_server_setu= p_pages(s, is_default, handle_bufioreq);=0A+ if ( bufioreq_handling = =3D=3D HVM_IOREQSRV_BUFIOREQ_ATOMIC )=0A+ s->bufioreq_atomic =3D = 1;=0A+=0A+ rc =3D hvm_ioreq_server_setup_pages(=0A+ s, = is_default, bufioreq_handling !=3D HVM_IOREQSRV_BUFIOREQ_OFF);=0A if ( = rc )=0A goto fail_map;=0A =0A@@ -997,12 +1001,15 @@ static = ioservid_t next_ioservid(struct d=0A }=0A =0A static int hvm_create_ioreq_s= erver(struct domain *d, domid_t domid,=0A- = bool_t is_default, bool_t handle_bufioreq,=0A+ = bool_t is_default, int bufioreq_handling,=0A = ioservid_t *id)=0A {=0A struct hvm_ioreq_server *s;=0A = int rc;=0A =0A+ if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC = )=0A+ return -EINVAL;=0A+=0A rc =3D -ENOMEM;=0A s =3D = xzalloc(struct hvm_ioreq_server);=0A if ( !s )=0A@@ -1015,7 +1022,7 @@ = static int hvm_create_ioreq_server(struc=0A if ( is_default && = d->arch.hvm_domain.default_ioreq_server !=3D NULL )=0A goto = fail2;=0A =0A- rc =3D hvm_ioreq_server_init(s, d, domid, is_default, = handle_bufioreq,=0A+ rc =3D hvm_ioreq_server_init(s, d, domid, = is_default, bufioreq_handling,=0A next_ioser= vid(d));=0A if ( rc )=0A goto fail3;=0A@@ -2560,7 +2567,7 @@ = int hvm_buffered_io_send(ioreq_t *p)=0A =0A spin_lock(&s->bufioreq_lock= );=0A =0A- if ( (pg->write_pointer - pg->read_pointer) >=3D=0A+ if ( = (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=3D=0A = (IOREQ_BUFFER_SLOT_NUM - qw) )=0A {=0A /* The queue is full: = send the iopacket through the normal path. */=0A@@ -2568,17 +2575,29 @@ = int hvm_buffered_io_send(ioreq_t *p)=0A return 0;=0A }=0A =0A- = pg->buf_ioreq[pg->write_pointer % IOREQ_BUFFER_SLOT_NUM] =3D bp;=0A+ = pg->buf_ioreq[pg->ptrs.write_pointer % IOREQ_BUFFER_SLOT_NUM] =3D bp;=0A = =0A if ( qw )=0A {=0A bp.data =3D p->data >> 32;=0A- = pg->buf_ioreq[(pg->write_pointer+1) % IOREQ_BUFFER_SLOT_NUM] =3D bp;=0A+ = pg->buf_ioreq[(pg->ptrs.write_pointer+1) % IOREQ_BUFFER_SLOT_NUM] = =3D bp;=0A }=0A =0A /* Make the ioreq_t visible /before/ write_poin= ter. */=0A wmb();=0A- pg->write_pointer +=3D qw ? 2 : 1;=0A+ = pg->ptrs.write_pointer +=3D qw ? 2 : 1;=0A+=0A+ /* Canonicalize = read/write pointers to prevent their overflow. */=0A+ while ( s->bufiore= q_atomic && qw++ < IOREQ_BUFFER_SLOT_NUM &&=0A+ pg->ptrs.read_po= inter >=3D IOREQ_BUFFER_SLOT_NUM )=0A+ {=0A+ union bufioreq_point= ers old =3D pg->ptrs, new;=0A+ unsigned int n =3D old.read_pointer = / IOREQ_BUFFER_SLOT_NUM;=0A+=0A+ new.read_pointer =3D old.read_point= er - n * IOREQ_BUFFER_SLOT_NUM;=0A+ new.write_pointer =3D old.write_= pointer - n * IOREQ_BUFFER_SLOT_NUM;=0A+ cmpxchg(&pg->ptrs.full, = old.full, new.full);=0A+ }=0A =0A notify_via_xen_event_channel(d, = s->bufioreq_evtchn);=0A spin_unlock(&s->bufioreq_lock);=0A@@ -5446,7 = +5465,7 @@ static int hvmop_create_ioreq_server(=0A goto out;=0A = =0A rc =3D hvm_create_ioreq_server(d, curr_d->domain_id, 0,=0A- = !!op.handle_bufioreq, &op.id);=0A+ = op.handle_bufioreq, &op.id);=0A if ( rc !=3D 0 )=0A = goto out;=0A =0A@@ -5928,7 +5947,8 @@ static int hvmop_get_param(=0A = =0A /* May need to create server. */=0A domid =3D = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN];=0A- rc =3D = hvm_create_ioreq_server(d, domid, 1, 1, NULL);=0A+ rc =3D hvm_create= _ioreq_server(d, domid, 1,=0A+ = HVM_IOREQSRV_BUFIOREQ_LEGACY, NULL);=0A if ( rc !=3D 0 && rc !=3D = -EEXIST )=0A goto out;=0A }=0A--- a/xen/include/asm-x86/hvm= /domain.h=0A+++ b/xen/include/asm-x86/hvm/domain.h=0A@@ -70,6 +70,7 @@ = struct hvm_ioreq_server {=0A evtchn_port_t bufioreq_evtchn;=0A= struct rangeset *range[NR_IO_RANGE_TYPES];=0A bool_t = enabled;=0A+ bool_t bufioreq_atomic;=0A };=0A = =0A struct hvm_domain {=0A--- a/xen/include/public/hvm/hvm_op.h=0A+++ = b/xen/include/public/hvm/hvm_op.h=0A@@ -266,6 +266,13 @@ typedef uint16_t = ioservid_t;=0A #define HVMOP_create_ioreq_server 17=0A struct xen_hvm_creat= e_ioreq_server {=0A domid_t domid; /* IN - domain to be = serviced */=0A+#define HVM_IOREQSRV_BUFIOREQ_OFF 0=0A+#define HVM_IOREQS= RV_BUFIOREQ_LEGACY 1=0A+/*=0A+ * Use this when read_pointer gets updated = atomically and=0A+ * the pointer pair gets read atomically:=0A+ */=0A+#defi= ne HVM_IOREQSRV_BUFIOREQ_ATOMIC 2=0A uint8_t handle_bufioreq; /* IN - = should server handle buffered ioreqs */=0A ioservid_t id; /* = OUT - server id */=0A };=0A--- a/xen/include/public/hvm/ioreq.h=0A+++ = b/xen/include/public/hvm/ioreq.h=0A@@ -83,8 +83,17 @@ typedef struct = buf_ioreq buf_ioreq_t;=0A =0A #define IOREQ_BUFFER_SLOT_NUM 511 /* 8 = bytes each, plus 2 4-byte indexes */=0A struct buffered_iopage {=0A- = unsigned int read_pointer;=0A- unsigned int write_pointer;=0A+#ifdef = __XEN__=0A+ union bufioreq_pointers {=0A+ struct {=0A+#endif=0A+ = uint32_t read_pointer;=0A+ uint32_t write_pointer;=0A= +#ifdef __XEN__=0A+ };=0A+ uint64_t full;=0A+ } ptrs;=0A+#= endif=0A buf_ioreq_t buf_ioreq[IOREQ_BUFFER_SLOT_NUM];=0A }; /* NB. = Size of this structure must be no greater than one page. */=0A typedef = struct buffered_iopage buffered_iopage_t;=0A --=__Part2D19F756.2__= 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 --=__Part2D19F756.2__=--