Skip to contentSkip to conversation

Implement pv_read_some

PATCH In review Felix Schmoll 3 participants 6 messages Jul 24, 2017 – Jul 25, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 Implement pv_read_some

Implement reading from PV console. Making use of polling.

---
Changed since v3:
* Add to comment: read function blocks if no data available

---
 common/console.c      | 26 ++++++++++++++++++++++++++
 include/xtf/console.h |  2 ++
 2 files changed, 28 insertions(+)
common/console.c +26 -0
index 7cb2361..b1a35f4 100644
@@ -46,6 +46,32 @@ static size_t pv_console_write_some(const char *buf, size_t len)
 }
 
 /*
+ * Read out data from the pv ring, either until buffer is filled or no
+ * more data are available. Might result in partial strings, depending
+ * on how xenconsoled passes in data.
+ *
+ * Will block if no data are available.
+ */
+size_t pv_console_read_some(char *buf, size_t len)
+{
+    size_t s = 0;
+    uint32_t cons, prod;
+
+    while ( !test_and_clear_bit(pv_evtchn, shared_info.evtchn_pending) ||
+            (pv_ring->in_cons == pv_ring->in_prod) )
+        hypercall_poll(pv_evtchn);
+
+    cons = pv_ring->in_cons, prod = LOAD_ACQUIRE(&pv_ring->in_prod);
+
+    while ( (s < len) && (0 < (prod - cons)) )
+        buf[s++] = pv_ring->in[cons++ & (sizeof(pv_ring->in) - 1)];
+
+    STORE_RELEASE(&pv_ring->in_cons, cons);
+
+    return s;
+}
+
+/*
  * Write some data into the pv ring, synchronously waiting for all data to be
  * consumed.
  */
include/xtf/console.h +2 -0
index 2a93c06..caec790 100644
@@ -25,6 +25,8 @@ void init_pv_console(xencons_interface_t *ring,
 void vprintk(const char *fmt, va_list args) __printf(1, 0);
 void printk(const char *fmt, ...) __printf(1, 2);
 
+size_t pv_console_read_some(char *buf, size_t len);
+
 #endif /* XTF_CONSOLE_H */
 
 /*
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw
In reply to Felix Schmoll
Quoted 26 lines · Felix Schmoll

Implement reading from PV console. Making use of polling.

---
Changed since v3:
* Add to comment: read function blocks if no data available

---
 common/console.c      | 26 ++++++++++++++++++++++++++
 include/xtf/console.h |  2 ++
 2 files changed, 28 insertions(+)
common/console.c +6 -0
index 7cb2361..b1a35f4 100644
@@ -46,6 +46,32 @@ static size_t pv_console_write_some(const char *buf, size_t len)
 }
 
 /*
+ * Read out data from the pv ring, either until buffer is filled or no
+ * more data are available. Might result in partial strings, depending
+ * on how xenconsoled passes in data.
+ *
+ * Will block if no data are available.
+ */

common/console.c static size_t pv_console_write_some(const char *buf, size_t len) · line 64

Quoted 10 lines
common/console.c +10 -0
+size_t pv_console_read_some(char *buf, size_t len)
+{
+    size_t s = 0;
+    uint32_t cons, prod;
+
+    while ( !test_and_clear_bit(pv_evtchn, shared_info.evtchn_pending) ||
+            (pv_ring->in_cons == pv_ring->in_prod) )
+        hypercall_poll(pv_evtchn);
+
+    cons = pv_ring->in_cons, prod = LOAD_ACQUIRE(&pv_ring->in_prod);

Andrew pointed out on IRC this will degrade to

    cons = prod = LOAD_ACQUIRE(...);

I failed to spot this earlier.

See

http://en.cppreference.com/w/c/language/operator_precedence

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
In reply to Felix Schmoll
Quoted 26 lines · Felix Schmoll

Implement reading from PV console. Making use of polling.

---
Changed since v3:
* Add to comment: read function blocks if no data available

---
 common/console.c      | 26 ++++++++++++++++++++++++++
 include/xtf/console.h |  2 ++
 2 files changed, 28 insertions(+)
common/console.c +6 -0
index 7cb2361..b1a35f4 100644
@@ -46,6 +46,32 @@ static size_t pv_console_write_some(const char *buf, size_t len)
 }
 
 /*
+ * Read out data from the pv ring, either until buffer is filled or no
+ * more data are available. Might result in partial strings, depending
+ * on how xenconsoled passes in data.
+ *
+ * Will block if no data are available.
+ */

common/console.c static size_t pv_console_write_some(const char *buf, size_t len) · line 64

Quoted 10 lines
common/console.c +10 -0
+size_t pv_console_read_some(char *buf, size_t len)
+{
+    size_t s = 0;
+    uint32_t cons, prod;
+
+    while ( !test_and_clear_bit(pv_evtchn, shared_info.evtchn_pending) ||
+            (pv_ring->in_cons == pv_ring->in_prod) )
+        hypercall_poll(pv_evtchn);
+
+    cons = pv_ring->in_cons, prod = LOAD_ACQUIRE(&pv_ring->in_prod);

Given the confusion this has caused on IRC, I'd prefer that the code was explicit. Is the following ok?

cons = pv_ring->in_cons;
prod = LOAD_ACQUIRE(&pv_ring->in_prod);

If so, I can fix up on commit.

~Andrew

Quoted 18 lines
common/console.c +10 -0
+
+    while ( (s < len) && (0 < (prod - cons)) )
+        buf[s++] = pv_ring->in[cons++ & (sizeof(pv_ring->in) - 1)];
+
+    STORE_RELEASE(&pv_ring->in_cons, cons);
+
+    return s;
+}
+
+/*
  * Write some data into the pv ring, synchronously waiting for all data to be
  * consumed.
  */
include/xtf/console.h +0 -0
index 2a93c06..caec790 100644

include/xtf/console.h void init_pv_console(xencons_interface_t *ring, · line 32

Quoted 8 lines
include/xtf/console.h +2 -0
@@ -25,6 +25,8 @@ void init_pv_console(xencons_interface_t *ring,
 void vprintk(const char *fmt, va_list args) __printf(1, 0);
 void printk(const char *fmt, ...) __printf(1, 2);
 
+size_t pv_console_read_some(char *buf, size_t len);
+
 #endif /* XTF_CONSOLE_H */
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

2017-07-24 12:38 GMT+02:00 Andrew Cooper <[email protected]>:

Quoted 5 lines

Given the confusion this has caused on IRC, I'd prefer that the code was explicit. Is the following ok?

Quoted 8 lines

cons = pv_ring->in_cons;
prod = LOAD_ACQUIRE(&pv_ring->in_prod);

If so, I can fix up on commit.

~Andrew

I'm not really convinced by this: If this was really happening, then you could never do multiple declarations in the same line because it would always degrade.

According to for example Wikipedia,

/** * Commas act as separators in this line, not as an operator. *
Results: a=1, b=2, c=3, i=0 */int a=1, b=2, c=3, i=0;

Also, read_some is obviously modeled after write_some, so you would have to change that one too.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

2017-07-25 8:58 GMT+02:00 Felix Schmoll <[email protected]>:

Quoted 18 lines

2017-07-24 12:38 GMT+02:00 Andrew Cooper <[email protected]>:

Quoted 5 lines

Given the confusion this has caused on IRC, I'd prefer that the code was explicit. Is the following ok?

Quoted 8 lines

cons = pv_ring->in_cons;
prod = LOAD_ACQUIRE(&pv_ring->in_prod);

If so, I can fix up on commit.

~Andrew

I'm not really convinced by this: If this was really happening, then you could never do multiple declarations in the same line because it would always degrade.

Quoted 7 lines

According to for example Wikipedia,

/** * Commas act as separators in this line, not as an operator. * Results: a=1, b=2, c=3, i=0 */int a=1, b=2, c=3, i=0;

Also, read_some is obviously modeled after write_some, so you would have to change that one too.

Nevermind, sure, go ahead.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel