linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH 5.10 097/102] tty: teach n_tty line discipline about the new "cookie continuations"
Date: Fri,  5 Mar 2021 13:21:56 +0100	[thread overview]
Message-ID: <20210305120908.050286869@linuxfoundation.org> (raw)
In-Reply-To: <20210305120903.276489876@linuxfoundation.org>

From: Linus Torvalds <torvalds@linux-foundation.org>

commit 15ea8ae8e03fdb845ed3ff5d9f11dd5f4f60252c upstream.

With the conversion to do the tty ldisc read operations in small chunks,
the n_tty line discipline became noticeably slower for throughput
oriented loads, because rather than read things in up to 2kB chunks, it
would return at most 64 bytes per read() system call.

The cost is mainly all in the "do system calls over and over", not
really in the new "copy to an extra kernel buffer".

This can be fixed by teaching the n_tty line discipline about the
"cookie continuation" model, which the chunking code supports because
things like hdlc need to be able to handle packets up to 64kB in size.

Doing that doesn't just get us back to the old performace, but to much
better performance: my stupid "copy 10MB of data over a pty" test
program is now almost twice as fast as it used to be (going down from
0.1s to 0.054s).

This is entirely because it now creates maximal chunks (which happens to
be "one byte less than one page" due to how we do the circular tty
buffers).

NOTE! This case only handles the simpler non-icanon case, which is the
one where people may care about throughput.  I'm going to do the icanon
case later too, because while performance isn't a major issue for that,
there may be programs that think they'll always get a full line and
don't like the 64-byte chunking for that reason.

Such programs are arguably buggy (signals etc can cause random partial
results from tty reads anyway), and good programs will handle such
partial reads, but expecting everybody to write "good programs" has
never been a winning policy for the kernel..

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/tty/n_tty.c |   52 ++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 10 deletions(-)

--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -1943,19 +1943,17 @@ static inline int input_available_p(stru
  *	Helper function to speed up n_tty_read.  It is only called when
  *	ICANON is off; it copies characters straight from the tty queue.
  *
- *	It can be profitably called twice; once to drain the space from
- *	the tail pointer to the (physical) end of the buffer, and once
- *	to drain the space from the (physical) beginning of the buffer
- *	to head pointer.
- *
  *	Called under the ldata->atomic_read_lock sem
  *
+ *	Returns true if it successfully copied data, but there is still
+ *	more data to be had.
+ *
  *	n_tty_read()/consumer path:
  *		caller holds non-exclusive termios_rwsem
  *		read_tail published
  */
 
-static void copy_from_read_buf(struct tty_struct *tty,
+static bool copy_from_read_buf(struct tty_struct *tty,
 				      unsigned char **kbp,
 				      size_t *nr)
 
@@ -1978,10 +1976,14 @@ static void copy_from_read_buf(struct tt
 		/* Turn single EOF into zero-length read */
 		if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
 		    (head == ldata->read_tail))
-			n = 0;
+			return false;
 		*kbp += n;
 		*nr -= n;
+
+		/* If we have more to copy, let the caller know */
+		return head != ldata->read_tail;
 	}
+	return false;
 }
 
 /**
@@ -2129,6 +2131,25 @@ static ssize_t n_tty_read(struct tty_str
 	int packet;
 	size_t tail;
 
+	/*
+	 * Is this a continuation of a read started earler?
+	 *
+	 * If so, we still hold the atomic_read_lock and the
+	 * termios_rwsem, and can just continue to copy data.
+	 */
+	if (*cookie) {
+		if (copy_from_read_buf(tty, &kb, &nr))
+			return kb - kbuf;
+
+		/* No more data - release locks and stop retries */
+		n_tty_kick_worker(tty);
+		n_tty_check_unthrottle(tty);
+		up_read(&tty->termios_rwsem);
+		mutex_unlock(&ldata->atomic_read_lock);
+		*cookie = NULL;
+		return kb - kbuf;
+	}
+
 	c = job_control(tty, file);
 	if (c < 0)
 		return c;
@@ -2223,9 +2244,20 @@ static ssize_t n_tty_read(struct tty_str
 				nr--;
 			}
 
-			/* See comment above copy_from_read_buf() why twice */
-			copy_from_read_buf(tty, &kb, &nr);
-			copy_from_read_buf(tty, &kb, &nr);
+			/*
+			 * Copy data, and if there is more to be had
+			 * and we have nothing more to wait for, then
+			 * let's mark us for retries.
+			 *
+			 * NOTE! We return here with both the termios_sem
+			 * and atomic_read_lock still held, the retries
+			 * will release them when done.
+			 */
+			if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) {
+				remove_wait_queue(&tty->read_wait, &wait);
+				*cookie = cookie;
+				return kb - kbuf;
+			}
 		}
 
 		n_tty_check_unthrottle(tty);



  parent reply	other threads:[~2021-03-05 12:33 UTC|newest]

Thread overview: 119+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-05 12:20 [PATCH 5.10 000/102] 5.10.21-rc1 review Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 001/102] net: usb: qmi_wwan: support ZTE P685M modem Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 002/102] Input: elantech - fix protocol errors for some trackpoints in SMBus mode Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 003/102] Input: elan_i2c - add new trackpoint report type 0x5F Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 004/102] drm/virtio: use kvmalloc for large allocations Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 005/102] x86/build: Treat R_386_PLT32 relocation as R_386_PC32 Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 006/102] JFS: more checks for invalid superblock Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 007/102] sched/core: Allow try_invoke_on_locked_down_task() with irqs disabled Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 008/102] udlfb: Fix memory leak in dlfb_usb_probe Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 009/102] media: mceusb: sanity check for prescaler value Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 010/102] erofs: fix shift-out-of-bounds of blkszbits Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 011/102] media: v4l2-ctrls.c: fix shift-out-of-bounds in std_validate Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 012/102] xfs: Fix assert failure in xfs_setattr_size() Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 013/102] net/af_iucv: remove WARN_ONCE on malformed RX packets Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 014/102] smackfs: restrict bytes count in smackfs write functions Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 015/102] tomoyo: ignore data race while checking quota Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 016/102] net: fix up truesize of cloned skb in skb_prepare_for_shift() Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 017/102] riscv: Get rid of MAX_EARLY_MAPPING_SIZE Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 018/102] nbd: handle device refs for DESTROY_ON_DISCONNECT properly Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 019/102] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 020/102] RDMA/rtrs: Do not signal for heatbeat Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 021/102] RDMA/rtrs-clt: Use bitmask to check sess->flags Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 022/102] RDMA/rtrs-srv: Do not signal REG_MR Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 023/102] tcp: fix tcp_rmem documentation Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 024/102] mptcp: do not wakeup listener for MPJ subflows Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 025/102] net: bridge: use switchdev for port flags set through sysfs too Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 026/102] net/sched: cls_flower: Reject invalid ct_state flags rules Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 027/102] net: dsa: tag_rtl4_a: Support also egress tags Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 028/102] net: ag71xx: remove unnecessary MTU reservation Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 029/102] net: hsr: add support for EntryForgetTime Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 030/102] net: psample: Fix netlink skb length with tunnel info Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 031/102] net: fix dev_ifsioc_locked() race condition Greg Kroah-Hartman
2021-03-08 12:50   ` Pavel Machek
2021-03-08 13:21     ` Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 032/102] dt-bindings: ethernet-controller: fix fixed-link specification Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 033/102] dt-bindings: net: btusb: DT fix s/interrupt-name/interrupt-names/ Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 034/102] ASoC: qcom: Remove useless debug print Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 035/102] rsi: Fix TX EAPOL packet handling against iwlwifi AP Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 036/102] rsi: Move card interrupt handling to RX thread Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 037/102] rcu/nocb: Trigger self-IPI on late deferred wake up before user resume Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 038/102] entry: Explicitly flush pending rcuog wakeup before last rescheduling point Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 039/102] entry/kvm: " Greg Kroah-Hartman
2021-03-05 12:20 ` [PATCH 5.10 040/102] EDAC/amd64: Do not load on family 0x15, model 0x13 Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 041/102] staging: fwserial: Fix error handling in fwserial_create Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 042/102] x86/reboot: Add Zotac ZBOX CI327 nano PCI reboot quirk Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 043/102] vt/consolemap: do font sum unsigned Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 044/102] wlcore: Fix command execute failure 19 for wl12xx Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 045/102] Bluetooth: hci_h5: Set HCI_QUIRK_SIMULTANEOUS_DISCOVERY for btrtl Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 046/102] Bluetooth: btusb: fix memory leak on suspend and resume Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 047/102] mt76: mt7615: reset token when mac_reset happens Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 048/102] pktgen: fix misuse of BUG_ON() in pktgen_thread_worker() Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 049/102] ath10k: fix wmi mgmt tx queue full due to race condition Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 050/102] net: sfp: add mode quirk for GPON module Ubiquiti U-Fiber Instant Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 051/102] Bluetooth: Add new HCI_QUIRK_NO_SUSPEND_NOTIFIER quirk Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 052/102] Bluetooth: Fix null pointer dereference in amp_read_loc_assoc_final_data Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 053/102] staging: most: sound: add sanity check for function argument Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 054/102] staging: bcm2835-audio: Replace unsafe strcpy() with strscpy() Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 055/102] brcmfmac: Add DMI nvram filename quirk for Predia Basic tablet Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 056/102] brcmfmac: Add DMI nvram filename quirk for Voyo winpad A15 tablet Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 057/102] drm/hisilicon: Fix use-after-free Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 058/102] crypto: tcrypt - avoid signed overflow in byte count Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 059/102] fs: make unlazy_walk() error handling consistent Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 060/102] drm/amdgpu: Add check to prevent IH overflow Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 061/102] PCI: Add a REBAR size quirk for Sapphire RX 5600 XT Pulse Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 062/102] ASoC: Intel: bytcr_rt5640: Add new BYT_RT5640_NO_SPEAKERS quirk-flag Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 063/102] drm/amd/display: Guard against NULL pointer deref when get_i2c_info fails Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 064/102] drm/amd/amdgpu: add error handling to amdgpu_virt_read_pf2vf_data Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 065/102] media: uvcvideo: Allow entities with no pads Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 066/102] f2fs: handle unallocated section and zone on pinned/atgc Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 067/102] f2fs: fix to set/clear I_LINKABLE under i_lock Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 068/102] nvme-core: add cancel tagset helpers Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 069/102] nvme-rdma: add clean action for failed reconnection Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 070/102] nvme-tcp: " Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 071/102] ASoC: Intel: Add DMI quirk table to soc_intel_is_byt_cr() Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 072/102] btrfs: fix error handling in commit_fs_roots Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 073/102] drm/amdgpu: enable only one high prio compute queue Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 074/102] perf/x86/kvm: Add Cascade Lake Xeon steppings to isolation_ucodes[] Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 075/102] ASoC: Intel: sof-sdw: indent and add quirks consistently Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 076/102] ASoC: Intel: sof_sdw: detect DMIC number based on mach params Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 077/102] parisc: Bump 64-bit IRQ stack size to 64 KB Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 078/102] sched/features: Fix hrtick reprogramming Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 079/102] ASoC: Intel: bytcr_rt5640: Add quirk for the Estar Beauty HD MID 7316R tablet Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 080/102] ASoC: Intel: bytcr_rt5640: Add quirk for the Voyo Winpad A15 tablet Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 081/102] ASoC: Intel: bytcr_rt5651: Add quirk for the Jumper EZpad 7 tablet Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 082/102] ASoC: Intel: bytcr_rt5640: Add quirk for the Acer One S1002 tablet Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 083/102] scsi: iscsi: Restrict sessions and handles to admin capabilities Greg Kroah-Hartman
2021-03-05 22:42   ` Pavel Machek
2021-03-06  0:05     ` Lee Duncan
2021-03-05 12:21 ` [PATCH 5.10 084/102] scsi: iscsi: Ensure sysfs attributes are limited to PAGE_SIZE Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 085/102] scsi: iscsi: Verify lengths on passthrough PDUs Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 086/102] Xen/gnttab: handle p2m update errors on a per-slot basis Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 087/102] xen-netback: respect gnttab_map_refs()s return value Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 088/102] xen: fix p2m size in dom0 for disabled memory hotplug case Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 089/102] zsmalloc: account the number of compacted pages correctly Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 090/102] remoteproc/mediatek: Fix kernel test robot warning Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 091/102] swap: fix swapfile read/write offset Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 092/102] powerpc/sstep: Check instruction validity against ISA version before emulation Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 093/102] powerpc/sstep: Fix incorrect return from analyze_instr() Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 094/102] tty: fix up iterate_tty_read() EOVERFLOW handling Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 095/102] tty: fix up hung_up_tty_read() conversion Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 096/102] tty: clean up legacy leftovers from n_tty line discipline Greg Kroah-Hartman
2021-03-05 12:21 ` Greg Kroah-Hartman [this message]
2021-03-05 12:21 ` [PATCH 5.10 098/102] tty: teach the n_tty ICANON case about the new "cookie continuations" too Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 099/102] media: v4l: ioctl: Fix memory leak in video_usercopy Greg Kroah-Hartman
2021-03-05 12:21 ` [PATCH 5.10 100/102] ALSA: hda/realtek: Add quirk for Clevo NH55RZQ Greg Kroah-Hartman
2021-03-05 12:22 ` [PATCH 5.10 101/102] ALSA: hda/realtek: Add quirk for Intel NUC 10 Greg Kroah-Hartman
2021-03-05 12:22 ` [PATCH 5.10 102/102] ALSA: hda/realtek: Apply dual codec quirks for MSI Godlike X570 board Greg Kroah-Hartman
2021-03-05 22:10 ` [PATCH 5.10 000/102] 5.10.21-rc1 review Pavel Machek
2021-03-07 17:18   ` Pavel Machek
2021-03-07 17:47     ` Greg Kroah-Hartman
2021-03-06  3:24 ` Guenter Roeck
2021-03-06  9:54   ` Greg Kroah-Hartman
2021-03-06 17:19     ` Greg Kroah-Hartman
2021-03-06  5:31 ` Florian Fainelli
2021-03-07 11:39   ` Greg Kroah-Hartman
2021-03-06  6:25 ` Samuel Zou
2021-03-07 11:38   ` Greg Kroah-Hartman
2021-03-06  9:36 ` Naresh Kamboju
2021-03-06 16:50 ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210305120908.050286869@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).