Skip to contentSkip to conversation

fs: select: reduce stack usage in do_select()

PATCH In review Chao Yu 2 participants 3 messages Jul 16, 2023 VFS

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/2 fs: select: reduce stack usage in do_select()

struct poll_wqueues table caused the stack usage of do_select() to grow beyond the warning limit on 32-bit architectures w/ gcc.

fs/select.c: In function ‘do_select’:
fs/select.c:615:1: warning: the frame size of 1152 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Allocating dynamic memory in do_select() to fix this issue.

---
 fs/select.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
fs/select.c +16 -8
index 0ee55af1a55c..1b729494b4e9 100644
@@ -479,23 +479,29 @@ static inline void wait_key_set(poll_table *wait, unsigned long in,
 static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
 {
 	ktime_t expire, *to = NULL;
-	struct poll_wqueues table;
+	struct poll_wqueues *table;
 	poll_table *wait;
 	int retval, i, timed_out = 0;
 	u64 slack = 0;
 	__poll_t busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;
 	unsigned long busy_start = 0;
 
+	table = kmalloc(sizeof(struct poll_wqueues), GFP_KERNEL);
+	if (!table)
+		return -ENOMEM;
+
 	rcu_read_lock();
 	retval = max_select_fd(n, fds);
 	rcu_read_unlock();
 
-	if (retval < 0)
+	if (retval < 0) {
+		kfree(table);
 		return retval;
+	}
 	n = retval;
 
-	poll_initwait(&table);
-	wait = &table.pt;
+	poll_initwait(table);
+	wait = &table->pt;
 	if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
 		wait->_qproc = NULL;
 		timed_out = 1;
@@ -578,8 +584,8 @@ static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
 		wait->_qproc = NULL;
 		if (retval || timed_out || signal_pending(current))
 			break;
-		if (table.error) {
-			retval = table.error;
+		if (table->error) {
+			retval = table->error;
 			break;
 		}
 
@@ -604,12 +610,14 @@ static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
 			to = &expire;
 		}
 
-		if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
+		if (!poll_schedule_timeout(table, TASK_INTERRUPTIBLE,
 					   to, slack))
 			timed_out = 1;
 	}
 
-	poll_freewait(&table);
+	poll_freewait(table);
+
+	kfree(table);
 
 	return retval;
 }
Signature · 1 line
2.40.1
author
#1 lore raw

PATCH 2/2 fs: select: reduce stack usage in do_sys_poll()

struct poll_wqueues table caused the stack usage of do_sys_poll() to grow beyond the warning limit on 32-bit architectures w/ gcc.

fs/select.c: In function ‘do_sys_poll’:
fs/select.c:1053:1: warning: the frame size of 1328 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Allocating dynamic memory in do_sys_poll() to fix this issue.

---
 fs/select.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
fs/select.c +14 -7
index 1b729494b4e9..2b9db5c938d9 100644
@@ -982,7 +982,7 @@ static int do_poll(struct poll_list *list, struct poll_wqueues *wait,
 static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 		struct timespec64 *end_time)
 {
-	struct poll_wqueues table;
+	struct poll_wqueues *table;
 	int err = -EFAULT, fdcount, len;
 	/* Allocate small arguments on the stack to save memory and be
 	   faster - use long to make sure the buffer is aligned properly
@@ -992,8 +992,14 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
  	struct poll_list *walk = head;
  	unsigned long todo = nfds;
 
-	if (nfds > rlimit(RLIMIT_NOFILE))
-		return -EINVAL;
+	table = kmalloc(sizeof(struct poll_wqueues), GFP_KERNEL);
+	if (!table)
+		return -ENOMEM;
+
+	if (nfds > rlimit(RLIMIT_NOFILE)) {
+		err = -EINVAL;
+		goto out_table;
+	}
 
 	len = min_t(unsigned int, nfds, N_STACK_PPS);
 	for (;;) {
@@ -1019,9 +1025,9 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 		}
 	}
 
-	poll_initwait(&table);
-	fdcount = do_poll(head, &table, end_time);
-	poll_freewait(&table);
+	poll_initwait(table);
+	fdcount = do_poll(head, table, end_time);
+	poll_freewait(table);
 
 	if (!user_write_access_begin(ufds, nfds * sizeof(*ufds)))
 		goto out_fds;
@@ -1043,7 +1049,8 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 		walk = walk->next;
 		kfree(pos);
 	}
-
+out_table:
+	kfree(table);
 	return err;
 
 Efault:
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH 2/2] fs: select: reduce stack usage in do_sys_poll()

Quoted 5 lines · Chao Yu

struct poll_wqueues table caused the stack usage of do_sys_poll() to grow beyond the warning limit on 32-bit architectures w/ gcc.

fs/select.c: In function ‘do_sys_poll’:
fs/select.c:1053:1: warning: the frame size of 1328 bytes is larger than 1024 bytes [-Wframe-larger-than=]

That seems particularly high. But it's only 604 bytes, so half of the stack frame:

struct poll_wqueues {

        poll_table                 pt;                   /*     0     8 */
        struct poll_table_page *   table;                /*     8     4 */
        struct task_struct *       polling_task;         /*    12     4 */
        int                        triggered;            /*    16     4 */
        int                        error;                /*    20     4 */
        int                        inline_index;         /*    24     4 */
        struct poll_table_entry    inline_entries[18];   /*    28   576 */
        /* size: 604, cachelines: 10, members: 7 */
        /* last cacheline: 28 bytes */
};

Also, you can see it's deliberately sized to fit on the stack (see include/linux/poll.h). So you're completely destroying that optimisation.

You need to figure out why the stack is now so big. This isn't the right solution.