Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp184970rbb; Fri, 23 Feb 2024 17:15:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWC3DSMVD9/ZYIATTdwXKoVQU+hJqNVowO1PYhrsJyTwWyZcUnb7eMUm5Bjy0XU3Us5UX8abOX1SdyVoE9oEpNJpsXaVzdmxKlscP8x7w== X-Google-Smtp-Source: AGHT+IE2Rae09T6Lg04wmk2eR+BX/u6HFOfhbFlaOtch/4a+wSW4dHX1Fv2xxgbfKNBrXUIP9tAO X-Received: by 2002:a05:6a00:3c86:b0:6e1:3cdb:76f1 with SMTP id lm6-20020a056a003c8600b006e13cdb76f1mr2064765pfb.20.1708737355067; Fri, 23 Feb 2024 17:15:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708737355; cv=pass; d=google.com; s=arc-20160816; b=YzxyYPpvgqOW5LIdqtIx5/agYL8AARg0ty0iGAyUqphsSytQ+4pN0MzR1lh3aUdjCO Rp7MVQZ4/26X7w1USreaCtL4J/fBM+KxY2KAi7QMBUMbdxa02XoS8AwNGn+gNnyLUlUt 5AAIgTDtAYYi50UNk1lZGiGn+qh2SkmTco6rlpUyYBHvOWzKac0NZoBE3chLdOvLYS2q U9wxGNz+CiXRnECkS5j31ZNqvIeGmZol8+SBiwTB7kkdjQ8kYTSEcr2pSsa4JnLJe0AY pB/4XaQPg7toQ7Xcn94z9PPprHe++Fix8e5KW2LJIFPI6Wu/zA1FBLlzj0F22s45UCmX wexw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:cc:to:from:subject:date:dkim-signature; bh=phO7GsXlaxgV8OYg0aeND8F2CSiLc3hcE7Px9D6RTmo=; fh=eGotrG2wUAPCtJbO4MhMKF1jcndrbQy7pSZLLDRnPjs=; b=EK7RZVpMUIeJ58cy916phXLDcQ9t8sdnGKdDdE2KHIWfhAR/2wqerBzPSBd0vgEtF9 4/nXspUaf3LC4gfk8xcqthGQe4ZCtG61Pk3pvtCWecaJr9dSs6a793DT6R2rm8CMrGRw m0WAfr2VE+eOI1W6r0c/BLtiJYT9DD4hl7u8jVWmoolvO3lIu3dDXOorNGJkmFb3qJV1 XY1ryLs/0u8vYe5Z+IKDoNwWyTU4EsC2lXXNPzhDk3yEd/+1IOZfShmZOrBAVvVQLPlj N1nUB9QFOIOIY7b3SAiIzgAHdxcrig7cQdWJnPjzzdwawQg7RYmu2GxgNl3MH/IntSXy X1nA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Nvctf3rP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79401-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79401-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id l7-20020a635707000000b005cfda2f9e48si141844pgb.860.2024.02.23.17.15.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 17:15:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79401-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Nvctf3rP; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79401-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79401-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 56338B241AE for ; Sat, 24 Feb 2024 01:14:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0DE624439; Sat, 24 Feb 2024 01:14:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Nvctf3rP" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3113417C9; Sat, 24 Feb 2024 01:14:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737241; cv=none; b=oxm7Y+C3V6BR4e4DgGUFRQX6feAJUBEK1LX8K7RB5VpsVlHRd+yzU1RLGvFBA9uMAwJflikyITUxS0+CbQxbdiJjhrza4XELBUaFIjkAl5U8sI+YmAlqwfVpexvOo+cdFBRTAHRiubCVe8yEvyGsmVR3ku+Vf7qB2DFjIo7Fyng= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737241; c=relaxed/simple; bh=UUGGJo6hKBHRWFMWSEO+bPNBd3Zh6s07ybvSES63b78=; h=Date:Subject:From:To:Cc:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=t8UDl7CesLx86mfyY/dkUYAF73PnSnviRkmvi6ZFVBPaLMJOBnS3iWQFBLMUhuyaUmvjKaXDRSuqxY3SpzlooZ9SuMeHKlfCzjNcbEnVjaEqsJ+EUdeP2r7vvcBDcvL108tCceXxJMwWsQz5IgG0sZpb4Nv1d+QdgmmB3lFbpM8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Nvctf3rP; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BD7E7C433F1; Sat, 24 Feb 2024 01:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708737240; bh=UUGGJo6hKBHRWFMWSEO+bPNBd3Zh6s07ybvSES63b78=; h=Date:Subject:From:To:Cc:In-Reply-To:References:From; b=Nvctf3rPhx0heBX5B5IJbfRJd87JvqO2t5LwUPuXzSSLtq3cvzZEgdOJ1PJqtnv8Q dLPjIENfHdfXpxPO96JqKrPat2SeQIRjbcKKt7LfoxJQVvgClscrFgEHAVy8inFkB9 C91X2Eoql7bIb1cRQ4yKwLZzh6s+gXxADHIcS/QXkx+OTsqdDOt84Z0Uu23Y6bXQvS Bg7keY5jGM2gx5keRGvqFbzOFbb1KIwx+eZ+3cRuUt2uw3X3t5Ow6AOwhMfczI6f5E tZ/n/oTU0LTXa8ayUSRZiqA7XnOzEXdkbO92Y5/vtomYuJbYyzxew+qVwLrnPkm7LM gwFFtqz57ydlQ== Date: Fri, 23 Feb 2024 17:14:00 -0800 Subject: [PATCH 01/10] bcachefs: thread_with_stdio: eliminate double buffering From: "Darrick J. Wong" To: akpm@linux-foundation.org, daniel@gluo.nz, kent.overstreet@linux.dev, djwong@kernel.org Cc: linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <170873668902.1861398.15653563093468101671.stgit@frogsfrogsfrogs> In-Reply-To: <170873668859.1861398.2367011381778949840.stgit@frogsfrogsfrogs> References: <170873668859.1861398.2367011381778949840.stgit@frogsfrogsfrogs> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit From: Kent Overstreet The output buffer lock has to be a spinlock so that we can write to it from interrupt context, so we can't use a direct copy_to_user; this switches thread_with_file_read() to use fault_in_writeable() and copy_to_user_nofault(), similar to how thread_with_file_write() works. Signed-off-by: Kent Overstreet Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong --- fs/bcachefs/thread_with_file.c | 56 ++++++++++++---------------------------- fs/bcachefs/thread_with_file.h | 1 - 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/fs/bcachefs/thread_with_file.c b/fs/bcachefs/thread_with_file.c index 9220d7de10db6..8c3afb4c3204f 100644 --- a/fs/bcachefs/thread_with_file.c +++ b/fs/bcachefs/thread_with_file.c @@ -67,16 +67,15 @@ int bch2_run_thread_with_file(struct thread_with_file *thr, static inline bool thread_with_stdio_has_output(struct thread_with_stdio *thr) { - return thr->stdio.output_buf.pos || - thr->output2.nr || - thr->thr.done; + return thr->stdio.output_buf.pos || thr->thr.done; } -static ssize_t thread_with_stdio_read(struct file *file, char __user *buf, +static ssize_t thread_with_stdio_read(struct file *file, char __user *ubuf, size_t len, loff_t *ppos) { struct thread_with_stdio *thr = container_of(file->private_data, struct thread_with_stdio, thr); + struct printbuf *buf = &thr->stdio.output_buf; size_t copied = 0, b; int ret = 0; @@ -89,44 +88,25 @@ static ssize_t thread_with_stdio_read(struct file *file, char __user *buf, if (ret) return ret; - if (thr->thr.done) - return 0; - - while (len) { - ret = darray_make_room(&thr->output2, thr->stdio.output_buf.pos); - if (ret) + while (len && buf->pos) { + if (fault_in_writeable(ubuf, len) == len) { + ret = -EFAULT; break; + } spin_lock_irq(&thr->stdio.output_lock); - b = min_t(size_t, darray_room(thr->output2), thr->stdio.output_buf.pos); + b = min_t(size_t, len, buf->pos); - memcpy(&darray_top(thr->output2), thr->stdio.output_buf.buf, b); - memmove(thr->stdio.output_buf.buf, - thr->stdio.output_buf.buf + b, - thr->stdio.output_buf.pos - b); - - thr->output2.nr += b; - thr->stdio.output_buf.pos -= b; + if (b && !copy_to_user_nofault(ubuf, buf->buf, b)) { + memmove(buf->buf, + buf->buf + b, + buf->pos - b); + buf->pos -= b; + ubuf += b; + len -= b; + copied += b; + } spin_unlock_irq(&thr->stdio.output_lock); - - b = min(len, thr->output2.nr); - if (!b) - break; - - b -= copy_to_user(buf, thr->output2.data, b); - if (!b) { - ret = -EFAULT; - break; - } - - copied += b; - buf += b; - len -= b; - - memmove(thr->output2.data, - thr->output2.data + b, - thr->output2.nr - b); - thr->output2.nr -= b; } return copied ?: ret; @@ -140,7 +120,6 @@ static int thread_with_stdio_release(struct inode *inode, struct file *file) bch2_thread_with_file_exit(&thr->thr); printbuf_exit(&thr->stdio.input_buf); printbuf_exit(&thr->stdio.output_buf); - darray_exit(&thr->output2); thr->exit(thr); return 0; } @@ -245,7 +224,6 @@ int bch2_run_thread_with_stdio(struct thread_with_stdio *thr, spin_lock_init(&thr->stdio.output_lock); init_waitqueue_head(&thr->stdio.output_wait); - darray_init(&thr->output2); thr->exit = exit; return bch2_run_thread_with_file(&thr->thr, &thread_with_stdio_fops, fn); diff --git a/fs/bcachefs/thread_with_file.h b/fs/bcachefs/thread_with_file.h index 05879c5048c87..b5098b52db709 100644 --- a/fs/bcachefs/thread_with_file.h +++ b/fs/bcachefs/thread_with_file.h @@ -20,7 +20,6 @@ int bch2_run_thread_with_file(struct thread_with_file *, struct thread_with_stdio { struct thread_with_file thr; struct stdio_redirect stdio; - DARRAY(char) output2; void (*exit)(struct thread_with_stdio *); };