Skip to content

[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] Backport NTSYNC misc driver into v6.6#845

Merged
opsiff merged 38 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:linux-6.6.y
Jun 5, 2025
Merged

[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] Backport NTSYNC misc driver into v6.6#845
opsiff merged 38 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:linux-6.6.y

Conversation

@Avenger-285714
Copy link
Copy Markdown
Member

@Avenger-285714 Avenger-285714 commented Jun 3, 2025

Summary by Sourcery

Backport the NTSYNC miscellaneous driver from upstream into the v6.6 kernel branch and integrate its user-space API, build configuration, documentation, and tests.

New Features:

  • Introduce the NTSYNC kernel driver implementing NT-style semaphore, mutex, and event primitives with ioctl interface

Build:

  • Enable CONFIG_NTSYNC in drivers/misc/Makefile and Kconfig and integrate NTSYNC test targets into the selftests Makefile

Documentation:

  • Add userspace API documentation in Documentation/userspace-api/ntsync.rst and update MAINTAINERS with NTSYNC entry

Tests:

  • Add comprehensive kselftests for NTSYNC under tools/testing/selftests/drivers/ntsync and include the ntsync selftest in the test suite

Elizabeth Figura and others added 4 commits June 3, 2025 23:59
[ Upstream commit 25b9cad ]

ntsync uses a misc device as the simplest and least intrusive uAPI interface.

Each file description on the device represents an isolated NT instance, intended
to correspond to a single NT virtual machine.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20240329000621.148791-2-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.9-rc3 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit b46271e ]

This corresponds to the NT syscall NtCreateSemaphore().

Semaphores are one of three types of object to be implemented in this driver,
the others being mutexes and events.

An NT semaphore contains a 32-bit counter, and is signaled and can be acquired
when the counter is nonzero. The counter has a maximum value which is specified
at creation time. The initial value of the semaphore is also specified at
creation time. There are no restrictions on the maximum and initial value.

Each object is exposed as an file, to which any number of fds may be opened.
When all fds are closed, the object is deleted.

Objects hold a pointer to the ntsync_device that created them. The device's
reference count is driven by struct file.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20240329000621.148791-3-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.9-rc3 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit dc806bd ]

This corresponds to the NT syscall NtReleaseSemaphore().

This increases the semaphore's internal counter by the given value, and returns
the previous value. If the counter would overflow the defined maximum, the
function instead fails and returns -EOVERFLOW.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20240329000621.148791-4-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.9-rc3 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit f5b335d ]

The ntsync code is only partially enabled in the kernel at this point in
time, creating the device node and that's about it.  Don't confuse
systems that expect to see a working ntsync interface by teasing it with
this basic structure at this point in time, so mark the code as "broken"
so that it is not built and enabled just yet.

Once the rest of the code is accepted, this will be reverted so that the
driver can be correctly built and used, but for now, this is the safest
way forward.

Reviewed-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/2024051450-abrasion-swizzle-550b@gregkh
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.10 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
@Avenger-285714 Avenger-285714 requested a review from Copilot June 3, 2025 16:01
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 3, 2025

Reviewer's Guide

Backport the NTSYNC misc driver into Linux v6.6 by integrating the kernel implementation, UAPI, documentation, build targets, and comprehensive selftests.

Sequence Diagram for NTSYNC Semaphore Creation

sequenceDiagram
    actor UserSpace
    participant KernelDriver as "ntsync.c"
    UserSpace->>KernelDriver: ioctl(fd_dev, NTSYNC_IOC_CREATE_SEM, &sem_args)
    activate KernelDriver
    KernelDriver->>KernelDriver: ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM)
    note right of KernelDriver: sem_args: initial count, max count
    KernelDriver->>KernelDriver: Initialize ntsync_obj (type=SEM, count, max)
    KernelDriver->>KernelDriver: ntsync_obj_get_fd(new_sem_obj)
    KernelDriver-->>UserSpace: Return new_fd (for semaphore object)
    deactivate KernelDriver
Loading

Sequence Diagram for NTSYNC Semaphore Release and Wake

sequenceDiagram
    actor UserSpace
    participant KernelDriver as "ntsync.c"
    participant SemaphoreObject as "ntsync_obj (Semaphore)"

    UserSpace->>KernelDriver: ioctl(fd_sem, NTSYNC_IOC_SEM_RELEASE, &release_count)
    activate KernelDriver
    KernelDriver->>SemaphoreObject: ntsync_lock_obj(sem)
    activate SemaphoreObject
    KernelDriver->>SemaphoreObject: prev_count = sem.u.sem.count
    KernelDriver->>SemaphoreObject: sem.u.sem.count += release_count
    opt sem.u.sem.count > sem.u.sem.max
      SemaphoreObject-->>KernelDriver: Error (EOVERFLOW)
    end
    KernelDriver->>SemaphoreObject: try_wake_any_sem()
    note right of SemaphoreObject: Wakes a task if sem.count > 0 and waiters exist
    opt Has all_waiters (all_hint > 0)
        KernelDriver->>SemaphoreObject: try_wake_all_obj()
    end
    SemaphoreObject-->>KernelDriver: ntsync_unlock_obj(sem)
    deactivate SemaphoreObject
    KernelDriver-->>UserSpace: Return (prev_count or error)
    deactivate KernelDriver
Loading

Class Diagram for NTSYNC UAPI Structures

classDiagram
    direction LR
    class ntsync_sem_args {
        +__u32 count
        +__u32 max
    }
    class ntsync_mutex_args {
        +__u32 owner
        +__u32 count
    }
    class ntsync_event_args {
        +__u32 manual
        +__u32 signaled
    }
    class ntsync_wait_args {
        +__u64 timeout
        +__u64 objs
        +__u32 count
        +__u32 index
        +__u32 flags
        +__u32 owner
        +__u32 alert
        +__u32 pad
    }
Loading

Class Diagram for NTSYNC Kernel Driver Structures

classDiagram
    direction LR
    class ntsync_obj {
        +spinlock_t lock
        +int dev_locked
        +ntsync_type type
        +file* file_ptr "struct file* file"
        +ntsync_device* dev
        +-- Semaphore Data (if type is SEM) --
        +__u32 sem_count
        +__u32 sem_max
        +-- Mutex Data (if type is MUTEX) --
        +__u32 mutex_count
        +pid_t mutex_owner
        +bool mutex_ownerdead
        +-- Event Data (if type is EVENT) --
        +bool event_manual
        +bool event_signaled
        +list_head any_waiters
        +list_head all_waiters
        +atomic_t all_hint
    }
    class ntsync_q_entry {
        +list_head node
        +ntsync_q* q
        +ntsync_obj* obj
        +__u32 index
    }
    class ntsync_q {
        +task_struct* task
        +__u32 owner
        +atomic_t signaled
        +bool all
        +bool ownerdead
        +__u32 count
        +ntsync_q_entry entries[]
    }
    class ntsync_device {
        +mutex wait_all_lock
        +file* file_ptr "struct file* file"
    }

    ntsync_obj "*" -- "1" ntsync_device : associated_with
    ntsync_q "1" -- "*" ntsync_q_entry : contains
    ntsync_q_entry "1" -- "1" ntsync_q : part_of_queue
    ntsync_q_entry "1" -- "1" ntsync_obj : references_object
Loading

File-Level Changes

Change Details Files
Add NTSYNC misc driver implementation
  • Introduce drivers/misc/ntsync.c with full kernel-side NT synchronization logic
  • Extend drivers/misc/Kconfig to add CONFIG_NTSYNC option
  • Update drivers/misc/Makefile to include ntsync.o in the misc build
drivers/misc/ntsync.c
drivers/misc/Kconfig
drivers/misc/Makefile
Introduce NTSYNC user API header
  • Create include/uapi/linux/ntsync.h with ioctl definitions
  • Define argument structures for semaphore, mutex, event, and wait operations
include/uapi/linux/ntsync.h
Document the NTSYNC userspace API
  • Add Documentation/userspace-api/ntsync.rst with API usage details
  • Update Documentation/userspace-api/index.rst to reference ntsync
  • Extend Documentation/userspace-api/ioctl/ioctl-number.rst with NTSYNC ioctls
Documentation/userspace-api/ntsync.rst
Documentation/userspace-api/index.rst
Documentation/userspace-api/ioctl/ioctl-number.rst
Add comprehensive kselftests for NTSYNC
  • Create tools/testing/selftests/drivers/ntsync/ntsync.c with full test suite
  • Add drivers/ntsync Makefile and config for selftests
  • Register tests in tools/testing/selftests/Makefile
tools/testing/selftests/drivers/ntsync/ntsync.c
tools/testing/selftests/drivers/ntsync/Makefile
tools/testing/selftests/drivers/ntsync/config
tools/testing/selftests/Makefile
Register NTSYNC maintainer
  • Add NTSYNC driver maintainer entry
MAINTAINERS

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR backports the NTSYNC misc driver into the v6.6 kernel while also removing the now‐redundant .llseek entries from multiple file_operations structures. The key changes include:

  • Removal of .llseek = no_llseek from various driver file_operations.
  • Updates to the Maintainers file to include the new NTSYNC driver.
  • Addition of documentation for the ntsync userspace API.

Reviewed Changes

Copilot reviewed 227 out of 227 changed files in this pull request and generated no comments.

Show a summary per file
File Description
drivers/block/mtip32xx/mtip32xx.c Removed redundant .llseek assignment
drivers/auxdisplay/charlcd.c Removed redundant .llseek assignment
drivers/acpi/apei/erst-dbg.c Removed redundant .llseek assignment
arch/x86/kernel/cpu/resctrl/pseudo_lock.c Removed redundant .llseek assignment
arch/x86/kernel/cpu/mce/dev-mcelog.c Removed redundant .llseek assignment
arch/um/drivers/hostaudio_kern.c Removed redundant .llseek assignments
arch/um/drivers/harddog_kern.c Removed redundant .llseek assignment
arch/s390/pci/pci_clp.c Removed redundant .llseek assignment
arch/s390/kernel/sysinfo.c Removed redundant .llseek assignment
arch/s390/kernel/perf_cpum_cf.c Removed redundant .llseek assignment
arch/s390/kernel/debug.c Removed redundant .llseek assignment
arch/s390/hypfs/inode.c Removed redundant .llseek assignment
arch/s390/hypfs/hypfs_dbfs.c Removed redundant .llseek assignment
arch/powerpc/platforms/cell/spufs/file.c Removed redundant .llseek assignments (multiple spots)
arch/parisc/kernel/perf.c Removed redundant .llseek assignment
MAINTAINERS Added entry for NTSYNC driver
Documentation/watchdog/convert_drivers_to_kernel_api.rst Removed outdated .llseek usage in example
Documentation/userspace-api/ntsync.rst Added comprehensive documentation for ntsync API
Documentation/userspace-api/ioctl/ioctl-number.rst Updated ioctl numbering for ntsync
Documentation/userspace-api/index.rst Added ntsync to the userspace API index

Elizabeth Figura added 18 commits June 4, 2025 00:02
[ Upstream commit d75abf2 ]

Simplify the user API a bit by returning the fd as return value from the ioctl
instead of through the argument pointer.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-2-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 5ec43d6 ]

Use the more common "release" terminology, which is also the term used by NT,
instead of "post" (which is used by POSIX).

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-3-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit b4a7b5f ]

This corresponds to part of the functionality of the NT syscall
NtWaitForMultipleObjects(). Specifically, it implements the behaviour where
the third argument (wait_any) is TRUE, and it does not handle alertable waits.
Those features have been split out into separate patches to ease review.

This patch therefore implements the wait/wake infrastructure which comprises the
core of ntsync's functionality.

NTSYNC_IOC_WAIT_ANY is a vectored wait function similar to poll(). Unlike
poll(), it "consumes" objects when they are signaled. For semaphores, this means
decreasing one from the internal counter. At most one object can be consumed by
this function.

This wait/wake model is fundamentally different from that used anywhere else in
the kernel, and for that reason ntsync does not use any existing infrastructure,
such as futexes, kernel mutexes or semaphores, or wait_event().

Up to 64 objects can be waited on at once. As soon as one is signaled, the
object with the lowest index is consumed, and that index is returned via the
"index" field.

A timeout is supported. The timeout is passed as a u64 nanosecond value, which
represents absolute time measured against either the MONOTONIC or REALTIME clock
(controlled by the flags argument). If U64_MAX is passed, the ioctl waits
indefinitely.

This ioctl validates that all objects belong to the relevant device. This is not
necessary for any technical reason related to NTSYNC_IOC_WAIT_ANY, but will be
necessary for NTSYNC_IOC_WAIT_ALL introduced in the following patch.

Some padding fields are added for alignment and for fields which will be added
in future patches (split out to ease review).

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-4-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit cdbb997 ]

This is similar to NTSYNC_IOC_WAIT_ANY, but waits until all of the objects are
simultaneously signaled, and then acquires all of them as a single atomic
operation.

Because acquisition of multiple objects is atomic, some complex locking is
required. We cannot simply spin-lock multiple objects simultaneously, as that
may disable preëmption for a problematically long time.

Instead, modifying any object which may be involved in a wait-all operation takes
a device-wide sleeping mutex, "wait_all_lock", instead of the normal object
spinlock.

Because wait-for-all is a rare operation, in order to optimize wait-for-any,
this lock is only taken when necessary. "all_hint" is used to mark objects which
are involved in a wait-for-all operation, and if an object is not, only its
spinlock is taken.

The locking scheme used here was written by Peter Zijlstra.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-5-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 5bc2479 ]

This corresponds to the NT syscall NtCreateMutant().

An NT mutex is recursive, with a 32-bit recursion counter. When acquired via
NtWaitForMultipleObjects(), the recursion counter is incremented by one. The OS
records the thread which acquired it.

The OS records the thread which acquired it. However, in order to keep this
driver self-contained, the owning thread ID is managed by user-space, and passed
as a parameter to all relevant ioctls.

The initial owner and recursion count, if any, are specified when the mutex is
created.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-6-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 31ca7bb ]

This corresponds to the NT syscall NtReleaseMutant().

This syscall decrements the mutex's recursion count by one, and returns the
previous value. If the mutex is not owned by the current task, the function
instead fails and returns -EPERM.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-7-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit ecc2ee3 ]

This does not correspond to any NT syscall. Rather, when a thread dies, it
should be called by the NT emulator for each mutex, with the TID of the dying
thread.

NT mutexes are robust (in the pthread sense). When an NT thread dies, any
mutexes it owned are immediately released. Acquisition of those mutexes by other
threads will return a special value indicating that the mutex was abandoned,
like EOWNERDEAD returned from pthread_mutex_lock(), and EOWNERDEAD is indeed
used here for that purpose.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-8-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 4c7404b ]

This correspond to the NT syscall NtCreateEvent().

An NT event holds a single bit of state denoting whether it is signaled or
unsignaled.

There are two types of events: manual-reset and automatic-reset. When an
automatic-reset event is acquired via a wait function, its state is reset to
unsignaled. Manual-reset events are not affected by wait functions.

Whether the event is manual-reset, and its initial state, are specified at
creation time.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-9-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 2dcba6f ]

This corresponds to the NT syscall NtSetEvent().

This sets the event to the signaled state, and returns its previous state.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-10-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit  bbb9797 ]

This corresponds to the NT syscall NtResetEvent().

This sets the event to the unsignaled state, and returns its previous state.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20241213193511.457338-11-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 12b29d3 ]

This corresponds to the NT syscall NtPulseEvent().

This wakes up any waiters as if the event had been set, but does not set the
event, instead resetting it if it had been signalled. Thus, for a manual-reset
event, all waiters are woken, whereas for an auto-reset event, at most one
waiter is woken.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20241213193511.457338-12-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit a948f41 ]

This corresponds to the NT syscall NtQuerySemaphore().

This returns the current count and maximum count of the semaphore.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-13-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 0b3c314 ]

This corresponds to the NT syscall NtQueryMutant().

This returns the recursion count, owner, and abandoned state of the mutex.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-14-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit e864071 ]

This corresponds to the NT syscall NtQueryEvent().

This returns the signaled state of the event and whether it is manual-reset.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-15-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit a138179 ]

NT waits can optionally be made "alertable". This is a special channel for
thread wakeup that is mildly similar to SIGIO. A thread has an internal single
bit of "alerted" state, and if a thread is alerted while an alertable wait, the
wait will return a special value, consume the "alerted" state, and will not
consume any of its objects.

Alerts are implemented using events; the user-space NT emulator is expected to
create an internal ntsync event for each thread and pass that event to wait
functions.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20241213193511.457338-16-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 7f853a2 ]

Wine has tests for its synchronization primitives, but these are more accessible
to kernel developers, and also allow us to test some edge cases that Wine does
not care about.

This patch adds tests for semaphore-specific ioctls NTSYNC_IOC_SEM_POST and
NTSYNC_IOC_SEM_READ, and waiting on semaphores.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-17-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit ae071ae ]

Test mutex-specific ioctls NTSYNC_IOC_MUTEX_UNLOCK and NTSYNC_IOC_MUTEX_READ,
and waiting on mutexes.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-18-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 4455456 ]

Test basic synchronous functionality of NTSYNC_IOC_WAIT_ANY, when objects are
considered signaled or not signaled, and how they are affected by a successful
wait.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-19-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
Elizabeth Figura and others added 16 commits June 4, 2025 00:02
[ Upstream commit d168f68 ]

Test basic synchronous functionality of NTSYNC_IOC_WAIT_ALL, and when objects
are considered simultaneously signaled.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-20-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
…IOC_WAIT_ANY.

[ Upstream commit f232798 ]

Test contended "wait-for-any" waits, to make sure that scheduling and wakeup
logic works correctly.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-21-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
…IOC_WAIT_ALL.

[ Upstream commit 72a651c ]

Test contended "wait-for-all" waits, to make sure that scheduling and wakeup
logic works correctly, and that the wait only exits once objects are all
simultaneously signaled.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-22-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit d2083b5 ]

Test event-specific ioctls NTSYNC_IOC_EVENT_SET, NTSYNC_IOC_EVENT_RESET,
NTSYNC_IOC_EVENT_PULSE, NTSYNC_IOC_EVENT_READ for manual-reset events, and
waiting on manual-reset events.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-23-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit b4e4dd5 ]

Test event-specific ioctls NTSYNC_IOC_EVENT_SET, NTSYNC_IOC_EVENT_RESET,
NTSYNC_IOC_EVENT_PULSE, NTSYNC_IOC_EVENT_READ for auto-reset events, and
waiting on auto-reset events.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-24-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit  a2e5a8c ]

Expand the contended wait tests, which previously only covered events and
semaphores, to cover events as well.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-25-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit dd914e0 ]

Test the "alert" functionality of NTSYNC_IOC_WAIT_ALL and NTSYNC_IOC_WAIT_ANY,
when a wait is woken with an alert and when it is woken by an object.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-26-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit c52b9cb ]

Expand the alert tests to cover alerting a thread mid-wait, to test that the
relevant scheduling logic works correctly.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-27-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit a22860e ]

Test a more realistic usage pattern, and one with heavy contention, in order to
actually exercise ntsync's internal synchronization.

This test has several threads in a tight loop acquiring a mutex, modifying some
shared data, and then releasing the mutex. At the end we check if the data is
consistent.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-28-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 79d42d9 ]

Add myself as maintainer, supported by CodeWeavers.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20241213193511.457338-29-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 6b695a7 ]

Add an overall explanation of the driver architecture, and complete and precise
specification for its intended behaviour.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20241213193511.457338-30-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit c301e1f ]

f5b335d ("misc: ntsync: mark driver as "broken"
to prevent from building") was committed to avoid the driver being used while
only part of its functionality was released. Since the rest of the functionality
has now been committed, revert this.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20241213193511.457338-31-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 0e7d523 ]

	struct ntsync_obj contains a reference to struct file
and that reference contributes to refcount - ntsync_alloc_obj()
grabs it.  Normally the object is destroyed (and reference
to obj->file dropped) in ntsync_obj_release().  However, in
case of ntsync_obj_get_fd() failure the object is destroyed
directly by its creator.

	That case should also drop obj->file; plain kfree(obj)
is not enough there - it ends up leaking struct file * reference.

	Take that logics into a helper (ntsync_free_obj()) and
use it in both codepaths that destroy ntsync_obj instances.

Fixes: b46271e "ntsync: Introduce NTSYNC_IOC_CREATE_SEM"
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Reviewed-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20250115025002.GA1977892@ZenIV
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 970b975 ]

When ntsync_obj_get_fd() fails, we free the ntsync object but forget to drop the
"file" member.

This was fixed for semaphores in 0e7d523, but
that commit did not fix the similar leak for events and mutexes, since they were
part of patches not yet in the mainline kernel. Fix those cases.

Fixes: 5bc2479 "ntsync: Introduce NTSYNC_IOC_CREATE_MUTEX."
Fixes: 4c7404b "ntsync: Introduce NTSYNC_IOC_CREATE_EVENT."
Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20250116190717.8923-1-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit fa2e558 ]

This allows ntsync to be usuable by non-root processes out of the box

Signed-off-by: Mike Lothian <mike@fireburn.co.uk>
Reviewed-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20250214122759.2629-2-mike@fireburn.co.uk
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.15 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit 92527e4 ]

GCC versions below 13 incorrectly detect the copy size as being static and too
small to fit in the "fds" array. Work around this by explicitly calculating the
size and returning EINVAL based on that, instead of based on the object count.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202502072019.LYoCR9bF-lkp@intel.com/
Suggested-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>

--

Suggested-by as per Arnd's request, but the only thing I changed was preserving
array_size() [as noted by Geert in the linked thread]. I tested and found no
regressions.

v2: Add missing sign-off
Link: https://lore.kernel.org/r/20250220192334.549167-1-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ Backport from v6.14 ]
Signed-off-by: WangYuli <wangyuli@uniontech.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Backports the NTSYNC synchronization primitive driver into the 6.6 kernel series, including its driver, UAPI interface, selftests, and documentation while removing obsolete no_llseek entries.

  • Introduces a new NTSYNC misc device driver with build and configuration integration.
  • Adds a comprehensive selftest suite and updates documentation and maintenance listings.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tools/testing/selftests/drivers/ntsync/config Adds a test configuration option (note the naming discrepancy).
tools/testing/selftests/drivers/ntsync/Makefile Adds build instructions for the ntsync selftest program.
tools/testing/selftests/drivers/ntsync/.gitignore Configures files to be ignored for ntsync selftests.
tools/testing/selftests/Makefile Includes the ntsync tests in the overall target list.
include/uapi/linux/ntsync.h Provides the UAPI header with ioctl definitions for ntsync.
drivers/misc/Makefile Integrates the new ntsync driver object file into the build.
drivers/misc/Kconfig Introduces a Kconfig option for building the ntsync driver.
MAINTAINERS Updates the maintainers file with ntsync information.
Documentation/userspace-api/ntsync.rst Adds detailed documentation for the ntsync userspace API.
Documentation/userspace-api/ioctl/ioctl-number.rst Allocates ioctl numbers for the new ntsync API.
Documentation/userspace-api/index.rst Lists ntsync as part of the userspace API documentation.
Comments suppressed due to low confidence (2)

tools/testing/selftests/drivers/ntsync/config:1

  • The configuration option is named 'CONFIG_WINESYNC' but the driver is referred to as NTSYNC elsewhere. Consider renaming it to 'CONFIG_NTSYNC=y' for consistency.
CONFIG_WINESYNC=y

Documentation/userspace-api/ntsync.rst:51

  • The term 'designaled' appears to be a typographical error. Consider replacing it with 'reset' or 'unsignaled' to improve clarity.
An auto-reset event is designaled when a wait is satisfied; a manual-reset event is not.

@Avenger-285714
Copy link
Copy Markdown
Member Author

经LLVM19手工测试,使能CONFIG_NTSYNC可通过编译。

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Avenger-285714 - I've reviewed your changes - here's some feedback:

  • The stress test uses a plain stress_counter incremented by multiple threads without synchronization, causing a data race—switch to an atomic counter or protected update to ensure correctness.
  • Given the complexity of wait_any/wait_all and the multi‐object locking protocol, consider adding a concise top‐level comment describing the lock acquisition order and invariants to aid future maintainers.
  • This backport spans uapi, driver implementation, docs, and a large test suite—splitting into distinct commits (core driver, uapi, tests) would simplify review and backport tracking.
Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread drivers/misc/ntsync.c
.minor = MISC_DYNAMIC_MINOR,
.name = NTSYNC_NAME,
.fops = &ntsync_fops,
.mode = 0666,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 issue (security): World-writable device node may have security implications.

If world-writable access isn't necessary, restrict permissions or make them configurable to reduce security risks.

Comment on lines +52 to +54
#define NTSYNC_IOC_EVENT_SET _IOR ('N', 0x88, __u32)
#define NTSYNC_IOC_EVENT_RESET _IOR ('N', 0x89, __u32)
#define NTSYNC_IOC_EVENT_PULSE _IOR ('N', 0x8a, __u32)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Inconsistent use of _IOR for event set/reset/pulse ioctls.

Since these ioctls modify state and return a value, using _IOWR may better represent their bidirectional behavior.

Suggested change
#define NTSYNC_IOC_EVENT_SET _IOR ('N', 0x88, __u32)
#define NTSYNC_IOC_EVENT_RESET _IOR ('N', 0x89, __u32)
#define NTSYNC_IOC_EVENT_PULSE _IOR ('N', 0x8a, __u32)
#define NTSYNC_IOC_EVENT_SET _IOWR('N', 0x88, __u32)
#define NTSYNC_IOC_EVENT_RESET _IOWR('N', 0x89, __u32)
#define NTSYNC_IOC_EVENT_PULSE _IOWR('N', 0x8a, __u32)

Comment on lines +177 to +178
'N' 80-8F uapi/linux/ntsync.h NT synchronization primitives
<mailto:wine-devel@winehq.org>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Misplaced mailto link in ioctl number registry.

Consider moving the mailto link to the MAINTAINERS file or relevant driver documentation, as contact details are not typically included in the ioctl number registry.

Suggested change
'N' 80-8F uapi/linux/ntsync.h NT synchronization primitives
<mailto:wine-devel@winehq.org>
'N' 80-8F uapi/linux/ntsync.h NT synchronization primitives

- Maximum count of the semaphore.

Fails with ``EINVAL`` if ``count`` is greater than ``max``.
On success, returns a file descriptor the created semaphore.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Missing preposition.

Consider revising to "returns a file descriptor for the created semaphore" or "to the created semaphore." Please update similar sentences on lines 131 and 145.

Suggested implementation:

  Fails with ``EINVAL`` if ``count`` is greater than ``max``.
  On success, returns a file descriptor for the created semaphore.

  On success, returns a file descriptor for the created mutex.

Comment on lines +209 to +212
performed as a single atomic operation. If two threads are waiting on
an auto-reset event which is pulsed, only one will be woken. If two
threads are waiting a manual-reset event which is pulsed, both will
be woken. However, in both cases, the event will be unsignaled
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Missing preposition.

It should be "waiting on a manual-reset event".

Suggested change
performed as a single atomic operation. If two threads are waiting on
an auto-reset event which is pulsed, only one will be woken. If two
threads are waiting a manual-reset event which is pulsed, both will
be woken. However, in both cases, the event will be unsignaled
performed as a single atomic operation. If two threads are waiting on
an auto-reset event which is pulsed, only one will be woken. If two
threads are waiting on a manual-reset event which is pulsed, both will
be woken. However, in both cases, the event will be unsignaled

Comment on lines +303 to +306
- Optional event object file descriptor. If nonzero, this
specifies an "alert" event object which, if signaled, will
terminate the wait. If nonzero, the identifier must point to a
valid event.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Clarity of "identifier" for alert argument.

Consider rephrasing to "the file descriptor must refer to a valid event object" to avoid confusion with the term "identifier," which is also used for "owner."

Suggested change
- Optional event object file descriptor. If nonzero, this
specifies an "alert" event object which, if signaled, will
terminate the wait. If nonzero, the identifier must point to a
valid event.
- Optional event object file descriptor. If nonzero, this
specifies an "alert" event object which, if signaled, will
terminate the wait. If nonzero, the file descriptor must refer to a
valid event object.

@opsiff opsiff merged commit 3ec4832 into deepin-community:linux-6.6.y Jun 5, 2025
6 of 7 checks passed
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: opsiff

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants