From nobody Sat Feb 7 11:38:17 2026 Received: from mail-dy1-f169.google.com (mail-dy1-f169.google.com [74.125.82.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AA6534252C for ; Fri, 23 Jan 2026 08:12:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769155954; cv=none; b=cXcfDmSQ5TWBVHjK5RicZYyjuS5c+/c+eHvZzrqCliCFpC7vhRhCWw6ErsRsKVoT7899qHObMBqu9iOKsY7jWv15cqz+OKPxUfr9QRIyTI0KrqgQi57FW1YpFHmOV5YRNQof9X0Dh9yVKqTxsfSCfC64sZPofGnwEbJrxZOByD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769155954; c=relaxed/simple; bh=Ecrm2+6N6Z/T/n+8rsIboMq5D9ci8gPIweMR0A03Q0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AzRQ0WImTcuU7J0qT6HxnhYHZIPQg2TYrs2idyGDkUaeA1usODkEjSc/2fj+qDNgDTLW01kf+uKJlabwzcVquVk6Y8PBISyI0U/ELLbLEFFk2UkoUS1GII3ynW+WiSg+3iqjnBtGzadozxt1xiJ7JaFcSm7dhhvgZbwguxooT9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=atcHtk62; arc=none smtp.client-ip=74.125.82.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="atcHtk62" Received: by mail-dy1-f169.google.com with SMTP id 5a478bee46e88-2b71557299dso2634371eec.1 for ; Fri, 23 Jan 2026 00:12:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769155952; x=1769760752; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pDt4ubMlKawjVjnkE1M7nH+9HIsoLEd8+NCdahhhvGE=; b=atcHtk62omvCxdYm9xtnlWgPE1Ax5tc1lyXL/jmJOCZTDyL+2jJKVESyXBhWHnB+Ux R+NQFxGt+OWE9IbCa9hm2IKRQAorVsyfu6QmjlTlpT/k5uT7Iw1sI4/yYwmCjKkyC2dV RKsJa2VKHm3ity2+kE8MRBP1n+JPz8Nwhus/rLwIQ5W8CabtbH6ILi8bu8GN/v/NNScW l+r1DNEqpiMiRe9sWDwIjw3F79iT7u/Rsq8e87407vZXEs3dH/En0ZKlekwEiVgkbKtc blcOgzAIDpG6u3wNdeL4d6hVpTYk9+AgK2kmi3A/M53Rv/qTuXhc5g+G6aQCy6gJ4NeT YZsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769155952; x=1769760752; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=pDt4ubMlKawjVjnkE1M7nH+9HIsoLEd8+NCdahhhvGE=; b=SkZXXNcseixqFe6u/wIjstT6hR911K+P4QrsF1htpDFNYc5m60NKaYNgG96iXJrGcc VVGBtDccOO6gh2cRc8p6nYOYEz1Aj+8HKzuOz8V9xzY2fZYi4udntS7VtmgKaJyItjuC XUxNLWOP34pQO331Tx/o5iP5PAj/EPx2AfrkoW1I/Zq3U+z7gk2UPX7BUFSmLXhjBMd7 333BDDJ+8pYOrcjvoUeQK1WyxUCw6+pzlX4az2RbKg90okeL82YjC+ZGiFEgLjyiIhWt fUs9iucF18sDyh4TuOleAVpTGTMIT/dh3PIluPFO4zWigbIsFHt/NyXJGIYZmvOhfNjI gjuA== X-Forwarded-Encrypted: i=1; AJvYcCXnpl9kR84Mxe/xpeyHAoaJyC8VOL1GUeTOpkzdMjV5gHaPjqY1GrY+KDJlVY4QPSlY03B0aG9SZq/J9Fc=@vger.kernel.org X-Gm-Message-State: AOJu0Yyg19AtzjTiztGJ/frTLxFh4g4wuRtiFyvllhw1clWWQ4rv1Kny aYk58/5+EOnrbGGa0h4HLsPyZ9bhHiZ4aR7KYxZZ4bwpPd7GYzuFbLTG X-Gm-Gg: AZuq6aKy3xiyfclVJLn+xVS8Hl+nrCcH/qt4l9/SXqCzmHerVa6vaGwDGx2TR8SBQnY 8l+z+DkUe3T1tPHIGIsvWxBZtLX96qTqyB54Qz4vDxkc+2FfkSFFTwlT/FltlxDrxgekHq73GSe z1jUFhmuGpaocTgEjQhA8iDNbc7iCPW5SaQD6U3wtuUoRco+M5H0zQqJejByaN/KIrQExUKrE+h jAQtkzsMXHzV5Sih9oiSMoaDD12vkUissb1bF+cwhCZlmB9V6cR3Abk/Zx3a2Oo7o3+lQKMFNUl Cef0Dcrgoq/fwp60dZXZ6y2wD+PX77T7DyRSys982O4EDXGCskLa2zMdFMh9DMpJ4xId2O/gjok O/7ipL6RKNFVEXuf+PTiOWdSkBSKuqjVms4YFuS9iNTik+4LlD+BoWj5++Dme+6xR0WldYEOCkB Dume0= X-Received: by 2002:a05:7300:a49a:b0:2b0:4c5f:c05c with SMTP id 5a478bee46e88-2b7399b0eeamr934441eec.4.1769155952292; Fri, 23 Jan 2026 00:12:32 -0800 (PST) Received: from debian ([74.48.213.230]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2b73a9e1ad3sm2055819eec.19.2026.01.23.00.12.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 00:12:32 -0800 (PST) From: Qiliang Yuan To: viro@zeniv.linux.org.uk Cc: brauner@kernel.org, jack@suse.cz, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, realwujing@gmail.com, yuanql9@chinatelecom.cn Subject: [PATCH v2] fs/file: optimize close_range() complexity from O(N) to O(Sparse) Date: Fri, 23 Jan 2026 03:12:21 -0500 Message-ID: <20260123081221.659125-1-realwujing@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260122171408.GF3183987@ZenIV> References: <20260122171408.GF3183987@ZenIV> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In close_range(), the kernel traditionally performs a linear scan over the [fd, max_fd] range, resulting in O(N) complexity where N is the range size. For processes with sparse FD tables, this is inefficient as it checks many unallocated slots. This patch optimizes __range_close() by using find_next_bit() on the open_fds bitmap to skip holes. This shifts the algorithmic complexity from O(Range Size) to O(Active FDs), providing a significant performance boost for large-range close operations on sparse file descriptor tables. Signed-off-by: Qiliang Yuan Signed-off-by: Qiliang Yuan Reviewed-by: Jan Kara --- v2: - Recalculate fdt after re-acquiring file_lock to avoid UAF if the table is expanded/reallocated during filp_close() or cond_resched(). v1: - Initial optimization using find_next_bit() on open_fds bitmap to skip holes, improving complexity to O(Active FDs). fs/file.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fs/file.c b/fs/file.c index 0a4f3bdb2dec..51ddcff0081a 100644 --- a/fs/file.c +++ b/fs/file.c @@ -777,23 +777,29 @@ static inline void __range_close(struct files_struct = *files, unsigned int fd, unsigned int max_fd) { struct file *file; + struct fdtable *fdt; unsigned n; =20 spin_lock(&files->file_lock); - n =3D last_fd(files_fdtable(files)); + fdt =3D files_fdtable(files); + n =3D last_fd(fdt); max_fd =3D min(max_fd, n); =20 - for (; fd <=3D max_fd; fd++) { + for (fd =3D find_next_bit(fdt->open_fds, max_fd + 1, fd); + fd <=3D max_fd; + fd =3D find_next_bit(fdt->open_fds, max_fd + 1, fd + 1)) { file =3D file_close_fd_locked(files, fd); if (file) { spin_unlock(&files->file_lock); filp_close(file, files); cond_resched(); spin_lock(&files->file_lock); + fdt =3D files_fdtable(files); } else if (need_resched()) { spin_unlock(&files->file_lock); cond_resched(); spin_lock(&files->file_lock); + fdt =3D files_fdtable(files); } } spin_unlock(&files->file_lock); --=20 2.51.0