Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp190630rdh; Thu, 23 Nov 2023 00:45:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0OaPVyXTH+gBds5l+j3pXZw2VHYHc5DHLpl9orHtP/ugjIEY08g7MxJuqj+v4OkDgw1BI X-Received: by 2002:a17:902:d48d:b0:1ca:e4b:148d with SMTP id c13-20020a170902d48d00b001ca0e4b148dmr4297878plg.65.1700729108908; Thu, 23 Nov 2023 00:45:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700729108; cv=none; d=google.com; s=arc-20160816; b=s/ZJmyqzqsdLmRE7wn0PJZ6H0jz5XSVAePGuenn1Wi1rt/DwHITIVeErhA3bJQKwqT 3k8q2cTJmvqQyvRlu5bc19FVC/WvZjro2/nQqvUDlKzOhl6S/fBR1LyoZMrFgJ5yJF8N heVQYaidrsWyizp8LD8PuAqqgi034RJRZBe6Lxk+m4sGBi16x+n9EYrwHwEKM0JKxE5a 18/M9/BjODKmSc4jxhuj8rlGG+9Nf/p3JVZoJQ005vwqbr2QotEd2NZk0+UnDREhxFg9 2v3fH43zRrjuCUssI3qzVf8rKU6RhnSzP0Dc9kv9BekYurTMDV5nZjvO7cKi0BertY45 MmJg== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+K/A6ge6fIrcPOLeyVqVZ7n/Tr4bf5Mr2/Co/8K5bPE=; fh=a+V2/MhRBAVNrfWuKbg2u5A3a9Ucl8Skda0qKJds0KM=; b=Odkw8mwF1WIG7X0mh/ssixVL6XmI2/jb65ZBSpIxfMOjgvXAG51TelbJ97MoQwG9+r j0Ls9n2i0yi7n88RvIhXfpqGbM5gt6GjADfPTgSTV7teq83DqMWZ/9JE8PYlOQWIXiUz mCMJVCQ9jhMFR7M9FvDJjlJtNdUQJAPn2oSzi0fddjMXsYEMt7spBYG9ALdbOz7AnkDg d48cY3Rx0x64ZfNklnN9h6y+THpK+ilgoNiT/8JXRXLEqefLOIqQYG03nA7D7t4auQP0 CQDYUqKkjeHWauhe/sGmbrco6uBQ24XXEU+f9MpZggOfJv9DkDhmBrZy0YBo2y59mndG tm/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fg5qffX8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id e13-20020a17090301cd00b001c747cafce9si746577plh.231.2023.11.23.00.45.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 00:45:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fg5qffX8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 93D1283B3616; Thu, 23 Nov 2023 00:45:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344936AbjKWIor (ORCPT + 99 others); Thu, 23 Nov 2023 03:44:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229714AbjKWIop (ORCPT ); Thu, 23 Nov 2023 03:44:45 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B19ACBC for ; Thu, 23 Nov 2023 00:44:51 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECF9AC433C9; Thu, 23 Nov 2023 08:44:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1700729091; bh=dtaLQq/bqc1M0xpR/qmqubmgPbyfTmY1kfj1QmiH7Ds=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fg5qffX8KHPfF3Apj4rjZwOrxOfUs5uLp+HPb8N3pCHXuXdVUW0TNK/NIMPq9nglQ q7Kt/a/FSDnXOJiW4jXHZTnEi3dMCAzWYYp9XoQnzN9lrkqKTMBLnQQDpFfXQ5LdPw t7z4XsCsJ2yRUkif0TZl4YjpCv74+ZC9XRbiJwdU= Date: Thu, 23 Nov 2023 08:44:46 +0000 From: Greg Kroah-Hartman To: Yewon Choi Cc: Bryan Tan , Vishnu Dasa , VMware PV-Drivers Reviewers , Arnd Bergmann , linux-kernel@vger.kernel.org, threeearcat@gmail.com Subject: Re: [PATCH] vmci_host: use smp_load_acquire/smp_store_release when accessing vmci_host_dev->ct_type Message-ID: <2023112331-wise-regain-72dc@gregkh> References: <20231122122005.GA4661@libra05> <2023112257-polymer-banknote-4869@gregkh> <20231123074920.GA10480@libra05> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231123074920.GA10480@libra05> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 23 Nov 2023 00:45:06 -0800 (PST) On Thu, Nov 23, 2023 at 04:49:22PM +0900, Yewon Choi wrote: > On Wed, Nov 22, 2023 at 02:34:55PM +0000, Greg Kroah-Hartman wrote: > > On Wed, Nov 22, 2023 at 09:20:08PM +0900, Yewon Choi wrote: > > > In vmci_host.c, missing memory barrier between vmci_host_dev->ct_type > > > and vmci_host_dev->context may cause uninitialized data access. > > > > > > One of possible execution flows is as follows: > > > > > > CPU 1 (vmci_host_do_init_context) > > > ===== > > > vmci_host_dev->context = vmci_ctx_create(...) // 1 > > > vmci_host_dev->ct_type = VMCIOBJ_CONTEXT; // 2 > > > > > > CPU 2 (vmci_host_poll) > > > ===== > > > if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) { // 3 > > > context = vmci_host_dev->context; // 4 > > > poll_wait(..., &context->host_context.wait_queue, ...); > > > > > > While ct_type serves as a flag indicating that context is initialized, > > > there is no memory barrier which prevents reordering between > > > 1,2 and 3, 4. So it is possible that 4 reads uninitialized > > > vmci_host_dev->context. > > > In this case, the null dereference occurs in poll_wait(). > > > > > > In order to prevent this kind of reordering, we change plain accesses > > > to ct_type into smp_load_acquire() and smp_store_release(). > > > > > > Signed-off-by: Yewon Choi > > > --- > > > drivers/misc/vmw_vmci/vmci_host.c | 40 ++++++++++++++++++------------- > > > 1 file changed, 23 insertions(+), 17 deletions(-) > > > > > > diff --git a/drivers/misc/vmw_vmci/vmci_host.c b/drivers/misc/vmw_vmci/vmci_host.c > > > index abe79f6fd2a7..e83b6e0fe55b 100644 > > > --- a/drivers/misc/vmw_vmci/vmci_host.c > > > +++ b/drivers/misc/vmw_vmci/vmci_host.c > > > @@ -139,7 +139,7 @@ static int vmci_host_close(struct inode *inode, struct file *filp) > > > { > > > struct vmci_host_dev *vmci_host_dev = filp->private_data; > > > > > > - if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) { > > > + if (smp_load_acquire(&vmci_host_dev->ct_type) == VMCIOBJ_CONTEXT) { > > > > This is getting tricky, why not use a normal lock to ensure that all is > > safe? close isn't on a "fast path", so this shouldn't be a speed issue, > > right? > > > > I think using locks can be considered orthogonal to correcting memory ordering. But they ensure proper memory ordering. > As you pointed out, vmci_host_close is not a performance-critical function > while other functions using vmci_host_dev->context are performance-critical. In what way? Why is the context being constantly checked in such situations? And if so, it can change right after being checked so a real lock needs to be used. > If the lock is needed, we will need to add locks in all of them. I cannot be > sure which is better. Besides that, it seems to be a separate issue. Nope, I think it's the same issue :) > On the other hand, the current implementation doesn't guarantee memory ordering > which leads to wrong behavior. > This patch fixes this issue by adding primitives. But it's still wrong, again, what keeps the value from changing right after checking it? thanks, greg k-h