From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B46F6C282CE for ; Wed, 22 May 2019 22:39:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2CFB920881 for ; Wed, 22 May 2019 22:39:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="HPp1yf7U" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2CFB920881 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DEDC56B0003; Wed, 22 May 2019 18:39:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9F0E6B0006; Wed, 22 May 2019 18:39:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C670C6B0007; Wed, 22 May 2019 18:39:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by kanga.kvack.org (Postfix) with ESMTP id A53416B0003 for ; Wed, 22 May 2019 18:39:08 -0400 (EDT) Received: by mail-qt1-f199.google.com with SMTP id 49so3514629qtn.23 for ; Wed, 22 May 2019 15:39:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :message-id:references:mime-version:content-disposition:in-reply-to :user-agent; bh=UMLE3HAwx27IoVBbcznO88OARo8jwYRssCcya47nG+w=; b=XXzaMerGY8MuZ9rh+zUJhHpcf8jeO74CpdGwr1D2eY0AwoGNbmOQnl/dI6gUNuRYug oGRHYtvVNUDeiNYJpFqjhC5vlY4Ln0SupNz4g49Vsjms29d5dGx7acIuqQHsSMt5heYd wCBL/kWPaX9qnXL+dDQA8KO8lhDXuii5LmkMdPvJcvH62PoFKfCOIibdEPq2rRuulaLP 6fu8aUR3AdqVWm1N3P78Hm7ZfgPTmVyv60JlYHmCjW6DetjTPQn7LyIlWEUjOyX5TN8Z mcFVFfMTpQn8x3yubez2uUW1Wv9HWuDZAdaVYBXuNyWgmGes0/NUV37xQSJDDOGdmglk DBDQ== X-Gm-Message-State: APjAAAXrvs3gU9hcibc37mXqJGvLOz/dcGN/e6ntR12Ls9f0wFdyyUCx fp6gkrQnQCo2iJFIp05OSSm5lqT80YPZkTyhont/Zad3Gpal1eOtvsqbq8OJtaibaVXp1dsDXjD rK8+tuzYUli7mnL2qXVJGrMtKTqNw2JZo+asfGxtGHUrl6xNTMdQTZvH7fSgYQkRu9A== X-Received: by 2002:ac8:2bbd:: with SMTP id m58mr77697824qtm.225.1558564748372; Wed, 22 May 2019 15:39:08 -0700 (PDT) X-Received: by 2002:ac8:2bbd:: with SMTP id m58mr77697785qtm.225.1558564747762; Wed, 22 May 2019 15:39:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558564747; cv=none; d=google.com; s=arc-20160816; b=RInbqiNSkQg2U2Gtj8IgrUcupIvBt3+C8ZzSug2X/7YK9FYgk8B9zkRi7Q3Ogc/kam 7PYuK3cstTk8KUV9m56QPi8H3jR0+Xi1tc2SZ2hLlEgoUng6xGwd/cLDPz5RAnV4jXW4 Js3wrTwXsrCtFzKv1V1lF7L8p7+zzcldJxvwxUJi4ouNMjFv1mqtmg7SY43n8XvUUut4 jRv7pV3WZxD9UzB2TicjgSdklYFQOEu6MBfrxpKSrn4qThacO9pnx4mEJtwI9FWyyuq2 0pASTwZf7AOtlZiELNwRygwQytnx+6c1Upy3oAdfnWb/Q6lqRAm24axhRU1RKfArK9Ln Dz0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:dkim-signature; bh=UMLE3HAwx27IoVBbcznO88OARo8jwYRssCcya47nG+w=; b=tjmo9vCz72e4H0lTz4td8PbenSVQDsDxkQfvV39A74JVIr8BO9Pj3hVvaZe4K9pS0o VqC59zSCEKTfErtQPmu5M4RRTv+yAzhHTvTMhe7XNMmR38rYwh9o2kWaVo1BYT6x0Wcs Xmqmu/tJpgIbjkmweinmU3J52/1TRHzAFriH/eW/t8tIWWkT80BRN/utzKADEMt12830 FsiZzgVlZG1OHTS4zPmt3K98h0m7dTx5+UDREx71EOh0AaGE+ZUYV/xir6XsLmlDH7YQ buPlzVqiYXHPSOlaN08M2ZRjscWLiagM2p1TkU+lagXoZGasZybOocqZrr5mcyHDezuw Ea7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=HPp1yf7U; spf=pass (google.com: domain of jgg@ziepe.ca designates 209.85.220.65 as permitted sender) smtp.mailfrom=jgg@ziepe.ca Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id u46sor16334617qvf.52.2019.05.22.15.39.07 for (Google Transport Security); Wed, 22 May 2019 15:39:07 -0700 (PDT) Received-SPF: pass (google.com: domain of jgg@ziepe.ca designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=HPp1yf7U; spf=pass (google.com: domain of jgg@ziepe.ca designates 209.85.220.65 as permitted sender) smtp.mailfrom=jgg@ziepe.ca DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=UMLE3HAwx27IoVBbcznO88OARo8jwYRssCcya47nG+w=; b=HPp1yf7UaNRX/NHm7Qrb6+fAjoR2gcBQuHExLlVFljSVAj6YPgk7u5skIrg1t+Q676 L+k/DF0Ep78EEOSTSdjakOaul6IT4peNpU0CBpdsaewjQM5qFjASu8RuDwae8Lkm2wgp jm8zl83EuwQTP1qNxJMUNJPygZbR1PsHjnGhvWR3dYIyYw2wWZpKRqnHD/TpRw6n0FHS 3qvSUrjQ6yP/Pp8MSoZIhlXnd+WCyuzsdHTnwA++L7r5QQbNMt2cV3ivlSRvH1X6TNTH 2E9gAxB/pokrMV8S24PNfis99/Hc+RHOiv9NicXOZ4QkaAU4eZrpIg4BFNPp5GSW+Z44 b6Iw== X-Google-Smtp-Source: APXvYqxj4GChHyUq+eruEvlXXg5Xkh+OFkyniU+6VTOrc4UcOOoOthn42FnxYVAYbRcTGFHyw0omgw== X-Received: by 2002:a0c:9ac8:: with SMTP id k8mr73884142qvf.132.1558564747382; Wed, 22 May 2019 15:39:07 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-49-251.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.49.251]) by smtp.gmail.com with ESMTPSA id x3sm14024223qtk.75.2019.05.22.15.39.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 22 May 2019 15:39:06 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTZt4-00046O-2c; Wed, 22 May 2019 19:39:06 -0300 Date: Wed, 22 May 2019 19:39:06 -0300 From: Jason Gunthorpe To: Jerome Glisse Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, Leon Romanovsky , Doug Ledford , Artemy Kovalyov , Moni Shoua , Mike Marciniszyn , Kaike Wan , Dennis Dalessandro Subject: Re: [PATCH v4 0/1] Use HMM for ODP v4 Message-ID: <20190522223906.GA15389@ziepe.ca> References: <20190411181314.19465-1-jglisse@redhat.com> <20190506195657.GA30261@ziepe.ca> <20190521205321.GC3331@redhat.com> <20190522005225.GA30819@ziepe.ca> <20190522174852.GA23038@redhat.com> <20190522201247.GH6054@ziepe.ca> <20190522220419.GB20179@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190522220419.GB20179@redhat.com> User-Agent: Mutt/1.9.4 (2018-02-28) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, May 22, 2019 at 06:04:20PM -0400, Jerome Glisse wrote: > On Wed, May 22, 2019 at 05:12:47PM -0300, Jason Gunthorpe wrote: > > On Wed, May 22, 2019 at 01:48:52PM -0400, Jerome Glisse wrote: > > > > > static void put_per_mm(struct ib_umem_odp *umem_odp) > > > { > > > struct ib_ucontext_per_mm *per_mm = umem_odp->per_mm; > > > @@ -325,9 +283,10 @@ static void put_per_mm(struct ib_umem_odp *umem_odp) > > > up_write(&per_mm->umem_rwsem); > > > > > > WARN_ON(!RB_EMPTY_ROOT(&per_mm->umem_tree.rb_root)); > > > - mmu_notifier_unregister_no_release(&per_mm->mn, per_mm->mm); > > > + hmm_mirror_unregister(&per_mm->mirror); > > > put_pid(per_mm->tgid); > > > - mmu_notifier_call_srcu(&per_mm->rcu, free_per_mm); > > > + > > > + kfree(per_mm); > > > > Notice that mmu_notifier only uses SRCU to fence in-progress ops > > callbacks, so I think hmm internally has the bug that this ODP > > approach prevents. > > > > hmm should follow the same pattern ODP has and 'kfree_srcu' the hmm > > struct, use container_of in the mmu_notifier callbacks, and use the > > otherwise vestigal kref_get_unless_zero() to bail: > > > > From 0cb536dc0150ba964a1d655151d7b7a84d0f915a Mon Sep 17 00:00:00 2001 > > From: Jason Gunthorpe > > Date: Wed, 22 May 2019 16:52:52 -0300 > > Subject: [PATCH] hmm: Fix use after free with struct hmm in the mmu notifiers > > > > mmu_notifier_unregister_no_release() is not a fence and the mmu_notifier > > system will continue to reference hmm->mn until the srcu grace period > > expires. > > > > CPU0 CPU1 > > __mmu_notifier_invalidate_range_start() > > srcu_read_lock > > hlist_for_each () > > // mn == hmm->mn > > hmm_mirror_unregister() > > hmm_put() > > hmm_free() > > mmu_notifier_unregister_no_release() > > hlist_del_init_rcu(hmm-mn->list) > > mn->ops->invalidate_range_start(mn, range); > > mm_get_hmm() > > mm->hmm = NULL; > > kfree(hmm) > > mutex_lock(&hmm->lock); > > > > Use SRCU to kfree the hmm memory so that the notifiers can rely on hmm > > existing. Get the now-safe hmm struct through container_of and directly > > check kref_get_unless_zero to lock it against free. > > It is already badly handled with BUG_ON() You can't crash the kernel because userspace forced a race, and no it isn't handled today because there is no RCU locking in mm_get_hmm nor is there a kfree_rcu for the struct hmm to make the kref_get_unless_zero work without use-after-free. > i just need to convert those to return and to use > mmu_notifier_call_srcu() to free hmm struct. Isn't that what this patch does? > The way race is avoided is because mm->hmm will either be NULL or > point to another hmm struct before an existing hmm is free. There is no locking on mm->hmm so it is useless to prevent races. > Also if range_start/range_end use kref_get_unless_zero() but right > now this is BUG_ON if it turn out to be NULL, it should just return > on NULL. Still needs rcu. Also the container_of is necessary to avoid some race where you could be doing: CPU0 CPU1 CPU2 hlist_for_each () mmu_notifier_unregister_no_release(hmm1) spin_lock(&mm->page_table_lock); mm->hmm = NULL spin_unlock(&mm->page_table_lock); hmm2 = hmm_get_or_create() mn == hmm1->mn mn->ops->invalidate_range_start(mn, range) mm_get_mm() == hmm2 hist_for_each con't mn == hmm2->mn mn->ops->invalidate_range_start(mn, range) mm_get_mm() == hmm2 Now we called the same notifier twice on hmm2. Ooops. There is no reason to risk this confusion just to avoid container_of. So we agree this patch is necessary? Can you test it an ack it please? Jason