Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4640162pxa; Mon, 10 Aug 2020 14:18:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXkr2sXAvcjnPlmZJzUvUtH93Dk18nghHftGM/gRS9ThWwZ5n8KfSdbOJCBqohc+4w8ar8 X-Received: by 2002:aa7:cd04:: with SMTP id b4mr22269478edw.254.1597094332263; Mon, 10 Aug 2020 14:18:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597094332; cv=none; d=google.com; s=arc-20160816; b=RutH2o1c6JApG+ODWnhvvPT1fyKDJAknhF/X9/hUs+ZF05zJwitKYZvBXMYwmJQmL8 SF7pf6/7ClomfXfyMfDZBw+QAl/pJsGEDeXtArqoK4p4gpkWJ5RTHdgQbrwdxfDlvP01 82kbSYRbJwSCVAmuQfWcU3ZmSPwN6aWI2wWmFDzx/yFlsVmXV8JsSZsh8uF8g0bWK/ra F9+Dc3ToXP52E/KjBtUeCkOy80tByDjofn5pieNV4eILknD6z6whVItkiVMHJuve+/rl wHx3tZGuAzE4fZyyFn+BtNKOQIxu6UQ9XPG6ScYxlOXk65QwSkz6IDJ9la2EJEp48lFa oTRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=Fj2es0dCEPOQUTQgxshXQADi10l+RVsAVXH1AGZUZrE=; b=YBy4WcDTBTHsOwXp9yVnrLxzGSwCPAGMKbnJGpcykcO7sJgoWE4KJD7m3QMJV6rbVg 3zx5G0Mlth5+JyWv8WPRKXANNdSel+ulRWyZ4Wuw/u4fChL9X567OxtVmPP3t6xklVtr uyY6MQhkLai9Fa6S8fWtzLNfkm1vGSZaSukYifyvKmFcFPWEdxDL8eL00w+IK79CpHRv WjyOAEget7CO8iS6kNucwO4g+Wb+TGJ/NnsJuVQuenp42StEk/Tm6ADrguna129vWx3t 2bOgHU/siRvX6bZq+I7ArMmSppo0Shj9p3T4uo/ITXVE8q1nftb7HK4nSdBfOlaJNHrP Fmyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b="e049/iE5"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rl11si9889987ejb.519.2020.08.10.14.18.28; Mon, 10 Aug 2020 14:18:52 -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=@broadcom.com header.s=google header.b="e049/iE5"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726773AbgHJVRS (ORCPT + 99 others); Mon, 10 Aug 2020 17:17:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726624AbgHJVRR (ORCPT ); Mon, 10 Aug 2020 17:17:17 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85BAFC061787 for ; Mon, 10 Aug 2020 14:17:17 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id c19so786185wmd.1 for ; Mon, 10 Aug 2020 14:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=Fj2es0dCEPOQUTQgxshXQADi10l+RVsAVXH1AGZUZrE=; b=e049/iE5zjU3fuIZOmYEgRgRB8FElNKBQdO1T9yZ3dwFurZ/cVpoFb9P4ECfLn05Lh CnbpAUYssYvu6J8zWlzF9f6z4HCdfPzEnoJDcwANUr75zB4tOR961Gt3VQ2wEJ3vqcHs 84jIaz953mb7RCN0Xhza3wxmNmSeVDjTc5nus= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Fj2es0dCEPOQUTQgxshXQADi10l+RVsAVXH1AGZUZrE=; b=bIzRkB6RdIib3K+jVGRlHk2A3/4IRzYNpimx9qjcFQtwvjoQEueWn3BnJejI266/R5 a2JC34ik1kwoTjkax0MZ8Tzrx/+vVwBab4MML66kgmN6Z86JbBWXdoiMaa7v2+ADj85a ZePzbG7Qitz/gBG5SpS9YATan7ZcnfgUCznYhMDqNhl4/G7/amkm+toOYHpd2NxJ0Ecu wlPtPokG82eQ1qkMo13wq+IHZgDfMAMsDswpzPYLj1JgeRlJ2CKi6PYISjKhYsR+8U1f mt1jietXmRXSvQQY2NlSIg+4Uih03ZBL/QeG6i9yIXCGns6xpQx0lL2JiROfPhPxydbU 8djQ== X-Gm-Message-State: AOAM533dFeANgQuirZ30np6gkDbGL0CvVoPA+hV+qXVLfcZizx6CF9MI xLWsbp0YWrqupyC8Uar39wUVdw== X-Received: by 2002:a05:600c:258e:: with SMTP id 14mr929987wmh.141.1597094235871; Mon, 10 Aug 2020 14:17:15 -0700 (PDT) Received: from [10.230.0.249] ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id f63sm1372299wmf.9.2020.08.10.14.17.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Aug 2020 14:17:15 -0700 (PDT) Subject: Re: [PATCH v2] i2c: iproc: fix race between client unreg and isr To: Florian Fainelli , Dhananjay Phadke Cc: bcm-kernel-feedback-list@broadcom.com, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, rayagonda.kokatanur@broadcom.com, rjui@broadcom.com, wsa@kernel.org References: <00a30ca7-d533-94ba-994a-9a133fadb045@gmail.com> <1596858925-45763-1-git-send-email-dphadke@linux.microsoft.com> <0f7063f5-8ca7-f469-574b-82382d49e266@gmail.com> From: Ray Jui Message-ID: <18cf439a-8fde-02b0-31b6-9ac42f7e972c@broadcom.com> Date: Mon, 10 Aug 2020 14:17:10 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <0f7063f5-8ca7-f469-574b-82382d49e266@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 8/8/2020 7:47 AM, Florian Fainelli wrote: > > > On 8/7/2020 8:55 PM, Dhananjay Phadke wrote: >> On 8/7/2020, Florian Fainelli wrote: >>>> When i2c client unregisters, synchronize irq before setting >>>> iproc_i2c->slave to NULL. >>>> >>>> (1) disable_irq() >>>> (2) Mask event enable bits in control reg >>>> (3) Erase slave address (avoid further writes to rx fifo) >>>> (4) Flush tx and rx FIFOs >>>> (5) Clear pending event (interrupt) bits in status reg >>>> (6) enable_irq() >>>> (7) Set client pointer to NULL >>>> >>> >>>> @@ -1091,6 +1091,17 @@ static int bcm_iproc_i2c_unreg_slave(struct i2c_client *slave) >>>> tmp &= ~BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT); >>>> iproc_i2c_wr_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET, tmp); >>>> >>>> + /* flush TX/RX FIFOs */ >>>> + tmp = (BIT(S_FIFO_RX_FLUSH_SHIFT) | BIT(S_FIFO_TX_FLUSH_SHIFT)); >>>> + iproc_i2c_wr_reg(iproc_i2c, S_FIFO_CTRL_OFFSET, tmp); >>>> + >>>> + /* clear all pending slave interrupts */ >>>> + iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, ISR_MASK_SLAVE); >>>> + >>>> + enable_irq(iproc_i2c->irq); >>>> + >>>> + iproc_i2c->slave = NULL; >>> >>> There is nothing that checks on iproc_i2c->slave being valid within the >>> interrupt handler, we assume that the pointer is valid which is fin, >>> however non functional it may be, it may feel more natural to move the >>> assignment before the enable_irq()? >> >> As far as the teardown sequence ensures no more interrupts arrive after >> enable_irq() and they are enabled only after setting pointer during >> client register(); checking for NULL in ISR isn't necessary. > > Agreed. > Okay I think we all agree that this teardown sequence will guarantee that no further "slave" interrupts will be fired after it. >> >> If The teardown sequence doesn't guarantee quiescing of interrupts, >> setting NULL before or after enable_irq() is equally vulnerable. > > The teardown sequence is sort of a critical section if we may say, so > ensuring that everything happens within it and that enable_irq() is the > last operation would seem more natural to me at least. Thanks > I tend to agree with Florian here. 1. Enable/Disable IRQ is done on the interrupt line for both master and slave (or even other peripherals that share the same interrupt line, although that is not the case here since this interrupt is dedicated to I2C in all iProc based SoCs). 2. The tear down sequence here wrapped by disable/enable_irq is slave specific The effect of 1. is temporary, and the purpose of it is to ensure slave interrupts are quiesced properly at the end of the sequence. If we consider both 1. and 2., I agree with Florian that while the end result is the same, it is indeed more natural to wrap the entire slave tear down sequence within disable/enable irq. Thanks, Ray