These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / tty / tty_buffer.c
index 2f78b77..3cd31e0 100644 (file)
@@ -242,7 +242,10 @@ void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
        atomic_inc(&buf->priority);
 
        mutex_lock(&buf->lock);
-       while ((next = buf->head->next) != NULL) {
+       /* paired w/ release in __tty_buffer_request_room; ensures there are
+        * no pending memory accesses to the freed buffer
+        */
+       while ((next = smp_load_acquire(&buf->head->next)) != NULL) {
                tty_buffer_free(port, buf->head);
                buf->head = next;
        }
@@ -286,16 +289,19 @@ static int __tty_buffer_request_room(struct tty_port *port, size_t size,
        change = (b->flags & TTYB_NORMAL) && (~flags & TTYB_NORMAL);
        if (change || left < size) {
                /* This is the slow path - looking for new buffers to use */
-               if ((n = tty_buffer_alloc(port, size)) != NULL) {
+               n = tty_buffer_alloc(port, size);
+               if (n != NULL) {
                        n->flags = flags;
                        buf->tail = n;
-                       b->commit = b->used;
-                       /* paired w/ barrier in flush_to_ldisc(); ensures the
+                       /* paired w/ acquire in flush_to_ldisc(); ensures
+                        * flush_to_ldisc() sees buffer data.
+                        */
+                       smp_store_release(&b->commit, b->used);
+                       /* paired w/ acquire in flush_to_ldisc(); ensures the
                         * latest commit value can be read before the head is
                         * advanced to the next buffer
                         */
-                       smp_wmb();
-                       b->next = n;
+                       smp_store_release(&b->next, n);
                } else if (change)
                        size = 0;
                else
@@ -393,8 +399,11 @@ void tty_schedule_flip(struct tty_port *port)
 {
        struct tty_bufhead *buf = &port->buf;
 
-       buf->tail->commit = buf->tail->used;
-       schedule_work(&buf->work);
+       /* paired w/ acquire in flush_to_ldisc(); ensures
+        * flush_to_ldisc() sees buffer data.
+        */
+       smp_store_release(&buf->tail->commit, buf->tail->used);
+       queue_work(system_unbound_wq, &buf->work);
 }
 EXPORT_SYMBOL(tty_schedule_flip);
 
@@ -441,10 +450,9 @@ receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
                count = disc->ops->receive_buf2(tty, p, f, count);
        else {
                count = min_t(int, count, tty->receive_room);
-               if (count)
+               if (count && disc->ops->receive_buf)
                        disc->ops->receive_buf(tty, p, f, count);
        }
-       head->read += count;
        return count;
 }
 
@@ -468,7 +476,7 @@ static void flush_to_ldisc(struct work_struct *work)
        struct tty_struct *tty;
        struct tty_ldisc *disc;
 
-       tty = port->itty;
+       tty = READ_ONCE(port->itty);
        if (tty == NULL)
                return;
 
@@ -487,13 +495,15 @@ static void flush_to_ldisc(struct work_struct *work)
                if (atomic_read(&buf->priority))
                        break;
 
-               next = head->next;
-               /* paired w/ barrier in __tty_buffer_request_room();
+               /* paired w/ release in __tty_buffer_request_room();
                 * ensures commit value read is not stale if the head
                 * is advancing to the next buffer
                 */
-               smp_rmb();
-               count = head->commit - head->read;
+               next = smp_load_acquire(&head->next);
+               /* paired w/ release in __tty_buffer_request_room() or in
+                * tty_buffer_flush(); ensures we see the committed buffer data
+                */
+               count = smp_load_acquire(&head->commit) - head->read;
                if (!count) {
                        if (next == NULL) {
                                check_other_closed(tty);
@@ -507,6 +517,7 @@ static void flush_to_ldisc(struct work_struct *work)
                count = receive_buf(tty, head, count);
                if (!count)
                        break;
+               head->read += count;
        }
 
        mutex_unlock(&buf->lock);
@@ -576,3 +587,13 @@ void tty_buffer_set_lock_subclass(struct tty_port *port)
 {
        lockdep_set_subclass(&port->buf.lock, TTY_LOCK_SLAVE);
 }
+
+bool tty_buffer_restart_work(struct tty_port *port)
+{
+       return queue_work(system_unbound_wq, &port->buf.work);
+}
+
+bool tty_buffer_cancel_work(struct tty_port *port)
+{
+       return cancel_work_sync(&port->buf.work);
+}