On Mon, Mar 14, 2022 at 11:09:48AM +0100, Alexandr Nedvedicky wrote:
> 667 ipsec_delete_policy(struct ipsec_policy *ipo)                             
>                  
> 668 {
> 669         struct ipsec_acquire *ipa;
> 670         struct radix_node_head *rnh;
> 671         struct radix_node *rn = (struct radix_node *)ipo;                 
>                  
> 672 
> 673         NET_ASSERT_LOCKED();                                              
>                  
> 674 
> 675         if (refcnt_rele(&ipo->ipo_refcnt) == 0)                           
>                  
> 676                 return 0;                                                 
>                  
> 677 
> 678         /* Delete from SPD. */
> 679         if ((rnh = spd_table_get(ipo->ipo_rdomain)) == NULL ||
> 680             rn_delete(&ipo->ipo_addr, &ipo->ipo_mask, rnh, rn) == NULL)   
>                  
> 681                 return (ESRCH);                                     
> 
>     the suggestion is not related to your changes, but I think it's worth
>     to consider as a different diff. I think we should either panic()
>     at line 681 or we should at least log error. If I understand code
>     right we delete policy (ipo), which has been fully created so this
>     error should not happen here.

Maybe.  If you send a diff, I will test it.

> > +ipsp_delete_acquire_locked(struct ipsec_acquire *ipa)
> > +{
> > +   if (timeout_del(&ipa->ipa_timeout) == 1)
> > +           refcnt_rele(&ipa->ipa_refcnt);
>                ^^^^^^^^^^^^^^^^^^
>       can we also put ASSERT/check into this branch
>       to verify we are no releasing the last
>       reference to ipa. I suspect we might be doing
>       an extra reference drop here.

Later we call ipsp_unref_acquire_locked() and refcnt_rele() again.
This will KASSERT(refcnt != ~0) in the case you describe.

>       I believe this ASSERT
>       would be hit if we will compile kernel without IPSEC.
>       we grab the extra reference iff are adding a timer.

We release the extra reference only iff we delete the timer
that was added before.

>       I think this whole ipsp_delete_acquire() logic should
>       be slightly different. We should call ipsp_unref_acquire_locked(),
>       when timeout_del() returns 1, because function managed to unschedule
>       the pending timer. If it returns zero, we should leave things as
>       they are because timer is running already, waiting for
>       ipsec_acquire_mtx. As soon as timer will grab mutex, it will
>       drop the reference for ipa. That's my understanding.
>       if we go this way then we should not need to grab extra
>       reference for timer.

I copied this logic from tdb.  We refcount the pending timers.  By
checking the return value of timeout_add() and timeout_del() the
refcount is correct.  And the running timer is counted as reference.
At least this works fine with tdb.

> >     struct ipsec_acquire *ipa;
>                       ^^^^^^^^^^^
>     this needs to be initialized to NULL

As mentioned earlier, TAILQ_FOREACH(ipa,...) always initializes
ipa.

> > -   timeout_add_sec(&ipa->ipa_timeout, ipsec_expire_acquire);
> > +   if (timeout_add_sec(&ipa->ipa_timeout, ipsec_expire_acquire) == 1)      
> >                 refcnt_take(&ipa->ipa_refcnt);
>                           ^^^^^^^^^^^^^^^^^^^^^^^
>                       line is too long

Fixed before commit.

Reply via email to