Skip to content

Commit f1aaf26

Browse files
Imre Palikpcmoore
authored andcommitted
audit: move the tree pruning to a dedicated thread
When file auditing is enabled, during a low memory situation, a memory allocation with __GFP_FS can lead to pruning the inode cache. Which can, in turn lead to audit_tree_freeing_mark() being called. This can call audit_schedule_prune(), that tries to fork a pruning thread, and waits until the thread is created. But forking needs memory, and the memory allocations there are done with __GFP_FS. So we are waiting merrily for some __GFP_FS memory allocations to complete, while holding some filesystem locks. This can take a while ... This patch creates a single thread for pruning the tree from audit_add_tree_rule(), and thus avoids the deadlock that the on-demand thread creation can cause. Reported-by: Matt Wilson <msw@amazon.com> Cc: Matt Wilson <msw@amazon.com> Signed-off-by: Imre Palik <imrep@amazon.de> Reviewed-by: Richard Guy Briggs <rgb@redhat.com> Signed-off-by: Paul Moore <pmoore@redhat.com>
1 parent 2fded7f commit f1aaf26

1 file changed

Lines changed: 60 additions & 28 deletions

File tree

‎kernel/audit_tree.c‎

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct audit_chunk {
3737

3838
static LIST_HEAD(tree_list);
3939
static LIST_HEAD(prune_list);
40+
static struct task_struct *prune_thread;
4041

4142
/*
4243
* One struct chunk is attached to each inode of interest.
@@ -651,6 +652,57 @@ static int tag_mount(struct vfsmount *mnt, void *arg)
651652
return tag_chunk(mnt->mnt_root->d_inode, arg);
652653
}
653654

655+
/*
656+
* That gets run when evict_chunk() ends up needing to kill audit_tree.
657+
* Runs from a separate thread.
658+
*/
659+
static int prune_tree_thread(void *unused)
660+
{
661+
for (;;) {
662+
set_current_state(TASK_INTERRUPTIBLE);
663+
if (list_empty(&prune_list))
664+
schedule();
665+
__set_current_state(TASK_RUNNING);
666+
667+
mutex_lock(&audit_cmd_mutex);
668+
mutex_lock(&audit_filter_mutex);
669+
670+
while (!list_empty(&prune_list)) {
671+
struct audit_tree *victim;
672+
673+
victim = list_entry(prune_list.next,
674+
struct audit_tree, list);
675+
list_del_init(&victim->list);
676+
677+
mutex_unlock(&audit_filter_mutex);
678+
679+
prune_one(victim);
680+
681+
mutex_lock(&audit_filter_mutex);
682+
}
683+
684+
mutex_unlock(&audit_filter_mutex);
685+
mutex_unlock(&audit_cmd_mutex);
686+
}
687+
return 0;
688+
}
689+
690+
static int audit_launch_prune(void)
691+
{
692+
if (prune_thread)
693+
return 0;
694+
prune_thread = kthread_create(prune_tree_thread, NULL,
695+
"audit_prune_tree");
696+
if (IS_ERR(prune_thread)) {
697+
pr_err("cannot start thread audit_prune_tree");
698+
prune_thread = NULL;
699+
return -ENOMEM;
700+
} else {
701+
wake_up_process(prune_thread);
702+
return 0;
703+
}
704+
}
705+
654706
/* called with audit_filter_mutex */
655707
int audit_add_tree_rule(struct audit_krule *rule)
656708
{
@@ -674,6 +726,12 @@ int audit_add_tree_rule(struct audit_krule *rule)
674726
/* do not set rule->tree yet */
675727
mutex_unlock(&audit_filter_mutex);
676728

729+
if (unlikely(!prune_thread)) {
730+
err = audit_launch_prune();
731+
if (err)
732+
goto Err;
733+
}
734+
677735
err = kern_path(tree->pathname, 0, &path);
678736
if (err)
679737
goto Err;
@@ -811,36 +869,10 @@ int audit_tag_tree(char *old, char *new)
811869
return failed;
812870
}
813871

814-
/*
815-
* That gets run when evict_chunk() ends up needing to kill audit_tree.
816-
* Runs from a separate thread.
817-
*/
818-
static int prune_tree_thread(void *unused)
819-
{
820-
mutex_lock(&audit_cmd_mutex);
821-
mutex_lock(&audit_filter_mutex);
822-
823-
while (!list_empty(&prune_list)) {
824-
struct audit_tree *victim;
825-
826-
victim = list_entry(prune_list.next, struct audit_tree, list);
827-
list_del_init(&victim->list);
828-
829-
mutex_unlock(&audit_filter_mutex);
830-
831-
prune_one(victim);
832-
833-
mutex_lock(&audit_filter_mutex);
834-
}
835-
836-
mutex_unlock(&audit_filter_mutex);
837-
mutex_unlock(&audit_cmd_mutex);
838-
return 0;
839-
}
840872

841873
static void audit_schedule_prune(void)
842874
{
843-
kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
875+
wake_up_process(prune_thread);
844876
}
845877

846878
/*
@@ -907,9 +939,9 @@ static void evict_chunk(struct audit_chunk *chunk)
907939
for (n = 0; n < chunk->count; n++)
908940
list_del_init(&chunk->owners[n].list);
909941
spin_unlock(&hash_lock);
942+
mutex_unlock(&audit_filter_mutex);
910943
if (need_prune)
911944
audit_schedule_prune();
912-
mutex_unlock(&audit_filter_mutex);
913945
}
914946

915947
static int audit_tree_handle_event(struct fsnotify_group *group,

0 commit comments

Comments
 (0)