Skip to content

Removed artificial PATH_MAX limits.#2025

Open
petterreinholdtsen wants to merge 2 commits intouxlfoundation:masterfrom
petterreinholdtsen:no-more-path-max
Open

Removed artificial PATH_MAX limits.#2025
petterreinholdtsen wants to merge 2 commits intouxlfoundation:masterfrom
petterreinholdtsen:no-more-path-max

Conversation

@petterreinholdtsen
Copy link
Copy Markdown
Contributor

Rewrote all code using PATH_MAX to instead dynamically allocate the required buffers. As the actually max path length depend on the file system used, it is not a good idea to pretend a compiled in buffer limit will work everywhere.

This also fixes build problems detected on GNU Hurd.

Type of change

  • bug fix - change that fixes an issue
  • new feature - change that adds functionality
  • tests - change in tests
  • infrastructure - change in infrastructure and CI
  • documentation - documentation update

Tests

  • added - required for new features and some bug fixes
  • not needed

Documentation

  • updated in # - add PR number
  • needs to be updated
  • not needed

Breaks backward compatibility

  • Yes
  • No
  • Unknown

@petterreinholdtsen petterreinholdtsen force-pushed the no-more-path-max branch 14 times, most recently from 39d0f6a to a6123da Compare March 28, 2026 10:25
@petterreinholdtsen petterreinholdtsen force-pushed the no-more-path-max branch 2 times, most recently from 3a6c982 to 69fad16 Compare March 31, 2026 18:46
@Alexandr-Konovalov
Copy link
Copy Markdown
Contributor

What is the reason for memory management be C-style vs using unique_ptr etc?

@petterreinholdtsen
Copy link
Copy Markdown
Contributor Author

petterreinholdtsen commented Apr 1, 2026 via email

@petterreinholdtsen
Copy link
Copy Markdown
Contributor Author

petterreinholdtsen commented Apr 1, 2026 via email

Copy link
Copy Markdown
Contributor

@aleksei-fedotov aleksei-fedotov left a comment

Choose a reason for hiding this comment

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

Given that the patch does not take into account hard limitation on the length of a relative path, I conclude that the limitations on the maximum path length is not a real motivator for the patch. Therefore, to avoid cumbersome process of measuring performance penalties potentially implied by dynamic memory allocations, I would suggest simply defining PATH_MAX constant on the systems where it is missing.

Below are the comments to start with if we still want to go that way.

static bool try_read_cgroup_v1_num_cpus_from(const char* dir, int& num_cpus) {
char path[PATH_MAX] = {0};
if (std::snprintf(path, PATH_MAX, "%s/cpu.cfs_quota_us", dir) < 0)
std::size_t pathlen = strlen(dir) + 30;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why +30? The longest addition is only 18 bytes, if I am not mistaken.

char path[PATH_MAX] = {0};
if (std::snprintf(path, PATH_MAX, "%s/cpu.cfs_quota_us", dir) < 0)
std::size_t pathlen = strlen(dir) + 30;
char *path = (char*)calloc(pathlen, 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Usually, allocating memory dynamically takes a way more time than work with static arrays. Running over arrays using strlen can also add some penalties. We need to look at the performance data to make a weighed decision.

std::size_t pathlen = strlen(dir) + 30;
char *path = (char*)calloc(pathlen, 1);
if (std::snprintf(path, pathlen, "%s/cpu.cfs_quota_us", dir) < 0) {
free(path);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use RAII approach. It is safer and cleaner.

@petterreinholdtsen
Copy link
Copy Markdown
Contributor Author

petterreinholdtsen commented Apr 1, 2026 via email

@Alexandr-Konovalov
Copy link
Copy Markdown
Contributor

Can we make one step further and look at PATH_MAX semantics under Linux? According to my understanding, it limits number of bytes to be written by "system" to a buffer of unspecified size. As war as I can tell, TBB don't use such API, so for TBB PATH_MAX is used only as size of a buffer on stack that has enough size. So, can we remove PATH_MAX and set it manually to OS-independent value? There is no need in dynamic memory allocation after that.

@petterreinholdtsen
Copy link
Copy Markdown
Contributor Author

petterreinholdtsen commented Apr 1, 2026 via email

@Alexandr-Konovalov
Copy link
Copy Markdown
Contributor

There are actually 2 different problem here we are discussing. 1st is PATH_MAX dependence, and 2nd is artificial limitations on paths. Sure, if 2nd is resolved, the 1st is also disappeared. But the developers are not happy with possible impact of dynamic memory allocation on performance, so thinking about possibility to keep buffers on stack somehow.

For microbenchmark with only
volatile int num_cpus = tbb::detail::r1::cgroup_info<>::parse_cpu_constraints(tbb::detail::r1::default_cgroup_settings{});
I see ~1% performance drop, and most time-consuming things are syscalls. Hope this is the relevant benchmark.

How do you imagine determining "enough size"? What is the program is
ported to a system with deeper path depths in the future? Do you want
to keep extending the limit indefinitely to avoid dynamically handle any
size?

Sorry, I think only about cgroup_info.h. That is not the complete picture.

static struct ap_data_t {
char _path[PATH_MAX+1];
char *_path;
std::size_t _capacity;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is _capacity really need to be saved? If I understand correctly, it only used for doubling the buffer, so probably can be lokal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I believe it is required on Windows, and for consistency I used it the same way on non-Windows platforms. I was tempted to switch also this to use std::string, but decided against it as std::string is not much used in this library.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe it is required on Windows.

I'm not sure in that. It seems it only used to keep value of MAX_PATH and then zeroed in case of unsuccessful allocation. At which part of the code are you talking about?

@Alexandr-Konovalov
Copy link
Copy Markdown
Contributor

Mostly because I am mostly familir with C style, and could not get unique_ptr with char* working with the surrounding code. Can you get it working?

It can be something like

        std::unique_ptr<char[]> dir(new char[dirlen]);
        __TBB_ASSERT(*pcd.relative_path, nullptr);
        if (0 <= std::snprintf(dir.get(), dirlen, "%s/%s", cg_cfg.sys_fs_cgroup_dir_path,
                               pcd.relative_path))
            try_read_cgroup_num_cpus_from(dir.get(), num_cpus, pcd.version);

I think.

@petterreinholdtsen petterreinholdtsen force-pushed the no-more-path-max branch 3 times, most recently from b5223e1 to c3a2c7b Compare April 2, 2026 20:11
Petter Reinholdtsen added 2 commits April 3, 2026 09:01
Rewrote all code using PATH_MAX to instead dynamically allocate the required
buffers.  As the actually max path length depend on the file system used, it
is not a good idea to pretend a compiled in buffer limit will work everywhere.

This also fixes build problems detected on GNU Hurd.
@petterreinholdtsen
Copy link
Copy Markdown
Contributor Author

I switched to unique_ptr everywhere except in ap_data. I do not fully understand the purpose of the internal ap_data_t structure, and am unsure about the consequences for this code.

@petterreinholdtsen petterreinholdtsen changed the title Removed artificial limits using PATH_MAX. Removed artificial PATH_MAX limits. Apr 3, 2026
@petterreinholdtsen
Copy link
Copy Markdown
Contributor Author

petterreinholdtsen commented Apr 3, 2026 via email

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants