Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp7554161rwd; Tue, 6 Jun 2023 12:30:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6FxFUBxMFRNKa6OBISzrAqW88T0RpcL9d+oz6+se0nHBW/AEcNbsxnuilM5T6RuJtGwyzi X-Received: by 2002:a05:622a:307:b0:3f3:9013:5839 with SMTP id q7-20020a05622a030700b003f390135839mr736244qtw.23.1686079829722; Tue, 06 Jun 2023 12:30:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686079829; cv=none; d=google.com; s=arc-20160816; b=wfRclYkTMAwWSAEmZDWYbmAL/1nrYLZ4iX+2GAalE9dzcLq3Pb2mCvF3hfq7gCCGqW e5Uisy86Sy/wNGLjFKARALSCP6RvuXJ9i9buaPRW4cNS759hAv6EOv9q6wJntZL+PoPt Sa3DLyEHRAy/TKVYemfoz96uChRMcRPmfveht9KoPYjsX9cGJWjdQH2eputQGky3LtQY ULHmUKssdvdJhm/i5G7F335fbNgIqz4mwM8uieM7xyigiaLEcaXTjBI+vGvWmmohvHAF WUD3O3kpvVctmqyhhy/m8InGeBqlUkedi1pu4gvvJ7EcCkguc+kSk6liGXLQ0s9DNZRp IuZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=lOGMp2aX3Ucoc1C0ztJi8jmSuTW02zRbHZwFnDEIZRU=; b=YfUZdEmgnxbMgLskh5nEhcpkU66VqPH+YWQ5SCe5pvnj+yG8NTKF1V30mNLqHxUedr dU3ThgZpuK99bV3PqI1t7T3HOBGCw1yWUZC5J0I76rhvRPIlVeObmyngrosL7I3jpTII 0tki35oJdIWT+jNZ7nKYraLK2JEb/udIhiCD7AWq3hHnSDTmU//0E42pYvyuR4Uowc3I TACK82vP+oNSxhvuTt0xvdV6ntnJrjz6ClJj94pOAgJijioblNZ6frpzvYktYIk89LrO lR0O2+b9riKMdR24Hwz/BZz4KcgHbenQhk9ZnseKgqbRCOhdHo2gfiNke+XlDla5YD50 poxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=sUJ5Pjue; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=lyAN4IZv; 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=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e4-20020a05622a110400b003d2f408a002si6695438qty.218.2023.06.06.12.30.15; Tue, 06 Jun 2023 12:30:29 -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; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=sUJ5Pjue; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=lyAN4IZv; 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=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233975AbjFFTRf (ORCPT + 99 others); Tue, 6 Jun 2023 15:17:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233921AbjFFTRb (ORCPT ); Tue, 6 Jun 2023 15:17:31 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BA00101; Tue, 6 Jun 2023 12:17:30 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 02CA560226; Tue, 6 Jun 2023 21:17:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1686079047; bh=jNSA09J/c7o0xmn9ZYlEHNwBrSX+r+EnE4ht4ZumTKE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=sUJ5PjueDmmtfj+rBKb8C4JO07gAonT/bPqcUKrih7mmMknR02QVsPGQu2fwZjIaK ovKpWvldMK7i9u4bHWJSbiFuhPmH9M498dEAflggiIeD1rnfYCBWAsORGdHm7h2aI0 9b5gB1zYFjXsZHI714m2DeMrq6e3I03YTFICUbcIQlKpJjRhbji3c6bCi97u6g/uI2 DHzotZ7pdyzZHilI7fe6lKMN6gIZSQI33h14/u9bsanrGxTaweWmbKb/26QphM1sUv kJe/DQAB611BDFFcwnShbLlZjgXgQ8UFqqbhPeMbb5Kx13Jl/IOSEWldheBu9TP+jL lsksNbP5tvywA== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mb78sT88mhyA; Tue, 6 Jun 2023 21:17:24 +0200 (CEST) Received: from [192.168.1.6] (unknown [77.237.113.62]) by domac.alu.hr (Postfix) with ESMTPSA id 5151D60225; Tue, 6 Jun 2023 21:17:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1686079044; bh=jNSA09J/c7o0xmn9ZYlEHNwBrSX+r+EnE4ht4ZumTKE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=lyAN4IZvHAqsUelelIvW9bA4vbgZi7KGpgp3TeZ2oOCxly3MLo4L0s9r8iiFyASNF 1l5/PeWrUdVZYz1Q/TgaAgU1wLuB7nxA90v4REJc0QBh/ZkTTte0EpTMnlntXFu42T IKBa2v55GWkRNDDni4IJPyL219r/AFBHMXpaiF+gJsPKsrls8d2B8w5s8D0ieuRYje Q8rn/3tDfrPSra121bTGScDVoxtDPugv+H3ePHZ/YvalL3NRChSY18phx4rmDnh5hf HfqDCiWPsHS0SzmioS2R5uvHrW8D4M/ITr1Z6BCxnjmTKYi0lOczvX6qHmvYMubLmF HH8d5nAfXsNOA== Message-ID: <174c6928-3498-8fb0-9f83-b01fa346a221@alu.unizg.hr> Date: Tue, 6 Jun 2023 21:17:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: POSSIBLE BUG: selftests/net/fcnal-test.sh: [FAIL] in vrf "bind - ns-B IPv6 LLA" test Content-Language: en-US To: Guillaume Nault Cc: netdev@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org References: <60f78eaa-ace7-c27d-8e45-4777ecf3faa2@alu.unizg.hr> <12c34bed-0885-3bb3-257f-3b2438ba206f@alu.unizg.hr> From: Mirsad Goran Todorovac In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 6/6/23 20:50, Guillaume Nault wrote: > On Tue, Jun 06, 2023 at 04:28:02PM +0200, Mirsad Todorovac wrote: >> On 6/6/23 16:11, Guillaume Nault wrote: >>> On Tue, Jun 06, 2023 at 03:57:35PM +0200, Mirsad Todorovac wrote: >>>> + if (oif) { >>>> + rcu_read_lock(); >>>> + dev = dev_get_by_index_rcu(net, oif); >>>> + rcu_read_unlock(); >>> >>> You can't assume '*dev' is still valid after rcu_read_unlock() unless >>> you hold a reference on it. >>> >>>> + rtnl_lock(); >>>> + mdev = netdev_master_upper_dev_get(dev); >>>> + rtnl_unlock(); >>> >>> Because of that, 'dev' might have already disappeared at the time >>> netdev_master_upper_dev_get() is called. So it may dereference an >>> invalid pointer here. >> >> Good point, thanks. I didn't expect those to change. >> >> This can be fixed, provided that RCU and RTNL locks can be nested: > > Well, yes and no. You can call rcu_read_{lock,unlock}() while under the > rtnl protection, but not the other way around. > >> rcu_read_lock(); >> if (oif) { >> dev = dev_get_by_index_rcu(net, oif); >> rtnl_lock(); >> mdev = netdev_master_upper_dev_get(dev); >> rtnl_unlock(); >> } > > This is invalid: rtnl_lock() uses a mutex, so it can sleep and that's > forbidden inside an RCU critical section. Obviously, that's bad. Mea culpa. >> if (sk->sk_bound_dev_if) { >> bdev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); >> } >> >> addr_type = ipv6_addr_type(daddr); >> if ((__ipv6_addr_needs_scope_id(addr_type) && !oif) || >> (addr_type & IPV6_ADDR_MAPPED) || >> (oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if && >> !(mdev && sk->sk_bound_dev_if && bdev && mdev == bdev))) { >> rcu_read_unlock(); >> return -EINVAL; >> } >> rcu_read_unlock(); >> >> But again this is still probably not race-free (bdev might also disappear before >> the mdev == bdev test), even if it passed fcnal-test.sh, there is much duplication >> of code, so your one-line solution is obviously by far better. :-) > > The real problem is choosing the right function for getting the master > device. In particular netdev_master_upper_dev_get() was a bad choice. > It forces you to take the rtnl, which is unnatural here and obliges you > to add extra code, while all this shouldn't be necessary in the first > place. Thank you for the additional insight. I had poor luck with Googling on these. I made a blunder after blunder. But it was insightful and brainstorming. Good exercise for my little grey cells. However, learning without making any errors appears to be simply a lot of blunt memorising. :-/ It's good to be in an environment when one can learn from errors. :-) Regards, Mirsad