From patchwork Sat Feb 24 01:16:52 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: 205758 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp934418dyb; Fri, 23 Feb 2024 17:18:05 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUAJfrwU5t7j04DwYX4nfzQu8l+6hhZkW7qU1PY7ZwdOOEGBEexXGbeUYfBBukB1fk9+BqLhja58xQWoPGQgAP/VNSnzg== X-Google-Smtp-Source: AGHT+IFDd0HffyiDgdZI6jPoQvtE8BRSTgnRIEjnlYNimqrbBqEISyw5aPUuVxLGxgZFcOjrGSl2 X-Received: by 2002:a05:620a:4088:b0:787:6909:777f with SMTP id f8-20020a05620a408800b007876909777fmr2718827qko.34.1708737484894; Fri, 23 Feb 2024 17:18:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708737484; cv=pass; d=google.com; s=arc-20160816; b=O6kUvYET34M6Ppihf2oNJvqX3igQdARNLdH3f9ZHgO+gNbAmzbxSldC+xlBWzPEj8K Z5ptbz9uolJQXGz2rASI5alq30Cp9Hf2crkUGIsNgueJqJdZsOxn2LqgKYCI2/FU9WxW SGawZAsiExSM6IXr1jZAz3EbzFILpoW++f9YKSLiQepEC72l8MPrleS7e3E2bNqz9toK CgPfjRKVDzYvwL0z9mTRwcbCv/mya8SCr0d9SIGtzXKg/OIPt0unrPXtLcpAdoCETW18 mfIYUewECmloTo4/aFh481uamFpQ3f9e29b0aPGaqW/Icjdhuz2+tY0lQUOM6zyq8hPE 8JyQ== 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=Wi81AeYxnnovLZKcPq/G0T5rrMuBssJM4Ff6lAC93Qo=; fh=eGotrG2wUAPCtJbO4MhMKF1jcndrbQy7pSZLLDRnPjs=; b=Ot7vTl3sWUpQsiGGzh94Zl0CtDLS0tDLiXXe/VWg1hSyTPI20deqhrCYvkiXoOQk61 bGz36TnqOZ4ECY80rdjM5EgYzwKgkjmM12xKS3Ud23/B7AgiMALQlJb7eGpahhcQFSq7 xGwxJXT9RUGJNS7EPBPAw6eRy+wcYjTfb+ocRbX9W5P8fzsiP1g0kN/DoWPASU4UJDPt 02EIK+Bd/c5Cw+8981mVgipIMIdgw7FA8auPLXgh8/6cckuD22uOMrUHF7VLI3yHJtIl 75gcYJ+szQP2F52loTHNf2PgWQBubgGZw/n1Uf2VPCXI5ZMVNuMe1yl2thexm/9Zdr7r pJow==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vOIqvMcy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79415-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79415-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 c21-20020a05620a201500b00787a71e63d5si135772qka.151.2024.02.23.17.18.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 17:18:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79415-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=vOIqvMcy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79415-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79415-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 9AD3E1C227A9 for ; Sat, 24 Feb 2024 01:18:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A047E4A04; Sat, 24 Feb 2024 01:16:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="vOIqvMcy" 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 F1833AD5E; Sat, 24 Feb 2024 01:16:52 +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=1708737413; cv=none; b=OWy40jXb9vlNUomKA3F9CtRoi8AvfYDQ1vLJ/wfWUat/LGjcI/puX0Yk2luGxC4FJAH74T2tR2vx1CUAvSVVOMvCygHbCDZ0NOrZITyCbXf1riyuMPYvM5cJnjOkWlsvTjlQy9W2403Y/T0d2x0T0EhALH4/28AeSSdJ0auuGKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737413; c=relaxed/simple; bh=9DdYqFD9SCt6Bs3yagJsGZvwLwjDk5KJDyGnik7FRVc=; h=Date:Subject:From:To:Cc:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fe5zGy/V0Qfw0I8PZAHZ0jwNM0/wBH8tRrQLSjLObRwFouTOMoIJR3Up+ExpFkUHLUGpDOVunzrx7Pi4NXXrNjjVyd8HtLMUxLvvZKVvV/upy2oc4xnSATYra85Y0bDtk1/86He0em9aMJz8FXpzfMgQEfOa8QCb+JH1ynmVagw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=vOIqvMcy; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A9A2C433F1; Sat, 24 Feb 2024 01:16:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708737412; bh=9DdYqFD9SCt6Bs3yagJsGZvwLwjDk5KJDyGnik7FRVc=; h=Date:Subject:From:To:Cc:In-Reply-To:References:From; b=vOIqvMcyt+32zXP2UCe88hRtJCelg0FwZfmQBcvXE9j0z1IgTAdoOaFQkHQ/l0rbU 9CjbeiD3ZXFQCIS3hoMbSDyXnuBNdd39egbSvfLmJu1dMSdhN90Bcw09VbzxGQtom+ QUQxqWjLykes47at+0dAhupRIlXPhwsUi8KZ2+0nYbvQ1qGBl4DyCKOIAC8K4U8BM8 nzr5mOYLCybLbmxcUfE+0gHR967oE8lDZ2y1zw3fE2P6qVP1Qzc2dieZ2I4tirC876 0BH4yRRXChejM2VlctRB7MxbZDnLTeUnk1Jqch48Ey488lGOTdo6rORdxFLpryBOvW ztmTQ55BtRteQ== Date: Fri, 23 Feb 2024 17:16:52 -0800 Subject: [PATCH 2/5] thread_with_file: fix various printf problems 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: <170873669433.1861696.1893338653788145361.stgit@frogsfrogsfrogs> In-Reply-To: <170873669390.1861696.14195299906744951144.stgit@frogsfrogsfrogs> References: <170873669390.1861696.14195299906744951144.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: 1791741117090587733 X-GMAIL-MSGID: 1791741117090587733 From: Darrick J. Wong Experimentally fix some problems with stdio_redirect_vprintf by creating a MOO variant with which we can experiment. We can't do a GFP_KERNEL allocation while holding the spinlock, and I don't like how the printf function can silently truncate the output if memory allocation fails. Signed-off-by: Darrick J. Wong Signed-off-by: Kent Overstreet --- include/linux/thread_with_file.h | 4 +-- lib/thread_with_file.c | 55 ++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/include/linux/thread_with_file.h b/include/linux/thread_with_file.h index 5f7e85bc8322b..7b133a15d3540 100644 --- a/include/linux/thread_with_file.h +++ b/include/linux/thread_with_file.h @@ -68,7 +68,7 @@ int run_thread_with_stdout(struct thread_with_stdio *, int stdio_redirect_read(struct stdio_redirect *, char *, size_t); int stdio_redirect_readline(struct stdio_redirect *, char *, size_t); -__printf(3, 0) void stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list); -__printf(3, 4) void stdio_redirect_printf(struct stdio_redirect *, bool, const char *, ...); +__printf(3, 0) ssize_t stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list); +__printf(3, 4) ssize_t stdio_redirect_printf(struct stdio_redirect *, bool, const char *, ...); #endif /* _LINUX_THREAD_WITH_FILE_H */ diff --git a/lib/thread_with_file.c b/lib/thread_with_file.c index 71028611b8d59..70a805ef017f9 100644 --- a/lib/thread_with_file.c +++ b/lib/thread_with_file.c @@ -108,49 +108,68 @@ int stdio_redirect_readline(struct stdio_redirect *stdio, char *ubuf, size_t len EXPORT_SYMBOL_GPL(stdio_redirect_readline); __printf(3, 0) -static void darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args) +static ssize_t darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args) { - size_t len; + ssize_t ret; do { va_list args2; + size_t len; + va_copy(args2, args); - len = vsnprintf(out->data + out->nr, darray_room(*out), fmt, args2); - } while (len + 1 > darray_room(*out) && !darray_make_room_gfp(out, len + 1, gfp)); + if (len + 1 <= darray_room(*out)) { + out->nr += len; + return len; + } - out->nr += min(len, darray_room(*out)); + ret = darray_make_room_gfp(out, len + 1, gfp); + } while (ret == 0); + + return ret; } -void stdio_redirect_vprintf(struct stdio_redirect *stdio, bool nonblocking, - const char *fmt, va_list args) +ssize_t stdio_redirect_vprintf(struct stdio_redirect *stdio, bool nonblocking, + const char *fmt, va_list args) { struct stdio_buf *buf = &stdio->output; unsigned long flags; + ssize_t ret; - if (!nonblocking) - wait_event(buf->wait, stdio_redirect_has_output_space(stdio)); - else if (!stdio_redirect_has_output_space(stdio)) - return; - if (stdio->done) - return; - +again: spin_lock_irqsave(&buf->lock, flags); - darray_vprintf(&buf->buf, nonblocking ? GFP_NOWAIT : GFP_KERNEL, fmt, args); + ret = darray_vprintf(&buf->buf, GFP_NOWAIT, fmt, args); spin_unlock_irqrestore(&buf->lock, flags); + if (ret < 0) { + if (nonblocking) + return -EAGAIN; + + ret = wait_event_interruptible(buf->wait, + stdio_redirect_has_output_space(stdio)); + if (ret) + return ret; + goto again; + } + wake_up(&buf->wait); + return ret; + } EXPORT_SYMBOL_GPL(stdio_redirect_vprintf); -void stdio_redirect_printf(struct stdio_redirect *stdio, bool nonblocking, - const char *fmt, ...) +ssize_t stdio_redirect_printf(struct stdio_redirect *stdio, bool nonblocking, + const char *fmt, ...) { va_list args; + ssize_t ret; + va_start(args, fmt); - stdio_redirect_vprintf(stdio, nonblocking, fmt, args); + ret = stdio_redirect_vprintf(stdio, nonblocking, fmt, args); va_end(args); + + return ret; } EXPORT_SYMBOL_GPL(stdio_redirect_printf);