Skip to content

Commit 1bcab12

Browse files
committed
afs: Fix permit refcounting
Fix four refcount bugs in afs_cache_permit(): (1) When checking the result of the kzalloc(), we can't just return, but must put 'permits'. (2) We shouldn't put permits immediately after hashing a new permit as we need to keep the pointer stable so that we can check to see if vnode->permit_cache has changed before we decide whether to assign to it. (3) 'permits' is being put twice. (4) We need to put either the replacement or the thing replaced after the assignment to vnode->permit_cache. Without this, lots of the following are seen: Kernel BUG at ffffffffa039857b [verbose debug info unavailable] ------------[ cut here ]------------ Kernel BUG at ffffffffa039858a [verbose debug info unavailable] ------------[ cut here ]------------ The addresses are in the .text..refcount section of the kafs.ko module. Following the relocation records for the __ex_table section shows one to be due to the decrement in afs_put_permits() and the other to be key_get() in afs_cache_permit(). Occasionally, the following is seen: refcount_t overflow at afs_cache_permit+0x57d/0x5c0 [kafs] in cc1[562], uid/euid: 0/0 WARNING: CPU: 0 PID: 562 at kernel/panic.c:657 refcount_error_report+0x9c/0xac ... Reported-by: Marc Dionne <marc.dionne@auristor.com> Signed-off-by: David Howells <dhowells@redhat.com> Tested-by: Marc Dionne <marc.dionne@auristor.com>
1 parent df8ba95 commit 1bcab12

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

fs/afs/security.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void afs_hash_permits(struct afs_permits *permits)
120120
void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
121121
unsigned int cb_break)
122122
{
123-
struct afs_permits *permits, *xpermits, *replacement, *new = NULL;
123+
struct afs_permits *permits, *xpermits, *replacement, *zap, *new = NULL;
124124
afs_access_t caller_access = READ_ONCE(vnode->status.caller_access);
125125
size_t size = 0;
126126
bool changed = false;
@@ -204,7 +204,7 @@ void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
204204
new = kzalloc(sizeof(struct afs_permits) +
205205
sizeof(struct afs_permit) * size, GFP_NOFS);
206206
if (!new)
207-
return;
207+
goto out_put;
208208

209209
refcount_set(&new->usage, 1);
210210
new->nr_permits = size;
@@ -229,8 +229,6 @@ void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
229229

230230
afs_hash_permits(new);
231231

232-
afs_put_permits(permits);
233-
234232
/* Now see if the permit list we want is actually already available */
235233
spin_lock(&afs_permits_lock);
236234

@@ -262,11 +260,15 @@ void afs_cache_permit(struct afs_vnode *vnode, struct key *key,
262260
kfree(new);
263261

264262
spin_lock(&vnode->lock);
265-
if (cb_break != (vnode->cb_break + vnode->cb_interest->server->cb_s_break) ||
266-
permits != rcu_access_pointer(vnode->permit_cache))
267-
goto someone_else_changed_it_unlock;
268-
rcu_assign_pointer(vnode->permit_cache, replacement);
263+
zap = rcu_access_pointer(vnode->permit_cache);
264+
if (cb_break == (vnode->cb_break + vnode->cb_interest->server->cb_s_break) &&
265+
zap == permits)
266+
rcu_assign_pointer(vnode->permit_cache, replacement);
267+
else
268+
zap = replacement;
269269
spin_unlock(&vnode->lock);
270+
afs_put_permits(zap);
271+
out_put:
270272
afs_put_permits(permits);
271273
return;
272274

0 commit comments

Comments
 (0)