From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paul Moore Subject: Re: [RFC PATCH v3] audit: move the tree pruning to a dedicated thread Date: Wed, 28 Jan 2015 16:40:39 -0500 Message-ID: <4828238.7JGSA0NNE9@sifl> References: <1421324870-15070-1-git-send-email-imrep.amz@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mx1.redhat.com (ext-mx12.extmail.prod.ext.phx2.redhat.com [10.5.110.17]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t0SLeiQs002482 for ; Wed, 28 Jan 2015 16:40:44 -0500 Received: from mail-qg0-f46.google.com (mail-qg0-f46.google.com [209.85.192.46]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t0SLegLe017202 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=FAIL) for ; Wed, 28 Jan 2015 16:40:43 -0500 Received: by mail-qg0-f46.google.com with SMTP id i50so19894074qgf.5 for ; Wed, 28 Jan 2015 13:40:42 -0800 (PST) In-Reply-To: <1421324870-15070-1-git-send-email-imrep.amz@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-audit-bounces@redhat.com Errors-To: linux-audit-bounces@redhat.com To: Imre Palik Cc: "Palik, Imre" , linux-audit@redhat.com, Matt Wilson , linux-kernel@vger.kernel.org List-Id: linux-audit@redhat.com On Thursday, January 15, 2015 01:27:50 PM Imre Palik wrote: > From: "Palik, Imre" > > 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 > Cc: Matt Wilson > Signed-off-by: Imre Palik ... > diff --git a/kernel/audit_tree.c b/kernel/audit_tree.c > index 2e0c974..4883b6e 100644 > --- a/kernel/audit_tree.c > +++ b/kernel/audit_tree.c > @@ -37,6 +37,7 @@ struct audit_chunk { > > static LIST_HEAD(tree_list); > static LIST_HEAD(prune_list); > +static struct task_struct *prune_thread; > > /* > * One struct chunk is attached to each inode of interest. > @@ -651,6 +652,55 @@ static int tag_mount(struct vfsmount *mnt, void *arg) > return tag_chunk(mnt->mnt_root->d_inode, arg); > } > > +/* > + * That gets run when evict_chunk() ends up needing to kill audit_tree. > + * Runs from a separate thread. > + */ > +static int prune_tree_thread(void *unused) > +{ > + for (;;) { > + set_current_state(TASK_INTERRUPTIBLE); > + if (list_empty(&prune_list)) > + schedule(); > + __set_current_state(TASK_RUNNING); > + > + mutex_lock(&audit_cmd_mutex); > + mutex_lock(&audit_filter_mutex); > + > + while (!list_empty(&prune_list)) { > + struct audit_tree *victim; > + > + victim = list_entry(prune_list.next, > + struct audit_tree, list); > + list_del_init(&victim->list); > + > + mutex_unlock(&audit_filter_mutex); > + > + prune_one(victim); > + > + mutex_lock(&audit_filter_mutex); > + } > + > + mutex_unlock(&audit_filter_mutex); > + mutex_unlock(&audit_cmd_mutex); > + } > + return 0; > +} > + > +static int launch_prune_thread(void) > +{ > + prune_thread = kthread_create(prune_tree_thread, NULL, > + "audit_prune_tree"); > + if (IS_ERR(prune_thread)) { > + pr_err("cannot start thread audit_prune_tree"); > + prune_thread = NULL; > + return -ENOMEM; > + } else { > + wake_up_process(prune_thread); > + return 0; > + } > +} Before trying to create a new instance of prune_tree_thread, should we check to see if one exists? I know you have a check for this in audit_add_tree_rule() but I would rather it be in the function above to help prevent accidental misuse in the future. Also, how about we rename this to audit_launch_prune() so are naming is more consistent, see audit_schedule_prune()? -- paul moore www.paul-moore.com