Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 224C547770 for ; Wed, 6 Dec 2023 16:39:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="O6L2EpCm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5485AC433C8; Wed, 6 Dec 2023 16:39:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701880747; bh=1NFQvaeqPQy74Jp+IPaGkTgA8ipB7fhgOLgtT7FAh9g=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=O6L2EpCmKd2/rLii38fu4e0BoU4Q6mGcZE63GKAFq+tyM1lYZ/p1rTyep7/5xQYgo IdH6FKHC7VU6ltiXpxtk55AX1qzGmwZf7U8Ei+UX6AAtNYAlCGKXuCMWK7OeKEiBSe THAsqO+rU2QhMAJMRM+lBn7Zu8Maz/bCVB1TGSaafKWLc1fq5ebNNJdRz0KvpCYVNA k0ppuF0ct1hrDmbKHj5dY4a0yEnRhcDo/UAr7uWTPq/+VhoaI6g7rbB8UKaFIyoChv 9XyXWyQ4D0kcKf2Z3qg+QEOvdm7U8dGLXDJpHhfvcXbntNiyxCmQtXuJw5vuiBskK8 7OqMKZ9YlISew== Message-ID: Date: Wed, 6 Dec 2023 09:39:06 -0700 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH net-next v2] Use READ/WRITE_ONCE() for IP local_port_range. Content-Language: en-US To: David Laight , "netdev@vger.kernel.org" Cc: Jakub Kicinski , Paolo Abeni , "David S. Miller" , Stephen Hemminger , "jakub@cloudflare.com" , Eric Dumazet , 'Mat Martineau' References: <4e505d4198e946a8be03fb1b4c3072b0@AcuMS.aculab.com> From: David Ahern In-Reply-To: <4e505d4198e946a8be03fb1b4c3072b0@AcuMS.aculab.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/6/23 6:44 AM, David Laight wrote: > Commit 227b60f5102cd added a seqlock to ensure that the low and high > port numbers were always updated together. > This is overkill because the two 16bit port numbers can be held in > a u32 and read/written in a single instruction. > > More recently 91d0b78c5177f added support for finer per-socket limits. > The user-supplied value is 'high << 16 | low' but they are held > separately and the socket options protected by the socket lock. > > Use a u32 containing 'high << 16 | low' for both the 'net' and 'sk' > fields and use READ_ONCE()/WRITE_ONCE() to ensure both values are > always updated together. > > Change (the now trival) inet_get_local_port_range() to a static inline > to optimise the calling code. > (In particular avoiding returning integers by reference.) > > Signed-off-by: David Laight > --- > Changes for v2: > - minor layout changes. > - remove unlikely() from comparisons when per-socket range set. > - avoid shifts of signed values that generate unsigned 32bit results. > I fiddled with the code that validates the argument to IP_LOCAL_PORT_RANGE > then decided to leave it (mostly) unchanged because it is also moved. > (There is a 'u16 x = int_val >> 16' which is required to move bit 31 to > bit 15 and is probably undefined behaviour - but will be ok on all sane cpu.) > > include/net/inet_sock.h | 5 +---- > include/net/ip.h | 8 +++++++- > include/net/netns/ipv4.h | 3 +-- > net/ipv4/af_inet.c | 4 +--- > net/ipv4/inet_connection_sock.c | 29 ++++++++++------------------- > net/ipv4/ip_sockglue.c | 33 ++++++++++++++++----------------- > net/ipv4/sysctl_net_ipv4.c | 18 +++++++----------- > 7 files changed, 43 insertions(+), 57 deletions(-) > Reviewed-by: David Ahern