Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752422AbbHaIuh (ORCPT ); Mon, 31 Aug 2015 04:50:37 -0400 Received: from collab.rosalab.ru ([195.19.76.181]:58927 "EHLO collab.rosalab.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750922AbbHaIuf (ORCPT ); Mon, 31 Aug 2015 04:50:35 -0400 Subject: Re: [PATCH 2/2] usbnet: Fix a race between usbnet_stop() and the BH To: =?UTF-8?Q?Bj=c3=b8rn_Mork?= References: <55AD3A41.2040100@rosalab.ru> <1440447223-15945-1-git-send-email-eugene.shatokhin@rosalab.ru> <1440447223-15945-3-git-send-email-eugene.shatokhin@rosalab.ru> <87k2sk9zaf.fsf@nemi.mork.no> <55E01750.4010202@rosalab.ru> <87mvxbzta9.fsf@nemi.mork.no> <55E03B28.6050305@rosalab.ru> <87io7vzzdl.fsf@nemi.mork.no> Cc: Oliver Neukum , David Miller , netdev@vger.kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org From: Eugene Shatokhin Organization: ROSA Message-ID: <55E41557.1060803@rosalab.ru> Date: Mon, 31 Aug 2015 11:50:31 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1 MIME-Version: 1.0 In-Reply-To: <87io7vzzdl.fsf@nemi.mork.no> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2140 Lines: 56 31.08.2015 10:32, Bjørn Mork пишет: > Eugene Shatokhin writes: >> 28.08.2015 11:55, Bjørn Mork пишет: >> >>> I guess you are right. At least I cannot prove that you are not :) >>> >>> There is a bit too much complexity involved here for me... >> >> :-) >> >> Yes, it is quite complex. >> >> I admit, it was easier for me to find the races in usbnet (the tools >> like KernelStrider and RaceHound do the dirty work) than to analyze >> their consequences. The latter often requires some time and effort, >> and so it did this time. >> >> Well, any objections to this patch? > > No objections from me. > > But I would have liked it much better if the code became simpler instead > of more complex. Me too, but I can see no other way here. The code is simpler without locking, indeed, but locking is needed to prevent the problems described earlier. One needs to make sure that checking if txq or rxq is empty in usbnet_terminate_urbs() cannot get inbetween of processing of these queues and dev->done in defer_bh(). So 'list' and 'dev->done' must be updated under a common lock in defer_bh(). list->lock is an obvious candidate for this. For the same reason, skb_queue_empty(q) must be called under q->lock. So the code takes it, calls skb_queue_empty(q) and then releases it to wait a little. Rinse and repeat. The last complex piece is that spin_lock_nested() in defer_bh. It is safe to take both list->lock and dev->done.lock there (defer_bh can only be called for list = dev->rxq or dev->txq but not for dev->done). For lockdep, however, this is suspicious because '*list' and 'dev->done' are of the same type so the lock class is the same. So it complained. To tell lockdep it is OK to use such locking scheme in this particular case, the recommended pattern was used: spin_lock_nested with SINGLE_DEPTH_NESTING. Regards, Eugene -- 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/