fix: validate and cap thread and queue counts

- std::stoi threw std::out_of_range for a func.readdir concurrency or
  queue depth above INT_MAX, e.g. cor:99999999999. Given at mount,
  mergerfs aborted. Set at runtime through .mergerfs, the daemon shut
  down and the mount went away, or with process-thread-count>0 the
  request never got a reply. Parse with str::from, which reports the
  overflow, and reject the value like any other malformed one.
- Reject a readdir queue depth whose product with the concurrency
  overflows an int, e.g. cor:2147483647:2147483647, instead of
  wrapping it.
- Limit read-thread-count, process-thread-count and the cor/cosr pool
  size to 64 threads per pool, after resolving automatic and
  CPU-divided counts, and log a warning when clamping. Resolve
  negative FUSE counts without overflowing on INT_MIN, and drop the
  unreachable negative readdir concurrency branch.
- process-thread-queue-depth is multiplied by the process thread count.
  Saturate the product at INT_MAX instead of overflowing.
- Correct the docs: cor/cosr counts accept digits only and 0 means the
  core count, at most 8; a queue depth of 0 means 2 per thread;
  read-thread-count=0 with process-thread-count=0 is 2 read threads
  and max(2, cores - 2) process threads, at most 8; the queue limit is
  depth times process threads, each read thread holding one in-flight
  buffer of its own. Point at threads.md for how the settings combine.
- Document the per-read-thread /dev/fuse clones and their fallback.
- tests.cpp covers the overflow cases.

Performance: default thread counts are unchanged (8 on hosts with 8 or
more cores). Only explicit or CPU-divided counts above 64, which
2.42.0 honored, are reduced.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:33:27 -05:00
parent 61597c8dfd
commit d679fdd265
7 changed files with 143 additions and 44 deletions

View file

@ -22,11 +22,16 @@
#include "fuse_readdir_cosr.hpp"
#include "fuse_readdir_seq.hpp"
#include "from_string.hpp"
#include "syslog.hpp"
#include "thread_pool.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <regex>
// Regex:
@ -39,7 +44,8 @@ void
_read_cfg(const std::string_view str_,
std::string &type_,
int &concurrency_,
int &max_queue_depth_)
int &max_queue_depth_,
const bool warn_)
{
std::string type;
int concurrency;
@ -56,11 +62,18 @@ _read_cfg(const std::string_view str_,
re);
if(matched)
{
type = match[1];
int rv = 0;
// A count too large for an int leaves type empty, so the value
// is rejected as invalid instead of throwing.
if(match[2].matched)
concurrency = std::stoi(match[2]);
if(match[3].matched)
max_queue_depth = std::stoi(match[3]);
rv = str::from(std::string_view(match[2].first,match[2].length()),
&concurrency);
if((rv == 0) && match[3].matched)
rv = str::from(std::string_view(match[3].first,match[3].length()),
&max_queue_depth);
if(rv == 0)
type = match[1];
}
if(concurrency == 0)
@ -69,18 +82,24 @@ _read_cfg(const std::string_view str_,
if(concurrency > 8)
concurrency = 8;
}
else if(concurrency < 0)
{
concurrency = (std::thread::hardware_concurrency() / std::abs(concurrency));
}
if(concurrency <= 0)
concurrency = 1;
const bool clamped = (concurrency > ThreadPool::max_thread_count());
concurrency = std::min(concurrency,ThreadPool::max_thread_count());
if(max_queue_depth <= 0)
max_queue_depth = 2;
max_queue_depth *= concurrency;
// Reject a queue depth whose product overflows instead of wrapping.
if(max_queue_depth > (std::numeric_limits<int>::max() / concurrency))
type.clear();
else
max_queue_depth *= concurrency;
if(warn_ && clamped && ((type == "cor") || (type == "cosr")))
SysLog::warning("func.readdir={} exceeds the thread limit; clamping to {} threads",
str_,ThreadPool::max_thread_count());
type_ = type;
concurrency_ = concurrency;
@ -94,7 +113,7 @@ FUSE::ReadDirFactory::valid(const std::string_view str_)
int max_queue_depth;
std::string type;
::_read_cfg(str_,type,concurrency,max_queue_depth);
::_read_cfg(str_,type,concurrency,max_queue_depth,false);
return ((type == "seq") ||
(type == "cosr") ||
@ -108,7 +127,7 @@ FUSE::ReadDirFactory::make(const std::string_view str_)
int max_queue_depth;
std::string type;
::_read_cfg(str_,type,concurrency,max_queue_depth);
::_read_cfg(str_,type,concurrency,max_queue_depth,true);
if(type == "seq")
return std::make_shared<FUSE::ReadDirSeq>();