Skip to content

Conversation

@rota1001
Copy link
Contributor

@rota1001 rota1001 commented Mar 26, 2025

After using insmod to insert the module, we open three terminals A, B, C.
In terminal C, use the following command to observe the info messages:

sudo dmesg --follow

In terminal A, B, we both use the following command:

sudo cat /dev/simrupt

Then, they print out the characters that are produced respectively.
Then, we kill terminal B. We will find that, in terminal A, it stops producing any character as well.

At this time, observing the terminal C, we will find that the reference count is 1, but the producer doesn't produce characters anymore.

[256191.736783] simrupt: [CPU#6] simrupt_tasklet_func in_softirq: 7 usec
[256191.736815] simrupt: [CPU#9] simrupt_work_func
[256191.748253] release, current cnt: 1

It is because the producer will stop producing characters when atomic_dec_and_test(&open_cnt) returns zero in simrupt_release.

static int simrupt_release(struct inode *inode, struct file *filp)
{
    pr_debug("simrupt: %s\n", __func__);
    if (atomic_dec_and_test(&open_cnt) == 0) {
        del_timer_sync(&timer);
        flush_workqueue(simrupt_workqueue);
        fast_buf_clear();
    }
    pr_info("release, current cnt: %d\n", atomic_read(&open_cnt));

    return 0;
}

Reading the comment of atomic_dec_and_test,we will find that it will return true if the resulting value of the variable is zero.

/**
 * atomic_dec_and_test() - atomic decrement and test if zero with full ordering
 ...
 * Return: @true if the resulting value of @v is zero, @false otherwise.
 */

Therefore, if we want producer to stop producing characters when the open_cnt becomes zero, we should do the following change.

- if (atomic_dec_and_test(&open_cnt) == 0) {
+ if (atomic_dec_and_test(&open_cnt)) {

As the result, it works normally.

In the original code, the producer stops producing characters
when atomic_dec_and_test(&open_cnt) returns zero, but it will
return zero when the resulting value of open_cnt is nonzero.
Therefore, in the original code, if the open_cnt changes from
2 to 1, it will stop producing characters, but it is supposed
to consistently produce characters until the open_cnt becomes
zero.

Therefore, we modify the code so that it stops producing characters
when atomic_dec_and_test(&open_cnt) returns one.
@jserv jserv merged commit 3b85299 into sysprog21:main Mar 26, 2025
@jserv
Copy link
Contributor

jserv commented Mar 26, 2025

Thank @rota1001 for contributing!

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.

2 participants