Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

driver/at86rf2xx: Fix possible race condition where at86rf2xx_configure returns to the wrong state if waited on finished state in at86rf2xx_setstate #7115

Merged
merged 1 commit into from
Jun 26, 2017

Conversation

DipSwitch
Copy link
Member

at86rf2xx_configure() could possibly return to the wrong state after updating the phy's configuration.

@DipSwitch DipSwitch added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: drivers Area: Device drivers Area: network Area: Networking labels May 31, 2017
@DipSwitch DipSwitch added this to the Release 2017.04 milestone May 31, 2017
@thomaseichinger
Copy link
Member

@DipSwitch Sorry, this one totally flew under my radar.

I still fail to see the relation between moving an unmatched check and the mentioned race condition. What was the wrong state you landed in?

@DipSwitch
Copy link
Member Author

It would try to return to AT86RF2XX_STATE_BUSY_TX_ARET in my case.

@thomaseichinger
Copy link
Member

Sorry, it took me a while. I think your change is good, the version below might still optimize the code size a bit.

uint8_t at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state)
{
    uint8_t old_state;

    /* make sure there is no ongoing transmission, or state transition already
     * in progress */
    do {
        old_state = at86rf2xx_get_status(dev);
    } while (old_state == AT86RF2XX_STATE_BUSY_RX_AACK ||
             old_state == AT86RF2XX_STATE_BUSY_TX_ARET ||
             old_state == AT86RF2XX_STATE_IN_PROGRESS)

    if (state == AT86RF2XX_STATE_FORCE_TRX_OFF) {
        _set_state(dev, AT86RF2XX_STATE_TRX_OFF, state);
        return old_state;
    }

    if (state == old_state) {
        return old_state;
    }

    /* we need to go via PLL_ON if we are moving between RX_AACK_ON <-> TX_ARET_ON */
    if ((old_state == AT86RF2XX_STATE_RX_AACK_ON &&
             state == AT86RF2XX_STATE_TX_ARET_ON) ||
        (old_state == AT86RF2XX_STATE_TX_ARET_ON &&
             state == AT86RF2XX_STATE_RX_AACK_ON)) {
        _set_state(dev, AT86RF2XX_STATE_PLL_ON, AT86RF2XX_STATE_PLL_ON);
    }
    /* check if we need to wake up from sleep mode */
    else if (old_state == AT86RF2XX_STATE_SLEEP) {
        DEBUG("at86rf2xx: waking up from sleep mode\n");
        at86rf2xx_assert_awake(dev);
    }

    if (state == AT86RF2XX_STATE_SLEEP) {
        /* First go to TRX_OFF */
        at86rf2xx_set_state(dev, AT86RF2XX_STATE_FORCE_TRX_OFF);
        /* Discard all IRQ flags, framebuffer is lost anyway */
        at86rf2xx_reg_read(dev, AT86RF2XX_REG__IRQ_STATUS);
        /* Go to SLEEP mode from TRX_OFF */
        gpio_set(dev->params.sleep_pin);
        dev->state = state;
    } else {
        _set_state(dev, state, state);
    }

    return old_state;
}

@DipSwitch
Copy link
Member Author

Will update the pr tomorrow.

@thomaseichinger
Copy link
Member

Awesome, thanks!

I also saw that at least at86rf2xx_tx_prepare and at86rf2xx_reset_state_machine can be simplified then too.

@aabadie aabadie added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 21, 2017
@aabadie
Copy link
Contributor

aabadie commented Jun 21, 2017

Just activated the CI. Can we have a positive review (ACK) and merge this one for the release ? It will also be useful for #7149.

@biboc
Copy link
Member

biboc commented Jun 21, 2017

@DipSwitch, can you merge @thomaseichinger PR and let's merge this PR since we've tested it and it works
I'll give my ACK as soon as thomas's PR is merged

@aabadie
Copy link
Contributor

aabadie commented Jun 21, 2017

Murdock is complaining for some variable scope reassingment reason. Can you fix this @DipSwitch ?

@aabadie aabadie force-pushed the pr/fix/at86rf2xx_setstate branch 2 times, most recently from 4e695c7 to 86a2af0 Compare June 23, 2017 08:06
@aabadie
Copy link
Contributor

aabadie commented Jun 23, 2017

@thomaseichinger @DipSwitch I allowed myself to directly push the requested change proposed above and fix Murdock reported issue.

Let me know if that fine for you so we can move forward here and here. Thanks !

@biboc
Copy link
Member

biboc commented Jun 23, 2017

@aabadie, Did you check that what you pushed is same as the PR @thomaseichinger suggested? (the on we ACK from my side)
DipSwitch#6

@aabadie
Copy link
Contributor

aabadie commented Jun 23, 2017

Did you check that what you pushed is same as the PR @thomaseichinger suggested? (the on we ACK from my side)

Well, I missed it, sorry. Need to compare and will fix asap.

@aabadie
Copy link
Contributor

aabadie commented Jun 23, 2017

@biboc, should be fine now.

@miri64
Copy link
Member

miri64 commented Jun 23, 2017

Does this fix #5486 by any chance?

@thomaseichinger thomaseichinger added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jun 23, 2017
@thomaseichinger
Copy link
Member

@aabadie Could you please also change the commit message?
(Since when can you push to other people's branches?)

@aabadie
Copy link
Contributor

aabadie commented Jun 24, 2017

Could you please also change the commit message?

Done

Since when can you push to other people's branches?

Every maintainer of RIOT is able to do that under certain conditions. I don't recommend this practice but sometimes it helps.

@aabadie
Copy link
Contributor

aabadie commented Jun 26, 2017

ping @thomaseichinger

@biboc
Copy link
Member

biboc commented Jun 26, 2017

ACK for me, wait @thomaseichinger before merging

@aabadie
Copy link
Contributor

aabadie commented Jun 26, 2017

when this on is merged, I'll be able to test the OT PR on IoT-LAB tomorrow afternoon.

Copy link
Member

@thomaseichinger thomaseichinger left a comment

Choose a reason for hiding this comment

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

Code seems good to me. ACK

@aabadie I think we should do some more thorough release testing for the at86rf2xx though.

@thomaseichinger thomaseichinger merged commit c76fdf5 into RIOT-OS:master Jun 26, 2017
@miri64
Copy link
Member

miri64 commented Jun 27, 2017

Does this fix #5486 by any chance?

Nope :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: drivers Area: Device drivers Area: network Area: Networking CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants