Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp134305pxb; Wed, 23 Mar 2022 14:47:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGXtRt1PxvygzFmmFuBeDjkToJ8fg6oSWcrMiqkM8uFHpTZ17ndTTkvaznx3qxlzeJUyaF X-Received: by 2002:a05:6402:3d3:b0:419:4781:aac7 with SMTP id t19-20020a05640203d300b004194781aac7mr2834932edw.347.1648072023773; Wed, 23 Mar 2022 14:47:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648072023; cv=none; d=google.com; s=arc-20160816; b=ra+hVfTMBKA8Tr53hqk199Yfhev93IQwn1y8Pte+xy0Wh41PfqQ/UrJzfJJ+wmUPBO Yrlf3tON4JRJ+GuaIyJWjoYgnGJcLYGKE/G8dsf69nkSHsxS/SZC7kzHRQpCOBZPXyqQ g6ZYxPx+6UaqbbZ1k3SGkJKq6+DK6imYU3nX5aTibbxSS1GDUfoTogTGd9gMVWBGbECg FD6PHiJZNEZN6uiBXgEUUQGLElAMuQOGRQ/C9ptjUUYBGcFlYr3jnZF7J0G3tkyTM8Fx okKCoHxUzkRsocNcmlN0igipfe+vifOxxmNvBolM3RuWHzAad5lFw/Q2CtCWqMCFmYuh 7iCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=QrnKrN1J0be5aGhbUhpBdOm3mgQtzJ3AbarAe1cgyz4=; b=vfTujBGB1loN2NF9v6PDH8wy5aF/fczlUyoijx8ttMmvT21oMnydYgT7Vg0GMKlPcy agA160JxWUGqwsB/EeeaBB7qw/Nmr7ezpAfe7o892VisiPd5nQX2CTcAgv5jNpbg9Sr6 mjJ0C905/CLV+V2KA1kfHrj6FdtdFbXQtDDb7YblKhGL9TmZBH7oxESMSvFEvNptJpaS bix0MAwtLYsdwHrTbt7fP3yuTo8qPKIx6IoXFTcqsB7dJeJsGt7/hr6awAZqUbb+wpnj zo5Awu5kVCA+hZ97NTABqV+nMmd/mGrey/hrwDMM1dy3jaygUqK+zmS3mrC5liNki0Yb mxZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PNp6vqLv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s9-20020a508d09000000b00418e660c27csi16427327eds.40.2022.03.23.14.46.38; Wed, 23 Mar 2022 14:47:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PNp6vqLv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243309AbiCWMsb (ORCPT + 99 others); Wed, 23 Mar 2022 08:48:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243298AbiCWMsa (ORCPT ); Wed, 23 Mar 2022 08:48:30 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C169B7C175 for ; Wed, 23 Mar 2022 05:46:59 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id m30so1984992wrb.1 for ; Wed, 23 Mar 2022 05:46:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=QrnKrN1J0be5aGhbUhpBdOm3mgQtzJ3AbarAe1cgyz4=; b=PNp6vqLvvi186d7HmPfAqbvJGTx7NW1Fx8uwCXWK6FDJi8hDa0A4Obyl0BJlO5+1lB vMizD2ozDTrd1SWvW6k6EbB7UdNw60K8M08R7jTHpovxIB7hs0FOgMsE6ChIt7QkfrIk bTGHkOg+xnrkgrIEahmcKVKPCqZRSxCqwzYZUfEPQXRZLytNNrKHkdlH6ytPTaCbezNf 6KqsyEBPIYlSSPXBmqKdIRnKAWl5fyQw0hqv/fail0P9VB0VcVBOKzUv2UrSnseSFA4P 4qliOHKw8/ytu9CL4ol6/j71Mf3nRJBkXmqtRzicETMnTCLQtXImq4sJeK616gwzDtE4 tvGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=QrnKrN1J0be5aGhbUhpBdOm3mgQtzJ3AbarAe1cgyz4=; b=h0PyIGNd+lSuJHe3E4vRs0HLF0IBL0bhRwulsSIKy97Rpm273DVPBC6dhKJ3Aiua6g Bore//SuJ+IfzopDVPfWPYVGUQEaL6kWiFqKoUEuH7Il3KB3/mnk0IDy4UjeM2LVYcx+ bQJJ+BWTnX/W9Ht5Kl2Twxd6DrzAiUsgJdRxWXDOnVKXaV9UEXK+js2EYjsc+nENz2yz ZHUorrPfVqsha6mHrccaKng8TRtKfy5wAAQPvsWbecAdCzT4ayL1p7jfGAHAFfFrkKjv QVpDw0UrH8a8/67nn61s0uDbJ5a/MWBUfY9wZlpJS6N7b+WIl9wfZXt13laTl2/5NyHq FITA== X-Gm-Message-State: AOAM530Rx0jaqa9eL73h7DpZw8VnZRSdEMxgrv2Qz+nm3WUfZG3MtHvZ yG4BBmqZ8gMVreWociqJlEJtCBs4236Eog== X-Received: by 2002:a5d:64e7:0:b0:205:8cc7:aa82 with SMTP id g7-20020a5d64e7000000b002058cc7aa82mr1486949wri.247.1648039618247; Wed, 23 Mar 2022 05:46:58 -0700 (PDT) Received: from google.com (cpc155339-bagu17-2-0-cust87.1-3.cable.virginm.net. [86.27.177.88]) by smtp.gmail.com with ESMTPSA id o8-20020a5d6488000000b002051f1028f6sm4375837wri.111.2022.03.23.05.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Mar 2022 05:46:57 -0700 (PDT) Date: Wed, 23 Mar 2022 12:46:55 +0000 From: Lee Jones To: philip yang Cc: Felix Kuehling , David Airlie , "Pan, Xinhui" , linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Alex Deucher , Christian =?iso-8859-1?Q?K=F6nig?= Subject: Re: [PATCH 1/1] drm/amdkfd: Protect the Client whilst it is being operated on Message-ID: References: <20220317131610.554347-1-lee.jones@linaro.org> <8702f8a5-62a1-c07e-c7b7-e9378be069b6@amd.com> <1f003356-3cf9-7237-501e-950d0aa124d1@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 17 Mar 2022, Lee Jones wrote: > On Thu, 17 Mar 2022, philip yang wrote: > > > On 2022-03-17 11:13 a.m., Lee Jones wrote: > > > > On Thu, 17 Mar 2022, Felix Kuehling wrote: > > > > > > Am 2022-03-17 um 11:00 schrieb Lee Jones: > > > > Good afternoon Felix, > > > > Thanks for your review. > > > > > > Am 2022-03-17 um 09:16 schrieb Lee Jones: > > > > Presently the Client can be freed whilst still in use. > > > > Use the already provided lock to prevent this. > > > > Cc: Felix Kuehling [1] > > Cc: Alex Deucher [2] > > Cc: "Christian König" [3] > > Cc: "Pan, Xinhui" [4] > > Cc: David Airlie [5] > > Cc: Daniel Vetter [6] > > Cc: [7]amd-gfx@lists.freedesktop.org > > Cc: [8]dri-devel@lists.freedesktop.org > > Signed-off-by: Lee Jones [9] > > --- > > drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 6 ++++++ > > 1 file changed, 6 insertions(+) > > > > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c b/drivers/gpu/drm/amd/a > > mdkfd/kfd_smi_events.c > > index e4beebb1c80a2..3b9ac1e87231f 100644 > > --- a/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c > > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c > > @@ -145,8 +145,11 @@ static int kfd_smi_ev_release(struct inode *inode, struct f > > ile *filep) > > spin_unlock(&dev->smi_lock); > > synchronize_rcu(); > > + > > + spin_lock(&client->lock); > > kfifo_free(&client->fifo); > > kfree(client); > > + spin_unlock(&client->lock); > > > > The spin_unlock is after the spinlock data structure has been freed. > > > > Good point. > > > > If we go forward with this approach the unlock should perhaps be moved > > to just before the kfree(). > > > > > > There > > should be no concurrent users here, since we are freeing the data structure. > > If there still are concurrent users at this point, they will crash anyway. > > So the locking is unnecessary. > > > > The users may well crash, as does the kernel unfortunately. > > > > We only get to kfd_smi_ev_release when the file descriptor is closed. User > > mode has no way to use the client any more at this point. This function also > > removes the client from the dev->smi_cllients list. So no more events will > > be added to the client. Therefore it is safe to free the client. > > > > If any of the above were not true, it would not be safe to kfree(client). > > > > But if it is safe to kfree(client), then there is no need for the locking. > > > > I'm not keen to go into too much detail until it's been patched. > > > > However, there is a way to free the client while it is still in use. > > > > Remember we are multi-threaded. > > > > files_struct->count refcount is used to handle this race, as > > vfs_read/vfs_write takes file refcount and fput calls release only if > > refcount is 1, to guarantee that read/write from user space is finished > > here. > > > > Another race is driver add_event_to_kfifo while closing the handler. We > > use rcu_read_lock in add_event_to_kfifo, and kfd_smi_ev_release calls > > synchronize_rcu to wait for all rcu_read done. So it is safe to call > > kfifo_free(&client->fifo) and kfree(client). > > Philip, please reach out to Felix. Philip, Felix, are you receiving my direct messages? I have a feeling they're being filtered out by AMD's mail server. -- Lee Jones [李琼斯] Principal Technical Lead - Developer Services Linaro.org │ Open source software for Arm SoCs Follow Linaro: Facebook | Twitter | Blog