Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755343AbbLABLo (ORCPT ); Mon, 30 Nov 2015 20:11:44 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47668 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752636AbbLABLn (ORCPT ); Mon, 30 Nov 2015 20:11:43 -0500 Date: Mon, 30 Nov 2015 19:11:39 -0600 From: Josh Poimboeuf To: Li Bin Cc: sjenning@redhat.com, jikos@kernel.org, vojtech@suse.com, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, guohanjun@huawei.com, dingtianhong@huawei.com, xiexiuqi@huawei.com, zhouchengming1@huawei.com, Miroslav Benes Subject: Re: [PATCH] livepatch: fix race between enabled_store() and klp_unregister_patch() Message-ID: <20151201011139.GB12513@treble.redhat.com> References: <1448855677-8392-1-git-send-email-huawei.libin@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1448855677-8392-1-git-send-email-huawei.libin@huawei.com> User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1813 Lines: 46 On Mon, Nov 30, 2015 at 11:54:37AM +0800, Li Bin wrote: > There is a potential race as following: > > CPU0 | CPU1 > -----------------------------|----------------------------------- > enabled_store() | klp_unregister_patch() > | |-mutex_lock(&klp_mutex); > |-mutex_lock(&klp_mutex); | |-klp_free_patch(); > | |-mutex_unlock(&klp_mutex); > |-[process the patch's state]| > |-mutex_unlock(&klp_mutex) | > > Fix this race condition by adding klp_is_patch_registered() check in > enabled_store() after get the lock klp_mutex. I'm thinking this race isn't possible, and that instead it would deadlock. When I try to recreate something similar by putting a delay in enabled_store(), klp_free_patch() just sleeps on its call to kobject_put() until enabled_store() returns. The unregister stack looks like: [] __kernfs_remove+0x1fb/0x380 [] kernfs_remove+0x23/0x40 [] sysfs_remove_dir+0x51/0x80 [] kobject_del+0x18/0x50 [] kobject_release+0x5a/0x190 [] kobject_put+0x27/0x50 [] klp_unregister_patch+0x71/0x80 It seems to be waiting on a lock which is held by the kernfs code which called enabled_store(). So when enabled_store() tries to get the klp_mutex, it deadlocks. Miroslav and I previously discussed a few options to fix this: https://lkml.kernel.org/r/alpine.LNX.2.00.1502171728520.29490@pobox.suse.cz -- Josh -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/