From 3f731241cb07090ed54b3b3b9e7df1fd20d86506 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 14 Apr 2016 10:14:38 -0600 Subject: [PATCH] fs/inode/, fs/vfs/, and sched/task/: File and socket descriptors are no longer allocated for kernel threads. They must use SYSLOG for output and the low-level psock interfaces for network I/O. This saves a little memory which might be important for small footprint configurations. --- ChangeLog | 6 ++++- fs/inode/fs_files.c | 53 +++++++++++++++++++++----------------- fs/inode/fs_foreachinode.c | 1 + fs/inode/fs_inode.c | 3 ++- fs/vfs/fs_getfilep.c | 12 +++++---- sched/task/task_create.c | 29 ++++++++++++++------- 6 files changed, 63 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index 675c984e20..2455cd6d27 100755 --- a/ChangeLog +++ b/ChangeLog @@ -11642,4 +11642,8 @@ * net/sockets/listen.c and accept.c and include/nuttx/net: Separate out psock_listen() and psock_accepti() for internal OS usage (2016-04-14). - + * fs/inode/, fs/vfs/, and sched/task/: File and socket descriptors are + no longer allocated for kernel threads. They must use SYSLOG for + output and the low-level psock interfaces for network I/O. This + saves a little memory which might be important for small footprint + configurations (2015-04-14). diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index 8366b55ce7..84f78ee400 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/inode/fs_files.c * - * Copyright (C) 2007-2009, 2011-2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011-2013, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -51,22 +51,6 @@ #include "inode/inode.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -222,9 +206,18 @@ int file_dup2(FAR struct file *filep1, FAR struct file *filep2) } list = sched_getfiles(); - DEBUGASSERT(list); - _files_semtake(list); + /* The file list can be NULL under two cases: (1) One is an obscure + * cornercase: When memory management debug output is enabled. Then + * there may be attempts to write to stdout from malloc before the group + * data has been allocated. The other other is (2) if this is a kernel + * thread. Kernel threads have no allocated file descriptors. + */ + + if (list != NULL) + { + _files_semtake(list); + } /* If there is already an inode contained in the new file structure, * close the file and release the inode. @@ -278,7 +271,11 @@ int file_dup2(FAR struct file *filep1, FAR struct file *filep2) } } - _files_semgive(list); + if (list != NULL) + { + _files_semgive(list); + } + return OK; /* Handler various error conditions */ @@ -291,7 +288,11 @@ errout_with_inode: errout_with_ret: err = -ret; - _files_semgive(list); + + if (list != NULL) + { + _files_semgive(list); + } errout: set_errno(err); @@ -312,8 +313,10 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, int minfd) FAR struct filelist *list; int i; + /* Get the file descriptor list. It should not be NULL in this context. */ + list = sched_getfiles(); - DEBUGASSERT(list); + DEBUGASSERT(list != NULL); _files_semtake(list); for (i = minfd; i < CONFIG_NFILE_DESCRIPTORS; i++) @@ -349,10 +352,12 @@ int files_close(int fd) FAR struct filelist *list; int ret; - /* Get the thread-specific file list */ + /* Get the thread-specific file list. It should never be NULL in this + * context. + */ list = sched_getfiles(); - DEBUGASSERT(list); + DEBUGASSERT(list != NULL); /* If the file was properly opened, there should be an inode assigned */ diff --git a/fs/inode/fs_foreachinode.c b/fs/inode/fs_foreachinode.c index 41ef0f0160..9af70dfbf3 100644 --- a/fs/inode/fs_foreachinode.c +++ b/fs/inode/fs_foreachinode.c @@ -52,6 +52,7 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ + /* Is it better to allocate the struct inode_path_s from the heap? or * from the stack? This decision depends on how often this is down and * how much stack space you can afford. diff --git a/fs/inode/fs_inode.c b/fs/inode/fs_inode.c index 52985f8ad7..e2e86eaa00 100644 --- a/fs/inode/fs_inode.c +++ b/fs/inode/fs_inode.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/inode/fs_inode.c * - * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011-2012, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -57,6 +57,7 @@ /**************************************************************************** * Private Types ****************************************************************************/ + /* Implements a re-entrant mutex for inode access. This must be re-entrant * because there can be cycles. For example, it may be necessary to destroy * a block driver inode on umount() after a removable block device has been diff --git a/fs/vfs/fs_getfilep.c b/fs/vfs/fs_getfilep.c index 165d00434c..79daf09b65 100644 --- a/fs/vfs/fs_getfilep.c +++ b/fs/vfs/fs_getfilep.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/vfs/fs_getfilep.c * - * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -91,12 +91,14 @@ FAR struct file *fs_getfilep(int fd) list = sched_getfiles(); - /* The file list can be NULL under one obscure cornercase: When memory - * management debug output is enabled. Then there may be attempts to - * write to stdout from malloc before the group data has been allocated. + /* The file list can be NULL under two cases: (1) One is an obscure + * cornercase: When memory management debug output is enabled. Then + * there may be attempts to write to stdout from malloc before the group + * data has been allocated. The other other is (2) if this is a kernel + * thread. Kernel threads have no allocated file descriptors. */ - if (!list) + if (list == NULL) { errcode = EAGAIN; goto errout; diff --git a/sched/task/task_create.c b/sched/task/task_create.c index 1ced3b9515..1cc59793cc 100644 --- a/sched/task/task_create.c +++ b/sched/task/task_create.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/task/task_create.c * - * Copyright (C) 2007-2010, 2013-2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2010, 2013-2014, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -85,7 +85,8 @@ ****************************************************************************/ static int thread_create(FAR const char *name, uint8_t ttype, int priority, - int stack_size, main_t entry, FAR char * const argv[]) + int stack_size, main_t entry, + FAR char * const argv[]) { FAR struct task_tcb_s *tcb; pid_t pid; @@ -115,14 +116,20 @@ static int thread_create(FAR const char *name, uint8_t ttype, int priority, } #endif - /* Associate file descriptors with the new task */ - #if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 - ret = group_setuptaskfiles(tcb); - if (ret < OK) + /* Associate file descriptors with the new task. Exclude kernel threads; + * kernel threads do not have file or socket descriptors. They must use + * SYSLOG for output and the low-level psock interfaces for network I/O. + */ + + if (ttype != TCB_FLAG_TTYPE_KERNEL) { - errcode = -ret; - goto errout_with_tcb; + ret = group_setuptaskfiles(tcb); + if (ret < OK) + { + errcode = -ret; + goto errout_with_tcb; + } } #endif @@ -228,7 +235,8 @@ errout: int task_create(FAR const char *name, int priority, int stack_size, main_t entry, FAR char * const argv[]) { - return thread_create(name, TCB_FLAG_TTYPE_TASK, priority, stack_size, entry, argv); + return thread_create(name, TCB_FLAG_TTYPE_TASK, priority, stack_size, + entry, argv); } #endif @@ -251,5 +259,6 @@ int task_create(FAR const char *name, int priority, int kernel_thread(FAR const char *name, int priority, int stack_size, main_t entry, FAR char * const argv[]) { - return thread_create(name, TCB_FLAG_TTYPE_KERNEL, priority, stack_size, entry, argv); + return thread_create(name, TCB_FLAG_TTYPE_KERNEL, priority, stack_size, + entry, argv); }