Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp7556103rwn; Wed, 14 Sep 2022 00:03:41 -0700 (PDT) X-Google-Smtp-Source: AA6agR54sAb7TMCq3Z/4CUazxe+t0lyOyiUzsubIMHdXGuWGnuyYqubltQa9RyN+7Z/B1lTxwt/d X-Received: by 2002:a17:907:9484:b0:738:6f9f:6032 with SMTP id dm4-20020a170907948400b007386f9f6032mr24729301ejc.602.1663139021449; Wed, 14 Sep 2022 00:03:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663139021; cv=none; d=google.com; s=arc-20160816; b=ckz0Q/R/1QTPzimS0Ov40/6pbPpi8w+jZav0haWaW1rZeOhQMJwFMf3ar7pL4TdctU fmPTLjaRVEcdD9aVd8VP6wQ6VFS1aHyrS/Fo8ig63F1AAViQJxHo+5wq5Q0wqL+VnIkL vSoRp0lIrZkHeyKcvgYw5fhEn0S+x2h/WQMExsmSGAaz9tR0g9TvTQq1NpgUS3LNdPoe suHKUtAb1Osi1LJ9BChZXfWINQrtbHTcmIDfCmpK2G/JtbviVwv+S9QE0OfiDZkHZDLt +Yg9p/Hf+Qlmg2RlaVT7xfqmUlW2GPEdUM39N7WFTsObXOLg+0bATpXkZzSpFaisg1W4 gy9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=yFUaYl7Suw/FQbUNeSnQBje35szKVHV5xeq3kuXZVyQ=; b=JptKBhoZwpC56Ff+iwYQjVA+8BziQmPmGItUIXmywUttzso6EvW5HJAjCWxeuCWuW+ Hkw3VZgh2007jGLITVuFaRdCLPDdvjKEkkdBKAe9rhpo/unwYsxAlJNm6zCI8tBJsIAo 8v+QdVG14M4poR9P1F6v3FoJ/nvkSfwCcXXgFu0GtZ1fYujDjsX9w8XLEU+mfxa/AjWr B3wLjpg65FsXBcY5c393Wf07DgeThOZKlc7PGUEEz+z0wtki+Hh9mGwv5V+ckYZqb/3r 7qc1XoYXUhVHmXx3yks1P7xATbVhZLTx8Uh/JPqec6YnWVAXK3G+lxgsS9XSFhHCtgRH uySw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s8-20020a056402520800b0043d19ac7ff2si13623275edd.87.2022.09.14.00.03.15; Wed, 14 Sep 2022 00:03:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230051AbiINGmp (ORCPT + 99 others); Wed, 14 Sep 2022 02:42:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229528AbiINGmn (ORCPT ); Wed, 14 Sep 2022 02:42:43 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE9C425EB8; Tue, 13 Sep 2022 23:42:41 -0700 (PDT) Received: from canpemm500006.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MS9f70n03zmV94; Wed, 14 Sep 2022 14:38:55 +0800 (CST) Received: from [10.174.179.200] (10.174.179.200) by canpemm500006.china.huawei.com (7.192.105.130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 14 Sep 2022 14:42:39 +0800 Subject: Re: [PATCH 1/2] can: bcm: registration process optimization in bcm_module_init() To: Oliver Hartkopp , Marc Kleine-Budde CC: , , , , References: <823cff0ebec33fa9389eeaf8b8ded3217c32cb38.1662606045.git.william.xuanziyang@huawei.com> <381dd961-f786-2400-0977-9639c3f7006e@hartkopp.net> <7b063d38-311c-76d6-4e31-02f9cccc9bcb@huawei.com> <053c7de3-c76c-82fd-2d44-2e7c1673ae98@hartkopp.net> <9228b20a-3baa-32ad-6059-5cf0ffdb97a3@huawei.com> <20220912120020.dlxuryltw4sii635@pengutronix.de> <4791cc31-db17-7720-4a86-f83e7bf0918d@hartkopp.net> From: "Ziyang Xuan (William)" Message-ID: Date: Wed, 14 Sep 2022 14:42:39 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <4791cc31-db17-7720-4a86-f83e7bf0918d@hartkopp.net> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.179.200] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500006.china.huawei.com (7.192.105.130) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > > > On 12.09.22 14:00, Marc Kleine-Budde wrote: >> On 09.09.2022 17:04:06, Oliver Hartkopp wrote: >>> >>> >>> On 09.09.22 05:58, Ziyang Xuan (William) wrote: >>>>> >>>>> >>>>> On 9/8/22 13:14, Ziyang Xuan (William) wrote: >>>>>>> Just another reference which make it clear that the reordering of function calls in your patch is likely not correct: >>>>>>> >>>>>>> https://elixir.bootlin.com/linux/v5.19.7/source/net/packet/af_packet.c#L4734 >>>>>>> >>>>>>> static int __init packet_init(void) >>>>>>> { >>>>>>>            int rc; >>>>>>> >>>>>>>            rc = proto_register(&packet_proto, 0); >>>>>>>            if (rc) >>>>>>>                    goto out; >>>>>>>            rc = sock_register(&packet_family_ops); >>>>>>>            if (rc) >>>>>>>                    goto out_proto; >>>>>>>            rc = register_pernet_subsys(&packet_net_ops); >>>>>>>            if (rc) >>>>>>>                    goto out_sock; >>>>>>>            rc = register_netdevice_notifier(&packet_netdev_notifier); >>>>>>>            if (rc) >>>>>>>                    goto out_pernet; >>>>>>> >>>>>>>            return 0; >>>>>>> >>>>>>> out_pernet: >>>>>>>            unregister_pernet_subsys(&packet_net_ops); >>>>>>> out_sock: >>>>>>>            sock_unregister(PF_PACKET); >>>>>>> out_proto: >>>>>>>            proto_unregister(&packet_proto); >>>>>>> out: >>>>>>>            return rc; >>>>>>> } >>>>>>> >>> >>>> Yes,all these socket operations need time, most likely, register_netdevice_notifier() and register_pernet_subsys() had been done. >>>> But it maybe not for some reasons, for example, cpu# that runs {raw,bcm}_module_init() is stuck temporary, >>>> or pernet_ops_rwsem lock competition in register_netdevice_notifier() and register_pernet_subsys(). >>>> >>>> If the condition which I pointed happens, I think my solution can solve. >>>> >>> >>> No, I don't think so. >>> >>> We need to maintain the exact order which is depicted in the af_packet.c >>> code from above as the notifier call references the sock pointer. >> >> The notifier calls bcm_notifier() first, which will loop over the >> bcm_notifier_list. The list is empty if there are no sockets open, yet. >> So from my point of view this change looks fine. >> >> IMHO it's better to make a series where all these notifiers are moved in >> front of the respective socket proto_register(). > > Notifiers and/or pernet_subsys ? > > But yes, that would be better to have a clean consistent sequence in all these cases. > > Would this affect af_packet.c then too? Yes. When we create a sock by packet_create() after proto_register() and sock_register(). It will use net->packet.sklist_lock and net->packet.sklist directly in packet_create(). net->packet.sklist_lock and net->packet.sklist are initialized in packet_net_init(). The code snippet is as follows: static int packet_create(struct net *net, struct socket *sock, int protocol, int kern) { ... mutex_lock(&net->packet.sklist_lock); sk_add_node_tail_rcu(sk, &net->packet.sklist); mutex_unlock(&net->packet.sklist_lock); ... } static int __net_init packet_net_init(struct net *net) { mutex_init(&net->packet.sklist_lock); INIT_HLIST_HEAD(&net->packet.sklist); ... } So, if the sock is created firstly, we will get illegal access bug. > > Regards, > Oliver > > .