Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3702458pxb; Mon, 24 Jan 2022 15:48:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJwJtbWYylMJX3ASYO5+85X6d1SJn9YGcskNMWHCZoEG03s+ui9jt4snzDlmITh/O1tg8opg X-Received: by 2002:a05:6a00:244f:b0:4c4:3915:2d70 with SMTP id d15-20020a056a00244f00b004c439152d70mr15827862pfj.44.1643068115345; Mon, 24 Jan 2022 15:48:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643068115; cv=none; d=google.com; s=arc-20160816; b=hZMzyXa0KDcWxH2b7t7lOhyfJP743pVHgvTxLmvn/1RXoDN4JjMSaej8fbIdS0y6mI 9Dq4bDYP7OnO6Sco9w9ADMfoWNrcQtqBLwBgecEfmsFIYK0cet7dyhyd0ebj59WhdGJF V/aKDS5OFrJrTnkEFSbyQiwDCHfMaKVaVGv8MqY7KI4M4rc3PxAxLwf/xO8GkMBkVr24 b5H7m614WwffpACwYsMG6jv4j7x/Zbl1qMLseVPD/12ji2xclvMuP3skHeEbLiCi3tRU 1mtuVNlm/JETHZap+onxRP20v40yLsptFCVnpj1wsv5VFiImDYnK7Yhml4kElthUy4hH wppw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=onvqEeW+8PsEiNVZtKkEX4h87rD3P94+XmuUsueT5PE=; b=si5to1a6uaI91vQfyfpnSpT8JV7PQPUDekJUzXTiv1NZqlqHxpZHcDJatzbyCkrmnn tXW5hfGhpOKryobC3dLvquMVd2xAabjqtNxTx92bpfX8HlyvUjJoxVAH7A92Wy4RLkpT /AM89Vgh/Ity/GeNIyZHtjM2f8zjP0tRFGrQsv1SRjUojHpzDsvn7vHICFI3BX67KkvI e0UQYI+284CtY2UuHDDOKNOCeYCv5NU4gHUyiFdZmVpcCnFqXRnY2pYN7ZmLcN/3O+cx b1WXOIJTwuCbTd4hIoTPpeNoJTDv0cTIRGLwsQ9AAiB3JfN1+wyZkfOrlp2MjJsoEz2Y RznQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tnEOIYi8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si14825127pgi.577.2022.01.24.15.48.22; Mon, 24 Jan 2022 15:48:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tnEOIYi8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2362311AbiAXXmL (ORCPT + 99 others); Mon, 24 Jan 2022 18:42:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1841091AbiAXW5g (ORCPT ); Mon, 24 Jan 2022 17:57:36 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA85FC055ABE; Mon, 24 Jan 2022 13:12:01 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6B8A66147D; Mon, 24 Jan 2022 21:12:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 506B0C340E5; Mon, 24 Jan 2022 21:12:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058720; bh=RLQt645kFXrizzQINI9AoTr4vJ0Bgr5GYFhh/k5hya0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tnEOIYi8EPGNwvbEhbseyNOl7+paVpl9TdrCp6r/4fyZIIyhq5lL1zlyz8jRlqPo6 xoZmCEoHtP6rkeTHOM8gCPt6qt+vIPT2f/RbmJeq9oKpZjBdKVq8zhlQSUffy5o7rI iCwc/jia0U+5DtJUDKQKLk7eaQxFsDOLvefXz8Bo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miroslav Lichvar , Yangbo Lu , Richard Cochran , "David S. Miller" , Sasha Levin Subject: [PATCH 5.16 0381/1039] net: fix SOF_TIMESTAMPING_BIND_PHC to work with multiple sockets Date: Mon, 24 Jan 2022 19:36:10 +0100 Message-Id: <20220124184138.119678932@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miroslav Lichvar [ Upstream commit 007747a984ea5e895b7d8b056b24ebf431e1e71d ] When multiple sockets using the SOF_TIMESTAMPING_BIND_PHC flag received a packet with a hardware timestamp (e.g. multiple PTP instances in different PTP domains using the UDPv4/v6 multicast or L2 transport), the timestamps received on some sockets were corrupted due to repeated conversion of the same timestamp (by the same or different vclocks). Fix ptp_convert_timestamp() to not modify the shared skb timestamp and return the converted timestamp as a ktime_t instead. If the conversion fails, return 0 to not confuse the application with timestamps corresponding to an unexpected PHC. Fixes: d7c088265588 ("net: socket: support hardware timestamp conversion to PHC bound") Signed-off-by: Miroslav Lichvar Cc: Yangbo Lu Cc: Richard Cochran Acked-by: Richard Cochran Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/ptp/ptp_vclock.c | 10 +++++----- include/linux/ptp_clock_kernel.h | 12 +++++++----- net/socket.c | 9 ++++++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/drivers/ptp/ptp_vclock.c b/drivers/ptp/ptp_vclock.c index baee0379482bc..ab1d233173e13 100644 --- a/drivers/ptp/ptp_vclock.c +++ b/drivers/ptp/ptp_vclock.c @@ -185,8 +185,8 @@ out: } EXPORT_SYMBOL(ptp_get_vclocks_index); -void ptp_convert_timestamp(struct skb_shared_hwtstamps *hwtstamps, - int vclock_index) +ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, + int vclock_index) { char name[PTP_CLOCK_NAME_LEN] = ""; struct ptp_vclock *vclock; @@ -198,12 +198,12 @@ void ptp_convert_timestamp(struct skb_shared_hwtstamps *hwtstamps, snprintf(name, PTP_CLOCK_NAME_LEN, "ptp%d", vclock_index); dev = class_find_device_by_name(ptp_class, name); if (!dev) - return; + return 0; ptp = dev_get_drvdata(dev); if (!ptp->is_virtual_clock) { put_device(dev); - return; + return 0; } vclock = info_to_vclock(ptp->info); @@ -215,7 +215,7 @@ void ptp_convert_timestamp(struct skb_shared_hwtstamps *hwtstamps, spin_unlock_irqrestore(&vclock->lock, flags); put_device(dev); - hwtstamps->hwtstamp = ns_to_ktime(ns); + return ns_to_ktime(ns); } EXPORT_SYMBOL(ptp_convert_timestamp); #endif diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index 2e5565067355b..554454cb86931 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -351,15 +351,17 @@ int ptp_get_vclocks_index(int pclock_index, int **vclock_index); * * @hwtstamps: skb_shared_hwtstamps structure pointer * @vclock_index: phc index of ptp vclock. + * + * Returns converted timestamp, or 0 on error. */ -void ptp_convert_timestamp(struct skb_shared_hwtstamps *hwtstamps, - int vclock_index); +ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, + int vclock_index); #else static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index) { return 0; } -static inline void ptp_convert_timestamp(struct skb_shared_hwtstamps *hwtstamps, - int vclock_index) -{ } +static inline ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, + int vclock_index) +{ return 0; } #endif diff --git a/net/socket.c b/net/socket.c index 7f64a6eccf63f..5053eb0100e48 100644 --- a/net/socket.c +++ b/net/socket.c @@ -829,6 +829,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, int empty = 1, false_tstamp = 0; struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); + ktime_t hwtstamp; /* Race occurred between timestamp enabling and packet receiving. Fill in the current time for now. */ @@ -877,10 +878,12 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && !skb_is_swtx_tstamp(skb, false_tstamp)) { if (sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC) - ptp_convert_timestamp(shhwtstamps, sk->sk_bind_phc); + hwtstamp = ptp_convert_timestamp(shhwtstamps, + sk->sk_bind_phc); + else + hwtstamp = shhwtstamps->hwtstamp; - if (ktime_to_timespec64_cond(shhwtstamps->hwtstamp, - tss.ts + 2)) { + if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) { empty = 0; if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) && -- 2.34.1