Skip to content

Commit c22fcde

Browse files
jankarapcmoore
authored andcommitted
audit: Drop all unused chunk nodes during deletion
When deleting chunk from a tree, drop all unused nodes in a chunk instead of just the one used by the tree. This gets rid of possibly lingering unused nodes (created due to fallback path in untag_chunk()) and also removes some special cases and will allow us to simplify locking in untag_chunk(). Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Richard Guy Briggs <rgb@redhat.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 49a4ee7 commit c22fcde

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

‎kernel/audit_tree.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ static struct audit_chunk *find_chunk(struct node *p)
277277
return container_of(p, struct audit_chunk, owners[0]);
278278
}
279279

280-
static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old,
281-
struct node *skip)
280+
static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old)
282281
{
283282
struct audit_tree *owner;
284283
int i, j;
@@ -288,7 +287,7 @@ static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old,
288287
list_for_each_entry(owner, &new->trees, same_root)
289288
owner->root = new;
290289
for (i = j = 0; j < old->count; i++, j++) {
291-
if (&old->owners[j] == skip) {
290+
if (!old->owners[j].owner) {
292291
i--;
293292
continue;
294293
}
@@ -322,20 +321,28 @@ static void remove_chunk_node(struct audit_chunk *chunk, struct node *p)
322321
put_tree(owner);
323322
}
324323

324+
static int chunk_count_trees(struct audit_chunk *chunk)
325+
{
326+
int i;
327+
int ret = 0;
328+
329+
for (i = 0; i < chunk->count; i++)
330+
if (chunk->owners[i].owner)
331+
ret++;
332+
return ret;
333+
}
334+
325335
static void untag_chunk(struct node *p)
326336
{
327337
struct audit_chunk *chunk = find_chunk(p);
328338
struct fsnotify_mark *entry = chunk->mark;
329339
struct audit_chunk *new = NULL;
330-
int size = chunk->count - 1;
340+
int size;
331341

332342
remove_chunk_node(chunk, p);
333343
fsnotify_get_mark(entry);
334344
spin_unlock(&hash_lock);
335345

336-
if (size)
337-
new = alloc_chunk(size);
338-
339346
mutex_lock(&entry->group->mark_mutex);
340347
/*
341348
* mark_mutex protects mark from getting detached and thus also from
@@ -348,6 +355,7 @@ static void untag_chunk(struct node *p)
348355
goto out;
349356
}
350357

358+
size = chunk_count_trees(chunk);
351359
if (!size) {
352360
chunk->dead = 1;
353361
spin_lock(&hash_lock);
@@ -360,6 +368,7 @@ static void untag_chunk(struct node *p)
360368
goto out;
361369
}
362370

371+
new = alloc_chunk(size);
363372
if (!new)
364373
goto out_mutex;
365374

@@ -375,7 +384,7 @@ static void untag_chunk(struct node *p)
375384
* This has to go last when updating chunk as once replace_chunk() is
376385
* called, new RCU readers can see the new chunk.
377386
*/
378-
replace_chunk(new, chunk, p);
387+
replace_chunk(new, chunk);
379388
spin_unlock(&hash_lock);
380389
fsnotify_detach_mark(entry);
381390
mutex_unlock(&entry->group->mark_mutex);
@@ -520,7 +529,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
520529
* This has to go last when updating chunk as once replace_chunk() is
521530
* called, new RCU readers can see the new chunk.
522531
*/
523-
replace_chunk(chunk, old, NULL);
532+
replace_chunk(chunk, old);
524533
spin_unlock(&hash_lock);
525534
fsnotify_detach_mark(old_entry);
526535
mutex_unlock(&audit_tree_group->mark_mutex);

0 commit comments

Comments
 (0)