--- v10
+++ v11
@@ -7,18 +7,18 @@
is explained in commit c4baad5029(virtio-console: avoid DMA from stack):
1, c[] is on stack,
hvc_console_print():
- char c[N_OUTBUF] __ALIGNED__;
- cons_ops[index]->put_chars(vtermnos[index], c, i);
+ char c[N_OUTBUF] __ALIGNED__;
+ cons_ops[index]->put_chars(vtermnos[index], c, i);
2, ch is on stack,
static void hvc_poll_put_char(,,char ch)
{
- struct tty_struct *tty = driver->ttys[0];
- struct hvc_struct *hp = tty->driver_data;
- int n;
-
- do {
- n = hp->ops->put_chars(hp->vtermno, &ch, 1);
- } while (n <= 0);
+ struct tty_struct *tty = driver->ttys[0];
+ struct hvc_struct *hp = tty->driver_data;
+ int n;
+
+ do {
+ n = hp->ops->put_chars(hp->vtermno, &ch, 1);
+ } while (n <= 0);
}
Commit c4baad5029 is just the fix to avoid DMA from stack memory, which
@@ -32,9 +32,8 @@
In this patch, add 'char cons_outbuf[]' as part of 'struct hvc_struct',
so hp->cons_outbuf is no longer the stack memory, we can use it in above
-case 1. Add 'char outchar' as part of 'struct hvc_struct', we can use it
-in above case 2. We also add lock for each above buf to protect them
-separately instead of using the global lock of hvc.
+cases safely. We also add lock to protect cons_outbuf instead of using
+the global lock of hvc.
Introduce another array(cons_hvcs[]) for hvc pointers next to the
cons_ops[] and vtermnos[] arrays. With the array, we can easily find
@@ -45,12 +44,12 @@
Signed-off-by: Xianting Tian <xianting.tian@linux.alibaba.com>
Signed-off-by: Shile Zhang <shile.zhang@linux.alibaba.com>
---
- drivers/tty/hvc/hvc_console.c | 37 +++++++++++++++++++++--------------
- drivers/tty/hvc/hvc_console.h | 24 +++++++++++++++++++++--
- 2 files changed, 44 insertions(+), 17 deletions(-)
+ drivers/tty/hvc/hvc_console.c | 36 ++++++++++++++++++++---------------
+ drivers/tty/hvc/hvc_console.h | 21 +++++++++++++++++++-
+ 2 files changed, 41 insertions(+), 16 deletions(-)
diff --git a/drivers/tty/hvc/hvc_console.c b/drivers/tty/hvc/hvc_console.c
-index 5bb8c4e44..4d8f112f2 100644
+index 5957ab728..11f2463a1 100644
--- a/drivers/tty/hvc/hvc_console.c
+++ b/drivers/tty/hvc/hvc_console.c
@@ -41,16 +41,6 @@
@@ -65,7 +64,7 @@
-#define N_OUTBUF 16
-#define N_INBUF 16
-
--#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
+-#define __ALIGNED__ __attribute__((__aligned__(L1_CACHE_BYTES)))
-
static struct tty_driver *hvc_driver;
static struct task_struct *hvc_task;
@@ -121,10 +120,10 @@
do {
- n = hp->ops->put_chars(hp->vtermno, &ch, 1);
-+ spin_lock_irqsave(&hp->outchar_lock, flags);
-+ hp->outchar = ch;
-+ n = hp->ops->put_chars(hp->vtermno, hp->outchar, 1);
-+ spin_unlock_irqrestore(&hp->outchar_lock, flags);
++ spin_lock_irqsave(&hp->cons_outbuf_lock, flags);
++ hp->cons_outbuf[0] = ch;
++ n = hp->ops->put_chars(hp->vtermno, &hp->cons_outbuf[0], 1);
++ spin_unlock_irqrestore(&hp->cons_outbuf_lock, flags);
} while (n <= 0);
}
#endif
@@ -138,7 +137,7 @@
if (!hp)
return ERR_PTR(-ENOMEM);
-@@ -931,13 +935,14 @@ struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
+@@ -931,13 +935,13 @@ struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
hp->data = data;
hp->ops = ops;
hp->outbuf_size = outbuf_size;
@@ -149,12 +148,11 @@
INIT_WORK(&hp->tty_resize, hvc_set_winsz);
spin_lock_init(&hp->lock);
-+ spin_lock_init(&hp->outchar_lock);
+ spin_lock_init(&hp->cons_outbuf_lock);
mutex_lock(&hvc_structs_mutex);
/*
-@@ -964,6 +969,7 @@ struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
+@@ -964,6 +968,7 @@ struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
if (i < MAX_NR_HVC_CONSOLES) {
cons_ops[i] = ops;
vtermnos[i] = vtermno;
@@ -162,7 +160,7 @@
}
list_add_tail(&(hp->next), &hvc_structs);
-@@ -988,6 +994,7 @@ int hvc_remove(struct hvc_struct *hp)
+@@ -988,6 +993,7 @@ int hvc_remove(struct hvc_struct *hp)
if (hp->index < MAX_NR_HVC_CONSOLES) {
vtermnos[hp->index] = -1;
cons_ops[hp->index] = NULL;
@@ -171,10 +169,10 @@
/* Don't whack hp->irq because tty_hangup() will need to free the irq. */
diff --git a/drivers/tty/hvc/hvc_console.h b/drivers/tty/hvc/hvc_console.h
-index 18d005814..98f0ced83 100644
+index 18d005814..2c32ab67b 100644
--- a/drivers/tty/hvc/hvc_console.h
+++ b/drivers/tty/hvc/hvc_console.h
-@@ -32,13 +32,21 @@
+@@ -32,12 +32,21 @@
*/
#define HVC_ALLOC_TTY_ADAPTERS 8
@@ -186,7 +184,7 @@
+#define N_OUTBUF 16
+#define N_INBUF 16
+
-+#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
++#define __ALIGNED__ __attribute__((__aligned__(L1_CACHE_BYTES)))
+
struct hvc_struct {
struct tty_port port;
@@ -194,26 +192,23 @@
int index;
int do_wakeup;
- char *outbuf;
-- int outbuf_size;
+ int outbuf_size;
int n_outbuf;
uint32_t vtermno;
- const struct hv_ops *ops;
-@@ -48,6 +56,18 @@ struct hvc_struct {
+@@ -48,6 +57,16 @@ struct hvc_struct {
struct work_struct tty_resize;
struct list_head next;
unsigned long flags;
+
-+ /* the buf is used in hvc console api for putting chars */
++ /*
++ * the buf and its lock are used in hvc console api for putting chars,
++ * and also used in hvc_poll_put_char() for putting single char.
++ */
++ spinlock_t cons_outbuf_lock;
+ char cons_outbuf[N_OUTBUF] __ALIGNED__;
-+ spinlock_t cons_outbuf_lock;
-+
-+ /* the buf is for putting single char to tty */
-+ char outchar;
-+ spinlock_t outchar_lock;
-+
-+ /* the buf is for putting chars to tty */
-+ int outbuf_size;
-+ char outbuf[0] __ALIGNED__;
++
++ /* the buf is used for putting chars to tty */
++ char outbuf[] __ALIGNED__;
};
/* implemented by a low level driver */