Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp1480553pxu; Fri, 16 Oct 2020 13:07:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+mTtc2Pu5qWHUivrmvr6XnsVCRgWoJFCrTVpe81IXvbugC6/lXgW+BEpdR4HPMiI0gyho X-Received: by 2002:a17:906:2b5b:: with SMTP id b27mr5552087ejg.400.1602878836708; Fri, 16 Oct 2020 13:07:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602878836; cv=none; d=google.com; s=arc-20160816; b=oIKoOZaRhKpRB9wHMp3s6rcsWLVZp9F3lBtlKo7uvSs+qr1rbEQQEe1oYthh5C+IGe f11DS2V8gujqPL8yzimlRaIiPRnW9jgUKdZK5TQB+3M88e1oKnH/tOy6wvvP9KxeTxSe ZMuCCbF/jYcb2fMNiiBzDwFRDalklcd0pw9DrmLV5KHAgj5JYydA53SvEWxr2ysXmac2 g/da0omJKRedFO3kCFxnRSn0FWBDkfjSozZ9lEKevwPiswhKC4TxqThoBXfpnYux5bt5 SztMcQYGplVu6524LqbzG3rV22q7fcOZxyquK1O3zEfvte3F1v5ssmR4hLQxJ4759/G4 kOXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=1DJnrCVnvy4PoMtVGFycjUVirWQQwi3AYGu5HmZZQwQ=; b=n7xghF+KRvbGT66VuqF0PAmgeFLh7I20fj7Pr5PdpQ5rlLbsHHzuFpzVHbav9JzEKP NJoKjige9q/VwKcUbFn4yIQ+jOgxEmPfWpPkkGn3VcHd5H84zcwtaEXvcmeC8bRlJb6B uwZVNmf6Mnl7jZeN8gf1eFGFOTlbRImHbU1PzPXSnKPOB3f9nW6bA4KbBZrwMjr1Wph9 USFKhl1/TmolfS/Y21sJgKO+6OSMi/hAxhF9EfTsS6BzN3+YdrOjX43gUSjri7qkRKtO RXqpXBB32kGt/1W04GhBqHpN4g0qbj89TpMyn8z05B4YRnrjjX9zKcnCaBb7JzSb0mdk 4gfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=X9rp63JS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j14si2483095edy.562.2020.10.16.13.06.54; Fri, 16 Oct 2020 13:07:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=X9rp63JS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436522AbgJPT1z (ORCPT + 99 others); Fri, 16 Oct 2020 15:27:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2410753AbgJPT1z (ORCPT ); Fri, 16 Oct 2020 15:27:55 -0400 Received: from mail-il1-x144.google.com (mail-il1-x144.google.com [IPv6:2607:f8b0:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96724C061755 for ; Fri, 16 Oct 2020 12:27:54 -0700 (PDT) Received: by mail-il1-x144.google.com with SMTP id t18so3859445ilo.12 for ; Fri, 16 Oct 2020 12:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1DJnrCVnvy4PoMtVGFycjUVirWQQwi3AYGu5HmZZQwQ=; b=X9rp63JScAmPAW+kyhIqursnDhfJTrj0ZGbEnAzBHYg+kq0WkEUosaRnDPVOyc4rPm aly52LA2VUl/vVmAsXzsxWDfNFoAY/NvEwJZdD7AgzlR0BZ8Nv+AY0REwg9IpNTZB1J+ AI8XkXWArpm8XI9ZHfcPNFZPqJIP+w4jsFPOLJDmTuqLdD+7Gv485x9kgOvx2B84bQSJ vLy9dWeKBnMNZqk1QWVgAUbHrmwDn2WO89Hr5jE0jwmFQIf5YR02mD7JehIFzoD+ZchC tLLrw+YVM8Fj9JQQfG9cKIkkdfnSq06ogxMvn5WzgahHCaRS4+hx9TUyKOCtHEOURRVD N6Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1DJnrCVnvy4PoMtVGFycjUVirWQQwi3AYGu5HmZZQwQ=; b=oYUIM+TF7BmTTJclr8UI3aKArbozw/t520F/DwjlSXNQdZp8BuJJFHqVkTR17gT81L etUZITAYwy+HVC54b7LhQS7u4rmgS3+tXzApVILDmEhm32aGbd6KTHd9aZbj/UKk2uxe GALLP2ltJLM7vP9iNtSWbqRkoBD+f+9oI4mQHIxqp4LbYzlo2VEGdrloQ4Nf/U8SXh8C /bxKE+yg89xK/W3vA9+00cJOc7xcpIzowQiLt8mMxotZTToxa+5UZykEdStZHwyssOqZ 0gfepEG+vcEqyoV3iOeeWTx9aFLhmUaVmURVyPwoHgyJfSHE0OMdq/ZNBt2LTiljqrS4 VOjw== X-Gm-Message-State: AOAM533uCVHDG4EMevsswt1wr5Rq5m6+HATrwCIVPXmqPXa3sbR04Aeo SYpAHs1B8rSrV5f2ChEK3fg3YjUHFNxaa1DVC8A= X-Received: by 2002:a92:8910:: with SMTP id n16mr4077254ild.239.1602876473992; Fri, 16 Oct 2020 12:27:53 -0700 (PDT) MIME-Version: 1.0 References: <20200923123916.1115962-1-jbrunet@baylibre.com> <20201015134628.GA11989@arm.com> <1jlfg7k2ux.fsf@starbuckisacylon.baylibre.com> <20201016085217.GA12323@arm.com> <1jk0vqk0ju.fsf@starbuckisacylon.baylibre.com> <1jft6ej91c.fsf@starbuckisacylon.baylibre.com> In-Reply-To: <1jft6ej91c.fsf@starbuckisacylon.baylibre.com> From: Jassi Brar Date: Fri, 16 Oct 2020 14:27:43 -0500 Message-ID: Subject: Re: [PATCH] mailbox: cancel timer before starting it To: Jerome Brunet Cc: Ionela Voinescu , Kevin Hilman , "open list:ARM/Amlogic Meson..." , Da Xue , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 16, 2020 at 1:54 PM Jerome Brunet wrote: > > > On Fri 16 Oct 2020 at 19:33, Jassi Brar wrote: > > > On Fri, Oct 16, 2020 at 4:00 AM Jerome Brunet wrote: > >> > >> > >> On Fri 16 Oct 2020 at 10:52, Ionela Voinescu wrote: > >> > >> > On Thursday 15 Oct 2020 at 13:45:54 (-0500), Jassi Brar wrote: > >> > [..] > >> >> > >> --- a/drivers/mailbox/mailbox.c > >> >> > >> +++ b/drivers/mailbox/mailbox.c > >> >> > >> @@ -82,9 +82,13 @@ static void msg_submit(struct mbox_chan *chan) > >> >> > >> exit: > >> >> > >> spin_unlock_irqrestore(&chan->lock, flags); > >> >> > >> > >> >> > >> - if (!err && (chan->txdone_method & TXDONE_BY_POLL)) > >> >> > >> - /* kick start the timer immediately to avoid delays */ > >> >> > >> + if (!err && (chan->txdone_method & TXDONE_BY_POLL)) { > >> >> > >> + /* Disable the timer if already active ... */ > >> >> > >> + hrtimer_cancel(&chan->mbox->poll_hrt); > >> >> > >> + > >> >> > >> + /* ... and kick start it immediately to avoid delays */ > >> >> > >> hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL); > >> >> > >> + } > >> >> > >> } > >> >> > >> > >> >> > >> static void tx_tick(struct mbox_chan *chan, int r) > >> >> > > > >> >> > > I've tracked a regression back to this commit. Details to reproduce: > >> >> > > >> >> > Hi Ionela, > >> >> > > >> >> > I don't have access to your platform and I don't get what is going on > >> >> > from the log below. > >> >> > > >> >> > Could you please give us a bit more details about what is going on ? > >> >> > > >> >> > All this patch does is add hrtimer_cancel(). > >> >> > * It is needed if the timer had already been started, which is > >> >> > appropriate AFAIU > >> >> > * It is a NO-OP is the timer is not active. > >> >> > > >> >> Can you please try using hrtimer_try_to_cancel() instead ? > >> >> > >> > > >> > Yes, using hrtimer_try_to_cancel() instead works for me. But doesn't > >> > this limit how effective this change is? AFAIU, this will possibly only > >> > reduce the chances for the race condition, but not solve it. > >> > > >> > >> It is also my understanding, hrtimer_try_to_cancel() would remove a > >> timer which as not already started but would return withtout doing > >> anything if the callback is already running ... which is the original > >> problem > >> > > If we are running in the callback path, hrtimer_try_to_cancel will > > return -1, in which case we could skip hrtimer_start. > > Anyways, I think simply checking for hrtimer_active should effect the same. > > I have submitted a patch, of course not tested. > > Yes it sloves this race but ... > Thanks for confirmation. > If a race is possible between a timer callback rescheduling itself (which > is not that uncommon) and another thread trying to cancel it > In our case, we should not be cancelling+restarting the timer in the first place, because txdone_hrtimer will take care of it via hrtimer_forward_now. >, maybe > there is something worth fixing in hrtimer ? Also, mailbox calls > hrtimer_cancel() in unregister ... are we confident this would work ? > Yes. After unregister() every channel is supposed to die and so must its resources. -jassi