Skip to content

Commit 5e17b5c

Browse files
committed
Merge tag 'fuse-update-6.15' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse
Pull fuse updates from Miklos Szeredi: - Allow connection to server to time out (Joanne Koong) - If server doesn't support creating a hard link, return EPERM rather than ENOSYS (Matt Johnston) - Allow file names longer than 1024 chars (Bernd Schubert) - Fix a possible race if request on io_uring queue is interrupted (Bernd Schubert) - Misc fixes and cleanups * tag 'fuse-update-6.15' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse: fuse: remove unneeded atomic set in uring creation fuse: fix uring race condition for null dereference of fc fuse: Increase FUSE_NAME_MAX to PATH_MAX fuse: Allocate only namelen buf memory in fuse_notify_ fuse: add default_request_timeout and max_request_timeout sysctls fuse: add kernel-enforced timeout option for requests fuse: optmize missing FUSE_LINK support fuse: Return EPERM rather than ENOSYS from link() fuse: removed unused function fuse_uring_create() from header fuse: {io-uring} Fix a possible req cancellation race
2 parents 0cc5543 + 2d06680 commit 5e17b5c

File tree

10 files changed

+356
-41
lines changed

10 files changed

+356
-41
lines changed

Documentation/admin-guide/sysctl/fs.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,28 @@ filesystems:
347347
``/proc/sys/fs/fuse/max_pages_limit`` is a read/write file for
348348
setting/getting the maximum number of pages that can be used for servicing
349349
requests in FUSE.
350+
351+
``/proc/sys/fs/fuse/default_request_timeout`` is a read/write file for
352+
setting/getting the default timeout (in seconds) for a fuse server to
353+
reply to a kernel-issued request in the event where the server did not
354+
specify a timeout at mount. If the server set a timeout,
355+
then default_request_timeout will be ignored. The default
356+
"default_request_timeout" is set to 0. 0 indicates no default timeout.
357+
The maximum value that can be set is 65535.
358+
359+
``/proc/sys/fs/fuse/max_request_timeout`` is a read/write file for
360+
setting/getting the maximum timeout (in seconds) for a fuse server to
361+
reply to a kernel-issued request. A value greater than 0 automatically opts
362+
the server into a timeout that will be set to at most "max_request_timeout",
363+
even if the server did not specify a timeout and default_request_timeout is
364+
set to 0. If max_request_timeout is greater than 0 and the server set a timeout
365+
greater than max_request_timeout or default_request_timeout is set to a value
366+
greater than max_request_timeout, the system will use max_request_timeout as the
367+
timeout. 0 indicates no max request timeout. The maximum value that can be set
368+
is 65535.
369+
370+
For timeouts, if the server does not respond to the request by the time
371+
the set timeout elapses, then the connection to the fuse server will be aborted.
372+
Please note that the timeouts are not 100% precise (eg you may set 60 seconds but
373+
the timeout may kick in after 70 seconds). The upper margin of error for the
374+
timeout is roughly FUSE_TIMEOUT_TIMER_FREQ seconds.

fs/fuse/dev.c

Lines changed: 139 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,100 @@ MODULE_ALIAS("devname:fuse");
3232

3333
static struct kmem_cache *fuse_req_cachep;
3434

35+
const unsigned long fuse_timeout_timer_freq =
36+
secs_to_jiffies(FUSE_TIMEOUT_TIMER_FREQ);
37+
38+
bool fuse_request_expired(struct fuse_conn *fc, struct list_head *list)
39+
{
40+
struct fuse_req *req;
41+
42+
req = list_first_entry_or_null(list, struct fuse_req, list);
43+
if (!req)
44+
return false;
45+
return time_is_before_jiffies(req->create_time + fc->timeout.req_timeout);
46+
}
47+
48+
bool fuse_fpq_processing_expired(struct fuse_conn *fc, struct list_head *processing)
49+
{
50+
int i;
51+
52+
for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
53+
if (fuse_request_expired(fc, &processing[i]))
54+
return true;
55+
56+
return false;
57+
}
58+
59+
/*
60+
* Check if any requests aren't being completed by the time the request timeout
61+
* elapses. To do so, we:
62+
* - check the fiq pending list
63+
* - check the bg queue
64+
* - check the fpq io and processing lists
65+
*
66+
* To make this fast, we only check against the head request on each list since
67+
* these are generally queued in order of creation time (eg newer requests get
68+
* queued to the tail). We might miss a few edge cases (eg requests transitioning
69+
* between lists, re-sent requests at the head of the pending list having a
70+
* later creation time than other requests on that list, etc.) but that is fine
71+
* since if the request never gets fulfilled, it will eventually be caught.
72+
*/
73+
void fuse_check_timeout(struct work_struct *work)
74+
{
75+
struct delayed_work *dwork = to_delayed_work(work);
76+
struct fuse_conn *fc = container_of(dwork, struct fuse_conn,
77+
timeout.work);
78+
struct fuse_iqueue *fiq = &fc->iq;
79+
struct fuse_dev *fud;
80+
struct fuse_pqueue *fpq;
81+
bool expired = false;
82+
83+
if (!atomic_read(&fc->num_waiting))
84+
goto out;
85+
86+
spin_lock(&fiq->lock);
87+
expired = fuse_request_expired(fc, &fiq->pending);
88+
spin_unlock(&fiq->lock);
89+
if (expired)
90+
goto abort_conn;
91+
92+
spin_lock(&fc->bg_lock);
93+
expired = fuse_request_expired(fc, &fc->bg_queue);
94+
spin_unlock(&fc->bg_lock);
95+
if (expired)
96+
goto abort_conn;
97+
98+
spin_lock(&fc->lock);
99+
if (!fc->connected) {
100+
spin_unlock(&fc->lock);
101+
return;
102+
}
103+
list_for_each_entry(fud, &fc->devices, entry) {
104+
fpq = &fud->pq;
105+
spin_lock(&fpq->lock);
106+
if (fuse_request_expired(fc, &fpq->io) ||
107+
fuse_fpq_processing_expired(fc, fpq->processing)) {
108+
spin_unlock(&fpq->lock);
109+
spin_unlock(&fc->lock);
110+
goto abort_conn;
111+
}
112+
113+
spin_unlock(&fpq->lock);
114+
}
115+
spin_unlock(&fc->lock);
116+
117+
if (fuse_uring_request_expired(fc))
118+
goto abort_conn;
119+
120+
out:
121+
queue_delayed_work(system_wq, &fc->timeout.work,
122+
fuse_timeout_timer_freq);
123+
return;
124+
125+
abort_conn:
126+
fuse_abort_conn(fc);
127+
}
128+
35129
static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req)
36130
{
37131
INIT_LIST_HEAD(&req->list);
@@ -40,6 +134,7 @@ static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req)
40134
refcount_set(&req->count, 1);
41135
__set_bit(FR_PENDING, &req->flags);
42136
req->fm = fm;
137+
req->create_time = jiffies;
43138
}
44139

45140
static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags)
@@ -407,6 +502,24 @@ static int queue_interrupt(struct fuse_req *req)
407502
return 0;
408503
}
409504

505+
bool fuse_remove_pending_req(struct fuse_req *req, spinlock_t *lock)
506+
{
507+
spin_lock(lock);
508+
if (test_bit(FR_PENDING, &req->flags)) {
509+
/*
510+
* FR_PENDING does not get cleared as the request will end
511+
* up in destruction anyway.
512+
*/
513+
list_del(&req->list);
514+
spin_unlock(lock);
515+
__fuse_put_request(req);
516+
req->out.h.error = -EINTR;
517+
return true;
518+
}
519+
spin_unlock(lock);
520+
return false;
521+
}
522+
410523
static void request_wait_answer(struct fuse_req *req)
411524
{
412525
struct fuse_conn *fc = req->fm->fc;
@@ -428,22 +541,20 @@ static void request_wait_answer(struct fuse_req *req)
428541
}
429542

430543
if (!test_bit(FR_FORCE, &req->flags)) {
544+
bool removed;
545+
431546
/* Only fatal signals may interrupt this */
432547
err = wait_event_killable(req->waitq,
433548
test_bit(FR_FINISHED, &req->flags));
434549
if (!err)
435550
return;
436551

437-
spin_lock(&fiq->lock);
438-
/* Request is not yet in userspace, bail out */
439-
if (test_bit(FR_PENDING, &req->flags)) {
440-
list_del(&req->list);
441-
spin_unlock(&fiq->lock);
442-
__fuse_put_request(req);
443-
req->out.h.error = -EINTR;
552+
if (test_bit(FR_URING, &req->flags))
553+
removed = fuse_uring_remove_pending_req(req);
554+
else
555+
removed = fuse_remove_pending_req(req, &fiq->lock);
556+
if (removed)
444557
return;
445-
}
446-
spin_unlock(&fiq->lock);
447558
}
448559

449560
/*
@@ -1533,14 +1644,10 @@ static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
15331644
struct fuse_copy_state *cs)
15341645
{
15351646
struct fuse_notify_inval_entry_out outarg;
1536-
int err = -ENOMEM;
1537-
char *buf;
1647+
int err;
1648+
char *buf = NULL;
15381649
struct qstr name;
15391650

1540-
buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1541-
if (!buf)
1542-
goto err;
1543-
15441651
err = -EINVAL;
15451652
if (size < sizeof(outarg))
15461653
goto err;
@@ -1550,13 +1657,18 @@ static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
15501657
goto err;
15511658

15521659
err = -ENAMETOOLONG;
1553-
if (outarg.namelen > FUSE_NAME_MAX)
1660+
if (outarg.namelen > fc->name_max)
15541661
goto err;
15551662

15561663
err = -EINVAL;
15571664
if (size != sizeof(outarg) + outarg.namelen + 1)
15581665
goto err;
15591666

1667+
err = -ENOMEM;
1668+
buf = kzalloc(outarg.namelen + 1, GFP_KERNEL);
1669+
if (!buf)
1670+
goto err;
1671+
15601672
name.name = buf;
15611673
name.len = outarg.namelen;
15621674
err = fuse_copy_one(cs, buf, outarg.namelen + 1);
@@ -1581,14 +1693,10 @@ static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
15811693
struct fuse_copy_state *cs)
15821694
{
15831695
struct fuse_notify_delete_out outarg;
1584-
int err = -ENOMEM;
1585-
char *buf;
1696+
int err;
1697+
char *buf = NULL;
15861698
struct qstr name;
15871699

1588-
buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1589-
if (!buf)
1590-
goto err;
1591-
15921700
err = -EINVAL;
15931701
if (size < sizeof(outarg))
15941702
goto err;
@@ -1598,13 +1706,18 @@ static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
15981706
goto err;
15991707

16001708
err = -ENAMETOOLONG;
1601-
if (outarg.namelen > FUSE_NAME_MAX)
1709+
if (outarg.namelen > fc->name_max)
16021710
goto err;
16031711

16041712
err = -EINVAL;
16051713
if (size != sizeof(outarg) + outarg.namelen + 1)
16061714
goto err;
16071715

1716+
err = -ENOMEM;
1717+
buf = kzalloc(outarg.namelen + 1, GFP_KERNEL);
1718+
if (!buf)
1719+
goto err;
1720+
16081721
name.name = buf;
16091722
name.len = outarg.namelen;
16101723
err = fuse_copy_one(cs, buf, outarg.namelen + 1);
@@ -2275,6 +2388,9 @@ void fuse_abort_conn(struct fuse_conn *fc)
22752388
LIST_HEAD(to_end);
22762389
unsigned int i;
22772390

2391+
if (fc->timeout.req_timeout)
2392+
cancel_delayed_work(&fc->timeout.work);
2393+
22782394
/* Background queuing checks fc->connected under bg_lock */
22792395
spin_lock(&fc->bg_lock);
22802396
fc->connected = 0;

fs/fuse/dev_uring.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ void fuse_uring_abort_end_requests(struct fuse_ring *ring)
140140
}
141141
}
142142

143+
bool fuse_uring_request_expired(struct fuse_conn *fc)
144+
{
145+
struct fuse_ring *ring = fc->ring;
146+
struct fuse_ring_queue *queue;
147+
int qid;
148+
149+
if (!ring)
150+
return false;
151+
152+
for (qid = 0; qid < ring->nr_queues; qid++) {
153+
queue = READ_ONCE(ring->queues[qid]);
154+
if (!queue)
155+
continue;
156+
157+
spin_lock(&queue->lock);
158+
if (fuse_request_expired(fc, &queue->fuse_req_queue) ||
159+
fuse_request_expired(fc, &queue->fuse_req_bg_queue) ||
160+
fuse_fpq_processing_expired(fc, queue->fpq.processing)) {
161+
spin_unlock(&queue->lock);
162+
return true;
163+
}
164+
spin_unlock(&queue->lock);
165+
}
166+
167+
return false;
168+
}
169+
143170
void fuse_uring_destruct(struct fuse_conn *fc)
144171
{
145172
struct fuse_ring *ring = fc->ring;
@@ -211,7 +238,6 @@ static struct fuse_ring *fuse_uring_create(struct fuse_conn *fc)
211238
ring->nr_queues = nr_queues;
212239
ring->fc = fc;
213240
ring->max_payload_sz = max_payload_size;
214-
atomic_set(&ring->queue_refs, 0);
215241
smp_store_release(&fc->ring, ring);
216242

217243
spin_unlock(&fc->lock);
@@ -726,8 +752,6 @@ static void fuse_uring_add_req_to_ring_ent(struct fuse_ring_ent *ent,
726752
struct fuse_req *req)
727753
{
728754
struct fuse_ring_queue *queue = ent->queue;
729-
struct fuse_conn *fc = req->fm->fc;
730-
struct fuse_iqueue *fiq = &fc->iq;
731755

732756
lockdep_assert_held(&queue->lock);
733757

@@ -737,9 +761,7 @@ static void fuse_uring_add_req_to_ring_ent(struct fuse_ring_ent *ent,
737761
ent->state);
738762
}
739763

740-
spin_lock(&fiq->lock);
741764
clear_bit(FR_PENDING, &req->flags);
742-
spin_unlock(&fiq->lock);
743765
ent->fuse_req = req;
744766
ent->state = FRRS_FUSE_REQ;
745767
list_move(&ent->list, &queue->ent_w_req_queue);
@@ -1238,6 +1260,8 @@ void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req)
12381260
if (unlikely(queue->stopped))
12391261
goto err_unlock;
12401262

1263+
set_bit(FR_URING, &req->flags);
1264+
req->ring_queue = queue;
12411265
ent = list_first_entry_or_null(&queue->ent_avail_queue,
12421266
struct fuse_ring_ent, list);
12431267
if (ent)
@@ -1276,6 +1300,8 @@ bool fuse_uring_queue_bq_req(struct fuse_req *req)
12761300
return false;
12771301
}
12781302

1303+
set_bit(FR_URING, &req->flags);
1304+
req->ring_queue = queue;
12791305
list_add_tail(&req->list, &queue->fuse_req_bg_queue);
12801306

12811307
ent = list_first_entry_or_null(&queue->ent_avail_queue,
@@ -1306,6 +1332,13 @@ bool fuse_uring_queue_bq_req(struct fuse_req *req)
13061332
return true;
13071333
}
13081334

1335+
bool fuse_uring_remove_pending_req(struct fuse_req *req)
1336+
{
1337+
struct fuse_ring_queue *queue = req->ring_queue;
1338+
1339+
return fuse_remove_pending_req(req, &queue->lock);
1340+
}
1341+
13091342
static const struct fuse_iqueue_ops fuse_io_uring_ops = {
13101343
/* should be send over io-uring as enhancement */
13111344
.send_forget = fuse_dev_queue_forget,

0 commit comments

Comments
 (0)