From patchwork Wed Dec 18 22:26:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joanne Koong X-Patchwork-Id: 13914180 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEB0117A586 for ; Wed, 18 Dec 2024 22:27:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734560850; cv=none; b=n7gsCDz+dJ1zn4lOlsYvzfS/OzFiFl5w0CC0oHeED7pV8ud8v+9VjXBJwVHcXjGiTP8/bUTYs6qxHkJjUGCgK4nFjDKUYSpfNwY+fYY7PXuMzCzmRwn7upheD0i08wc5u2EsRmzAf/GnL2VjKYhMFRr92XxhwrC7LYsvuhumG0E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734560850; c=relaxed/simple; bh=QKR2e8ZK+gm6XpPlZEZRJnnEo0O7cGnPoZfjRf0n7MI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L1t8nux2199xrgAJjTCu30jE3yTMRp9uh74W4/NVuYhTKlENIMrnYWQGYmvQH8G6aHfG/F4ybZmLqqH9SE9Ym4L2TFXUgu7H9+OcOmTSA01myzR0Uy01Yz3ZqEgYwGfGA46JFAdoK4WZH00OC/MpvM6Qpgnr/qPmEmGzRzl4rMM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dy9dzi40; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dy9dzi40" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-6eff8289d0eso1891177b3.0 for ; Wed, 18 Dec 2024 14:27:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734560848; x=1735165648; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YjnUNJp09N5jqMZtBUT9EJxEXi4xNx2uay8VUsQTWlM=; b=dy9dzi40fylZ1NUFLomgyllCVvI9nGI0Ri2frA7ySxqN5klZLa0mL2hYHsO3QaoKfQ DeZISksKyk0joTrKTJxbaOOOnJ27w4l4lpi5SAqasw1ukvtY/zyhdoBj1ZA7kDZqCKkA mmIsnShkTRpjGyl4SH2M6pRwGFKcK1+90+858cM/Y53W+ATCUNw+Ejh+AeOKGOOQkDO/ E5edvMFmvgPrGfwzYcbLfhFg/QRGBlu3c8YNFQ4TLGjF5/Hvb9qHGWpknOmxmcNyPw2o t1phkMJhC4jyLnLXOO9qKrJn+Q3VqQ1s6FRPMykSmPwSr759DSyEbqsfut4wCvUzIc9x BGYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734560848; x=1735165648; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YjnUNJp09N5jqMZtBUT9EJxEXi4xNx2uay8VUsQTWlM=; b=VUEs54OhV53dDbXRy8CjzC7z8GddMMbnzAuuNgZLDOPmbGTvWdbx3XxR6yb2yyE/c0 GakimNDzN26/CwMsddivZcGjimyUPVduqqylSKYfxedWnsNxvwNQWMxRiJqHLtRoIYS7 xqtyn+rmRzkmwa0rmFlxS5heKaElirSgPo9HKNSpjFaYoD6uIC19qHIB/rvCN5IF75U6 tpoSwfK+CWq6U5chNLw7GmrZj33MHA7HcJPxE2lhx75SlPvDvYJ6KPbgT+AU05cxZTc7 uD0uh/B4mLIJhySj7lmMqjNvL604QWWeTNjKvCD2oJVEVUdeP6PnByuhx8ZIisvmUDcP PAng== X-Forwarded-Encrypted: i=1; AJvYcCWUzteEBU0+sd9qwrS1LjjCqIfzh2XUxqvPkKKP9OwbHEW8Sq6PwLACSx39SzWuV80jaJ+X8iNfCdGmmfXv@vger.kernel.org X-Gm-Message-State: AOJu0YxvgmFc+kfXqVA9mu2Zx0y3P5gHXjLNY0k5y0KllJdN41dPdFmU JgmzWah+QsTLVxXzEhnPC6QGbC6BDqoM46xH9I0dQiUepPILcuHtFEzsyg== X-Gm-Gg: ASbGncumCT/ys+tLANrUwFHAsk8FxBXFwMEa+GGQ5B1L4csGfsv7MxCcStOlIdOE+tp YlJP1aSqUVJ3LOcm7FmxL8FsGKdkW9w0pYA3iSfFmlrI/w1vA+iD4FaUYjUUwfDs8PsqHIgM4j6 Z1nni4q7gektjvhO3fr3hgRZnCbjmPRVRcpDn1Q74vl5MKc3y77KxXG0tpAcZLIwPaZx6lijqSY sFjppQaWWNtirbZLkzR8Wwp4yqZ1iijZNCJKFaLAQGFlTbtCKG6SYY= X-Google-Smtp-Source: AGHT+IG+f08QwKMVtDDytWyBVMGUJaEYdfhjGH+XqzqmmAnPTVOSkaItrfoPVLdxcwUuqFPiGAuXKw== X-Received: by 2002:a05:690c:6e90:b0:6ef:64e8:c704 with SMTP id 00721157ae682-6f3d269b84fmr38011477b3.36.1734560847624; Wed, 18 Dec 2024 14:27:27 -0800 (PST) Received: from localhost ([2a03:2880:25ff:73::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6f2890c80b7sm25828007b3.75.2024.12.18.14.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 14:27:27 -0800 (PST) From: Joanne Koong To: miklos@szeredi.hu, linux-fsdevel@vger.kernel.org Cc: josef@toxicpanda.com, bernd.schubert@fastmail.fm, jefflexu@linux.alibaba.com, laoar.shao@gmail.com, jlayton@kernel.org, senozhatsky@chromium.org, tfiga@chromium.org, bgeffon@google.com, etmartin4313@gmail.com, kernel-team@meta.com Subject: [PATCH v11 1/2] fuse: add kernel-enforced timeout option for requests Date: Wed, 18 Dec 2024 14:26:29 -0800 Message-ID: <20241218222630.99920-2-joannelkoong@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241218222630.99920-1-joannelkoong@gmail.com> References: <20241218222630.99920-1-joannelkoong@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There are situations where fuse servers can become unresponsive or stuck, for example if the server is deadlocked. Currently, there's no good way to detect if a server is stuck and needs to be killed manually. This commit adds an option for enforcing a timeout (in seconds) for requests where if the timeout elapses without the server responding to the request, the connection will be automatically aborted. Please note that these timeouts are not 100% precise. For example, the request may take roughly an extra FUSE_TIMEOUT_TIMER_FREQ seconds beyond the requested timeout due to internal implementation, in order to mitigate overhead. Signed-off-by: Joanne Koong Reviewed-by: Jeff Layton --- fs/fuse/dev.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/fuse/fuse_i.h | 22 +++++++++++++ fs/fuse/inode.c | 23 +++++++++++++ 3 files changed, 130 insertions(+) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 27ccae63495d..bcf8a7994944 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -45,6 +45,87 @@ static struct fuse_dev *fuse_get_dev(struct file *file) return READ_ONCE(file->private_data); } +static bool request_expired(struct fuse_conn *fc, struct list_head *list) +{ + struct fuse_req *req; + + req = list_first_entry_or_null(list, struct fuse_req, list); + if (!req) + return false; + return time_is_before_jiffies(req->create_time + fc->timeout.req_timeout); +} + +/* + * Check if any requests aren't being completed by the time the request timeout + * elapses. To do so, we: + * - check the fiq pending list + * - check the bg queue + * - check the fpq io and processing lists + * + * To make this fast, we only check against the head request on each list since + * these are generally queued in order of creation time (eg newer requests get + * queued to the tail). We might miss a few edge cases (eg requests transitioning + * between lists, re-sent requests at the head of the pending list having a + * later creation time than other requests on that list, etc.) but that is fine + * since if the request never gets fulfilled, it will eventually be caught. + */ +void fuse_check_timeout(struct work_struct *work) +{ + struct delayed_work *dwork = to_delayed_work(work); + struct fuse_conn *fc = container_of(dwork, struct fuse_conn, + timeout.work); + struct fuse_iqueue *fiq = &fc->iq; + struct fuse_dev *fud; + struct fuse_pqueue *fpq; + bool expired = false; + int i; + + if (!atomic_read(&fc->num_waiting)) + goto out; + + spin_lock(&fiq->lock); + expired = request_expired(fc, &fiq->pending); + spin_unlock(&fiq->lock); + if (expired) + goto abort_conn; + + spin_lock(&fc->bg_lock); + expired = request_expired(fc, &fc->bg_queue); + spin_unlock(&fc->bg_lock); + if (expired) + goto abort_conn; + + spin_lock(&fc->lock); + if (!fc->connected) { + spin_unlock(&fc->lock); + return; + } + list_for_each_entry(fud, &fc->devices, entry) { + fpq = &fud->pq; + spin_lock(&fpq->lock); + if (request_expired(fc, &fpq->io)) + goto fpq_abort; + + for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) { + if (request_expired(fc, &fpq->processing[i])) + goto fpq_abort; + } + spin_unlock(&fpq->lock); + } + spin_unlock(&fc->lock); + +out: + queue_delayed_work(system_wq, &fc->timeout.work, + secs_to_jiffies(FUSE_TIMEOUT_TIMER_FREQ)); + return; + +fpq_abort: + spin_unlock(&fpq->lock); + spin_unlock(&fc->lock); +abort_conn: + fuse_abort_conn(fc); +} + static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req) { INIT_LIST_HEAD(&req->list); @@ -53,6 +134,7 @@ static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req) refcount_set(&req->count, 1); __set_bit(FR_PENDING, &req->flags); req->fm = fm; + req->create_time = jiffies; } static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags) @@ -2260,6 +2342,9 @@ void fuse_abort_conn(struct fuse_conn *fc) LIST_HEAD(to_end); unsigned int i; + if (fc->timeout.req_timeout) + cancel_delayed_work(&fc->timeout.work); + /* Background queuing checks fc->connected under bg_lock */ spin_lock(&fc->bg_lock); fc->connected = 0; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 74744c6f2860..26eb00e5f043 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -438,6 +438,9 @@ struct fuse_req { /** fuse_mount this request belongs to */ struct fuse_mount *fm; + + /** When (in jiffies) the request was created */ + unsigned long create_time; }; struct fuse_iqueue; @@ -528,6 +531,17 @@ struct fuse_pqueue { struct list_head io; }; +/* Frequency (in seconds) of request timeout checks, if opted into */ +#define FUSE_TIMEOUT_TIMER_FREQ 15 + +struct fuse_timeout { + /* Worker for checking if any requests have timed out */ + struct delayed_work work; + + /* Request timeout (in jiffies). 0 = no timeout */ + unsigned long req_timeout; +}; + /** * Fuse device instance */ @@ -574,6 +588,8 @@ struct fuse_fs_context { enum fuse_dax_mode dax_mode; unsigned int max_read; unsigned int blksize; + /* Request timeout (in seconds). 0 = no timeout (infinite wait) */ + unsigned int req_timeout; const char *subtype; /* DAX device, may be NULL */ @@ -923,6 +939,9 @@ struct fuse_conn { /** IDR for backing files ids */ struct idr backing_files_map; #endif + + /** Only used if the connection enforces request timeouts */ + struct fuse_timeout timeout; }; /* @@ -1191,6 +1210,9 @@ void fuse_request_end(struct fuse_req *req); void fuse_abort_conn(struct fuse_conn *fc); void fuse_wait_aborted(struct fuse_conn *fc); +/* Check if any requests timed out */ +void fuse_check_timeout(struct work_struct *work); + /** * Invalidate inode attributes */ diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index 3ce4f4e81d09..02dac88d922e 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -765,6 +765,7 @@ enum { OPT_ALLOW_OTHER, OPT_MAX_READ, OPT_BLKSIZE, + OPT_REQUEST_TIMEOUT, OPT_ERR }; @@ -779,6 +780,7 @@ static const struct fs_parameter_spec fuse_fs_parameters[] = { fsparam_u32 ("max_read", OPT_MAX_READ), fsparam_u32 ("blksize", OPT_BLKSIZE), fsparam_string ("subtype", OPT_SUBTYPE), + fsparam_u32 ("request_timeout", OPT_REQUEST_TIMEOUT), {} }; @@ -874,6 +876,10 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param) ctx->blksize = result.uint_32; break; + case OPT_REQUEST_TIMEOUT: + ctx->req_timeout = result.uint_32; + break; + default: return -EINVAL; } @@ -1004,6 +1010,8 @@ void fuse_conn_put(struct fuse_conn *fc) if (IS_ENABLED(CONFIG_FUSE_DAX)) fuse_dax_conn_free(fc); + if (fc->timeout.req_timeout) + cancel_delayed_work_sync(&fc->timeout.work); if (fiq->ops->release) fiq->ops->release(fiq); put_pid_ns(fc->pid_ns); @@ -1723,6 +1731,20 @@ int fuse_init_fs_context_submount(struct fs_context *fsc) } EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount); +static void fuse_init_fc_timeout(struct fuse_conn *fc, struct fuse_fs_context *ctx) +{ + if (ctx->req_timeout) { + if (check_mul_overflow(ctx->req_timeout, HZ, &fc->timeout.req_timeout)) + fc->timeout.req_timeout = ULONG_MAX; + + INIT_DELAYED_WORK(&fc->timeout.work, fuse_check_timeout); + queue_delayed_work(system_wq, &fc->timeout.work, + secs_to_jiffies(FUSE_TIMEOUT_TIMER_FREQ)); + } else { + fc->timeout.req_timeout = 0; + } +} + int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) { struct fuse_dev *fud = NULL; @@ -1785,6 +1807,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) fc->destroy = ctx->destroy; fc->no_control = ctx->no_control; fc->no_force_umount = ctx->no_force_umount; + fuse_init_fc_timeout(fc, ctx); err = -ENOMEM; root = fuse_get_root_inode(sb, ctx->rootmode); From patchwork Wed Dec 18 22:26:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joanne Koong X-Patchwork-Id: 13914181 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 936F31FC7C5 for ; Wed, 18 Dec 2024 22:27:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734560852; cv=none; b=CHFfqu59REIg7cVynPAh/8XCKcygARM4v6inGoRP2iYAY6vmzbddmLMPN8LeKsSMojycHeI44Xz6dbooijU2ShEP2TyqyWREXodjd4eDDy1gZOH26GwqVS0EMw8AgmwpHc8EZRYTx9U19BKh56I02lqeIl/qbeLeyoUP7s6/oo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734560852; c=relaxed/simple; bh=lRNSMiB/9Fl1NSyos3odsFHpo01K6p3fG7YlyR81ODs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cvkuYv0L9dHm3qBZkwv775zmY++D41NiCbcJ8RPP6iZVDWrSysc9YYgJ2HOuJtzJs73y3qqtw+JvsQPRdI+SgdeC/Iova3RYMFbPOzpBH7QZR4pdN/Ks05j5okrkeKBJrieyIQrRYspdhsJl6dKMa55e98+SG8ixTuozRssmqSA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HEVilTUT; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HEVilTUT" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6f14626c5d3so1595347b3.3 for ; Wed, 18 Dec 2024 14:27:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734560849; x=1735165649; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lJxClIbYoiDmP4Vx6vopU9i/iNYeol7ozMiBYaXiU8I=; b=HEVilTUTWVIO6ElIXhxEjqwQ547IQNkzx0m4GYodAbz63sj6wp/CWJ8fGxGwPsT5MO IxOu1ANcuzbcT2gvWvTjqAGfECSJ7s/FRf0aRONRvxsshU+KmrKCJ4iHsK0KgjnRG0gx RH8LCLZ9gsIzURmppBeF/G7kooNORkqYqyFkljNJJWkb8FxvFcFaJds8xV2Ns7dZ2klV wiBybyS2T0YzTqvlkmgsqPujwrwlmsHlS0fjDFu1Wfl0l75H+ZMcYQDNBjY5EynZE7Ur wR1pYs65CnCeMirta34PkexOjv6s84Ay1Vbx8a9t6vzl2SAGv4PfH/kWZ1r2xk8iTN+J 6aew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734560849; x=1735165649; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lJxClIbYoiDmP4Vx6vopU9i/iNYeol7ozMiBYaXiU8I=; b=gvnnTCZvPZnM1tBNen6ju1nvGuMO5OCV39Ef9hLTcXCBFNjCgZcs55xVWfTrKozqPc YqH5zB1+wtZo1LD46qrxEB4AoQ9RGT2yvA2cJu40d8Z9Yoq9WZ+5583+VQc2IRtejSir gFP1MjZhJ+VVEjDmUARQXgenVQV7qEsOAUJyYpPvtJNsVZy65cQ2C42rX9rqtw6qThrZ 1PZ6WNAMa6LlKflY/AnouwWddsYzS2IDpuY3l2UnFRQ09Iuu7k1BO7RegRydOntlKYU4 M3scfBBEaTyAAIqnkTmW3dVjMVsL4wzOUnHVgOLST+5wrHa0BKnRIX++VNOFmyQD9F/Y 5kYQ== X-Forwarded-Encrypted: i=1; AJvYcCUY+LBGl6dALH3i2ZZTQEMCzChYcXBaYuDxldo+jjQziyFgPFiIpylRw0JGt1UtNurH2fedz4JAu4PSsC22@vger.kernel.org X-Gm-Message-State: AOJu0YyptWXcj6dXARcKNszQBz9BhVxTMMi2/OGLSQbxar8aBcSyQQT4 1C+/MMLYcByTbXm/Zt4DJEKR/Qj3FKpfho306lDCZjvhjQ0tk+7V X-Gm-Gg: ASbGnctlDSTcegMaTGGHE43wpwSbsYEiRB7g9Rzov5TJYJgS0S5l3kwfEYvYZwHIAzD gZfhwWZ38GlnsARdwXFuJgXpAnDcsL3Yz5OiyDN3p1TjGHpGe6gWKqD6ZbWeWlVLcjI8hyun160 P2UuY7n0B3ku5S130fA7XULFZjjpTNSilOj4m7hYwZBFXNAnsxATfopvabXtxBqPKC8syRS2bw+ mzT/x4FGFercrbmssMxKxgTPp93IuJuJx8CYqBTB95db45QaWrt0cI= X-Google-Smtp-Source: AGHT+IE0Sj8Zhaq1zRefD1QN6xx+rWEhY//mf5xaf8/dYJXynpxBbU77zQdS5XE+aOBeLxg1pk84PQ== X-Received: by 2002:a05:690c:ed2:b0:6e2:1527:446b with SMTP id 00721157ae682-6f3ccc29354mr36263757b3.3.1734560849462; Wed, 18 Dec 2024 14:27:29 -0800 (PST) Received: from localhost ([2a03:2880:25ff:73::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6f288fc5059sm26375117b3.5.2024.12.18.14.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 14:27:29 -0800 (PST) From: Joanne Koong To: miklos@szeredi.hu, linux-fsdevel@vger.kernel.org Cc: josef@toxicpanda.com, bernd.schubert@fastmail.fm, jefflexu@linux.alibaba.com, laoar.shao@gmail.com, jlayton@kernel.org, senozhatsky@chromium.org, tfiga@chromium.org, bgeffon@google.com, etmartin4313@gmail.com, kernel-team@meta.com, Bernd Schubert Subject: [PATCH v11 2/2] fuse: add default_request_timeout and max_request_timeout sysctls Date: Wed, 18 Dec 2024 14:26:30 -0800 Message-ID: <20241218222630.99920-3-joannelkoong@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241218222630.99920-1-joannelkoong@gmail.com> References: <20241218222630.99920-1-joannelkoong@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce two new sysctls, "default_request_timeout" and "max_request_timeout". These control how long (in seconds) a server can take to reply to a request. If the server does not reply by the timeout, then the connection will be aborted. The upper bound on these sysctl values is U32_MAX. "default_request_timeout" sets the default timeout if no timeout is specified by the fuse server on mount. 0 (default) indicates no default timeout should be enforced. If the server did specify a timeout, then default_request_timeout will be ignored. "max_request_timeout" sets the max amount of time the server may take to reply to a request. 0 (default) indicates no maximum timeout. If max_request_timeout is set and the fuse server attempts to set a timeout greater than max_request_timeout, the system will use max_request_timeout as the timeout. Similarly, if default_request_timeout is greater than max_request_timeout, the system will use max_request_timeout as the timeout. If the server does not request a timeout and default_request_timeout is set to 0 but max_request_timeout is set, then the timeout will be max_request_timeout. Please note that these timeouts are not 100% precise. The request may take roughly an extra FUSE_TIMEOUT_TIMER_FREQ seconds beyond the set max timeout due to how it's internally implemented. $ sysctl -a | grep fuse.default_request_timeout fs.fuse.default_request_timeout = 0 $ echo 4294967296 | sudo tee /proc/sys/fs/fuse/default_request_timeout tee: /proc/sys/fs/fuse/default_request_timeout: Invalid argument $ echo 4294967295 | sudo tee /proc/sys/fs/fuse/default_request_timeout 4294967295 $ sysctl -a | grep fuse.default_request_timeout fs.fuse.default_request_timeout = 4294967295 $ echo 0 | sudo tee /proc/sys/fs/fuse/default_request_timeout 0 $ sysctl -a | grep fuse.default_request_timeout fs.fuse.default_request_timeout = 0 Signed-off-by: Joanne Koong Reviewed-by: Bernd Schubert Reviewed-by: Josef Bacik --- Documentation/admin-guide/sysctl/fs.rst | 25 +++++++++++++++++++++++++ fs/fuse/fuse_i.h | 10 ++++++++++ fs/fuse/inode.c | 16 ++++++++++++++-- fs/fuse/sysctl.c | 14 ++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/sysctl/fs.rst b/Documentation/admin-guide/sysctl/fs.rst index f5ec6c9312e1..12169a5e19dd 100644 --- a/Documentation/admin-guide/sysctl/fs.rst +++ b/Documentation/admin-guide/sysctl/fs.rst @@ -347,3 +347,28 @@ filesystems: ``/proc/sys/fs/fuse/max_pages_limit`` is a read/write file for setting/getting the maximum number of pages that can be used for servicing requests in FUSE. + +``/proc/sys/fs/fuse/default_request_timeout`` is a read/write file for +setting/getting the default timeout (in seconds) for a fuse server to +reply to a kernel-issued request in the event where the server did not +specify a timeout at mount. If the server set a timeout, +then default_request_timeout will be ignored. The default +"default_request_timeout" is set to 0. 0 indicates no default timeout. +The maximum value that can be set is U32_MAX. + +``/proc/sys/fs/fuse/max_request_timeout`` is a read/write file for +setting/getting the maximum timeout (in seconds) for a fuse server to +reply to a kernel-issued request. A value greater than 0 automatically opts +the server into a timeout that will be set to at most "max_request_timeout", +even if the server did not specify a timeout and default_request_timeout is +set to 0. If max_request_timeout is greater than 0 and the server set a timeout +greater than max_request_timeout or default_request_timeout is set to a value +greater than max_request_timeout, the system will use max_request_timeout as the +timeout. 0 indicates no max request timeout. The maximum value that can be set +is U32_MAX. + +For the timeouts, if the server does not respond to the request by the time +the set timeout elapses, then the connection to the fuse server will be aborted. +Please note that the timeouts are not 100% precise (eg you may set 60 seconds but +the timeout may kick in after 70 seconds). The upper margin of error for the +timeout is roughly FUSE_TIMEOUT_TIMER_FREQ seconds. diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 26eb00e5f043..310885b51087 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -46,6 +46,16 @@ /** Maximum of max_pages received in init_out */ extern unsigned int fuse_max_pages_limit; +/* + * Default timeout (in seconds) for the server to reply to a request + * before the connection is aborted, if no timeout was specified on mount. + */ +extern unsigned int fuse_default_req_timeout; +/* + * Max timeout (in seconds) for the server to reply to a request before + * the connection is aborted. + */ +extern unsigned int fuse_max_req_timeout; /** List of active connections */ extern struct list_head fuse_conn_list; diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index 02dac88d922e..9f0be79eab74 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -36,6 +36,9 @@ DEFINE_MUTEX(fuse_mutex); static int set_global_limit(const char *val, const struct kernel_param *kp); unsigned int fuse_max_pages_limit = 256; +/* default is no timeout */ +unsigned int fuse_default_req_timeout = 0; +unsigned int fuse_max_req_timeout = 0; unsigned max_user_bgreq; module_param_call(max_user_bgreq, set_global_limit, param_get_uint, @@ -1733,8 +1736,17 @@ EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount); static void fuse_init_fc_timeout(struct fuse_conn *fc, struct fuse_fs_context *ctx) { - if (ctx->req_timeout) { - if (check_mul_overflow(ctx->req_timeout, HZ, &fc->timeout.req_timeout)) + unsigned int timeout = ctx->req_timeout ?: fuse_default_req_timeout; + + if (fuse_max_req_timeout) { + if (!timeout) + timeout = fuse_max_req_timeout; + else + timeout = min(timeout, fuse_max_req_timeout); + } + + if (timeout) { + if (check_mul_overflow(timeout, HZ, &fc->timeout.req_timeout)) fc->timeout.req_timeout = ULONG_MAX; INIT_DELAYED_WORK(&fc->timeout.work, fuse_check_timeout); diff --git a/fs/fuse/sysctl.c b/fs/fuse/sysctl.c index b272bb333005..5017059513f1 100644 --- a/fs/fuse/sysctl.c +++ b/fs/fuse/sysctl.c @@ -23,6 +23,20 @@ static struct ctl_table fuse_sysctl_table[] = { .extra1 = SYSCTL_ONE, .extra2 = &sysctl_fuse_max_pages_limit, }, + { + .procname = "default_request_timeout", + .data = &fuse_default_req_timeout, + .maxlen = sizeof(fuse_default_req_timeout), + .mode = 0644, + .proc_handler = proc_douintvec, + }, + { + .procname = "max_request_timeout", + .data = &fuse_max_req_timeout, + .maxlen = sizeof(fuse_max_req_timeout), + .mode = 0644, + .proc_handler = proc_douintvec, + }, }; int fuse_sysctl_register(void)