1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

26 Commits

Author SHA1 Message Date
Bartosz Golaszewski
7a768dc5b7 tests: gpiosim: revert minimum kernel version back to v5.17.4
Commit b4009e2e67 ("tests: gpiosim: provide
gpiosim_bank_set_line_valid()") added support for marking simulated GPIO
lines as valid/invalid using a new attribute of the kernel configfs
interface of the gpio-sim module. In the process it bumped the minimum
kernel version required to use it to v6.17 where this feature first
appeared. The test cases we added later only appear in gpiod-test which
is the test suite for the core C library. However with the kernel
version change in libgpiosim other test suites have also seen their
minimum kernel versions implicitly increase.

Since the libgpiosim library will simply return an error in the event
that the attribute in question doesn't exist when the new function is
called, we can revert the required kernel version to what it was before
and allow just the individual test suites to bump their respective
minimums separately.

Reported-by: Vincent Fazio <vfazio@gmail.com>
Tested-by: Vincent Fazio <vfazio@gmail.com>
Link: https://lore.kernel.org/r/20251010094951.18927-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-10-13 09:54:31 +02:00
Bartosz Golaszewski
b4009e2e67 tests: gpiosim: provide gpiosim_bank_set_line_valid()
Provide a function allowing to mark a GPIO at given offset as valid or
invalid (all lines are valid by default). This will make it impossible
for any user to request it once the simulator is up.

Link: https://lore.kernel.org/r/20251006-gpiosim-valid-lines-v1-5-b399373e90a9@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-10-09 10:12:59 +02:00
Bartosz Golaszewski
8379438130 tests: gpiosim: selftests: add more test cases
Add simple self tests for libgpiosim functions that were not tested
before.

Link: https://lore.kernel.org/r/20251006-gpiosim-valid-lines-v1-4-b399373e90a9@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-10-09 10:12:59 +02:00
Bartosz Golaszewski
43d100a49c tests: gpiosim: selftests: shrink the self-test code
Most tests failures end with a perror() print and an exit(1) so wrap it
in a helper function and a macro and shrink the code.

Link: https://lore.kernel.org/r/20251006-gpiosim-valid-lines-v1-3-b399373e90a9@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-10-09 10:12:59 +02:00
Bartosz Golaszewski
1aca992f8b tests: gpiosim: don't allow clearing hogs on active devices
Check if the GPIO device is pending in gpiosim_bank_clear_hog() before
actually trying to remove the hog directory.

Fixes: a2f6e8cd35 ("libgpiosim: new library for controlling the gpio-sim module")
Link: https://lore.kernel.org/r/20251006-gpiosim-valid-lines-v1-2-b399373e90a9@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-10-09 10:12:59 +02:00
Bartosz Golaszewski
e3c641ec7c gpiosim: selftests: check that a disabled device can be re-enabled
Extend the test sequence to check that a device can be disabled and
re-enabled without problems.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:15:13 +01:00
Bartosz Golaszewski
3dac2c5d9c gpiosim: defer removal of bank entries when device is not disabled
Linux kernel commit 8bd76b3d3f3a ("gpio: sim: lock up configfs that an
instantiated device depends on") has uncovered an issue in libgpiosim:
we use reference counting for all objects (to make RAII easier for
bindings) but, with the above change, if the user doesn't explicitly
disable the device before dropping the last reference to a bank object,
the parent device will never get correctly cleaned up.

This is because we remove the hog and line directories in bank's
release() callback unconditionally. With the directories being locked
now, this fails if device is active and then when we eventually call the
device object's release(), we'll end up stuck with leftover bank
directories and never actually clean-up correctly.

Extend the gpiosim device with a list of deferred banks. When releasing
a bank on an active device, defer any actual cleanup until the time the
device gets deactivated which may be triggered by the user explictly or
will happen automatically when the last reference to the device object is
dropped. For inactive devices, there's no functional change.

Link: https://lore.kernel.org/r/20250214102648.71382-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:14:54 +01:00
Bartosz Golaszewski
37b3ac4bb5 build: remove redundant SOURCES assignments
For any given target (let's say foobar), automake defaults to looking for
foobar.c if foobar_SOURCES are not specified. Remove redundant assignments
as we've seen multiple hidden typos in makefiles already.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
2023-06-30 14:51:34 +02:00
Erik Schilling
370bf6825d gpiosim: fix data race that corrupts heap
Hit this while seeing some heap corruptions when running cargo test on
the Rust bindings.

Took a bit to track down since I first used address sanitizers, but with
the thread sanitizer it becomes obvious immediately (output simplified):

    ==================
    WARNING: ThreadSanitizer: data race (pid=288119)
      Write of size 8 at 0x0000018f1e78 by thread T6:
        #0 id_free /libgpiod/tests/gpiosim/gpiosim.c:141:17
        #1 dev_release /libgpiod/tests/gpiosim/gpiosim.c:600:2
        #2 refcount_dec /libgpiod/tests/gpiosim/gpiosim.c:176:3
        #3 gpiosim_dev_unref /libgpiod/tests/gpiosim/gpiosim.c:671:2
        #4 bank_release /libgpiod/tests/gpiosim/gpiosim.c:873:2
        #5 refcount_dec /libgpiod/tests/gpiosim/gpiosim.c:176:3
        #6 gpiosim_bank_unref /libgpiod/tests/gpiosim/gpiosim.c:941:2
        [...]

      Previous write of size 8 at 0x0000018f1e78 by thread T1:
        #0 id_free /libgpiod/tests/gpiosim/gpiosim.c:141:17
        #1 bank_release /libgpiod/tests/gpiosim/gpiosim.c:878:2
        #2 refcount_dec /libgpiod/tests/gpiosim/gpiosim.c:176:3
        #3 gpiosim_bank_unref /libgpiod/tests/gpiosim/gpiosim.c:941:2
        [...]

      Location is global 'id_del_ctx' of size 16 at 0x0000018f1e70

      Thread T6 'chip::verify::f' (tid=288126, running) created by main thread at:
        #7 test::run_tests::hd53a07a011bd771f /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:407:21
        [...]

      Thread T1 'chip::open::gpi' (tid=288121, finished) created by main thread at:
        #7 test::run_tests::hd53a07a011bd771f /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:407:21
        [...]

    SUMMARY: ThreadSanitizer: data race /libgpiod/tests/gpiosim/gpiosim.c:141:17 in id_free
    ==================

This eventually can either lead to leaks or double free's that corrupt
the heap and lead to crashes.

The issue got introduced when a previously local variable that did not
require protection was turned into a global variable.

Fixes: 5e111df2fc ("gpiosim: use twalk() instead of twalk_r()")
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-26 16:31:39 +02:00
Bartosz Golaszewski
968c7c8752 gpiosim: remove unused define
ARRAY_SIZE() is defined but unused so remove it.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-02-17 14:05:30 +01:00
Bartosz Golaszewski
5e111df2fc gpiosim: use twalk() instead of twalk_r()
twalk_r() is a GNU extension for the binary search tree API. Musl libc
doesn't provide it and after inquiring with the maintainer it turned out
it won't provide it anytime soon as musl strives to only implement well
standardized functions.

In order to not limit building libgpiosim and tests to glibc only, let's
replace twalk_r() with twalk() and use global variables for the state.
It's not a big deal as we're already using a global root node and a mutex
to protect it.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-02-14 09:13:59 +01:00
Bartosz Golaszewski
936785d36e gpiosim: fix error reporting in gpiosim_bank_hog_line()
We don't return errno numbers from public functions - we set errno and
indicate an error by returning -1 or NULL depending on the function
signature. Make gpiosim_bank_hog_line() consistent with that behavior.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-01-02 15:31:58 +01:00
Bartosz Golaszewski
43fed2c87c gpiosim: add missing fcntl.h include
The fcntl.h header is needed for openat() and mkdirat() that the gpiosim
code uses. It seems to be pulled in indirectly on most toolchains but for
the sake of correctness, include it explicitly.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-09 11:09:07 +01:00
Bartosz Golaszewski
3006d924e4 gpiosim: get the process name using prctl()
program_invocation_short_name is set once at the program's start. If we
change the process name using prctl(), we need to retrieve it using the
same system call as program_invocation_short_name will not be updated.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-09 10:06:53 +01:00
Bartosz Golaszewski
ba0fa90b8b gpiosim: fix a resource leak
If we set the number of lines to x, set line names or hogs for lines
from 0 through x, then set the number of lines to (x - y), we will not
unlink all the line directories as we only iterate up to num_lines in
bank_release().

Allocate a small structure for every line we create a lineX directory
for and iterate over it in bank_release() to know exactly which ones
need freeing.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-07 10:14:48 +01:00
Bartosz Golaszewski
2064a05984 gpiosim: use wrappers around container_of()
Simplify the code a bit by providing helpers that extract the underlying
structure from struct refcount using container_of().

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-07 10:14:48 +01:00
Bartosz Golaszewski
0acc1fb29e gpiosim: rename HOG_DIR to DIRECTION
The enum itself should just define direction settings, it's the functions
that use it that should refer to hogging.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
2022-12-07 09:53:28 +01:00
Bartosz Golaszewski
3fe7de7a65 treewide: apply formatting changes with clang-format
Use linux kernel's .clang-format file to automatically improve the coding
style of libgpiod's C code base. We don't import the file into the
repository as it's not perfect and certain converted fragments were
rolled back because they looked better before the conversion.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-07 09:45:06 +01:00
Bartosz Golaszewski
e065ef962a treewide: use C enum types explicitly
Use enum types explicitly across the entire C API. Modern compilers can
catch some bugs with enums and it also helps IDEs and general readability.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
[Viresh: make rust bindings work with negative enum values]
Co-authored-by: Viresh Kumar <viresh.kumar@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
2022-12-07 09:42:02 +01:00
Bartosz Golaszewski
b7ba732e6a treewide: libgpiod v2 implementation
This commit is the result of a squash of all the commits created during
the development of libgpiod v2. This is done to preserve bisectability
of the git tree. It contains significant re-writes of the core C library
as well as C++ and python bindings. The tools have been adjusted to work
with the new API but have not undergone significant changes yet.

This commit contains the bulk of the work on libgpiod v2 but the
development continues with more changes planned.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
[Ben: fix format specifier for uint64_t]
Signed-off-by: Ben Hutchings <ben.hutchings@mind.be>
[Viresh: Fix ioctl number for gpiod_line_request_reconfigure_lines()]
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
[Kent: bug fixes, improvements, better pointer contracts, type strictness]
Signed-off-by: Kent Gibson <warthog618@gmail.com>
2022-10-31 11:15:14 +01:00
Bartosz Golaszewski
d5ea8c9ed1 gpiosim: initialize the bank's num_lines field to 1
If we create a simulated chip and never set the num_lines property, the
num_lines field remains set to 0 and the potential lineX and hog
directories within the bank's directory will not get removed so the
removal of the bank's and device's directories will fail too leaving
a dangling chip in place.

Initialize bank->num_lines to 1 to avoid this issue.

Fixes: a2f6e8cd35 ("libgpiosim: new library for controlling the gpio-sim module")
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-04-25 14:41:55 +02:00
Bartosz Golaszewski
38f57b1084 gpiosim: fix file descriptor leak
If a bank gets released before the device is disabled, the sysfs file
descriptor of that bank is leaked. close() it when releasing the bank.

While at it: improve two other details - unlink the configfs directory
after closing the file descriptor associated with it and remove an
unnecessary check when closing the sysfs descriptor when disabling the
device.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-03-18 15:31:24 +01:00
Bartosz Golaszewski
54957b0572 gpiosim: add an enum representing line active state
In order to avoid confusion and stress the fact that "value" represents
the logical state of a GPIO line, add a two-value enum with the names
"ACTIVE" and "INACTIVE" to the libgpiosim API.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-03-09 10:14:33 +01:00
Bartosz Golaszewski
d7d0aae7b9 gpiosim: use size_t in the public interface where applicable
Use the standard integer type for representing sizes in the API of
libgpiod wherever it makes sense.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-03-09 10:09:24 +01:00
Bartosz Golaszewski
ffb006dd4a gpiosim: fix a memory leak
If a bank gets dropped when the device is still enabled and before the
parent device, we leak the device path and chip name strings. This makes
sure we always free them.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-02-23 22:14:17 +01:00
Bartosz Golaszewski
a2f6e8cd35 libgpiosim: new library for controlling the gpio-sim module
Add a C library for controlling the gpio-sim kernel module from various
libgpiod test suites. This aims at replacing the old gpio-mockup module
and its user-space library - libgpio-mockup - in the project's tree.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2022-02-10 14:42:44 +01:00