From nobody Wed Oct 12 15:54:11 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Mncdv5BY4z4fd3H; Wed, 12 Oct 2022 15:54:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Mncdv4SCYz3GcT; Wed, 12 Oct 2022 15:54:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1665590051; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=RSzQ5CWi1xyqaEgIsQRPd166RM35SAKByEJzykvbMt4=; b=GjPpxtfWTKGwn7bWxVtqpsmASuqMa6onAnSrvltbV8ZD1rpJMLADhc/Gapr2Gxae4luEUW AcWZmd3eYMAyr930kXZRZRDynINbwCXF/KI+JoxrP4gyc6LqEsZsG7znTcAc90WjBmkZSr N0tlE0ScGSrOHp8TCjyLbEp/mNy46ePJ0tAKtqEliCjjObM2E/hbyV+DinWLYpx8qfStJk VJp8UglwH0OR3dTk3+LP2Rl5pxXyPtkxTMr1vXxFO0/+tMN5/WYOeEAK9SI2wOKvkRaurq 8BG4r2JhQmN2StY7WxyHunoTl/7/ZBxFQnwbX/EaUswIidCXwKif1a3pOjtAVQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Mncdv3WhGz1M4c; Wed, 12 Oct 2022 15:54:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 29CFsB65043730; Wed, 12 Oct 2022 15:54:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29CFsBFV043729; Wed, 12 Oct 2022 15:54:11 GMT (envelope-from git) Date: Wed, 12 Oct 2022 15:54:11 GMT Message-Id: <202210121554.29CFsBFV043729@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Hans Petter Selasky Subject: git: 7c5ac67bc330 - stable/13 - cuse(3): Optimise small reads and writes. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hselasky X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7c5ac67bc330a4dba71bb1ca80928ff01083fd07 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1665590051; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=RSzQ5CWi1xyqaEgIsQRPd166RM35SAKByEJzykvbMt4=; b=N8CIjnB/p0AsSj0Xaef8xvaCyBvv05HnnIqPyFm2pR1IEMFpn+4Ev8295Ln4sTKlFKk0Xj zfx4TQAIXHE/6ZiQDCG+phTZ0tvgIU234L6EqlVM6SLYJj7eTyxz0OEdsXWZWeVxKFmKVZ 6mIgoKtgOeqAGlw2SnlWtLMAcIKz+BZeC9hcxwghyx54T1k1z5f+5d8T8zUzt9t5Ik8M9y lelM7nK8XU9O8UD4D2BFk4+tCBcXNez8S7lLvnBoD9baeQHyWn+/na1mtZkH2BWyHHGRbh k82p8I3WAhLtQR+jsYc/V3oGX2dqA0DQs2EUR9ISooDQGnYq5wawZ4jI3tiRxg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1665590051; a=rsa-sha256; cv=none; b=nDqEFT3MlKVSpAgLZJH06P4KFuxErnZ3bwrOTE8MpoxybBAfxdQMwwjmLdAs5lIkPOZ8LW iLu+EQXU0KowlHHXlPaPgeFl63tvLhlb9W+nIJEGWZtb2E4NH0GQSE2KY9bkrz6WiADUKg dXdmB9P/llHUQeFtxo/yFu6xEUJeSgEY5H+Tvr/sK6p1StflEdzmx7e2i9uwvCQ8R/1TUp HyOKAxmO6QpbwIMzfhcbzb9C2dXwKjCP6Tfufp+4Ak/oEqhCGbZIT1LgCATxBP2pvdCrxD xQXbdK6HObgno+DrS2K7LYSYzpgmL37zM/J8KcONlfBZH8OG4m+4xBuQnQsjhg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=7c5ac67bc330a4dba71bb1ca80928ff01083fd07 commit 7c5ac67bc330a4dba71bb1ca80928ff01083fd07 Author: Hans Petter Selasky AuthorDate: 2022-09-20 13:58:36 +0000 Commit: Hans Petter Selasky CommitDate: 2022-10-12 15:53:21 +0000 cuse(3): Optimise small reads and writes. When doing small reads and writes use an intermediate buffer to store the data to save locking the remote process to access data. Reviewed by: imp @ Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D36633 (cherry picked from commit 5e59b2734f77da24e2bc45154c7db949b9d790c5) --- sys/fs/cuse/cuse.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 130 insertions(+), 6 deletions(-) diff --git a/sys/fs/cuse/cuse.c b/sys/fs/cuse/cuse.c index 62b53d232ee2..8922ad0e7d60 100644 --- a/sys/fs/cuse/cuse.c +++ b/sys/fs/cuse/cuse.c @@ -1,6 +1,6 @@ /* $FreeBSD$ */ /*- - * Copyright (c) 2010-2022 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2010-2022 Hans Petter Selasky * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -64,6 +64,10 @@ #include #include +/* set this define to zero to disable this feature */ +#define CUSE_COPY_BUFFER_MAX \ + CUSE_BUFFER_MAX + #define CUSE_ALLOC_PAGES_MAX \ (CUSE_ALLOC_BYTES_MAX / PAGE_SIZE) @@ -154,6 +158,12 @@ struct cuse_client { struct cuse_server *server; struct cuse_server_dev *server_dev; + uintptr_t read_base; + uintptr_t write_base; + int read_length; + int write_length; + uint8_t read_buffer[CUSE_COPY_BUFFER_MAX] __aligned(4); + uint8_t write_buffer[CUSE_COPY_BUFFER_MAX] __aligned(4); uint8_t ioctl_buffer[CUSE_BUFFER_MAX] __aligned(4); int fflags; /* file flags */ @@ -265,6 +275,12 @@ cuse_server_unlock(struct cuse_server *pcs) mtx_unlock(&pcs->mtx); } +static bool +cuse_server_is_locked(struct cuse_server *pcs) +{ + return (mtx_owned(&pcs->mtx)); +} + static void cuse_cmd_lock(struct cuse_client_command *pccmd) { @@ -967,6 +983,48 @@ cuse_server_data_copy_locked(struct cuse_server *pcs, return (error); } +static int +cuse_server_data_copy_optimized_locked(struct cuse_server *pcs, + struct cuse_client_command *pccmd, + struct cuse_data_chunk *pchk, bool isread) +{ + uintptr_t offset; + int error; + + /* + * Check if data is stored locally to avoid accessing + * other process's data space: + */ + if (isread) { + offset = pchk->peer_ptr - pccmd->client->write_base; + + if (offset < (uintptr_t)pccmd->client->write_length && + pchk->length <= (unsigned long)pccmd->client->write_length && + offset + pchk->length <= (uintptr_t)pccmd->client->write_length) { + cuse_server_unlock(pcs); + error = copyout(pccmd->client->write_buffer + offset, + (void *)pchk->local_ptr, pchk->length); + goto done; + } + } else { + offset = pchk->peer_ptr - pccmd->client->read_base; + + if (offset < (uintptr_t)pccmd->client->read_length && + pchk->length <= (unsigned long)pccmd->client->read_length && + offset + pchk->length <= (uintptr_t)pccmd->client->read_length) { + cuse_server_unlock(pcs); + error = copyin((void *)pchk->local_ptr, + pccmd->client->read_buffer + offset, pchk->length); + goto done; + } + } + + /* use process to process copy function */ + error = cuse_server_data_copy_locked(pcs, pccmd, pchk, isread); +done: + return (error); +} + static int cuse_alloc_unit_by_id_locked(struct cuse_server *pcs, int id) { @@ -1294,14 +1352,22 @@ cuse_server_ioctl(struct cdev *dev, unsigned long cmd, error = ENXIO; /* invalid request */ } else if (pchk->peer_ptr < CUSE_BUF_MIN_PTR) { error = EFAULT; /* NULL pointer */ + } else if (pchk->length == 0) { + /* NOP */ } else if (pchk->peer_ptr < CUSE_BUF_MAX_PTR) { error = cuse_server_ioctl_copy_locked(pcs, pccmd, pchk, cmd == CUSE_IOCTL_READ_DATA); } else { - error = cuse_server_data_copy_locked(pcs, pccmd, - pchk, cmd == CUSE_IOCTL_READ_DATA); + error = cuse_server_data_copy_optimized_locked( + pcs, pccmd, pchk, cmd == CUSE_IOCTL_READ_DATA); } - cuse_server_unlock(pcs); + + /* + * Sometimes the functions above drop the server lock + * early as an optimization: + */ + if (cuse_server_is_locked(pcs)) + cuse_server_unlock(pcs); break; case CUSE_IOCTL_SELWAKEUP: @@ -1581,6 +1647,7 @@ cuse_client_read(struct cdev *dev, struct uio *uio, int ioflag) struct cuse_client *pcc; struct cuse_server *pcs; int error; + int temp; int len; error = cuse_client_get(&pcc); @@ -1605,13 +1672,41 @@ cuse_client_read(struct cdev *dev, struct uio *uio, int ioflag) len = uio->uio_iov->iov_len; cuse_server_lock(pcs); + if (len <= CUSE_COPY_BUFFER_MAX) { + /* set read buffer region for small reads */ + pcc->read_base = (uintptr_t)uio->uio_iov->iov_base; + pcc->read_length = len; + } cuse_client_send_command_locked(pccmd, (uintptr_t)uio->uio_iov->iov_base, (unsigned long)(unsigned int)len, pcc->fflags, ioflag); error = cuse_client_receive_command_locked(pccmd, 0, 0); + /* + * After finishing reading data, disable the read + * region for the cuse_server_data_copy_optimized_locked() + * function: + */ + pcc->read_base = 0; + pcc->read_length = 0; cuse_server_unlock(pcs); + /* + * The return value indicates the read length, when + * not negative. Range check it just in case to avoid + * passing invalid length values to uiomove(). + */ + if (error > len) { + error = ERANGE; + break; + } else if (error > 0 && len <= CUSE_COPY_BUFFER_MAX) { + temp = copyout(pcc->read_buffer, + uio->uio_iov->iov_base, error); + if (temp != 0) { + error = temp; + break; + } + } if (error < 0) { error = cuse_convert_error(error); break; @@ -1664,15 +1759,43 @@ cuse_client_write(struct cdev *dev, struct uio *uio, int ioflag) } len = uio->uio_iov->iov_len; + if (len <= CUSE_COPY_BUFFER_MAX) { + error = copyin(uio->uio_iov->iov_base, + pcc->write_buffer, len); + if (error != 0) + break; + } + cuse_server_lock(pcs); + if (len <= CUSE_COPY_BUFFER_MAX) { + /* set write buffer region for small writes */ + pcc->write_base = (uintptr_t)uio->uio_iov->iov_base; + pcc->write_length = len; + } cuse_client_send_command_locked(pccmd, (uintptr_t)uio->uio_iov->iov_base, (unsigned long)(unsigned int)len, pcc->fflags, ioflag); error = cuse_client_receive_command_locked(pccmd, 0, 0); + + /* + * After finishing writing data, disable the write + * region for the cuse_server_data_copy_optimized_locked() + * function: + */ + pcc->write_base = 0; + pcc->write_length = 0; cuse_server_unlock(pcs); - if (error < 0) { + /* + * The return value indicates the write length, when + * not negative. Range check it just in case to avoid + * passing invalid length values to uiomove(). + */ + if (error > len) { + error = ERANGE; + break; + } else if (error < 0) { error = cuse_convert_error(error); break; } else if (error == len) { @@ -1686,7 +1809,8 @@ cuse_client_write(struct cdev *dev, struct uio *uio, int ioflag) } cuse_cmd_unlock(pccmd); - uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */ + /* restore segment flag */ + uio->uio_segflg = UIO_USERSPACE; if (error == EWOULDBLOCK) cuse_client_kqfilter_poll(dev, pcc);