From patchwork Mon Oct 7 17:43:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Al Viro X-Patchwork-Id: 13825081 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [62.89.141.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CAFE1DDA0D for ; Mon, 7 Oct 2024 17:44:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=62.89.141.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728323043; cv=none; b=l+RUPST/s3JlyyITc9R746q01rX3Lfk3VW8Tikl0rIKaAk7DT5g/oTPSnq1eJGbM8oimGYig0vyBUUWXbDItBe5piY9+OiEU+oNrUvFMn2m5xfMhJcIXtNfdwAwVj1JTiZ9rD+sCmEDTJOPcqwzTRPW8VofFpcDUTbFYZLQI9lw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728323043; c=relaxed/simple; bh=ay+/NoFrXski6gA9i1bGz8+W2715gb0X5omzPWMQJBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UrT05fkX832BSdjjBMTuuYpLsKCMrB+W201N2bggqOk/tQFIgA5OMl0TmKYEowyWd78CkneOy7Ch3taz9alKCOlJFJMAlYmct9FFkyG3salIvzudo2DkKZbWm5v5Wg2Sus+os9Aniykq2xAgVn1hf+FmQHtWmMBMGOjcSc4Gd3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk; spf=none smtp.mailfrom=ftp.linux.org.uk; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b=N6QdDNlE; arc=none smtp.client-ip=62.89.141.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=ftp.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b="N6QdDNlE" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=psZlJdAmK1Anujk8Rz1uDQ/z4TSLLHrGmQ3IDEPrv+4=; b=N6QdDNlEqhnsmkU+NfW2tFNPGw Lw9pudSKWu7CWSXWuXO8UrSHdwahPlE2W/BLVLg1afadxcUIbzxtmc9Eup6Sr13oMyoFi1hfd8WWs lG4udFea3MxBHmwpSOVxrLdxSNXgRN7SojYEQrNpz3CC5QalEs/NYSAmG+jjWJUlKD/pq4AugXlwQ /WeGXAGxfy9YO0fhzwHuX6i/5MkG3cYtwQewpD20uj3KQDLyE1vb+zTz0+4Hf3oUq5Z/DMiQypJ0O o8nTEXVfEUbFaaUV9HkPQmylHmZ5ztjcSEjX4mtH99osSiaoPtV7uPK71WLV5lzFSbcWtIHEFXSWT j8Iefubw==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.98 #2 (Red Hat Linux)) id 1sxrm3-00000001f3K-0yjf; Mon, 07 Oct 2024 17:43:59 +0000 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, brauner@kernel.org Subject: [PATCH v3 05/11] fs/file.c: remove sanity_check and add likely/unlikely in alloc_fd() Date: Mon, 7 Oct 2024 18:43:52 +0100 Message-ID: <20241007174358.396114-5-viro@zeniv.linux.org.uk> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241007174358.396114-1-viro@zeniv.linux.org.uk> References: <20241007173912.GR4017910@ZenIV> <20241007174358.396114-1-viro@zeniv.linux.org.uk> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Sender: Al Viro From: Yu Ma alloc_fd() has a sanity check inside to make sure the struct file mapping to the allocated fd is NULL. Remove this sanity check since it can be assured by exisitng zero initilization and NULL set when recycling fd. Meanwhile, add likely/unlikely and expand_file() call avoidance to reduce the work under file_lock. Reviewed-by: Jan Kara Reviewed-by: Tim Chen Signed-off-by: Yu Ma Link: https://lore.kernel.org/r/20240717145018.3972922-2-yu.ma@intel.com Signed-off-by: Christian Brauner Signed-off-by: Al Viro --- fs/file.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/fs/file.c b/fs/file.c index 8e8f504782bf..90b8aa2378cc 100644 --- a/fs/file.c +++ b/fs/file.c @@ -496,7 +496,7 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags) if (fd < files->next_fd) fd = files->next_fd; - if (fd < fdt->max_fds) + if (likely(fd < fdt->max_fds)) fd = find_next_fd(fdt, fd); /* @@ -504,19 +504,21 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags) * will limit the total number of files that can be opened. */ error = -EMFILE; - if (fd >= end) + if (unlikely(fd >= end)) goto out; - error = expand_files(files, fd); - if (error < 0) - goto out; + if (unlikely(fd >= fdt->max_fds)) { + error = expand_files(files, fd); + if (error < 0) + goto out; - /* - * If we needed to expand the fs array we - * might have blocked - try again. - */ - if (error) - goto repeat; + /* + * If we needed to expand the fs array we + * might have blocked - try again. + */ + if (error) + goto repeat; + } if (start <= files->next_fd) files->next_fd = fd + 1; @@ -527,13 +529,6 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags) else __clear_close_on_exec(fd, fdt); error = fd; -#if 1 - /* Sanity check */ - if (rcu_access_pointer(fdt->fd[fd]) != NULL) { - printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd); - rcu_assign_pointer(fdt->fd[fd], NULL); - } -#endif out: spin_unlock(&files->file_lock); @@ -599,7 +594,7 @@ void fd_install(unsigned int fd, struct file *file) rcu_read_unlock_sched(); spin_lock(&files->file_lock); fdt = files_fdtable(files); - BUG_ON(fdt->fd[fd] != NULL); + WARN_ON(fdt->fd[fd] != NULL); rcu_assign_pointer(fdt->fd[fd], file); spin_unlock(&files->file_lock); return;