From: sashiko-bot@kernel.org
To: "Birger Koblitz" <mail@birger-koblitz.de>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation
Date: Mon, 30 Nov 2026 09:30:20 +0000 [thread overview]
Message-ID: <sashiko-outbox-240776@kernel.org> (raw)
In-Reply-To: <20261129-ptp-v1-2-8148c402beaf@birger-koblitz.de>
Thank you for your contribution! Sashiko AI review found 14 potential
issues to consider:
- [High] net: usb: ax88179: PTP_1588_CLOCK_OPTIONAL added to
USB_NET_AX8817X in Kconfig
- [High] net: usb: ax88179: uninitialized or dangling ptp_hdr in
ax88279_process_tx_ts()
- [High] net: usb: ax88179: ptp_info[0] read instead of ptp_info[i] in
timestamp readers
- [High] net: usb: ax88179: broken error handling and leaks in
ax88179a_ptp_register()
- [High] net: usb: ax88179: incomplete teardown and leaks in
ax88179a_ptp_unregister()
- [High] net: usb: ax88179: GFP_KERNEL and error resubmit in
ax88279_read_ts_complete()
- [High] net: usb: ax88179: ring drain and full-queue wraparound on
ptp_info_buf
- [High] net: usb: ax88179: unlocked __skb_unlink() on tx_skbs and
ptp_info_buf race
- [High] net: usb: ax88179: unlocked tx_skbs unlink in
ax88279_process_tx_ts()
- [Medium] net: usb: ax88179: speed enum mismatch in
ax88179a_ptp_config()
- [Medium] net: usb: ax88179: missing endian conversion in 32-bit PTP
writes
- [Medium] net: usb: ax88179: wrong transfer size in
ax88179a_ptp_read_ts()
- [Medium] net: usb: ax88179: off-by-one nanosecond carry in
ax88179a_ptp_adjtime()
- [Low] net: usb: ax88179: duplicate macro definitions in ax88179a_ptp.h
--
commit fe13df4a07a9671fd74415abcea9a55992d30d82
Author: Birger Koblitz <mail@birger-koblitz.de>
net: usb: ax88179: Add PTP Implementation
This commit adds PTP hardware clock and TX/RX hardware timestamping support
for the AX88179A and AX88279 USB Ethernet controllers.
> diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig
> index cc8f2e1503399..2a811116d34dc 100644
> --- a/drivers/net/usb/Kconfig
> +++ b/drivers/net/usb/Kconfig
> @@ -166,6 +166,7 @@ config USB_USBNET
> config USB_NET_AX8817X
> tristate "ASIX AX88xxx Based USB 2.0 Ethernet Adapters"
> depends on USB_USBNET
> + depends on PTP_1588_CLOCK_OPTIONAL
> select CRC32
> select PHYLINK
> select AX88796B_PHY
> diff --git a/drivers/net/usb/Makefile b/drivers/net/usb/Makefile
> index 4026f19ecb826..812adcf839dc4 100644
> --- a/drivers/net/usb/Makefile
> +++ b/drivers/net/usb/Makefile
> @@ -13,7 +13,7 @@ obj-$(CONFIG_USB_LAN78XX) += lan78xx.o
> obj-$(CONFIG_USB_NET_AX8817X) += asix.o
> asix-y := asix_devices.o asix_common.o ax88172a.o
> obj-$(CONFIG_USB_NET_AX88179) += ax88179.o
> -ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o
> +ax88179-y := ax88179_178a.o ax88179a_devices.o ax88179_lib.o ax88179a_ptp.o
[Severity: High]
Should the PTP_1588_CLOCK_OPTIONAL dependency be on USB_NET_AX88179
instead of USB_NET_AX8817X?
ax88179a_ptp.o is linked into ax88179.o under CONFIG_USB_NET_AX88179, so
building with CONFIG_USB_NET_AX88179=y and CONFIG_PTP_1588_CLOCK=m fails
at link time with undefined references to ptp_clock_register(),
ptp_clock_index() and ptp_clock_unregister().
> diff --git a/drivers/net/usb/ax88179a_ptp.c b/drivers/net/usb/ax88179a_ptp.c
> new file mode 100644
> index 0000000000000..9a04a93e50952
> --- /dev/null
> +++ b/drivers/net/usb/ax88179a_ptp.c
> @@ -0,0 +1,845 @@
[ ... ]
> +static int ax88279_ptp_pbus_write(struct usbnet *dev, u16 offset,
> + u16 len, void *data)
> +{
> + int ret;
> +
> + ret = ax88179_write_cmd(dev, AX88279_PBUS_A32, offset,
> + AX88279_PTP_REG_BASE_ADDR_HI, len, data);
> + if (ret < 0)
> + return ret;
> +
> + return 0;
> +}
> +
> +static int ax88279_ptp_pbus_write_u32(struct usbnet *dev, u16 offset, u32 data)
> +{
> + u32 buf = data;
> +
> + return ax88279_ptp_pbus_write(dev, offset, 4, &buf);
> +}
[Severity: Medium]
Does ax88279_ptp_pbus_write_u32() write byte-reversed 32-bit values on
big-endian hosts?
ax88179_write_cmd() only byte-swaps 2-byte buffers and passes 4-byte
buffers through raw, unlike ax88179_write_u32() which calls
cpu_to_le32s(). The same endianness mismatch affects the 4-byte
ax88179_write_cmd() calls in ax88179a_ptp_pps_ctrl(),
ax88279_ptp_pps_ctrl() (after ax88179_read_cmd() converts reg32 to CPU
byte order) and ax88179a_ptp_config(), as well as the unguarded C
bitfields in struct ptp_info_279 and struct ptp_info_179a.
[ ... ]
> +static int ax88179a_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
> +{
> + struct ax88179a_ptp *ax88179a_ptp = ptp_to_dev(ptp);
> + u8 timestamp[AX_PTP_DATA_LEN] = {0};
> + struct ax88179_data *ax179_data;
> + bool delta_is_neg = false;
> + u64 sec, d, d_sec;
> + u32 d_nsec;
> + u32 nsec;
> + int ret;
> +
> + ax179_data = ax88179a_ptp->ax179_data;
> + if (ax179_data->chip_version == AX_VERSION_AX88279)
> + ret = ax88279_ptp_clk_read(ax88179a_ptp->dev, AX88179A_PTP_GET_80B_LCK,
> + AX_PTP_DATA_LEN, timestamp);
> + else
> + ret = ax88179_read_cmd(ax88179a_ptp->dev, AX_PTP_OP, AX_GET_LOCAL_CLOCK, 0,
> + AX_GET_LOCAL_CLOCK_SIZE, timestamp);
> +
> + if (ret < 0)
> + return ret;
> +
> + nsec = get_unaligned_le32(timestamp);
> + sec = get_unaligned_le64(×tamp[4]);
> + if (delta < 0)
> + delta_is_neg = true;
> + d = abs(delta);
> +
> + d_sec = div_u64_rem(d, NSEC_PER_SEC, &d_nsec);
> + if (delta_is_neg) {
> + if (d_nsec > nsec) {
> + nsec += NSEC_PER_SEC;
> + sec -= 1;
> + }
> + nsec -= d_nsec;
> + sec -= d_sec;
> + } else {
> + nsec += d_nsec;
> + if (nsec > NSEC_PER_SEC) {
> + sec += 1;
> + nsec -= NSEC_PER_SEC;
> + }
> + sec += d_sec;
> + }
[Severity: Medium]
Should the nanosecond carry check in ax88179a_ptp_adjtime() be
"nsec >= NSEC_PER_SEC" instead of "nsec > NSEC_PER_SEC"?
When nsec + d_nsec equals NSEC_PER_SEC, this writes 1000000000 into the
nanosecond field without incrementing sec. In addition, on AX88279 both
ax88179a_ptp_adjtime() and ax88179a_ptp_gettime64() read AX_PTP_DATA_LEN
(12) bytes from the 80-bit clock register without masking the upper 16
bits of sec, and the read-modify-write sequence in ax88179a_ptp_adjtime()
is not serialized against concurrent clock adjustments.
[ ... ]
> +static int match_ptp_ts(struct ax88179a_ptp *ptp, struct ptp_header *ptp_hdr,
> + struct sk_buff *skb, int type)
> +{
> + u16 sequence_id;
> +
> + sequence_id = ntohs(ptp_hdr->sequence_id);
> + if (ptp->ax179_data->chip_version == AX_VERSION_AX88179A)
> + sequence_id &= 0xff;
> +
> + while (ptp->ptp_info_buf_tail != ptp->ptp_info_buf_head) {
> + struct ptp_info *ptp_info = &ptp->ptp_info_buf[ptp->ptp_info_buf_tail];
> +
> + if (ptp_info->sequence_id == sequence_id &&
> + (ptp_hdr->tsmt & 0xf) == ptp_info->msg_type) {
> + struct skb_shared_hwtstamps shhwtstamps;
> + u64 timestamp_h, timestamp_l;
> + u8 msgtype;
> + u64 time64;
> +
> + timestamp_l = ptp_info->nsec;
> + timestamp_h = ptp_info->sec;
> + time64 = timestamp_h * NSEC_PER_SEC;
> + time64 += timestamp_l & 0xFFFFFFFF;
> + memset(&shhwtstamps, 0, sizeof(shhwtstamps));
> + shhwtstamps.hwtstamp = ns_to_ktime(time64);
> +
> + msgtype = ptp_get_msgtype(ptp_hdr, type);
> + if (ptp_hdr->flag_field[0] & 0x2 || (msgtype != 0 && msgtype != 3))
> + skb_tstamp_tx(skb, &shhwtstamps);
> + }
> + ptp->ptp_info_buf_tail++;
> + ptp->ptp_info_buf_tail %= AX88179A_PTP_QUEUE_SIZE;
> + }
> +
> + return 0;
> +}
[Severity: High]
Doesn't match_ptp_ts() drain the entire ptp_info_buf ring on the first
skb in ptp->tx_skbs?
Because the loop never breaks after a match and advances
ptp_info_buf_tail to ptp_info_buf_head unconditionally, all remaining
queued timestamps are discarded and any subsequent skbs in ptp->tx_skbs
see an empty ring. Furthermore, ptp_info_buf has AX88179A_PTP_QUEUE_SIZE
(5) slots, so when all 5 slots are populated in a batch, advancing
ptp_info_buf_head 5 times modulo 5 wraps it back to ptp_info_buf_tail and
makes the full ring appear empty.
> +
> +static void ax88279_process_tx_ts(struct work_struct *work)
> +{
> + struct ptp_header *ptp_hdr;
> + struct sk_buff *skb, *tmp;
> + struct ax88179a_ptp *ptp;
> + int type;
> +
> + ptp = container_of(work, struct ax88179a_ptp, process_ptp_info_buf);
> +
> + skb_queue_walk_safe(&ptp->tx_skbs, skb, tmp) {
> + type = ptp_classify_raw(skb);
> + if (type != PTP_CLASS_NONE) {
> + ptp_hdr = ptp_parse_header(skb, type);
> + if (!ptp_hdr)
> + goto frame_err;
> + }
> + match_ptp_ts(ptp, ptp_hdr, skb, type);
> +frame_err:
> + __skb_unlink(skb, &ptp->tx_skbs);
> + dev_kfree_skb_any(skb);
> + }
> +}
[Severity: High]
When ptp_classify_raw(skb) returns PTP_CLASS_NONE, doesn't
ax88279_process_tx_ts() pass an uninitialized or dangling ptp_hdr to
match_ptp_ts()?
If a non-PTP packet is queued in ptp->tx_skbs (or CONFIG_NET_PTP_CLASSIFY
is disabled), ptp_hdr is not set and execution still falls through to
match_ptp_ts(), which dereferences ptp_hdr->sequence_id. On the first
iteration ptp_hdr is uninitialized stack data; on later iterations it
points into the skb freed by dev_kfree_skb_any() on a previous iteration.
[Severity: High]
Can walking ptp->tx_skbs and calling __skb_unlink() without holding
ptp->tx_skbs.lock corrupt the queue?
CPU0 (TX path) CPU1 (kworker)
ax88179a_tx_fixup() ax88279_process_tx_ts()
skb_queue_tail(&ptp->tx_skbs) skb_queue_walk_safe(&ptp->tx_skbs, ...)
spin_lock_irqsave(&list->lock)
__skb_queue_tail(...) __skb_unlink(skb, &ptp->tx_skbs)
<-- unlocked list mutation
__skb_unlink() is a non-atomic helper that requires ptp->tx_skbs.lock;
using skb_dequeue(&ptp->tx_skbs) in a while loop avoids racing with
skb_queue_tail() and skb_queue_purge(). Likewise, match_ptp_ts() reads
ptp->ptp_info_buf and updates ptp_info_buf_tail without synchronization
while ax88279_read_ts_complete() (in URB completion context) or
ax88179a_ptp_read_ts() writes ptp_info_buf and updates ptp_info_buf_head.
> +
> +static void ax88279_read_ts_complete(struct urb *urb)
> +{
> + struct ax88179_data *ax179_data;
> + struct ptp_info_279 *ptp_info;
> + struct ax88179a_ptp *ptp;
> + struct usbnet *dev;
> + int offset;
> +
> + dev = urb->context;
> + ax179_data = dev->driver_priv;
> + if (!ax179_data)
> + return;
> +
> + ptp = ax179_data->ptp;
> + if (!ptp)
> + return;
> +
> + if (!netif_carrier_ok(dev->net))
> + return;
> +
> + if (urb->status < 0) {
> + netdev_err(dev->net, "Timestamp URB status error: %d", urb->status);
> + goto out;
> + }
> +
> + usb_mark_last_busy(dev->udev);
> +
> + // There can only be 2 packets in this buffer
> + offset = (ptp->ep4_buf[AX88279_PTP_EP4_SIZE - 1] & AX_TS_SEG_1) ?
> + 0 : (sizeof(struct ptp_info_279) * AX88179A_PTP_QUEUE_SIZE);
> +
> + ptp_info = (struct ptp_info_279 *)&ptp->ep4_buf[offset];
> + for (int i = 0; i < AX88179A_PTP_QUEUE_SIZE; i++) {
> + int j;
> +
> + j = ptp->ptp_info_buf_head;
> + if (ptp_info[i].status) {
> + ptp->ptp_info_buf[j].msg_type = ptp_info->msg_type;
> + ptp->ptp_info_buf[j].sequence_id = le16_to_cpu(ptp_info->sequence_id);
> + ptp->ptp_info_buf[j].nsec = le32_to_cpu(ptp_info->nsec);
> + ptp->ptp_info_buf[j].sec = le16_to_cpu(ptp_info->sec_h);
> + ptp->ptp_info_buf[j].sec <<= 32;
> + ptp->ptp_info_buf[j].sec |= le32_to_cpu(ptp_info->sec_l);
> + ptp->ptp_info_buf_head++;
> + ptp->ptp_info_buf_head %= AX88179A_PTP_QUEUE_SIZE;
> + }
> + }
> + schedule_work(&ptp->process_ptp_info_buf);
> +out:
> + ptp->ptp_read_ts(dev);
> +}
[Severity: High]
Doesn't this loop copy from ptp_info (element 0) instead of ptp_info[i]
when ptp_info[i].status is set?
Any timestamp reported in slots 1..4 is overwritten with the contents of
slot 0. The same bug is present in ax88179a_ptp_read_ts() below.
> +
> +void ax88179a_ptp_kill_urbs(struct usbnet *dev)
> +{
> + struct ax88179_data *ax179_data = dev->driver_priv;
> +
> + if (ax179_data->chip_version == AX_VERSION_AX88279 && ax179_data->ptp &&
> + ax179_data->ptp->urb) {
> + netdev_info(dev->net, "PTP: killing URBs\n");
> + usb_kill_urb(ax179_data->ptp->urb);
> + }
> +}
> +
> +static int ax88279_ptp_read_ts(struct usbnet *dev)
> +{
> + struct ax88179_data *ax179_data = dev->driver_priv;
> + struct ax88179a_ptp *ptp = ax179_data->ptp;
> + struct urb *urb = ptp->urb;
> + int ret;
> +
> + memset(ptp->ep4_buf, 0, AX88279_PTP_EP4_SIZE);
> + usb_fill_bulk_urb(urb, dev->udev,
> + usb_rcvbulkpipe(dev->udev, 4),
> + (void *)ptp->ep4_buf, AX88279_PTP_EP4_SIZE,
> + (usb_complete_t)ax88279_read_ts_complete,
> + dev);
> +
> + ret = usb_submit_urb(urb, GFP_KERNEL);
> + if (ret == -ENODEV) {
> + netdev_err(dev->net, "Error submitting PTP bulk message: status=%d", ret);
> + netif_device_detach(dev->net);
> + }
> +
> + urb->actual_length = 0;
> +
> + return ret;
> +}
[Severity: High]
ax88279_read_ts_complete() runs in atomic URB completion context and
calls ptp->ptp_read_ts(dev) -> ax88279_ptp_read_ts(), which calls
usb_submit_urb(urb, GFP_KERNEL). Doesn't that sleep in atomic context?
In addition, when urb->status < 0 in ax88279_read_ts_complete(), jumping
to "out:" resubmits the URB even on -ENOENT, -ECONNRESET or -ESHUTDOWN
(such as when usb_kill_urb() is called while carrier is up).
ax88279_ptp_read_ts() also writes urb->actual_length = 0 after
usb_submit_urb() has already submitted the URB, and DMA-maps ptp->ep4_buf,
which is embedded in struct ax88179a_ptp without cacheline alignment.
> +
> +static int ax88179a_ptp_read_ts(struct usbnet *dev)
> +{
> + struct ax88179_data *ax179_data = dev->driver_priv;
> + struct ax88179a_ptp *ptp = ax179_data->ptp;
> + struct ptp_info_179a *ptp_info;
> + int count = 0;
> + int ret;
> +
> + ret = ax88179_read_cmd(dev, AX_PTP_TIMESTAMP_REQ, 0, 0, AX88279_PTP_EP4_SIZE, ptp->ep4_buf);
> + if (ret < 0) {
> + netdev_err(dev->net, "Error submitting PTP control message: status=%d", ret);
> + netif_device_detach(dev->net);
> + return ret;
> + }
[Severity: Medium]
Should ax88179a_ptp_read_ts() request
sizeof(struct ptp_info_179a) * AX88179A_PTP_QUEUE_SIZE (60 bytes) instead
of AX88279_PTP_EP4_SIZE (131 bytes)?
ax88179a_ptp_config() programs AX88179A_PTP_MEM_SEG_SIZE to
sizeof(struct ptp_info_179a) * AX88179A_PTP_QUEUE_SIZE. Also, ptp->ep4_buf
is not zeroed before the read and short reads are not checked, so a short
transfer can leave stale status bits in ptp->ep4_buf, and calling
netif_device_detach() on any negative return value detaches the netdev even
on transient control transfer errors (unlike ax88279_ptp_read_ts(), which
checks for -ENODEV).
[ ... ]
> +static int ax88179a_ptp_config(struct usbnet *dev, int speed)
> +{
[ ... ]
> + switch (speed) {
> + case ETHER_LINK_10:
> + ptp_tx_delay[0] = 0xB7;
> + ptp_tx_delay[1] = 0x0F;
> + ptp_tx_delay[4] = ptpset;
> + ptp_rx_delay[0] = 0xAC;
> + ptp_rx_delay[1] = 0x0A;
> + ptp_rx_delay[4] = ptpset;
> + break;
> + case ETHER_LINK_100:
> + ptp_tx_delay[0] = 0x64;
> + ptp_tx_delay[4] = ptpset;
> + ptp_rx_delay[0] = 0x22;
> + ptp_rx_delay[1] = 0x01;
> + ptp_rx_delay[4] = ptpset;
> + break;
> + case ETHER_LINK_1000:
> + default:
> + ptp_tx_delay[0] = 0x6E;
> + ptp_tx_delay[4] = ptpset;
> + ptp_rx_delay[0] = 0xE4;
> + ptp_rx_delay[4] = ptpset;
> + break;
> + }
[ ... ]
> + ptp_data->tx_tstamp = true;
> + ptp_data->rx_tstamp = true;
> +
> + return 0;
> +}
> +
> +static int ax88279_ptp_config(struct usbnet *dev, int speed)
> +{
[ ... ]
> + ret = ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8);
> + if (ret < 0)
> + goto out;
> + reg8 |= AX_CS_TRAIL_UDPV4_EN | AX_CS_TRAIL_UDPV6_EN;
> + ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8);
> +
> + switch (speed) {
> + case SPEED_100:
[ ... ]
> +out:
> + return ret;
> +}
[Severity: Medium]
Why does ax88179a_ptp_config() switch on ETHER_LINK_10 / ETHER_LINK_100 /
ETHER_LINK_1000 (1, 2, 3) while ax88279_ptp_config() switches on
SPEED_100 / SPEED_1000 / SPEED_2500 for the same ptp_config() callback?
When phylink passes SPEED_10 or SPEED_100 to ptp_config(),
ax88179a_ptp_config() always falls through to the 1000 Mbps default case.
Also, unlike ax88179a_ptp_config(), ax88279_ptp_config() never sets
ptp_data->tx_tstamp and ptp_data->rx_tstamp to true and returns ret (1 on
success from ax88179_read_cmd()) instead of 0.
[ ... ]
> +int ax88179a_ptp_register(struct usbnet *dev)
> +{
> + struct ax88179a_ptp *ax88179a_ptp;
> + struct ax88179_data *ax179_data;
> + u8 ptp_ver;
> + int ret = 0;
> +
> + ax179_data = dev->driver_priv;
> + ax88179a_ptp = kzalloc_obj(*ax88179a_ptp);
> + if (!ax88179a_ptp)
> + return -ENOMEM;
> +
> + ax179_data->ptp = ax88179a_ptp;
> + ax88179a_ptp->dev = dev;
> + ax88179a_ptp->ax179_data = ax179_data;
> +
> + ax88179a_ptp->ptp_info_buf_head = 0;
> + ax88179a_ptp->ptp_info_buf_tail = 0;
> + INIT_WORK(&ax88179a_ptp->process_ptp_info_buf, ax88279_process_tx_ts);
> + INIT_WORK(&ax88179a_ptp->req_tx_timestamp, ax88179a_ptp_read_ts_work);
> + skb_queue_head_init(&ax88179a_ptp->tx_skbs);
> +
> + switch (ax179_data->chip_version) {
> + case AX_VERSION_AX88279:
> + ax88179a_ptp->urb = usb_alloc_urb(0, GFP_KERNEL);
> + if (!ax88179a_ptp->urb)
> + goto err;
> + ax88179a_ptp->pps_ctrl = ax88279_ptp_pps_ctrl;
> + ax88179a_ptp->ptp_config = ax88279_ptp_config;
> + ax88179a_ptp->ptp_read_ts = ax88279_ptp_read_ts;
> + netdev_info(dev->net, "PTP enabled on AX88279\n");
> + break;
> +
> + case AX_VERSION_AX88179A:
> + ax88179a_ptp->urb = usb_alloc_urb(0, GFP_KERNEL);
> + if (!ax88179a_ptp->urb)
> + goto err;
> + ax88179a_ptp->ctrlreq = kzalloc_obj(*ax179_data->ptp->ctrlreq);
> + if (!ax88179a_ptp->ctrlreq)
> + goto no_ctrlreq;
> + if (ax88179_read_cmd(dev, AX88179A_ACCESS_BL,
> + AX88179A_HW_EC_VERSION, 1, 1, &ptp_ver) < 0)
> + return 0;
> +
> + if (ptp_ver < 3) {
> + netdev_warn(dev->net, "PTP on AX88179A version %d < 3 unsupported\n",
> + ptp_ver);
> + return 0;
> + }
> + netdev_info(dev->net, "PTP on AX88179A version %d enabled\n", ptp_ver);
> +
> + ax88179a_ptp->pps_ctrl = ax88179a_ptp_pps_ctrl;
> + ax88179a_ptp->ptp_config = ax88179a_ptp_config;
> + ax88179a_ptp->ptp_read_ts = ax88179a_ptp_read_ts;
> +
> + // PTP excludes TSO and SG on AX88179A
> + dev->net->features &= ~(NETIF_F_SG | NETIF_F_TSO);
> + dev->net->hw_features &= ~(NETIF_F_SG | NETIF_F_TSO);
> + dev->net->vlan_features &= ~(NETIF_F_SG | NETIF_F_TSO);
> + break;
> +
> + default:
> + netdev_warn(dev->net, "PTP not supported\n");
> + ret = -EOPNOTSUPP;
> + goto no_ctrlreq;
> + };
> +
> + ax88179a_ptp->ptp_ci = ax88179a_ptp_clock;
> + ax88179a_ptp->ptp_clock = ptp_clock_register(&ax88179a_ptp->ptp_ci, &dev->udev->dev);
> +
> + if (IS_ERR(ax88179a_ptp->ptp_clock)) {
> + dev_err(&dev->udev->dev, "ptp_clock_register failed\n");
> + ret = PTR_ERR(ax88179a_ptp->ptp_clock);
> + goto err;
> + }
> +
> + ax88179a_ptp->phc_index = ptp_clock_index(ax88179a_ptp->ptp_clock);
> +
> + ax88179a_ptp->ax179_data = ax179_data;
> +
> + return 0;
> +
> +no_ctrlreq:
> + kfree(ax88179a_ptp->urb);
> +err:
> + kfree(ax88179a_ptp);
> + return ret;
> +}
[Severity: High]
What happens on the early returns and error paths in
ax88179a_ptp_register()?
If reading AX88179A_HW_EC_VERSION fails or ptp_ver < 3, returning 0 leaks
urb, ctrlreq and ax88179a_ptp while leaving ax179_data->ptp non-NULL with
NULL pps_ctrl, ptp_config and ptp_read_ts callbacks, causing a NULL
function pointer dereference when callers check ax179_data->ptp and invoke
those callbacks (for example in ax88179a_ptp_unregister()).
On the goto error paths:
- ax179_data->ptp is never reset to NULL when ax88179a_ptp is freed,
leaving a dangling pointer;
- ret is still 0 when usb_alloc_urb() or kzalloc_obj() fails;
- no_ctrlreq frees ax88179a_ptp->urb with kfree() instead of
usb_free_urb();
- ptp_clock_register() failure jumps to err, leaking urb and ctrlreq.
> +
> +void ax88179a_ptp_unregister(struct usbnet *dev)
> +{
> + struct ax88179a_ptp *ax88179a_ptp;
> + struct ax88179_data *ax179_data;
> +
> + ax179_data = dev->driver_priv;
> + ax88179a_ptp = ax179_data->ptp;
> +
> + if (!ax179_data->ptp)
> + return;
> +
> + ax88179a_ptp->pps_ctrl(dev, false);
> + cancel_work_sync(&ax88179a_ptp->process_ptp_info_buf);
> +
> + if (ax179_data->chip_version == AX_VERSION_AX88279) {
> + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_LCK_CTRL0, 0);
> + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_RX_CTRL0, 0);
> + ax88279_ptp_pbus_write_u32(dev, AX88279_PTP_TX_CTRL0, 0);
> + usb_free_urb(ax88179a_ptp->urb);
> + } else {
> + u8 reg8 = 0;
> +
> + ax88179_read_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8);
> + reg8 &= ~(AX_PTP_CTRL_L3_EN | AX_PTP_CTRL_EN);
> + ax88179_write_cmd(dev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8);
> + kfree(ax88179a_ptp->ctrlreq);
> + }
> + skb_queue_purge(&ax88179a_ptp->tx_skbs);
> +
> + if (ax88179a_ptp && ax88179a_ptp->ptp_clock)
> + ptp_clock_unregister(ax88179a_ptp->ptp_clock);
> +
> + kfree(ax179_data->ptp);
> +}
[Severity: High]
Does ax88179a_ptp_unregister() leave req_tx_timestamp work and the URB
active, and leak ax88179a_ptp->urb on AX88179A?
req_tx_timestamp is never cancelled with cancel_work_sync(), and the URB
is not killed before cancelling process_ptp_info_buf, so an in-flight
req_tx_timestamp work item or URB completion can reschedule
process_ptp_info_buf and access ax88179a_ptp after it is freed. Also,
ax88179a_ptp->urb is allocated for AX_VERSION_AX88179A in
ax88179a_ptp_register() but only freed in the AX_VERSION_AX88279 branch
here, and ax179_data->ptp is not cleared to NULL after kfree().
> diff --git a/drivers/net/usb/ax88179a_ptp.h b/drivers/net/usb/ax88179a_ptp.h
> new file mode 100644
> index 0000000000000..534e73cdc951e
> --- /dev/null
> +++ b/drivers/net/usb/ax88179a_ptp.h
> @@ -0,0 +1,249 @@
[ ... ]
> +#define AX_PTP_CLK_EN 0x010000
> +#define AX_PTP_CLK_SELECT_DIVIDE 0x040000
> +
> +#define AX88179A_PBUS_REG 0x10
> +#define AX_PTP_CLK 0x13
> +
> +#define AX88279_PTP_LCK_CTRL0 0x1000
> +#define AX_PTP_LCK_CTRL0_EN 0x0001
> +#define AX_PTP_LCK_CTRL0_80B_NS_EN 0x0002
> +#define AX_PTP_LCK_CTRL0_80B_S_E 0x0004
> +#define AX_PTP_LCK_CTRL0_48B_EN 0x0008
> +#define AX_PTP_LCK_CTRL0_PPS_EN 0x0010
> +#define AX_PTP_LCK_CTRL0_TX_DEL_VEC 0x0040
> +
> +#define AX88279_PTP_RX_CTRL0 0x2000
> +#define AX88279_PTP_TX_CTRL0 0x3000
> +
> +#define AX88179A_PTP_CTRL_1 0x00
> +#define AX_PTP_CTRL_EN 0x01
> +#define AX_PTP_CTRL_L3_EN 0x02
> +#define AX_PTP_TX_FILTER_GENERAL_MSG 0x08
> +#define AX_PTP_RX_FILTER_GENERAL_MSG 0x10
> +#define AX_PTP_TX_PLUS_DELAY 0x20
> +#define AX88179A_PTP_MEM_SEG_SIZE 0x21
> +
> +#define AX88179A_PTP_CTRL_2 0x01
> +#define AX_PTP_CTRL_SET_PERIOD 0x08
> +
> +#define AX88179A_PTP_MEM_SEG_SIZE 0x21
[Severity: Low]
This isn't a bug, but several macros in ax88179a_ptp.h are defined twice:
AX88179A_PTP_MEM_SEG_SIZE here, AX_PTP_CLK_EN, and the AX_PTP_LCK_CTRL0_*
group (including the unused typo AX_PTP_LCK_CTRL0_80B_S_E alongside
AX_PTP_LCK_CTRL0_80B_S_EN further down).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129-ptp-v1-0-8148c402beaf@birger-koblitz.de?part=2
next prev parent reply other threads:[~2026-11-30 9:30 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 9:29 [PATCH net-next 0/3] net: usb: ax88179: Add support for PTP Birger Koblitz
2026-11-29 9:29 ` [PATCH net-next 1/3] net: usb: ax88179: Add PTP data structures and definitions Birger Koblitz
2026-11-30 9:30 ` sashiko-bot
2026-11-29 9:29 ` [PATCH net-next 2/3] net: usb: ax88179: Add PTP Implementation Birger Koblitz
2026-11-29 18:31 ` Manuel Ebner
2026-11-30 5:18 ` Birger Koblitz
2026-11-30 9:30 ` sashiko-bot [this message]
2026-11-29 9:29 ` [PATCH net-next 3/3] net: usb: ax88179: Enable PTP Birger Koblitz
2026-11-29 15:48 ` Andrew Lunn
2026-11-29 17:24 ` Birger Koblitz
2026-11-29 21:09 ` Andrew Lunn
2026-11-29 21:18 ` kernel test robot
2026-11-30 9:30 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-240776@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mail@birger-koblitz.de \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox