From patchwork Sat Feb 24 01:15:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 205757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp934275dyb; Fri, 23 Feb 2024 17:17:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUvOT6aw0P60Blu+CRPYdGn8oARBr0sDyhhMn49xUQyafwg/WRI/KaCU8iOgUn4TQD/wBd3qHHqDIw2mINhvB5fnChLcw== X-Google-Smtp-Source: AGHT+IFhzhKC+QXM6ohzY16WlDGJCaULArl5g8W+L+TNJOv5kxN49m97zOoTfAI5LXpgiAddEQwV X-Received: by 2002:a05:620a:14ac:b0:787:6a73:2826 with SMTP id x12-20020a05620a14ac00b007876a732826mr1508626qkj.14.1708737352829; Fri, 23 Feb 2024 17:15:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708737352; cv=pass; d=google.com; s=arc-20160816; b=lB5ra3GKR3aVra1xfBFun7xawehsMKiM2v8q234T8d5mG5VSvHrhOEzcMUc5AbRadE 7OomC61QZQ4t5Cu0cuuFbBfWBedL/QTWOTmsD05dhgelrYf8a9kSoJaMcF1cbreMyja8 5i7iD3aRi50X5eBMli7XfqK6lh82T8iPd8EowGOyAKrkTBRpb6XzIBFOdmAZg5Lf0M8D Vu362VRWP/06fMsj6dwyWG++ql/NXN61P8HeLExeaQ+8jEx0EOTVBmZybeBUJdU3cWZv geF5J3ZFcCIVHutc4nlPQWUqhg+HJHGrAroRoht/7nxk2xHZHAZf8U8+wFyVDpKswQ8M wYDQ== 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=IG1r8ChitPDmtdTwXDH+k1YMK4pjxvACavoOY205DNA=; fh=eGotrG2wUAPCtJbO4MhMKF1jcndrbQy7pSZLLDRnPjs=; b=awQK5TVlYxfnNn8Ljb4VUjDwwnhxXfi1r3pIJ5S1XV6224ec6We6ZgakIGTtKlyclr WL6iXbqpZ7uz2rw9tPmcQGyGhd8StOFmWUmRQ76SM4TBREno+FQCUyF93EF0oqbIxUpS xlAqn19igjXu+g6Q4xKkXBOqoCHeDSuTiGfsVSPcaWKBc2/4WO+qH6WTJKLx59e6oKY/ RIEsHR5OdKecP4gna1nLjBvyR36nMhmVJMLxt2Ec03G2jJSY0DeyK0y6KclK1wH2G1hF YRo1BYvCfJuBxojskPrh4GHSsq4t2REKhffutEF6YYgrCCLvsP9YiOIhj1oRi8O+8B0J LY7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zp4KSLCQ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79407-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79407-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id b13-20020a05620a04ed00b007878ae78a8dsi124756qkh.359.2024.02.23.17.15.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 17:15:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79407-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zp4KSLCQ; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79407-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79407-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 9C63E1C20312 for ; Sat, 24 Feb 2024 01:15:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6264D79DF; Sat, 24 Feb 2024 01:15:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Zp4KSLCQ" 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 9F32717FE; Sat, 24 Feb 2024 01:15:03 +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=1708737303; cv=none; b=XGtwkJvIm7cWHc13rX+0jGQ4DvokLVxUUHU5PawGVS3epdpjMAF0mrTMGbCT9JjFw/gGe5qv0j2Gq25G2f0tHztMJUkKuU4msjnoK640j3yUX+batRmfOeYA7v+rfE/26Ym9QURrrUWFruY7Zx1RmMhkJAlcYxq8acuYzBPZsXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737303; c=relaxed/simple; bh=5yQIyVqpWQrFoHd5KjccM3zKxOtIF5WCoB+ju21X2Zo=; h=Date:Subject:From:To:Cc:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mAjPAgrWVTtpLaUb9F3Um6BZpd2k92DRyLlQtZQIRnaVRKQJO2pkSb+pEzbIQuR5esB2ChX1IiKO/I+t4rJPVSEDMoBqbnCJMTB73dQwbULFsP77ghlYNfBvKpYRyQ/Vn0fB+S58/tWdJFs25fcx1Wtroc3Egi+GwQmlLGBl+Zg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Zp4KSLCQ; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 38E01C433C7; Sat, 24 Feb 2024 01:15:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708737303; bh=5yQIyVqpWQrFoHd5KjccM3zKxOtIF5WCoB+ju21X2Zo=; h=Date:Subject:From:To:Cc:In-Reply-To:References:From; b=Zp4KSLCQJ0jF7mwO0/PiYYwwqUlSIqJueOWx27wzftDP2h1/o9iDqsFnvwKFogqk9 lCe8EEL7hsaP1CX2kNNzrjWwoTd3flo3pBgIOuCGED3Yyvs7g1daIJI82D13kaHc+5 eQ3EwTGy6wvpx3nqF6PC6+pt+j/yNegxD1vIH/Hp/mF/HTB9ZUqnwLQPai0qFlKFMB Pb0SpNjgIbFSNEjxOQ0gpGyoSTC+7W5RlKYvR1fQBsCpt58WQS1KhmmXn213Y/jsfM TwhlAYdlcFBnJO8uXkv2hh6Cw5DNBpHSpzGNeHiv6TSAZA9+vL3UsLhE5gYl2H0Iwx Swn3wMNMbEb1Q== Date: Fri, 23 Feb 2024 17:15:02 -0800 Subject: [PATCH 05/10] bcachefs: Thread with file documentation 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: <170873668966.1861398.3623023711677925127.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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791740978547891729 X-GMAIL-MSGID: 1791740978547891729 From: Kent Overstreet Signed-off-by: Kent Overstreet Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong --- fs/bcachefs/thread_with_file.c | 15 ++++++++------- fs/bcachefs/thread_with_file.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/fs/bcachefs/thread_with_file.c b/fs/bcachefs/thread_with_file.c index 830efb06ef0be..dde9679b68b42 100644 --- a/fs/bcachefs/thread_with_file.c +++ b/fs/bcachefs/thread_with_file.c @@ -76,16 +76,16 @@ static bool stdio_redirect_has_output(struct stdio_redirect *stdio) return stdio->output.buf.nr || stdio->done; } -#define WRITE_BUFFER 4096 +#define STDIO_REDIRECT_BUFSIZE 4096 static bool stdio_redirect_has_input_space(struct stdio_redirect *stdio) { - return stdio->input.buf.nr < WRITE_BUFFER || stdio->done; + return stdio->input.buf.nr < STDIO_REDIRECT_BUFSIZE || stdio->done; } static bool stdio_redirect_has_output_space(struct stdio_redirect *stdio) { - return stdio->output.buf.nr < WRITE_BUFFER || stdio->done; + return stdio->output.buf.nr < STDIO_REDIRECT_BUFSIZE || stdio->done; } static void stdio_buf_init(struct stdio_buf *buf) @@ -171,11 +171,12 @@ static ssize_t thread_with_stdio_write(struct file *file, const char __user *ubu } spin_lock(&buf->lock); - if (buf->buf.nr < WRITE_BUFFER) - darray_make_room_gfp(&buf->buf, min(b, WRITE_BUFFER - buf->buf.nr), __GFP_NOWARN); + if (buf->buf.nr < STDIO_REDIRECT_BUFSIZE) + darray_make_room_gfp(&buf->buf, + min(b, STDIO_REDIRECT_BUFSIZE - buf->buf.nr), GFP_NOWAIT); b = min(len, darray_room(buf->buf)); - if (b && !copy_from_user_nofault(&buf->buf.data[buf->buf.nr], ubuf, b)) { + if (b && !copy_from_user_nofault(&darray_top(buf->buf), ubuf, b)) { buf->buf.nr += b; ubuf += b; len -= b; @@ -338,7 +339,7 @@ void bch2_stdio_redirect_vprintf(struct stdio_redirect *stdio, bool nonblocking, return; spin_lock_irqsave(&buf->lock, flags); - bch2_darray_vprintf(&buf->buf, nonblocking ? __GFP_NOWARN : GFP_KERNEL, fmt, args); + bch2_darray_vprintf(&buf->buf, nonblocking ? GFP_NOWAIT : GFP_KERNEL, fmt, args); spin_unlock_irqrestore(&buf->lock, flags); wake_up(&buf->wait); diff --git a/fs/bcachefs/thread_with_file.h b/fs/bcachefs/thread_with_file.h index 66212fcae226a..f06f8ff19a790 100644 --- a/fs/bcachefs/thread_with_file.h +++ b/fs/bcachefs/thread_with_file.h @@ -4,6 +4,38 @@ #include "thread_with_file_types.h" +/* + * Thread with file: Run a kthread and connect it to a file descriptor, so that + * it can be interacted with via fd read/write methods and closing the file + * descriptor stops the kthread. + * + * We have two different APIs: + * + * thread_with_file, the low level version. + * You get to define the full file_operations, including your release function, + * which means that you must call bch2_thread_with_file_exit() from your + * .release method + * + * thread_with_stdio, the higher level version + * This implements full piping of input and output, including .poll. + * + * Notes on behaviour: + * - kthread shutdown behaves like writing or reading from a pipe that has been + * closed + * - Input and output buffers are 4096 bytes, although buffers may in some + * situations slightly exceed that limit so as to avoid chopping off a + * message in the middle in nonblocking mode. + * - Input/output buffers are lazily allocated, with GFP_NOWAIT allocations - + * should be fine but might change in future revisions. + * - Output buffer may grow past 4096 bytes to deal with messages that are + * bigger than 4096 bytes + * - Writing may be done blocking or nonblocking; in nonblocking mode, we only + * drop entire messages. + * + * To write, use stdio_redirect_printf() + * To read, use stdio_redirect_read() or stdio_redirect_readline() + */ + struct task_struct; struct thread_with_file {