Skip to content

Commit 88c853c

Browse files
committed
afs: Fix cell refcounting by splitting the usage counter
Management of the lifetime of afs_cell struct has some problems due to the usage counter being used to determine whether objects of that type are in use in addition to whether anyone might be interested in the structure. This is made trickier by cell objects being cached for a period of time in case they're quickly reused as they hold the result of a setup process that may be slow (DNS lookups, AFS RPC ops). Problems include the cached root volume from alias resolution pinning its parent cell record, rmmod occasionally hanging and occasionally producing assertion failures. Fix this by splitting the count of active users from the struct reference count. Things then work as follows: (1) The cell cache keeps +1 on the cell's activity count and this has to be dropped before the cell can be removed. afs_manage_cell() tries to exchange the 1 to a 0 with the cells_lock write-locked, and if successful, the record is removed from the net->cells. (2) One struct ref is 'owned' by the activity count. That is put when the active count is reduced to 0 (final_destruction label). (3) A ref can be held on a cell whilst it is queued for management on a work queue without confusing the active count. afs_queue_cell() is added to wrap this. (4) The queue's ref is dropped at the end of the management. This is split out into a separate function, afs_manage_cell_work(). (5) The root volume record is put after a cell is removed (at the final_destruction label) rather then in the RCU destruction routine. (6) Volumes hold struct refs, but aren't active users. (7) Both counts are displayed in /proc/net/afs/cells. There are some management function changes: (*) afs_put_cell() now just decrements the refcount and triggers the RCU destruction if it becomes 0. It no longer sets a timer to have the manager do this. (*) afs_use_cell() and afs_unuse_cell() are added to increase and decrease the active count. afs_unuse_cell() sets the management timer. (*) afs_queue_cell() is added to queue a cell with approprate refs. There are also some other fixes: (*) Don't let /proc/net/afs/cells access a cell's vllist if it's NULL. (*) Make sure that candidate cells in lookups are properly destroyed rather than being simply kfree'd. This ensures the bits it points to are destroyed also. (*) afs_dec_cells_outstanding() is now called in cell destruction rather than at "final_destruction". This ensures that cell->net is still valid to the end of the destructor. (*) As a consequence of the previous two changes, move the increment of net->cells_outstanding that was at the point of insertion into the tree to the allocation routine to correctly balance things. Fixes: 989782d ("afs: Overhaul cell database management") Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 92e3cc9 commit 88c853c

9 files changed

Lines changed: 136 additions & 76 deletions

File tree

fs/afs/cell.c

Lines changed: 103 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static unsigned __read_mostly afs_cell_gc_delay = 10;
1919
static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
2020
static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
2121

22-
static void afs_manage_cell(struct work_struct *);
22+
static void afs_manage_cell_work(struct work_struct *);
2323

2424
static void afs_dec_cells_outstanding(struct afs_net *net)
2525
{
@@ -62,8 +62,7 @@ static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
6262
cell = net->ws_cell;
6363
if (!cell)
6464
return ERR_PTR(-EDESTADDRREQ);
65-
afs_get_cell(cell);
66-
return cell;
65+
goto found;
6766
}
6867

6968
p = net->cells.rb_node;
@@ -85,12 +84,12 @@ static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
8584
return ERR_PTR(-ENOENT);
8685

8786
found:
88-
if (!atomic_inc_not_zero(&cell->usage))
89-
return ERR_PTR(-ENOENT);
90-
91-
return cell;
87+
return afs_use_cell(cell);
9288
}
9389

90+
/*
91+
* Look up and get an activation reference on a cell record.
92+
*/
9493
struct afs_cell *afs_find_cell(struct afs_net *net,
9594
const char *name, unsigned int namesz)
9695
{
@@ -153,8 +152,9 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net,
153152
cell->name[i] = tolower(name[i]);
154153
cell->name[i] = 0;
155154

156-
atomic_set(&cell->usage, 2);
157-
INIT_WORK(&cell->manager, afs_manage_cell);
155+
atomic_set(&cell->ref, 1);
156+
atomic_set(&cell->active, 0);
157+
INIT_WORK(&cell->manager, afs_manage_cell_work);
158158
cell->volumes = RB_ROOT;
159159
INIT_HLIST_HEAD(&cell->proc_volumes);
160160
seqlock_init(&cell->volume_lock);
@@ -193,6 +193,7 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net,
193193
cell->dns_source = vllist->source;
194194
cell->dns_status = vllist->status;
195195
smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
196+
atomic_inc(&net->cells_outstanding);
196197

197198
_leave(" = %p", cell);
198199
return cell;
@@ -275,12 +276,12 @@ struct afs_cell *afs_lookup_cell(struct afs_net *net,
275276

276277
cell = candidate;
277278
candidate = NULL;
279+
atomic_set(&cell->active, 2);
278280
rb_link_node_rcu(&cell->net_node, parent, pp);
279281
rb_insert_color(&cell->net_node, &net->cells);
280-
atomic_inc(&net->cells_outstanding);
281282
up_write(&net->cells_lock);
282283

283-
queue_work(afs_wq, &cell->manager);
284+
afs_queue_cell(cell);
284285

285286
wait_for_cell:
286287
_debug("wait_for_cell");
@@ -305,16 +306,17 @@ struct afs_cell *afs_lookup_cell(struct afs_net *net,
305306
if (excl) {
306307
ret = -EEXIST;
307308
} else {
308-
afs_get_cell(cursor);
309+
afs_use_cell(cursor);
309310
ret = 0;
310311
}
311312
up_write(&net->cells_lock);
312-
kfree(candidate);
313+
if (candidate)
314+
afs_put_cell(candidate);
313315
if (ret == 0)
314316
goto wait_for_cell;
315317
goto error_noput;
316318
error:
317-
afs_put_cell(net, cell);
319+
afs_unuse_cell(net, cell);
318320
error_noput:
319321
_leave(" = %d [error]", ret);
320322
return ERR_PTR(ret);
@@ -359,15 +361,15 @@ int afs_cell_init(struct afs_net *net, const char *rootcell)
359361
}
360362

361363
if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
362-
afs_get_cell(new_root);
364+
afs_use_cell(new_root);
363365

364366
/* install the new cell */
365367
down_write(&net->cells_lock);
366368
old_root = net->ws_cell;
367369
net->ws_cell = new_root;
368370
up_write(&net->cells_lock);
369371

370-
afs_put_cell(net, old_root);
372+
afs_unuse_cell(net, old_root);
371373
_leave(" = 0");
372374
return 0;
373375
}
@@ -473,18 +475,21 @@ static int afs_update_cell(struct afs_cell *cell)
473475
static void afs_cell_destroy(struct rcu_head *rcu)
474476
{
475477
struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
478+
struct afs_net *net = cell->net;
479+
int u;
476480

477481
_enter("%p{%s}", cell, cell->name);
478482

479-
ASSERTCMP(atomic_read(&cell->usage), ==, 0);
483+
u = atomic_read(&cell->ref);
484+
ASSERTCMP(u, ==, 0);
480485

481-
afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
482-
afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
483-
afs_put_cell(cell->net, cell->alias_of);
486+
afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
487+
afs_unuse_cell(net, cell->alias_of);
484488
key_put(cell->anonymous_key);
485489
kfree(cell->name);
486490
kfree(cell);
487491

492+
afs_dec_cells_outstanding(net);
488493
_leave(" [destroyed]");
489494
}
490495

@@ -519,16 +524,50 @@ void afs_cells_timer(struct timer_list *timer)
519524
*/
520525
struct afs_cell *afs_get_cell(struct afs_cell *cell)
521526
{
522-
atomic_inc(&cell->usage);
527+
if (atomic_read(&cell->ref) <= 0)
528+
BUG();
529+
530+
atomic_inc(&cell->ref);
523531
return cell;
524532
}
525533

526534
/*
527535
* Drop a reference on a cell record.
528536
*/
529-
void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
537+
void afs_put_cell(struct afs_cell *cell)
538+
{
539+
if (cell) {
540+
unsigned int u, a;
541+
542+
u = atomic_dec_return(&cell->ref);
543+
if (u == 0) {
544+
a = atomic_read(&cell->active);
545+
WARN(a != 0, "Cell active count %u > 0\n", a);
546+
call_rcu(&cell->rcu, afs_cell_destroy);
547+
}
548+
}
549+
}
550+
551+
/*
552+
* Note a cell becoming more active.
553+
*/
554+
struct afs_cell *afs_use_cell(struct afs_cell *cell)
555+
{
556+
if (atomic_read(&cell->ref) <= 0)
557+
BUG();
558+
559+
atomic_inc(&cell->active);
560+
return cell;
561+
}
562+
563+
/*
564+
* Record a cell becoming less active. When the active counter reaches 1, it
565+
* is scheduled for destruction, but may get reactivated.
566+
*/
567+
void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell)
530568
{
531569
time64_t now, expire_delay;
570+
int a;
532571

533572
if (!cell)
534573
return;
@@ -541,11 +580,21 @@ void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
541580
if (cell->vl_servers->nr_servers)
542581
expire_delay = afs_cell_gc_delay;
543582

544-
if (atomic_dec_return(&cell->usage) > 1)
545-
return;
583+
a = atomic_dec_return(&cell->active);
584+
WARN_ON(a == 0);
585+
if (a == 1)
586+
/* 'cell' may now be garbage collected. */
587+
afs_set_cell_timer(net, expire_delay);
588+
}
546589

547-
/* 'cell' may now be garbage collected. */
548-
afs_set_cell_timer(net, expire_delay);
590+
/*
591+
* Queue a cell for management, giving the workqueue a ref to hold.
592+
*/
593+
void afs_queue_cell(struct afs_cell *cell)
594+
{
595+
afs_get_cell(cell);
596+
if (!queue_work(afs_wq, &cell->manager))
597+
afs_put_cell(cell);
549598
}
550599

551600
/*
@@ -645,12 +694,11 @@ static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
645694
* Manage a cell record, initialising and destroying it, maintaining its DNS
646695
* records.
647696
*/
648-
static void afs_manage_cell(struct work_struct *work)
697+
static void afs_manage_cell(struct afs_cell *cell)
649698
{
650-
struct afs_cell *cell = container_of(work, struct afs_cell, manager);
651699
struct afs_net *net = cell->net;
652700
bool deleted;
653-
int ret, usage;
701+
int ret, active;
654702

655703
_enter("%s", cell->name);
656704

@@ -660,10 +708,11 @@ static void afs_manage_cell(struct work_struct *work)
660708
case AFS_CELL_INACTIVE:
661709
case AFS_CELL_FAILED:
662710
down_write(&net->cells_lock);
663-
usage = 1;
664-
deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
665-
if (deleted)
711+
active = 1;
712+
deleted = atomic_try_cmpxchg_relaxed(&cell->active, &active, 0);
713+
if (deleted) {
666714
rb_erase(&cell->net_node, &net->cells);
715+
}
667716
up_write(&net->cells_lock);
668717
if (deleted)
669718
goto final_destruction;
@@ -688,7 +737,7 @@ static void afs_manage_cell(struct work_struct *work)
688737
goto again;
689738

690739
case AFS_CELL_ACTIVE:
691-
if (atomic_read(&cell->usage) > 1) {
740+
if (atomic_read(&cell->active) > 1) {
692741
if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
693742
ret = afs_update_cell(cell);
694743
if (ret < 0)
@@ -701,7 +750,7 @@ static void afs_manage_cell(struct work_struct *work)
701750
goto again;
702751

703752
case AFS_CELL_DEACTIVATING:
704-
if (atomic_read(&cell->usage) > 1)
753+
if (atomic_read(&cell->active) > 1)
705754
goto reverse_deactivation;
706755
afs_deactivate_cell(net, cell);
707756
smp_store_release(&cell->state, AFS_CELL_INACTIVE);
@@ -733,9 +782,18 @@ static void afs_manage_cell(struct work_struct *work)
733782
return;
734783

735784
final_destruction:
736-
call_rcu(&cell->rcu, afs_cell_destroy);
737-
afs_dec_cells_outstanding(net);
738-
_leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
785+
/* The root volume is pinning the cell */
786+
afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
787+
cell->root_volume = NULL;
788+
afs_put_cell(cell);
789+
}
790+
791+
static void afs_manage_cell_work(struct work_struct *work)
792+
{
793+
struct afs_cell *cell = container_of(work, struct afs_cell, manager);
794+
795+
afs_manage_cell(cell);
796+
afs_put_cell(cell);
739797
}
740798

741799
/*
@@ -769,21 +827,20 @@ void afs_manage_cells(struct work_struct *work)
769827
for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
770828
struct afs_cell *cell =
771829
rb_entry(cursor, struct afs_cell, net_node);
772-
unsigned usage;
830+
unsigned active;
773831
bool sched_cell = false;
774832

775-
usage = atomic_read(&cell->usage);
776-
_debug("manage %s %u", cell->name, usage);
833+
active = atomic_read(&cell->active);
834+
_debug("manage %s %u %u", cell->name, atomic_read(&cell->ref), active);
777835

778-
ASSERTCMP(usage, >=, 1);
836+
ASSERTCMP(active, >=, 1);
779837

780838
if (purging) {
781839
if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
782-
usage = atomic_dec_return(&cell->usage);
783-
ASSERTCMP(usage, ==, 1);
840+
atomic_dec(&cell->active);
784841
}
785842

786-
if (usage == 1) {
843+
if (active == 1) {
787844
struct afs_vlserver_list *vllist;
788845
time64_t expire_at = cell->last_inactive;
789846

@@ -806,7 +863,7 @@ void afs_manage_cells(struct work_struct *work)
806863
}
807864

808865
if (sched_cell)
809-
queue_work(afs_wq, &cell->manager);
866+
afs_queue_cell(cell);
810867
}
811868

812869
up_read(&net->cells_lock);
@@ -843,7 +900,7 @@ void afs_cell_purge(struct afs_net *net)
843900
ws = net->ws_cell;
844901
net->ws_cell = NULL;
845902
up_write(&net->cells_lock);
846-
afs_put_cell(net, ws);
903+
afs_unuse_cell(net, ws);
847904

848905
_debug("del timer");
849906
if (del_timer_sync(&net->cells_timer))

fs/afs/dynroot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int afs_probe_cell_name(struct dentry *dentry)
125125

126126
cell = afs_find_cell(net, name, len);
127127
if (!IS_ERR(cell)) {
128-
afs_put_cell(net, cell);
128+
afs_unuse_cell(net, cell);
129129
return 0;
130130
}
131131

fs/afs/internal.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ struct afs_cell {
363363
#endif
364364
time64_t dns_expiry; /* Time AFSDB/SRV record expires */
365365
time64_t last_inactive; /* Time of last drop of usage count */
366-
atomic_t usage;
366+
atomic_t ref; /* Struct refcount */
367+
atomic_t active; /* Active usage counter */
367368
unsigned long flags;
368369
#define AFS_CELL_FL_NO_GC 0 /* The cell was added manually, don't auto-gc */
369370
#define AFS_CELL_FL_DO_LOOKUP 1 /* DNS lookup requested */
@@ -920,8 +921,11 @@ extern int afs_cell_init(struct afs_net *, const char *);
920921
extern struct afs_cell *afs_find_cell(struct afs_net *, const char *, unsigned);
921922
extern struct afs_cell *afs_lookup_cell(struct afs_net *, const char *, unsigned,
922923
const char *, bool);
924+
extern struct afs_cell *afs_use_cell(struct afs_cell *);
925+
extern void afs_unuse_cell(struct afs_net *, struct afs_cell *);
923926
extern struct afs_cell *afs_get_cell(struct afs_cell *);
924-
extern void afs_put_cell(struct afs_net *, struct afs_cell *);
927+
extern void afs_put_cell(struct afs_cell *);
928+
extern void afs_queue_cell(struct afs_cell *);
925929
extern void afs_manage_cells(struct work_struct *);
926930
extern void afs_cells_timer(struct timer_list *);
927931
extern void __net_exit afs_cell_purge(struct afs_net *);

fs/afs/mntpt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
8888
ctx->force = true;
8989
}
9090
if (ctx->cell) {
91-
afs_put_cell(ctx->net, ctx->cell);
91+
afs_unuse_cell(ctx->net, ctx->cell);
9292
ctx->cell = NULL;
9393
}
9494
if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
@@ -124,7 +124,7 @@ static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
124124
char *buf;
125125

126126
if (src_as->cell)
127-
ctx->cell = afs_get_cell(src_as->cell);
127+
ctx->cell = afs_use_cell(src_as->cell);
128128

129129
if (size < 2 || size > PAGE_SIZE - 1)
130130
return -EINVAL;

0 commit comments

Comments
 (0)