Skip to content

Commit ed42fe0

Browse files
Al Virohubcapsc
authored andcommitted
orangefs: hopefully saner op refcounting and locking
* create with refcount 1 * make op_release() decrement and free if zero (i.e. old put_op() has become that). * mark when submitter has given up waiting; from that point nobody else can move between the lists, change state, etc. * have daemon read/write_iter grab a reference when picking op and *always* give it up in the end * don't put into hash until we know it's been successfully passed to daemon * move op->lock _lower_ than htab_in_progress_lock (and make sure to take it in purge_inprogress_ops()) Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Mike Marshall <hubcap@omnibond.com>
1 parent fee25ce commit ed42fe0

8 files changed

Lines changed: 107 additions & 143 deletions

File tree

‎fs/orangefs/devorangefs-req.c‎

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ static void orangefs_devreq_add_op(struct orangefs_kernel_op_s *op)
4343
{
4444
int index = hash_func(op->tag, hash_table_size);
4545

46-
spin_lock(&htable_ops_in_progress_lock);
4746
list_add_tail(&op->list, &htable_ops_in_progress[index]);
48-
spin_unlock(&htable_ops_in_progress_lock);
4947
}
5048

5149
static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
@@ -60,8 +58,9 @@ static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
6058
next,
6159
&htable_ops_in_progress[index],
6260
list) {
63-
if (op->tag == tag) {
64-
list_del(&op->list);
61+
if (op->tag == tag && !op_state_purged(op)) {
62+
list_del_init(&op->list);
63+
get_op(op); /* increase ref count. */
6564
spin_unlock(&htable_ops_in_progress_lock);
6665
return op;
6766
}
@@ -127,12 +126,17 @@ static ssize_t orangefs_devreq_read(struct file *file,
127126
return -EINVAL;
128127
}
129128

129+
restart:
130130
/* Get next op (if any) from top of list. */
131131
spin_lock(&orangefs_request_list_lock);
132132
list_for_each_entry_safe(op, temp, &orangefs_request_list, list) {
133133
__s32 fsid;
134134
/* This lock is held past the end of the loop when we break. */
135135
spin_lock(&op->lock);
136+
if (unlikely(op_state_purged(op))) {
137+
spin_unlock(&op->lock);
138+
continue;
139+
}
136140

137141
fsid = fsid_of_op(op);
138142
if (fsid != ORANGEFS_FS_ID_NULL) {
@@ -197,16 +201,10 @@ static ssize_t orangefs_devreq_read(struct file *file,
197201
spin_unlock(&orangefs_request_list_lock);
198202
return -EAGAIN;
199203
}
200-
201-
/*
202-
* Set the operation to be in progress and move it between lists since
203-
* it has been sent to the client.
204-
*/
205-
set_op_state_inprogress(cur_op);
206-
207-
list_del(&cur_op->list);
204+
list_del_init(&cur_op->list);
205+
get_op(op);
208206
spin_unlock(&orangefs_request_list_lock);
209-
orangefs_devreq_add_op(cur_op);
207+
210208
spin_unlock(&cur_op->lock);
211209

212210
/* Push the upcall out. */
@@ -224,6 +222,25 @@ static ssize_t orangefs_devreq_read(struct file *file,
224222
if (ret != 0)
225223
goto error;
226224

225+
spin_lock(&htable_ops_in_progress_lock);
226+
spin_lock(&cur_op->lock);
227+
if (unlikely(op_state_given_up(cur_op))) {
228+
spin_unlock(&cur_op->lock);
229+
spin_unlock(&htable_ops_in_progress_lock);
230+
op_release(cur_op);
231+
goto restart;
232+
}
233+
234+
/*
235+
* Set the operation to be in progress and move it between lists since
236+
* it has been sent to the client.
237+
*/
238+
set_op_state_inprogress(cur_op);
239+
orangefs_devreq_add_op(cur_op);
240+
spin_unlock(&cur_op->lock);
241+
spin_unlock(&htable_ops_in_progress_lock);
242+
op_release(cur_op);
243+
227244
/* The client only asks to read one size buffer. */
228245
return MAX_DEV_REQ_UPSIZE;
229246
error:
@@ -235,11 +252,13 @@ static ssize_t orangefs_devreq_read(struct file *file,
235252
gossip_err("orangefs: Failed to copy data to user space\n");
236253
spin_lock(&orangefs_request_list_lock);
237254
spin_lock(&cur_op->lock);
238-
set_op_state_waiting(cur_op);
239-
orangefs_devreq_remove_op(cur_op->tag);
240-
list_add(&cur_op->list, &orangefs_request_list);
255+
if (likely(!op_state_given_up(cur_op))) {
256+
set_op_state_waiting(cur_op);
257+
list_add(&cur_op->list, &orangefs_request_list);
258+
}
241259
spin_unlock(&cur_op->lock);
242260
spin_unlock(&orangefs_request_list_lock);
261+
op_release(cur_op);
243262
return -EFAULT;
244263
}
245264

@@ -278,15 +297,13 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
278297
__func__,
279298
total,
280299
(unsigned int) MAX_DEV_REQ_DOWNSIZE);
281-
ret = -EFAULT;
282-
goto out;
300+
return -EFAULT;
283301
}
284302

285303
n = copy_from_iter(&head, head_size, iter);
286304
if (n < head_size) {
287305
gossip_err("%s: failed to copy head.\n", __func__);
288-
ret = -EFAULT;
289-
goto out;
306+
return -EFAULT;
290307
}
291308

292309
if (head.version < ORANGEFS_MINIMUM_USERSPACE_VERSION) {
@@ -295,31 +312,26 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
295312
__func__,
296313
head.version,
297314
ORANGEFS_MINIMUM_USERSPACE_VERSION);
298-
ret = -EPROTO;
299-
goto out;
315+
return -EPROTO;
300316
}
301317

302318
if (head.magic != ORANGEFS_DEVREQ_MAGIC) {
303319
gossip_err("Error: Device magic number does not match.\n");
304-
ret = -EPROTO;
305-
goto out;
320+
return -EPROTO;
306321
}
307322

308323
op = orangefs_devreq_remove_op(head.tag);
309324
if (!op) {
310325
gossip_err("WARNING: No one's waiting for tag %llu\n",
311326
llu(head.tag));
312-
goto out;
327+
return ret;
313328
}
314329

315-
get_op(op); /* increase ref count. */
316-
317330
n = copy_from_iter(&op->downcall, downcall_size, iter);
318331
if (n != downcall_size) {
319332
gossip_err("%s: failed to copy downcall.\n", __func__);
320-
put_op(op);
321333
ret = -EFAULT;
322-
goto out;
334+
goto Broken;
323335
}
324336

325337
if (op->downcall.status)
@@ -339,9 +351,8 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
339351
downcall_size,
340352
op->downcall.trailer_size,
341353
total);
342-
put_op(op);
343354
ret = -EFAULT;
344-
goto out;
355+
goto Broken;
345356
}
346357

347358
/* Only READDIR operations should have trailers. */
@@ -350,9 +361,8 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
350361
gossip_err("%s: %x operation with trailer.",
351362
__func__,
352363
op->downcall.type);
353-
put_op(op);
354364
ret = -EFAULT;
355-
goto out;
365+
goto Broken;
356366
}
357367

358368
/* READDIR operations should always have trailers. */
@@ -361,9 +371,8 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
361371
gossip_err("%s: %x operation with no trailer.",
362372
__func__,
363373
op->downcall.type);
364-
put_op(op);
365374
ret = -EFAULT;
366-
goto out;
375+
goto Broken;
367376
}
368377

369378
if (op->downcall.type != ORANGEFS_VFS_OP_READDIR)
@@ -374,9 +383,8 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
374383
if (op->downcall.trailer_buf == NULL) {
375384
gossip_err("%s: failed trailer vmalloc.\n",
376385
__func__);
377-
put_op(op);
378386
ret = -ENOMEM;
379-
goto out;
387+
goto Broken;
380388
}
381389
memset(op->downcall.trailer_buf, 0, op->downcall.trailer_size);
382390
n = copy_from_iter(op->downcall.trailer_buf,
@@ -385,9 +393,8 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
385393
if (n != op->downcall.trailer_size) {
386394
gossip_err("%s: failed to copy trailer.\n", __func__);
387395
vfree(op->downcall.trailer_buf);
388-
put_op(op);
389396
ret = -EFAULT;
390-
goto out;
397+
goto Broken;
391398
}
392399

393400
wakeup:
@@ -404,15 +411,15 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
404411
* the buffers are done being used.
405412
*/
406413
if (op->downcall.type == ORANGEFS_VFS_OP_FILE_IO) {
407-
int timed_out = 0;
408414
DEFINE_WAIT(wait_entry);
409415

410416
/*
411417
* tell the vfs op waiting on a waitqueue
412418
* that this op is done
413419
*/
414420
spin_lock(&op->lock);
415-
set_op_state_serviced(op);
421+
if (!op_state_given_up(op))
422+
set_op_state_serviced(op);
416423
spin_unlock(&op->lock);
417424

418425
while (1) {
@@ -435,7 +442,6 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
435442
gossip_debug(GOSSIP_DEV_DEBUG,
436443
"%s: timed out.\n",
437444
__func__);
438-
timed_out = 1;
439445
break;
440446
}
441447
continue;
@@ -450,15 +456,6 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
450456
spin_lock(&op->lock);
451457
finish_wait(&op->io_completion_waitq, &wait_entry);
452458
spin_unlock(&op->lock);
453-
454-
/* NOTE: for I/O operations we handle releasing the op
455-
* object except in the case of timeout. the reason we
456-
* can't free the op in timeout cases is that the op
457-
* service logic in the vfs retries operations using
458-
* the same op ptr, thus it can't be freed.
459-
*/
460-
if (!timed_out)
461-
op_release(op);
462459
} else {
463460
/*
464461
* tell the vfs op waiting on a waitqueue that
@@ -468,11 +465,22 @@ static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
468465
* notification
469466
*/
470467
spin_lock(&op->lock);
471-
set_op_state_serviced(op);
468+
if (!op_state_given_up(op))
469+
set_op_state_serviced(op);
472470
spin_unlock(&op->lock);
473471
}
474472
out:
473+
op_release(op);
475474
return ret;
475+
476+
Broken:
477+
spin_lock(&op->lock);
478+
if (!op_state_given_up(op)) {
479+
op->downcall.status = ret;
480+
set_op_state_serviced(op);
481+
}
482+
spin_unlock(&op->lock);
483+
goto out;
476484
}
477485

478486
/* Returns whether any FS are still pending remounted */

‎fs/orangefs/file.c‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inod
105105
ssize_t ret;
106106

107107
new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
108-
if (!new_op) {
109-
ret = -ENOMEM;
110-
goto out;
111-
}
108+
if (!new_op)
109+
return -ENOMEM;
110+
112111
/* synchronous I/O */
113112
new_op->upcall.req.io.async_vfs_io = ORANGEFS_VFS_SYNC_IO;
114113
new_op->upcall.req.io.readahead_size = readahead_size;
@@ -234,12 +233,9 @@ static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inod
234233

235234
/*
236235
* tell the device file owner waiting on I/O that this read has
237-
* completed and it can return now. in this exact case, on
238-
* wakeup the daemon will free the op, so we *cannot* touch it
239-
* after this.
236+
* completed and it can return now.
240237
*/
241238
wake_up_daemon_for_return(new_op);
242-
new_op = NULL;
243239

244240
out:
245241
if (buffer_index >= 0) {
@@ -249,10 +245,7 @@ static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inod
249245
__func__, handle, buffer_index);
250246
buffer_index = -1;
251247
}
252-
if (new_op) {
253-
op_release(new_op);
254-
new_op = NULL;
255-
}
248+
op_release(new_op);
256249
return ret;
257250
}
258251

‎fs/orangefs/orangefs-cache.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct orangefs_kernel_op_s *op_alloc(__s32 type)
120120
init_waitqueue_head(&new_op->waitq);
121121

122122
init_waitqueue_head(&new_op->io_completion_waitq);
123-
atomic_set(&new_op->ref_count, 0);
123+
atomic_set(&new_op->ref_count, 1);
124124

125125
orangefs_op_initialize(new_op);
126126

@@ -149,14 +149,13 @@ struct orangefs_kernel_op_s *op_alloc(__s32 type)
149149
return new_op;
150150
}
151151

152-
void op_release(struct orangefs_kernel_op_s *orangefs_op)
152+
void __op_release(struct orangefs_kernel_op_s *orangefs_op)
153153
{
154154
if (orangefs_op) {
155155
gossip_debug(GOSSIP_CACHE_DEBUG,
156156
"Releasing OP (%p: %llu)\n",
157157
orangefs_op,
158158
llu(orangefs_op->tag));
159-
orangefs_op_initialize(orangefs_op);
160159
kmem_cache_free(op_cache, orangefs_op);
161160
} else {
162161
gossip_err("NULL pointer in op_release\n");

0 commit comments

Comments
 (0)