From nobody Mon Oct 03 15:36:58 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 4Mh4hB4W79z4d1lN; Mon, 3 Oct 2022 15:36:58 +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 4Mh4hB3vG7z3C4J; Mon, 3 Oct 2022 15:36:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664811418; 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=cm9+dsvrJltxViFCIwdr/gPPT5a1D7zViVr9T7M0E6w=; b=vIbsiKWotGpTvTiigevCidfnvsOiQ2RpJCis9cbrUBIvo2uyw/wwq7qFVdDAF4At9to/xI 2AzY5Buhw5dNUupQvxAFWmYtoPONTZFc4eMA1SLUseAQgNPhYB9wIjecEFIo6pfHTQGZ2A VybZ3zrNhdy8nbDGo66Fe1eZjStl5Z7itVQyPhqFuIWNSbwKAX68UMSco1RnEpfz1yEVdO wt23/aIaNlLGnNLEgSoZPglk1gnqzE8ZDHdWp7+GM2OxcEE+qYzoC8E+LRsMj5Guc7gojQ DbTdf54agx4Fob1QGAY4oGIzvhKpHEGX7V5YMzfcN344iY/L3+sOk5GaASwj9w== 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 4Mh4hB310vzFhf; Mon, 3 Oct 2022 15:36:58 +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 293FawSh085188; Mon, 3 Oct 2022 15:36:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 293Faw2r085187; Mon, 3 Oct 2022 15:36:58 GMT (envelope-from git) Date: Mon, 3 Oct 2022 15:36:58 GMT Message-Id: <202210031536.293Faw2r085187@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Hans Petter Selasky Subject: git: 5e59b2734f77 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 5e59b2734f77da24e2bc45154c7db949b9d790c5 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664811418; 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=cm9+dsvrJltxViFCIwdr/gPPT5a1D7zViVr9T7M0E6w=; b=bbnO5oLBYhRVsy0ojY593QbjOGvN5M8FvB35DY61LeBRViBn37NyTsnMSW/13rV+QCMMNJ IpxKrwSgl9FexkpJq5XUfGN9M+nr4hRTHBR5prxeR1eJ7Q2DsS9fGJ0VOnoj9NKakCMUg2 KGFm0HP4/q8mcNtn3RaT9ijMCW6EulehVr+kGP036wN14zJmrF4gm9tAGLC+1iVTFm+b+w uxdISCAjV+v9qKDdRu/1yTI2USUkbXgUsCFcNx/obccxvDfJLairN4qRRSfp4+WRo0vYFd baDM4vlor8jER7NSbPThmd1H821Jat3DVIy4ow639ZyY45CqFHqRJcw0WQzvpw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1664811418; a=rsa-sha256; cv=none; b=w9lRxyroXIL5/u/1ZSZJTXsOltISJmsVqRdJeXp4EA8mi4SPPfCTU8RssqJMQZmIztL2Vw COiZDltRI8eVPXzfa4nfX/10STUgRBph9LYv8BBk1emVSlHOtES85gxlOvRsSWb1Zo7LH1 EFjTMgRd2TyP4U/pJu7NRrgmFQRu0mESYoTHbpCWo4sulJIqheeuEjZI3JLi4U/rlYD0Va A1ZR+CrJq8FGZzVRHs+3XvZU3kBbpN8mpcSjl90WII2cyYjb7w7WNEuThYQUQ50QV+NRYX 5wS1Dn44bugPqvaa3LsnWvJ0P3ka3wZRPYllx61haAO6B+hQv8/gL2puMWC2Mw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=5e59b2734f77da24e2bc45154c7db949b9d790c5 commit 5e59b2734f77da24e2bc45154c7db949b9d790c5 Author: Hans Petter Selasky AuthorDate: 2022-09-20 13:58:36 +0000 Commit: Hans Petter Selasky CommitDate: 2022-10-03 15:35:14 +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 @ Differential Revision: https://reviews.freebsd.org/D36633 MFC after: 1 week Sponsored by: NVIDIA Networking --- 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 a870df1b07dc..41f8bdbe2bfe 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);