Skip to content

Conversation

@Zhangshoukui
Copy link
Contributor

Summary

ncrease the reference count of filep,fs_getfilep adds +1 to the reference count. After the call is complete, run the fs_putfilep command to release the reference count.When the reference count reaches 0, the file is actually closed.

Impact

This prevents the file from being closed while it is being read or written

Testing

@Zhangshoukui
Copy link
Contributor Author

Sorry, due to my irregular rebase, this PR is closed and a new trace is used.

image

./tools/configure.sh -l stm3240g-eval:knxwm
make -j7
I failed to build this configuration locally, any good suggestions?If not, I can only verify it through github's CI environment

@xiaoxiang781216
Copy link
Contributor

Sorry, due to my irregular rebase, this PR is closed and a new trace is used.

image

./tools/configure.sh -l stm3240g-eval:knxwm make -j7 I failed to build this configuration locally, any good suggestions?If not, I can only verify it through github's CI environment

the code size exceeds the reserved space, either:

  1. optimize the code
  2. extend the reserved space in ld script

Copy link
Contributor

@anchao anchao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a complete feature,please merge all patches into one

@Zhangshoukui
Copy link
Contributor Author

@anchao ./tools/configure.sh -l stm3240g-eval:knxwm
There are also configurations that do not compile locally, is this a general configuration, and is it reasonable to add it to CI?

@anchao
Copy link
Contributor

anchao commented Sep 6, 2024

@anchao ./tools/configure.sh -l stm3240g-eval:knxwm There are also configurations that do not compile locally, is this a general configuration, and is it reasonable to add it to CI?

You could set the Kconfig default value of CONFIG_FS_REFCOUNT to n to keep the original configuration without any overhead.

@Zhangshoukui Zhangshoukui force-pushed the master branch 3 times, most recently from 5873bd9 to a356d27 Compare September 12, 2024 06:40
@xiaoxiang781216
Copy link
Contributor

@Zhangshoukui please fix the conflict.

Zhangshoukui and others added 3 commits September 13, 2024 09:29
…eading writing...

Signed-off-by: Shoukui Zhang <zhangshoukui@xiaomi.com>
   60 |   FAR struct inode *inode;
      |                     ^~~~~

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
task1:
The refs is 0 but the inode has not been released
task2:
Executing files_duplist, get filep with refs is zero, but the inode has not yet released.

[    0.530600] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: backtrace:
[    0.530800] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x4045172>] backtrace_unwind+0x241/0x244
[    0.531200] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x4009150>] sched_backtrace+0x27/0x4c
[    0.531500] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x402f0fa>] sched_dumpstack+0x3d/0xa0
[    0.531700] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x4002c90>] _assert+0x1e3/0x564
[    0.532000] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x40221f6>] __assert+0x19/0x24
[    0.532200] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x41fe376>] tcp_stop_monitor+0x61/0x6c
[    0.532500] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x41fe19e>] tcp_close+0xad/0xdc
[    0.532700] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x41ecc18>] inet_close+0x7b/0x8c
[    0.533000] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x41ec374>] psock_close+0x27/0x70
[    0.533300] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x420b68e>] sock_file_close+0x15/0x3c
[    0.533500] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x4206704>] file_close+0x1f/0x80
[    0.533800] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x4205278>] files_duplist+0x39f/0x45c
[    0.534100] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x4000e3c>] group_setuptaskfiles+0x57/0xc0
[    0.534300] [CPU1] [ 6] [ ALERT] [ap] sched_dumpstack: [ 6] [<0x400bc6c>] nxtask_init+0x5b/0x13c

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
@Zhangshoukui
Copy link
Contributor Author

@Zhangshoukui please fix the conflict.

done

@Zhangshoukui
Copy link
Contributor Author

@anchao help me review this PR. thanks

@anchao
Copy link
Contributor

anchao commented Sep 13, 2024

@anchao help me review this PR. thanks

please set CONFIG_FS_REFCOUNT default value to n to keep the original configuration, we should not add unnecessary code size loads to IoT developers, especially for some chips that may only have 32k available RAM

@Zhangshoukui
Copy link
Contributor Author

@anchao I do not think this code is unnecessary, for the operation of a system security is the first.At present, CI has passed, I think it can be approve

@xiaoxiang781216
Copy link
Contributor

xiaoxiang781216 commented Sep 14, 2024

@anchao help me review this PR. thanks

please set CONFIG_FS_REFCOUNT default value to n to keep the original configuration, we should not add unnecessary code size loads to IoT developers, especially for some chips that may only have 32k available RAM

The default value should keep as safe as possible, since not all users understand the fs internal implementation. @Zhangshoukui let's change default value to !DEFAULT_SMALL.

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
arm-none-eabi-ld: /github/workspace/sources/nuttx/nuttx section `.bss' will not fit in region `ksram'
arm-none-eabi-ld: region `ksram' overflowed by 16 bytes

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
@Zhangshoukui
Copy link
Contributor Author

@xiaoxiang781216 help me review this PR.thanks

@xiaoxiang781216 xiaoxiang781216 merged commit 756faf3 into apache:master Sep 17, 2024
@yamt
Copy link
Contributor

yamt commented Oct 21, 2024

This prevents the file from being closed while it is being read or written

what happens w/o this enabled? a crash?

default !DEFAULT_SMALL
---help---
Enable will Records the number of filep references. The file is
actually closed when the count reaches 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my impression is that this kind of things should not be a user-visible option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, it should be used by default and should not be configured by the user, but it actually increases the code size, so it is configurable.Any better suggestions? I can try to optimize

@Zhangshoukui
Copy link
Contributor Author

This prevents the file from being closed while it is being read or written

what happens w/o this enabled? a crash?

Prevent multithreaded operations on the same filep when one is in r/w and the other is closed

@yamt
Copy link
Contributor

yamt commented Oct 21, 2024

This prevents the file from being closed while it is being read or written

what happens w/o this enabled? a crash?

Prevent multithreaded operations on the same filep when one is in r/w and the other is closed

my question is, why do you need to prevent it?

@Zhangshoukui
Copy link
Contributor Author

This prevents the file from being closed while it is being read or written

what happens w/o this enabled? a crash?

Prevent multithreaded operations on the same filep when one is in r/w and the other is closed

my question is, why do you need to prevent it?

This commit can solve the above problem and avoid the crash, in turn, why let such an operation crash? Reference counting makes the read and write process more robust

@yamt
Copy link
Contributor

yamt commented Oct 21, 2024

This prevents the file from being closed while it is being read or written

what happens w/o this enabled? a crash?

Prevent multithreaded operations on the same filep when one is in r/w and the other is closed

my question is, why do you need to prevent it?

This commit can solve the above problem and avoid the crash, in turn, why let such an operation crash? Reference counting makes the read and write process more robust

if this is to fix a crash, i don't understand why it should be optional.

@xiaoxiang781216
Copy link
Contributor

The patch tries to fix this common issue: thread 1 call read(fd, ...) and block, thread2 call close(fd).
without this patch, read will access the memory which is freed by close.

@xiaoxiang781216
Copy link
Contributor

This prevents the file from being closed while it is being read or written

what happens w/o this enabled? a crash?

Prevent multithreaded operations on the same filep when one is in r/w and the other is closed

my question is, why do you need to prevent it?

This commit can solve the above problem and avoid the crash, in turn, why let such an operation crash? Reference counting makes the read and write process more robust

if this is to fix a crash, i don't understand why it should be optional.

Yes, I also think it should be always enabled, but @anchao think this is a corner case and should give the user the freedom to disable it.

@anchao
Copy link
Contributor

anchao commented Oct 21, 2024

This commit can solve the above problem and avoid the crash, in turn, why let such an operation crash? Reference counting makes the read and write process more robust

if this is to fix a crash, i don't understand why it should be optional.

Yes, I also think it should be always enabled, but @anchao think this is a corner case and should give the user the freedom to disable it.

But this feature consumes a lot of rom size. e.g on ARM32 platform, the flash usage will increase by ~500 bytes caused by bl/blx instructions are inserted in many places in the fs.

@anchao
Copy link
Contributor

anchao commented Oct 21, 2024

We should ensure that NuttX remains compact and lightweight, rather than expanding endlessly like Linux. @xiaoxiang781216 @yamt

@yamt
Copy link
Contributor

yamt commented Oct 21, 2024

This commit can solve the above problem and avoid the crash, in turn, why let such an operation crash? Reference counting makes the read and write process more robust

if this is to fix a crash, i don't understand why it should be optional.

Yes, I also think it should be always enabled, but @anchao think this is a corner case and should give the user the freedom to disable it.

But this feature consumes a lot of rom size. e.g on ARM32 platform, the flash usage will increase by ~500 bytes caused by bl/blx instructions are inserted in many places in the fs.

do you have a suggestion of an alternative fix for the crash?
to me, saving 500 bytes doesn't sound like a good reason to leave a crashing bug.

@yamt
Copy link
Contributor

yamt commented Oct 21, 2024

We should ensure that NuttX remains compact and lightweight, rather than expanding endlessly like Linux. @xiaoxiang781216 @yamt

i agree.
i hope it doesn't mean to leave crashing bugs though.
also, i suspect moderate continuous size increase is natural for a software like this.

@anchao
Copy link
Contributor

anchao commented Oct 21, 2024

Most RTOS developers typically do not do the following things:

  1. Open and close file descriptors after system bringup frequently.
  2. Open and close file descriptors via different task contexts.

FS_REFCOUNT enhances the detection of such issues.

do you have a suggestion of an alternative fix for the crash? to me, saving 500 bytes doesn't sound like a good reason to leave a crashing bug.

if disable FS_REFCOUNT, I think we could add some error catch and print error log on failure case, which will help developers identify issues more quickly.

@yamt
Copy link
Contributor

yamt commented Oct 23, 2024

Most RTOS developers typically do not do the following things:

1. Open and close file descriptors after system bringup frequently.

2. Open and close file descriptors via different task contexts.

FS_REFCOUNT enhances the detection of such issues.

i guess what you need is a config option or whatever which allows users to
limit themselves to a small restricted subset of posix semantics, which doesn't need
these reference counting.

as we don't have such a mechanism right now, i suppose we need to enable this reference counting
(or whatever fix for the crash) unconditionally.

do you have a suggestion of an alternative fix for the crash? to me, saving 500 bytes doesn't sound like a good reason to leave a crashing bug.

if disable FS_REFCOUNT, I think we could add some error catch and print error log on failure case, which will help developers identify issues more quickly.

do you have an idea how to detect the failure cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants