From patchwork Thu May 23 15:34:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958079 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6DBA476 for ; Thu, 23 May 2019 15:34:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B1C528680 for ; Thu, 23 May 2019 15:34:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4EF352868D; Thu, 23 May 2019 15:34:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CD30E28680 for ; Thu, 23 May 2019 15:34:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACDE46B0274; Thu, 23 May 2019 11:34:40 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A7EA76B0275; Thu, 23 May 2019 11:34:40 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 944856B0277; Thu, 23 May 2019 11:34:40 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by kanga.kvack.org (Postfix) with ESMTP id 706136B0275 for ; Thu, 23 May 2019 11:34:40 -0400 (EDT) Received: by mail-qk1-f199.google.com with SMTP id v144so5751492qka.13 for ; Thu, 23 May 2019 08:34:40 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=gR5B+R57dzhfm++AjZVD0k9PFQ5ymVIe8oDIOybm+/Y=; b=jpSUczE0XUmUUMzXg6YEtO1YIOOS2zzeKzIPXe3qbSc5Hk+Qhcke+MKddfv4yuJs2l sNbKJAAqzKcS4lsEnGzBJt3gOtPUELKSWxRJFyxeV6P60pEjMRIuciwBgduXoulEBzYc kg6nV4QnZW0ye/Fs8C+Bp//5CLOgLmNScODQfQyFGjFSsfMjkzwS2sr5cZAtJfzbIVR7 jOd9uFkEtUnp0E7kRsqqmMb6Een5DshD67yFzdhU8Zmh8Fm9VTOlJe0oH3Z8mHU7JVLv gI7oqSZ6+QQ3/u06flFMeSCPUvrDCVUC1mWZ2dkJuv2UwXaDAsGc/muphjO5yLoXeURv rBPw== X-Gm-Message-State: APjAAAV2/0gBuJVw/yLoFB8bHgmj3i12VFHVHHLgv80CKybBX9pix1jo ZBTgdvauqdjLsCszMJZBgsSGJ6DPXwrMvtttU8DIcHAGmIi6TN5bJePliWaW0NlT8DtgkjLUvHp n2YG/n9MuXV3aZ6l6/KwG/ropPrHDgwknlAuuKTTe8pkPPlL9qyERRObaQRCsYAFTgQ== X-Received: by 2002:ae9:e806:: with SMTP id a6mr59488974qkg.247.1558625680218; Thu, 23 May 2019 08:34:40 -0700 (PDT) X-Received: by 2002:ae9:e806:: with SMTP id a6mr59488895qkg.247.1558625679454; Thu, 23 May 2019 08:34:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625679; cv=none; d=google.com; s=arc-20160816; b=H0Y1GTzEoxBtsYzRStEqREjx6BVtaylMa+tdlQpdm2I3v+zFUbOkVMw238qLWoo61b hrjPTCDW7zha/+gAFXnu6qkGAbPJwfbhMbLG7viRbQeQC5ZnfxL8MGAAeen9M3dxERQ/ cpH2YoqVYmZnUBLKoxBxdT6oVLbsmcFLaMY82x9dn1Z1qfMwdj3XiTgq32bxzbaUIr9M nfMhdq1cnLMpEzRB2t2FUqiljGnWLYN4w/o4oF9YvrS5346/k0XePkrAZOlyWPAyouP1 wfZpNoQ3KDCmdVtQf9oz/JeZiSrPYiGmUghMjHpFEfky39gOOO5UGSqVgsPy5NZaLBiS z0MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=gR5B+R57dzhfm++AjZVD0k9PFQ5ymVIe8oDIOybm+/Y=; b=Tz2xpnHOgNPdoeBsjSl1+3j1lPjpfJOlCoAt68s8bC5t+naw7komvp8YdfXsDto+1V HaCwJ5zAxscfM1LLa1bLj3ivghSRb5GGmd/lpbVyJDGFSg2fs0hZ1B+j7aBJH2wGoovt 1gnVPRWG9eAtojElMlYk7BimzrAGRC+/jO5E/Lie9k5+qIVbkO/Ox/djWPy9ceKq2xgw mlrbvv/Pyuhff0bldHJy3IS7kNSW48ZTTJ2WwpMzltO4eMh5zJchQ4Sqj1AO7K39NwzL R/3T7a19857rNhDqoN5/++bZCc6sdgmZcY4Q9J6uT5pc9IUtW53djnMShjKjXHT2u6kZ qyww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=K8z8HnLI; 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 d127sor4371798qkc.16.2019.05.23.08.34.39 for (Google Transport Security); Thu, 23 May 2019 08:34:39 -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=K8z8HnLI; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gR5B+R57dzhfm++AjZVD0k9PFQ5ymVIe8oDIOybm+/Y=; b=K8z8HnLIYzj0CvIlrQpPXO2d+uOz8Pq+97N2YabABx1Vrr+qMaFLmh2VnVLx7a2oGq wOLg9W+DBwbkikfS0ff5K3MxYHAk3TSgoQYOvWZ/gmtirGdLKUC9DG9R8Ipu8mV0wg1O AVWPOOkR7fDNM3E0R1Ft5vDpgr37TJcvqhe3MhUEFNRKFUO4zvfCWS/qD4Zi6BZH+zi+ 2nHw0N8KVoBb1X1p7s0lsRt99Yq5hmwdLBLjHEaLE1oOXEBXSTOo4PBEa5CReklY8/7U mJVOX/VX4CF+TvyigLMWsvggRdPp9oUrqqQAiJtfOXeUBS65Sy7nS52+0lUgbQD2cN4+ 7aZQ== X-Google-Smtp-Source: APXvYqx/CGFpFj+ej4XSzm0t6zCGOlTT7rmKjSiwJVztU6ABpwKrdUJowycnkB+WIld4Nb1SIXkSCw== X-Received: by 2002:a37:c445:: with SMTP id h5mr48174135qkm.105.1558625679185; Thu, 23 May 2019 08:34:39 -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 v126sm13212129qkh.86.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:38 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjp-0004zA-Sv; Thu, 23 May 2019 12:34:37 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 01/11] mm/hmm: Fix use after free with struct hmm in the mmu notifiers Date: Thu, 23 May 2019 12:34:26 -0300 Message-Id: <20190523153436.19102-2-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe 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. Resulting in use after free races like this: 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Ira Weiny --- include/linux/hmm.h | 1 + mm/hmm.c | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 51ec27a8466816..8b91c90d3b88cb 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -102,6 +102,7 @@ struct hmm { struct mmu_notifier mmu_notifier; struct rw_semaphore mirrors_sem; wait_queue_head_t wq; + struct rcu_head rcu; long notifiers; bool dead; }; diff --git a/mm/hmm.c b/mm/hmm.c index 816c2356f2449f..824e7e160d8167 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -113,6 +113,11 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm) return NULL; } +static void hmm_fee_rcu(struct rcu_head *rcu) +{ + kfree(container_of(rcu, struct hmm, rcu)); +} + static void hmm_free(struct kref *kref) { struct hmm *hmm = container_of(kref, struct hmm, kref); @@ -125,7 +130,7 @@ static void hmm_free(struct kref *kref) mm->hmm = NULL; spin_unlock(&mm->page_table_lock); - kfree(hmm); + mmu_notifier_call_srcu(&hmm->rcu, hmm_fee_rcu); } static inline void hmm_put(struct hmm *hmm) @@ -153,10 +158,14 @@ void hmm_mm_destroy(struct mm_struct *mm) static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) { - struct hmm *hmm = mm_get_hmm(mm); + struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); struct hmm_mirror *mirror; struct hmm_range *range; + /* hmm is in progress to free */ + if (!kref_get_unless_zero(&hmm->kref)) + return; + /* Report this HMM as dying. */ hmm->dead = true; @@ -194,13 +203,15 @@ static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) static int hmm_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *nrange) { - struct hmm *hmm = mm_get_hmm(nrange->mm); + struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); struct hmm_mirror *mirror; struct hmm_update update; struct hmm_range *range; int ret = 0; - VM_BUG_ON(!hmm); + /* hmm is in progress to free */ + if (!kref_get_unless_zero(&hmm->kref)) + return 0; update.start = nrange->start; update.end = nrange->end; @@ -248,9 +259,11 @@ static int hmm_invalidate_range_start(struct mmu_notifier *mn, static void hmm_invalidate_range_end(struct mmu_notifier *mn, const struct mmu_notifier_range *nrange) { - struct hmm *hmm = mm_get_hmm(nrange->mm); + struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); - VM_BUG_ON(!hmm); + /* hmm is in progress to free */ + if (!kref_get_unless_zero(&hmm->kref)) + return; mutex_lock(&hmm->lock); hmm->notifiers--; From patchwork Thu May 23 15:34:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958093 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E728C76 for ; Thu, 23 May 2019 15:34:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4AA4283EE for ; Thu, 23 May 2019 15:34:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D1D5C2869D; Thu, 23 May 2019 15:34:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 607FD283EE for ; Thu, 23 May 2019 15:34:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01FA76B0275; Thu, 23 May 2019 11:34:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EED416B0278; Thu, 23 May 2019 11:34:41 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8CB56B0279; Thu, 23 May 2019 11:34:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by kanga.kvack.org (Postfix) with ESMTP id B73E56B0275 for ; Thu, 23 May 2019 11:34:41 -0400 (EDT) Received: by mail-qt1-f197.google.com with SMTP id g14so5645667qta.12 for ; Thu, 23 May 2019 08:34:41 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ZePMKZ7jqBhopBdwVcCn1CrsRsr5VRg48k9A7BJXYb4=; b=ouA5FfzAkhencLhHyy2LtcN63Lg9dI2UC69W27l7yKpTTZI/q8Bi8gW3guPtiWRv9P FUFTXc7dfd8Pip2jrULFlNMUpqnloN6IIGhSgdfTRP1Cq6E88zOFHBRbe8+f+JRXski5 7ev5Dma2eZ46QQW00OvAjlUb9fFimTsW9Le7HbmBuOMI9uiDh7uUhCeDz5NUVDR8DA+9 l4q/h3omcQm6uKW6ZQhVDwTSYcGexTuyBZA6tBcXUBN55cEuXUlC47PfDeOMolZmXJEH oPOumqRPLsmnc/N6oV5VOeiepTvd5bCVh/nC2wtx34+ZxRnAGve7g/UabbptM6YJGuhi OpyA== X-Gm-Message-State: APjAAAUbu2GyzQkkk1czKHSkalqL+9cfH9ertWwcIgnCl1xBJpasLVT1 /ntvTSH9BrbQS/iKa1F7jWydeD8yw/pvY5T/D0NPDrQJnSwGkbr53e6AFgsM64wHH3FDl4CM4gl 40PLGwfilHlqj6eqfMyv7y6XAAuiPRQZtTAR9ruNvDRC77laH1ytaeT4XVLU91qMdyg== X-Received: by 2002:a0c:929a:: with SMTP id b26mr77569594qvb.148.1558625681486; Thu, 23 May 2019 08:34:41 -0700 (PDT) X-Received: by 2002:a0c:929a:: with SMTP id b26mr77569475qvb.148.1558625680352; Thu, 23 May 2019 08:34:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625680; cv=none; d=google.com; s=arc-20160816; b=vtFuxeG4zv+8QUG1h77m0ctKjAaMFiGJpz63qWwH6Sm+nq3P1LQvAIMlBz6v3NBHyM jkw6HdHux2LLzLdAWZ/PJFdpGHc/Qoe6I9JuIya6db2MzuEIqaf5EGJ58lanwYo4kffY YNiXB2/qYjQD5u5+1Ozgjkmv6V+VzIpWlGsoQBxlsq7+rVJEn/X0O8FPHeKhvJDXG4sI CHncll83aPr+r1yLUzlw2gQH4vHebIj4LTHkfVve4yCpyb/q7ZZdmLZPZ32XI+CiaGCV J5rNpHEl66nfw6Hhua6Jr7X8z7u6wNWdxcCMbsNhNNLIeWAgsrpxdNnluph3bdPdyIvZ RPcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=ZePMKZ7jqBhopBdwVcCn1CrsRsr5VRg48k9A7BJXYb4=; b=eYsl2hhL8IJ/fECEjHKYFcXh8gcdB7N6bUSyPQJmecPYdxGgh46IK76Hsqi3r329Fr bKxv0o38Pq5Wl7uNMj47G4GSdIzScSYCc0vP/FOnJY6/emRabL0vTbNZUqGuzSHei8GO 30OTAo3bvuVZFr6rKlEoJZSwtDh6nFdKPad4O3Om4xxHFOYxH/YQpXFUTQjpCxsgTdQW KdZgwHPKZI/0PJm3Je9tDLgDI4gFYrc+aYrxCxoV47lVtfPUpccHkFrIQeO7tmWZiuTU wIEvQcWZsXEIoiojR9TeUPMJNvCLfGJRZZPEVlbkbfBC5FDlIfLSyDLUobyTCXK/x70p pymQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=jv6BZA8C; 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 k63sor2739403qkc.103.2019.05.23.08.34.40 for (Google Transport Security); Thu, 23 May 2019 08:34:40 -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=jv6BZA8C; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZePMKZ7jqBhopBdwVcCn1CrsRsr5VRg48k9A7BJXYb4=; b=jv6BZA8C1NV02Y1wmdXHZ90xqpN8v3U2iUJqYKuvZM/jGdctWUbv+nypqASupEpUuN vIJUJzSEQT+oiGmCOeMcLnf/zJZZJYkp9fMkQWdbVn6lbTE2PWKG+0XU8rQzC0Hq7Y+X r1gaLm6hZu3mcuSUXfzmFexbK/YKvGzQEV5NqwCdYopvQ50bAWFzwmX3JWwZuuR6KItI LqdMy9kYprQxYkWCbGlKs8lxcUTCSD7OTwxmzLrQKpMbz0TWlcMw3K9W6VInHK08n7Qd 2fajaOkWWQEZaJr0X7BQVoog4kSz1uBsRTCfvef4Gw8N0ebsa2yCNtTsdP4xqFS6YoQD xjuA== X-Google-Smtp-Source: APXvYqzE9Wf6pPLXAKTOOnViH6zjZXhhQeJF2uy5/0EwAhBMsAqbYZTx1jGzOFC4t22sUwexJeiqqQ== X-Received: by 2002:ae9:e208:: with SMTP id c8mr74383498qkc.154.1558625680068; Thu, 23 May 2019 08:34:40 -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 x10sm2553300qkl.84.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:38 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjp-0004zH-Uv; Thu, 23 May 2019 12:34:37 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 02/11] mm/hmm: Use hmm_mirror not mm as an argument for hmm_register_range Date: Thu, 23 May 2019 12:34:27 -0300 Message-Id: <20190523153436.19102-3-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe Ralph observes that hmm_register_range() can only be called by a driver while a mirror is registered. Make this clear in the API by passing in the mirror structure as a parameter. This also simplifies understanding the lifetime model for struct hmm, as the hmm pointer must be valid as part of a registered mirror so all we need in hmm_register_range() is a simple kref_get. Suggested-by: Ralph Campbell Signed-off-by: Jason Gunthorpe --- include/linux/hmm.h | 7 ++++--- mm/hmm.c | 14 +++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 8b91c90d3b88cb..87d29e085a69f7 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -503,7 +503,7 @@ static inline bool hmm_mirror_mm_is_alive(struct hmm_mirror *mirror) * Please see Documentation/vm/hmm.rst for how to use the range API. */ int hmm_range_register(struct hmm_range *range, - struct mm_struct *mm, + struct hmm_mirror *mirror, unsigned long start, unsigned long end, unsigned page_shift); @@ -539,7 +539,8 @@ static inline bool hmm_vma_range_done(struct hmm_range *range) } /* This is a temporary helper to avoid merge conflict between trees. */ -static inline int hmm_vma_fault(struct hmm_range *range, bool block) +static inline int hmm_vma_fault(struct hmm_mirror *mirror, + struct hmm_range *range, bool block) { long ret; @@ -552,7 +553,7 @@ static inline int hmm_vma_fault(struct hmm_range *range, bool block) range->default_flags = 0; range->pfn_flags_mask = -1UL; - ret = hmm_range_register(range, range->vma->vm_mm, + ret = hmm_range_register(range, mirror, range->start, range->end, PAGE_SHIFT); if (ret) diff --git a/mm/hmm.c b/mm/hmm.c index 824e7e160d8167..fa1b04fcfc2549 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -927,7 +927,7 @@ static void hmm_pfns_clear(struct hmm_range *range, * Track updates to the CPU page table see include/linux/hmm.h */ int hmm_range_register(struct hmm_range *range, - struct mm_struct *mm, + struct hmm_mirror *mirror, unsigned long start, unsigned long end, unsigned page_shift) @@ -935,7 +935,6 @@ int hmm_range_register(struct hmm_range *range, unsigned long mask = ((1UL << page_shift) - 1UL); range->valid = false; - range->hmm = NULL; if ((start & mask) || (end & mask)) return -EINVAL; @@ -946,15 +945,12 @@ int hmm_range_register(struct hmm_range *range, range->start = start; range->end = end; - range->hmm = hmm_get_or_create(mm); - if (!range->hmm) - return -EFAULT; - /* Check if hmm_mm_destroy() was call. */ - if (range->hmm->mm == NULL || range->hmm->dead) { - hmm_put(range->hmm); + if (mirror->hmm->mm == NULL || mirror->hmm->dead) return -EFAULT; - } + + range->hmm = mirror->hmm; + kref_get(&range->hmm->kref); /* Initialize range to track CPU page table update */ mutex_lock(&range->hmm->lock); From patchwork Thu May 23 15:34:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958097 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A3E021395 for ; Thu, 23 May 2019 15:34:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9181E283EE for ; Thu, 23 May 2019 15:34:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8627228688; Thu, 23 May 2019 15:34:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1294C28673 for ; Thu, 23 May 2019 15:34:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1D936B0278; Thu, 23 May 2019 11:34:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 972076B027A; Thu, 23 May 2019 11:34:42 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FF4A6B027C; Thu, 23 May 2019 11:34:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by kanga.kvack.org (Postfix) with ESMTP id 4354E6B0278 for ; Thu, 23 May 2019 11:34:42 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id l20so5645699qtq.21 for ; Thu, 23 May 2019 08:34:42 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=o0fnCIS1UU2Yl5HFhaP9USXWVXi3ioUfilGqBeg9WXo=; b=JuKEI2pnFHD7k0z35Cb3SuaXJGs6fCjnF4Rhld1GGjCNW5JMj4SMEaU0sJLS9YSJ1M OYj4gKrb4bhwV+boqWle/UCMsFOd96pOnECVeS6Z4IAI3IVs0zurgKO5chHsXk/7QBau J/FdgxmNWnrJPqS+rwui1DFU7y+7dpbkZiOwY9IOmnGFhWtea63aIf1BAlgS4kkc4MsT 4NTJfTCrkttALnEPg7VBS+MWipfzGGMUJ15tAGqt0ymcj+Dirb5jFNr8uyShOqnykmAR RdeBcRLdBi7PyMniIRVWzTGBfVpWh3+4nq0Xwvt7hpcsWPsR6v2c3Yl+bajvMK6GFZy4 TJyg== X-Gm-Message-State: APjAAAXNnkAvVyZSP1/IY+QenTy0muCHAVug73JBb3IcatVPGKM+2jqM gDo/chTxg5YNe9nH9ImB0GTUn9FFzOZtyd1zPnGF6NZpJkSi3eGVFYKPE8609kYI5s08LVVMN64 qp8y0XEEOIMNzVDwMZB/dBlllFpytdNScj6zY2S95xlnhrw6UOc2ZvzDDY0D/sY+S4Q== X-Received: by 2002:aed:3a23:: with SMTP id n32mr28872611qte.360.1558625682018; Thu, 23 May 2019 08:34:42 -0700 (PDT) X-Received: by 2002:aed:3a23:: with SMTP id n32mr28872476qte.360.1558625680809; Thu, 23 May 2019 08:34:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625680; cv=none; d=google.com; s=arc-20160816; b=jEkvkGNeifjT0dhwfvLwYdhZlo6py4akIwBcd11mDuBGLA3jsfw+JFYfrPN3NgA4Nj tY2HkBfM6dvAtexkVW0DA0t15v3IktoRIxZW50BwUwPzSbxm6tzizKMO/kwppLYYtLHw d6YdniZNHsYj8kyhZfeJSERPGXQgO+bNfLXFxot7KQnr61yIJwtp29vxUVEFv1cyIvZB hWTU1IqvxIWHDnf2lY5t8NsB/3OQQHxA7x835En9jjXFknp2lkN5t+pTSnXTrSnJFFOt hQ1Kpw60ZnjpizrSQT6eOg9gV9iewDQj9s7rFQeYjxwal+lzAImtRxQXRwiGB/VTRHhg ZrAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=o0fnCIS1UU2Yl5HFhaP9USXWVXi3ioUfilGqBeg9WXo=; b=tL9wPKuCivM/mdZ+T7+RSdJxfdxXtaTTAp0tvuODTHDZKlYEYSzVe43taF9dkmNaHY 0V5EFnoK/Qsr7vvZxtsDkVYnZ7oiZ8kgFgl8B37tpJt5sJAyAYmdfpngaengxbzdQaYf VlM1uzA15aVzZRgfgq9ASTNr9z+iCxfF6v4DgvMJX9XdbQlKEWhG8tGNdomzWcQ6xJG/ NkLwBgM4Te9xHsV3RF8XgCWjxn6qlXRzSMPlVZ+tP3HhSXhEHkMB3XZ/9aw0KhHa7xfN ArFICw+r3o2uVrAiAtaBOiUt8xTGwQolXCGW5NpIkqGtY85B0r1Qy1WSCM39YaKsd4e2 mEYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=ZAWb6C2Z; 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 d2sor17611268qvc.0.2019.05.23.08.34.40 for (Google Transport Security); Thu, 23 May 2019 08:34:40 -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=ZAWb6C2Z; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o0fnCIS1UU2Yl5HFhaP9USXWVXi3ioUfilGqBeg9WXo=; b=ZAWb6C2Zy9X1DesFJzdUY1NrKsPiS5Jvp1PCkg+zTa3+HZUKgu72kwwZR9Zz7cuUsN hqHmPHEl1SZqi6Ksxs8IWDmWa8NYcaq27MZg4o43q9cS1rMdNIwjOHXVvrNrUhmerPLH 2C4RM0d83niUVSA60rDZxLiCaCg0XIRLCrxlMxIAkpDCL2qGgOJrwg93mDBo0owbS9wG GigRKCJfgXFzftD8yNj5WhTzw0oUfA/5axgHUIGjENJUBHkLPDeOC2pI2f/yYxuyr07Y 3SKahFyaohSEsYHGIuLk/ql8MKS182Eez2RiWCFyOULJUSZK8ElKWKLASN/yeR0Yf0fy jaIQ== X-Google-Smtp-Source: APXvYqyWStElkAeFxeRHIt8qw7jwn+e9hRhuM1POWa1lozD72Ww5AlAsfY/GROw/QPhwUqv3LHvK6A== X-Received: by 2002:a0c:8b6f:: with SMTP id d47mr30646505qvc.32.1558625680542; Thu, 23 May 2019 08:34:40 -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 r47sm18148670qtc.14.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjp-0004zN-WB; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 03/11] mm/hmm: Hold a mmgrab from hmm to mm Date: Thu, 23 May 2019 12:34:28 -0300 Message-Id: <20190523153436.19102-4-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe So long a a struct hmm pointer exists, so should the struct mm it is linked too. Hold the mmgrab() as soon as a hmm is created, and mmdrop() it once the hmm refcount goes to zero. Since mmdrop() (ie a 0 kref on struct mm) is now impossible with a !NULL mm->hmm delete the hmm_hmm_destroy(). Signed-off-by: Jason Gunthorpe --- include/linux/hmm.h | 3 --- kernel/fork.c | 1 - mm/hmm.c | 21 +++------------------ 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 87d29e085a69f7..2a7346384ead13 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -584,14 +584,11 @@ static inline int hmm_vma_fault(struct hmm_mirror *mirror, } /* Below are for HMM internal use only! Not to be used by device driver! */ -void hmm_mm_destroy(struct mm_struct *mm); - static inline void hmm_mm_init(struct mm_struct *mm) { mm->hmm = NULL; } #else /* IS_ENABLED(CONFIG_HMM_MIRROR) */ -static inline void hmm_mm_destroy(struct mm_struct *mm) {} static inline void hmm_mm_init(struct mm_struct *mm) {} #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ diff --git a/kernel/fork.c b/kernel/fork.c index b4cba953040a0f..51b114ec6c395c 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -672,7 +672,6 @@ void __mmdrop(struct mm_struct *mm) WARN_ON_ONCE(mm == current->active_mm); mm_free_pgd(mm); destroy_context(mm); - hmm_mm_destroy(mm); mmu_notifier_mm_destroy(mm); check_mm(mm); put_user_ns(mm->user_ns); diff --git a/mm/hmm.c b/mm/hmm.c index fa1b04fcfc2549..e27058e92508b9 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,7 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm) hmm->notifiers = 0; hmm->dead = false; hmm->mm = mm; + mmgrab(hmm->mm); spin_lock(&mm->page_table_lock); if (!mm->hmm) @@ -130,6 +132,7 @@ static void hmm_free(struct kref *kref) mm->hmm = NULL; spin_unlock(&mm->page_table_lock); + mmdrop(hmm->mm); mmu_notifier_call_srcu(&hmm->rcu, hmm_fee_rcu); } @@ -138,24 +141,6 @@ static inline void hmm_put(struct hmm *hmm) kref_put(&hmm->kref, hmm_free); } -void hmm_mm_destroy(struct mm_struct *mm) -{ - struct hmm *hmm; - - spin_lock(&mm->page_table_lock); - hmm = mm_get_hmm(mm); - mm->hmm = NULL; - if (hmm) { - hmm->mm = NULL; - hmm->dead = true; - spin_unlock(&mm->page_table_lock); - hmm_put(hmm); - return; - } - - spin_unlock(&mm->page_table_lock); -} - static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) { struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); From patchwork Thu May 23 15:34:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958099 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 062721395 for ; Thu, 23 May 2019 15:34:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E839628688 for ; Thu, 23 May 2019 15:34:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC8F5286A0; Thu, 23 May 2019 15:34:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A9BCC28686 for ; Thu, 23 May 2019 15:34:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C8E2D6B027A; Thu, 23 May 2019 11:34:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BE62F6B027D; Thu, 23 May 2019 11:34:42 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EA086B027B; Thu, 23 May 2019 11:34:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by kanga.kvack.org (Postfix) with ESMTP id 4B7486B027A for ; Thu, 23 May 2019 11:34:42 -0400 (EDT) Received: by mail-qk1-f197.google.com with SMTP id x68so5756569qka.6 for ; Thu, 23 May 2019 08:34:42 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=1UYOs1b1m4kqMa2Hcebr2aPMxfP9lZp6i0AyhspLKIc=; b=oSgJdJxDqzT6VTZl9fwiSNBAovSQo+iftNGjPASxLDlBE7Ixg+xlEwtn8dFayyZ59f Hm1hom46p+ZZocmTs3Hr2ManxdWSx1gTL1zlx3+phUaonNe1bCsWOQTe1EBNM7fQaoRS IIDBiqEXVUvKZa53jZO/kooIKoQqMpzCjgVWzqHcuDThosV9qw6XL9a1dBfCDUfH4yJy rAfKaUKtDOQa3+zejUVl27ZYEVF0lAj8j1rBf6tfk3CZUBbwPbmlaIFAEj4zWLzdf/fi BU65qEDW79eV+3SNRmgYutVQt/Ue0yraAswxjk+5a4opPJM4NF7/ETvgTEhePQlKdcWr Qc4A== X-Gm-Message-State: APjAAAUoeY+iF2E2ijA8jyma5XHWhtwomM0aD0k11/WD5Bu6x5x45y7w 6zTzJzfAbOjEqPMdfYUJjYGzLeUwfTQu22E9FVr1Odv+SSCRiOVQcih8FfpaiDIkyPdtsfQG9fW UTn3znYY2AXfp/teDqPE94n/0+y779R8qLO8sBMKJU3+7NEnU1UtugH+M3qf3bj3IAw== X-Received: by 2002:ac8:7313:: with SMTP id x19mr14949946qto.185.1558625682077; Thu, 23 May 2019 08:34:42 -0700 (PDT) X-Received: by 2002:ac8:7313:: with SMTP id x19mr14949859qto.185.1558625681210; Thu, 23 May 2019 08:34:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625681; cv=none; d=google.com; s=arc-20160816; b=D4/s1pP80Kx1BEzzeXRBGtHdIJ9kduc89WFcmMI7Ad4jBgotrMWNzjo2h4LoJE1Flu a4b2FzhqgL7wyHcmDSReajRU+sT4oUaZ9yfwxPVTBxvGHceWd310+NGIUw/Ld5bWWQca bAlOfEhPLlYyXiAHnfiBz0QZ9JVyQnB3sCzMQakrsDwsXgTvcuaCopSwPpohjCpCPPtF Texzt3MiLCML/u4S8vVnCW0AObrATIE0lj5sWHpm7jZjBk3l4FO6oZvwo73zazdiWw4v PzmL+bJlkdzh5F/J5IAQVsjf7kJkmenAh2PNzsuaCcZuZdjsf/HBOnfTjJqiOl3OBye/ VzQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=1UYOs1b1m4kqMa2Hcebr2aPMxfP9lZp6i0AyhspLKIc=; b=axkUymxY7bAh9f3zFpj4PUbZl78RvDYxGy+zCvpv1W/3VdVMdC27o9i4EbomPT3gfu DyTfB7WZFzZfJakU/sHZkYGWvgfGM5lr0oHP1E8IJ8W/Ji2le3oDV5hJj5TlZ2MZEyJy z0mPsry1tq/E4eFJMfVwy8bHTzXVDLO7fCtM71CPiew9nmzGkMOl4nOB+4tNagEhJ5T+ Lpk+K7AVQhNCtfj6YV0QY7/8d5cvPkN0JAMUbJbdTPYQMYDRbB5kGc7wNU6Uknugn3X5 ML83dricoKfT6XZdoVLILq2VqvH554hKOht52qbWRqq8HVDhKQ+CQZ06mnngrVsb1uYn U5Rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=LV2mKDWl; 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 y10sor22147912qvf.18.2019.05.23.08.34.41 for (Google Transport Security); Thu, 23 May 2019 08:34:41 -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=LV2mKDWl; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1UYOs1b1m4kqMa2Hcebr2aPMxfP9lZp6i0AyhspLKIc=; b=LV2mKDWl6z86EAVgXFrAKp9hIarl1v5CuBTZz0h3rmucixSlFycPZyRubUjCdCgxLA AtK7gRpiNk4P4k7v2McacqOVxWDzHDj1SIjoKD7Ak5PnFKzLI5rh/G1hioQExJlxX/qo 1aRAcCTHLzYgrrxhfgpVo6ETGueE0agyf8qN0BMT7WPG19ZYlmnMrra6KUdEXX3cEkGG BDEIIdpCyEyvvS0QDao0w2v4CajWdmWOKQIXYRhlqugAFNyeY4ReYthg9OXsCqyFabSi X0vD7hp+cgUvC8GCSciZF5t9Od7M9Jbnz3qcwMPWB1g/F6XF9aS65QgPOkraL59krHZQ OSog== X-Google-Smtp-Source: APXvYqx4TikclVwMPUpuo7NFKA6Xd+Jq88ZLLLLwtvyEZvxo6rVnUXsWNS36lZk+68Gag43lw4B0JA== X-Received: by 2002:a0c:ad85:: with SMTP id w5mr8251178qvc.242.1558625680953; Thu, 23 May 2019 08:34:40 -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 k53sm13877244qtb.65.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-0004zT-1F; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 04/11] mm/hmm: Simplify hmm_get_or_create and make it reliable Date: Thu, 23 May 2019 12:34:29 -0300 Message-Id: <20190523153436.19102-5-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe As coded this function can false-fail in various racy situations. Make it reliable by running only under the write side of the mmap_sem and avoiding the false-failing compare/exchange pattern. Also make the locking very easy to understand by only ever reading or writing mm->hmm while holding the write side of the mmap_sem. Signed-off-by: Jason Gunthorpe --- mm/hmm.c | 75 ++++++++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 48 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index e27058e92508b9..ec54be54d81135 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -40,16 +40,6 @@ #if IS_ENABLED(CONFIG_HMM_MIRROR) static const struct mmu_notifier_ops hmm_mmu_notifier_ops; -static inline struct hmm *mm_get_hmm(struct mm_struct *mm) -{ - struct hmm *hmm = READ_ONCE(mm->hmm); - - if (hmm && kref_get_unless_zero(&hmm->kref)) - return hmm; - - return NULL; -} - /** * hmm_get_or_create - register HMM against an mm (HMM internal) * @@ -64,11 +54,20 @@ static inline struct hmm *mm_get_hmm(struct mm_struct *mm) */ static struct hmm *hmm_get_or_create(struct mm_struct *mm) { - struct hmm *hmm = mm_get_hmm(mm); - bool cleanup = false; + struct hmm *hmm; - if (hmm) - return hmm; + lockdep_assert_held_exclusive(mm->mmap_sem); + + if (mm->hmm) { + if (kref_get_unless_zero(&mm->hmm->kref)) + return mm->hmm; + /* + * The hmm is being freed by some other CPU and is pending a + * RCU grace period, but this CPU can NULL now it since we + * have the mmap_sem. + */ + mm->hmm = NULL; + } hmm = kmalloc(sizeof(*hmm), GFP_KERNEL); if (!hmm) @@ -85,54 +84,34 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm) hmm->mm = mm; mmgrab(hmm->mm); - spin_lock(&mm->page_table_lock); - if (!mm->hmm) - mm->hmm = hmm; - else - cleanup = true; - spin_unlock(&mm->page_table_lock); - - if (cleanup) - goto error; - - /* - * We should only get here if hold the mmap_sem in write mode ie on - * registration of first mirror through hmm_mirror_register() - */ hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; - if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) - goto error_mm; + if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) { + kfree(hmm); + return NULL; + } + mm->hmm = hmm; return hmm; - -error_mm: - spin_lock(&mm->page_table_lock); - if (mm->hmm == hmm) - mm->hmm = NULL; - spin_unlock(&mm->page_table_lock); -error: - kfree(hmm); - return NULL; } static void hmm_fee_rcu(struct rcu_head *rcu) { + struct hmm *hmm = container_of(rcu, struct hmm, rcu); + + down_write(&hmm->mm->mmap_sem); + if (hmm->mm->hmm == hmm) + hmm->mm->hmm = NULL; + up_write(&hmm->mm->mmap_sem); + mmdrop(hmm->mm); + kfree(container_of(rcu, struct hmm, rcu)); } static void hmm_free(struct kref *kref) { struct hmm *hmm = container_of(kref, struct hmm, kref); - struct mm_struct *mm = hmm->mm; - - mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm); - spin_lock(&mm->page_table_lock); - if (mm->hmm == hmm) - mm->hmm = NULL; - spin_unlock(&mm->page_table_lock); - - mmdrop(hmm->mm); + mmu_notifier_unregister_no_release(&hmm->mmu_notifier, hmm->mm); mmu_notifier_call_srcu(&hmm->rcu, hmm_fee_rcu); } From patchwork Thu May 23 15:34:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958101 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1F22A76 for ; Thu, 23 May 2019 15:34:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C7A828682 for ; Thu, 23 May 2019 15:34:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0101528686; Thu, 23 May 2019 15:34:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8DD8828673 for ; Thu, 23 May 2019 15:34:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 702046B027D; Thu, 23 May 2019 11:34:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6386A6B0280; Thu, 23 May 2019 11:34:43 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52B8B6B027F; Thu, 23 May 2019 11:34:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by kanga.kvack.org (Postfix) with ESMTP id 297CD6B027C for ; Thu, 23 May 2019 11:34:43 -0400 (EDT) Received: by mail-qk1-f197.google.com with SMTP id w184so5747871qka.15 for ; Thu, 23 May 2019 08:34:43 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=8d3qZxuduenTYxmVtLo/HTiLB+fwW/lCyyNPj/qu33U=; b=G4POJojfjW5pONjrBiisa0WuaQLNY+aucoIVXi8gE5yvqRZumZsQHtPLhkr83BdtIh Rm0sQxnSlnvy9pC8BLQRMAcsiga2HTr60z7UYbIN1jqltq/R6AstiZQdloBKshVh9rD+ dd1k9BiObwvoILDvWDrqaHFJ/dNfHbWLeUMbyAT5TKk9ZEKOgIbmc+awyaQTh8UH0T8i J9h9TeDVL3SjmomOXqJuN5lm0ftNQnU8vpnTTSo6QxQXSXGv6es2+J3AYIsmuu8GzINE 9WPqVid5Z5p0DA5P4rNM2bSJZMWWElRLJ+xx7H0RHEakd6R9JpZdP5GKQgC3vLLhLjfK aw2g== X-Gm-Message-State: APjAAAXwj3eV9Q0H+SyQ7P8PfNvsEiSn/QX3aEhR3b3/LF0oo1Qk9kbC 051TRKE0jbGs2NTSbKmj/lgnjZvxRyyzSnlMDqjmyML4fGzrhz3i+nO1bb0MJbBaT26fRw/lKH2 zxaLESYxP+bB9ZxJGADiS0swa385n8PeJ/egABrfwgL0Ub4PEnBey/uBBL6St28TdiA== X-Received: by 2002:a05:620a:113a:: with SMTP id p26mr75100447qkk.12.1558625682918; Thu, 23 May 2019 08:34:42 -0700 (PDT) X-Received: by 2002:a05:620a:113a:: with SMTP id p26mr75100351qkk.12.1558625681846; Thu, 23 May 2019 08:34:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625681; cv=none; d=google.com; s=arc-20160816; b=F1vBZADb0q88hoaNmBNEFw58RNpqQy8LSPU+tamLw+lkfaMoSZ8S9HU47jrR0QODX/ nXjv0wp63IuUlaAQtY/vDIoGIVicJPXIQEGg/Ec161U9u1FlyoW/kg4Xg88m3HMUQNXH 7ITIJf1MsMKwXzaR1qY1ON/MaCees76o8f4JvX3u+dARIstVu4oilhLWPI50CMMFzybI RviFqN/6oooloYZK9ZRbaLsdSzej9+mcMThaFn8Us/MpR+uTYBftIyPXlNLR7zZQXE6l 1o4U4dbUeHSig90gaAolidmwvMnmZNE7UOX+0JWY7AuZ/yJJgOhSsAKgC41RDjmf0ArG kdqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=8d3qZxuduenTYxmVtLo/HTiLB+fwW/lCyyNPj/qu33U=; b=ByU+Q7VngtHVrGWSVqNDXFUMNF6cAM0TqKGjtxLNIultYe0PIcrtGb2G0d6+drgz8H fYx01vQEJ0s3EqwnEp5x4GFMliqO5R1rFRLAo2yeW6pWaEQw9VbLWl8F9U30i2duQ9UG Fe2LuYEz1uZUf2lVw1QaZxj+abD3JFfsfPiaShRThH/gnVbmHyUXh6jLh33wkqOt+NEO aj3f4dtRygjsNEDa86vRLz+lHgohkSr6YBysJKNtV+0uEjD6nfA0jU6LUL4gAOXLZytY v7+e9WK5EO8FcAzWVd3F2nMEpH5jcq+rxjubj65UDAwtOI+NN4tUD7d5E/wHFnb8d7o0 7MgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b="hQux6/iJ"; 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 z142sor15321342qka.71.2019.05.23.08.34.41 for (Google Transport Security); Thu, 23 May 2019 08:34:41 -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="hQux6/iJ"; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8d3qZxuduenTYxmVtLo/HTiLB+fwW/lCyyNPj/qu33U=; b=hQux6/iJ6hqg+JBrYX1VSUyG5Ws/3mnhZg2BHMv3L6kyfjzf8PCRUOnHPNnDo5Ei52 EzmG7qHzV3jffzCQXKj0JUoEfaIP43zLaY5Wr6VzQ8nmDtD5NmA7j9TfXODD1YOs1F9H PZH2Za61bIL+znufcpgTJCwAGDEtZBaDT+uGjIxs4vHGmpuGCYXgRA67X3+xj2YEGoS7 qxppgbz5Ak0ZlKqhXrbw3bOcAA4n662B0aWl2e6V6tWw4A7HXJaDEV3RGWl1ZbpgHojB dZ3Mex3pGWIaqI5HTw4cpQ7GxfHh8wiS7DQizBjbqw30fLSk5qlGsvcbGE4opOCxHQ2o yokw== X-Google-Smtp-Source: APXvYqxEddBfU0Rf4mgWBf/qIenpeQFY2LV64soWSgPl9ghoFmgdVdxNL+eh6sZGq9t3KI14a4o+gg== X-Received: by 2002:ae9:f70d:: with SMTP id s13mr75933217qkg.213.1558625681585; Thu, 23 May 2019 08:34:41 -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 v69sm745374qkb.60.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-0004zZ-2a; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 05/11] mm/hmm: Improve locking around hmm->dead Date: Thu, 23 May 2019 12:34:30 -0300 Message-Id: <20190523153436.19102-6-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe This value is being read without any locking, so it is just an unreliable hint, however in many cases we need to have certainty that code is not racing with mmput()/hmm_release(). For the two functions doing find_vma(), document that the caller is expected to hold mmap_sem and thus also have a mmget(). For hmm_range_register acquire a mmget internally as it must not race with hmm_release() when it sets valid. Signed-off-by: Jason Gunthorpe --- mm/hmm.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index ec54be54d81135..d97ec293336ea5 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -909,8 +909,10 @@ int hmm_range_register(struct hmm_range *range, range->start = start; range->end = end; - /* Check if hmm_mm_destroy() was call. */ - if (mirror->hmm->mm == NULL || mirror->hmm->dead) + /* + * We cannot set range->value to true if hmm_release has already run. + */ + if (!mmget_not_zero(mirror->hmm->mm)) return -EFAULT; range->hmm = mirror->hmm; @@ -928,6 +930,7 @@ int hmm_range_register(struct hmm_range *range, if (!range->hmm->notifiers) range->valid = true; mutex_unlock(&range->hmm->lock); + mmput(mirror->hmm->mm); return 0; } @@ -979,9 +982,13 @@ long hmm_range_snapshot(struct hmm_range *range) struct vm_area_struct *vma; struct mm_walk mm_walk; - /* Check if hmm_mm_destroy() was call. */ - if (hmm->mm == NULL || hmm->dead) - return -EFAULT; + /* + * Caller must hold the mmap_sem, and that requires the caller to have + * a mmget. + */ + lockdep_assert_held(hmm->mm->mmap_sem); + if (WARN_ON(!atomic_read(&hmm->mm->mm_users))) + return -EINVAL; do { /* If range is no longer valid force retry. */ @@ -1077,9 +1084,13 @@ long hmm_range_fault(struct hmm_range *range, bool block) struct mm_walk mm_walk; int ret; - /* Check if hmm_mm_destroy() was call. */ - if (hmm->mm == NULL || hmm->dead) - return -EFAULT; + /* + * Caller must hold the mmap_sem, and that requires the caller to have + * a mmget. + */ + lockdep_assert_held(hmm->mm->mmap_sem); + if (WARN_ON(!atomic_read(&hmm->mm->mm_users))) + return -EINVAL; do { /* If range is no longer valid force retry. */ From patchwork Thu May 23 15:34:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958103 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 28E4876 for ; Thu, 23 May 2019 15:34:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1489E2851E for ; Thu, 23 May 2019 15:34:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EE1D5286B9; Thu, 23 May 2019 15:34:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A16D286AE for ; Thu, 23 May 2019 15:34:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C089D6B027C; Thu, 23 May 2019 11:34:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B8FFC6B0280; Thu, 23 May 2019 11:34:43 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7FE56B0281; Thu, 23 May 2019 11:34:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by kanga.kvack.org (Postfix) with ESMTP id 5D3AE6B027C for ; Thu, 23 May 2019 11:34:43 -0400 (EDT) Received: by mail-qk1-f199.google.com with SMTP id a12so5783017qkb.3 for ; Thu, 23 May 2019 08:34:43 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=DCPTNx8CjmJ5EZR4Q7fEVnlEGDk6XaLfhfnTJM/xZQM=; b=P/33Fn9Cdk4MN/r2nAcFOxKXdEg2yN18SPYWaRMc2hL6RhHvlBnogVAnk0bxKixJPC h9iP3lU/z70E3r+uaBsrcYS6srhllGAo1MNYYplZZGUZHHYiZr7awJBF0sAZ82QVuBlq YUVlgQlE4XYrU7WFCodjL60BCTpx861cFhwQrYbqFjXGc3snHIX70Qtki8L1vvCV8TCI GcRttHcyWwOpP2iNBpFu2p8Hm/9idgFfHsOq25XHknalKxQR729JI6yUJ9xHhaNNQ8Zx mBqaFae0iCbCgNf7Oq8lfEVBEo7JYa8MeQcY1ceC40DekSRM894LWs3hkO9Pz0vqU6BC meKQ== X-Gm-Message-State: APjAAAU8Jwaj9cz2V0q8hqsi08Eig0agsxeWqEyT1WsFxMf1CvIr5zi8 nEsx/IdTe2cDF/DjGhwkd4Y9TOKeY0kDPqspotvOfW1QMhHQjE5XM5540nEUgA4j5yO8X6Sk1sx 5UaS++dDWlR1Lzq4idCv5gPdb4hYpflTDlui2/VkIICCmq0kLuuUqITpGDeah1sJvkQ== X-Received: by 2002:a0c:984b:: with SMTP id e11mr14604964qvd.174.1558625683155; Thu, 23 May 2019 08:34:43 -0700 (PDT) X-Received: by 2002:a0c:984b:: with SMTP id e11mr14604896qvd.174.1558625682468; Thu, 23 May 2019 08:34:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625682; cv=none; d=google.com; s=arc-20160816; b=izb/5uzrqdIWh9Js+lyUrwXRpQvgGGQG1Xg0mcokNYlbbD3X5AKsV3HTo4zuC9xD1S vCV/QkPi11NSyM2RKWF6Jdh9bJkO1LikgHVjXzu7n3rVuUij8m4UuUxcPqFswAW9Dl8z r8Sd/MriRmYa9SFwk86+Gx9TZHqdY08OCHSiyCHQ6cf6mhSh/xDd4tMyWsID/8v+T9e0 cyB8e4sZPBLkObvBIqwGxefaFEK2TQHtSi2VO262ORzCrTQZbDIWwMk8GhC4Dk3BPp6/ FdEtcOIISeN0fW0I1TJpQC9w5f1HKkqTsJX/r1Nh6+aaoU65PeOnKbrOtr0LmcMo71J4 unvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=DCPTNx8CjmJ5EZR4Q7fEVnlEGDk6XaLfhfnTJM/xZQM=; b=GHi6ZsC3ozU6w0dOVzLPm6bd71Htd/JEkFBDjN1rPle/cDeHCcP4awBR9OahQnuak1 eFVc8uQiZT/h7jAchwEUH8nTFYsZBLZtlonu+6uskQ2WLAnkqs0jEIu87m+Bvw2gojlE 8ZV7cg9soeqDsY12wJ8TlR1QmSrxf9EZx4j2k6bVwKCmQGAWv8pTpP0pw1YrbjwR1lDZ a4bFczOAK42+KTSpItSHXoXVgVVvUFJCj7aMLVo+VlaQXPy8sC8LeT3eb4Hk0l/wxCet atsN83xqmxDLcoRqBC8TGJqpALvpIBvrTVEd9CS31ZhK6IGpH/uAkim++8bqgzc0qix5 s6rA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=UscylUyC; 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 k31sor35753559qta.27.2019.05.23.08.34.42 for (Google Transport Security); Thu, 23 May 2019 08:34:42 -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=UscylUyC; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DCPTNx8CjmJ5EZR4Q7fEVnlEGDk6XaLfhfnTJM/xZQM=; b=UscylUyC/LbE+hmwHcoTPty0rCYV8C9l6BH2lAPjaiQ8KCpQHYGVFqbwt9qHDAXr8/ 0k3qTMBhSJf3S42PRZfbrX9UEI/6LGFwoDjvFpmqQlcDDdNyD6dTDJrchuLLNc489jB7 Y/rZmj+WB81MnhoL6YnHiAQeUu1bwtK84oY4YfOfklhLOiyLAZUJPzOkPy0cRE3mZC87 G/yVQEsPneUO8B683OG8mQMLHQ79F1URNwp0dMjSHeuCd7ZWZGchFIcBB3L628nY5y71 wweFi2dQmZAt9lY8+eQpEay8IdpvBecKhqsuVKkK81+YslCO9og21vt7HuyZ/YQvqfFx 2JDg== X-Google-Smtp-Source: APXvYqxlKr/De7Ph6kX9Z/iOmfGCPd6u0UkBZ6roajbjLbPCO1l9qWnchzPbSB+6YXzx7C5cfiG9rg== X-Received: by 2002:ac8:28ac:: with SMTP id i41mr57963862qti.388.1558625682235; Thu, 23 May 2019 08:34:42 -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 a51sm17403701qta.85.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-0004zf-3t; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 06/11] mm/hmm: Remove duplicate condition test before wait_event_timeout Date: Thu, 23 May 2019 12:34:31 -0300 Message-Id: <20190523153436.19102-7-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe The wait_event_timeout macro already tests the condition as its first action, so there is no reason to open code another version of this, all that does is skip the might_sleep() debugging in common cases, which is not helpful. Further, based on prior patches, we can no simplify the required condition test: - If range is valid memory then so is range->hmm - If hmm_release() has run then range->valid is set to false at the same time as dead, so no reason to check both. - A valid hmm has a valid hmm->mm. Also, add the READ_ONCE for range->valid as there is no lock held here. Signed-off-by: Jason Gunthorpe --- include/linux/hmm.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 2a7346384ead13..7f3b751fcab1ce 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -216,17 +216,9 @@ static inline unsigned long hmm_range_page_size(const struct hmm_range *range) static inline bool hmm_range_wait_until_valid(struct hmm_range *range, unsigned long timeout) { - /* Check if mm is dead ? */ - if (range->hmm == NULL || range->hmm->dead || range->hmm->mm == NULL) { - range->valid = false; - return false; - } - if (range->valid) - return true; - wait_event_timeout(range->hmm->wq, range->valid || range->hmm->dead, + wait_event_timeout(range->hmm->wq, range->valid, msecs_to_jiffies(timeout)); - /* Return current valid status just in case we get lucky */ - return range->valid; + return READ_ONCE(range->valid); } /* From patchwork Thu May 23 15:34:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958113 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ADA8B1395 for ; Thu, 23 May 2019 15:35:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9AED62864F for ; Thu, 23 May 2019 15:35:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8F65828686; Thu, 23 May 2019 15:35:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2470628699 for ; Thu, 23 May 2019 15:35:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37ADE6B0287; Thu, 23 May 2019 11:34:46 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 32C276B028A; Thu, 23 May 2019 11:34:46 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F6276B028B; Thu, 23 May 2019 11:34:46 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by kanga.kvack.org (Postfix) with ESMTP id ECF7E6B028A for ; Thu, 23 May 2019 11:34:45 -0400 (EDT) Received: by mail-qk1-f200.google.com with SMTP id x68so5756740qka.6 for ; Thu, 23 May 2019 08:34:45 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ncjASLGlHsj22sNocZs8CG3omWgokVRQnVtcSgoF4Do=; b=fHbCmyIqSsm5quP4FAkeHrTCyy+36/XVeelUxusRN7RfiCNyKsxtf+bTF1eq+AUili 582uGNlCEczo9UppxYNAVaXSss5j2QRXWNdGBMaZy/G0lA3+fgkmwO870BpjoC3aa8na vpSpfXwbsU6PZJr5krElRT8a0ibr32SudmzLIhayCrCwgDVEXu2Nnk3WWl3cxfScuHHP WOwTgNtXZjeGJ/DhPuSxjear1RIUQHwg/XWGlnvePjX7S3ykAfuJmJmuqmU6dIwR6uL+ 4jV5ci6G7mLMW/U6S82SBOAfKrJeqETFNXy+/8Y4Q4f+DHbvQDf35WEsTRjgCAGVqGxN V+zQ== X-Gm-Message-State: APjAAAU0rt8rOPKXo+qAJJltVEekHNz6e5h2ucJzUPnLToa36oqiRvQp HK/peO2an/O42t4NGU6jo3fVE5eQB230LvQwM1L0Hru46+6x/2nL4F83r1O11dDopf97AgrrN6r 14aT0UuEY2OkXKLIvIT+Q5w3QkhaAdKlvtZPybdnCV9BRJFJJQqREAyEDdqauQPKcQw== X-Received: by 2002:a05:620a:1362:: with SMTP id d2mr2676345qkl.40.1558625685700; Thu, 23 May 2019 08:34:45 -0700 (PDT) X-Received: by 2002:a05:620a:1362:: with SMTP id d2mr2676285qkl.40.1558625684968; Thu, 23 May 2019 08:34:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625684; cv=none; d=google.com; s=arc-20160816; b=pMJHXcIKM7+oNeDKHzQvqckMYj8MAbzaTDOhZZRTcXXsvFSY+929Z0BL78FQhhxUQS HFmHQjVzwJN65DTDNY8Hd+g/moMMZ1ynpg65aZkIBxGsX0gBiA03h6e6IAMQvkRk4BSK nMORQIB520vahh7jtdx2+fHz62Qu0GU/cQQyqAtl7IjssQcsPejdD2IG762C8H5CMqfI B4v4PI7mPnvyDpKF0NAbi9hnTCvRFY2Fnlg6wSa0J5S5DPNF4IZrZEhilFwuUKes0foq G50nwYWxUoLnBSSs+AVbHLTiQTBmm6VcsrpF7jQ+EZWtD2gW2TlyakI806VQ202gbMtt fOgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=ncjASLGlHsj22sNocZs8CG3omWgokVRQnVtcSgoF4Do=; b=tV9swozjP46Z0k0SL+FzLCnhmei2V4Q5KTMZM4Ogbp5Nnyy1BrvJNljm47jDibO4RF 5RSKhI4H3swMXNZiD9XP1QhYNljnL9Asmrq8C+SkqWjQ4RiWVbyapBm/hJuJAd1ZZiAC DXGJQyu25uDSTv5WcsUvGcCei88a/I90/wbLkCYfxqgd9id6uah8hzDcllylolDR94oe GfmCBQtRx6DlbO8AB82lOynyf2VLpgCLuAUI/NwlUadavRjFB+EJM1qZEaYeoHEasMeH 25WUlDArxx3A5i35HEOuXnuxJ9VUOFWbrsvkm4i35Gz9Mi7TqWt1uV0SgywRCuqro0Np ChIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=afbPPYk9; 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 u23sor18808426qtk.6.2019.05.23.08.34.44 for (Google Transport Security); Thu, 23 May 2019 08:34:44 -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=afbPPYk9; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ncjASLGlHsj22sNocZs8CG3omWgokVRQnVtcSgoF4Do=; b=afbPPYk9gBj4SFUfupZZE4bCnA6gDZMhgy4+OgszgUI8N1f+l4L4QbGabEscrkzZio 04dKwEZTY1ZZpMvvyuXHs/Zur4eepQiD1W+lZazrUaj4nn7/t4j1M+InHHRzuagZXcr8 KKv520d1DP6I7X4/cfCmplGSWFB7qHZa/I9bvrzKq1ogoyjdw+q2JJfFWS8aPCq+8IpA qzget2M+jxJ0rPEHJZXgXf8cijcYlCmoGK6a41MkyHSp5f24D9rssOEcYPsQesCbV+n0 ERLiPP7EiRneYQ5/ivup+VNYTXqW/BHKDFa3+vKYdV9zuvfZzb8dXYO7o2N+FXeqLQBm RkYA== X-Google-Smtp-Source: APXvYqxAz8IO0yDJzMp5j01V0l+mHdFZgyUtfceu0FECv0lekCVZ9tSBPQKcxg2vezV1TSB8NGjW9g== X-Received: by 2002:ac8:18b8:: with SMTP id s53mr76217225qtj.232.1558625684721; Thu, 23 May 2019 08:34:44 -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 g65sm2686228qkb.1.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-0004zl-5F; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 07/11] mm/hmm: Delete hmm_mirror_mm_is_alive() Date: Thu, 23 May 2019 12:34:32 -0300 Message-Id: <20190523153436.19102-8-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe Now that it is clarified that callers to hmm_range_dma_map() must hold the mmap_sem and thus the mmget, there is no purpose for this function. It was the last user of dead, so delete it as well. Signed-off-by: Jason Gunthorpe --- include/linux/hmm.h | 27 --------------------------- mm/hmm.c | 4 ---- 2 files changed, 31 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 7f3b751fcab1ce..6671643703a7ab 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -91,7 +91,6 @@ * @mirrors_sem: read/write semaphore protecting the mirrors list * @wq: wait queue for user waiting on a range invalidation * @notifiers: count of active mmu notifiers - * @dead: is the mm dead ? */ struct hmm { struct mm_struct *mm; @@ -104,7 +103,6 @@ struct hmm { wait_queue_head_t wq; struct rcu_head rcu; long notifiers; - bool dead; }; /* @@ -466,31 +464,6 @@ struct hmm_mirror { int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm); void hmm_mirror_unregister(struct hmm_mirror *mirror); -/* - * hmm_mirror_mm_is_alive() - test if mm is still alive - * @mirror: the HMM mm mirror for which we want to lock the mmap_sem - * Returns: false if the mm is dead, true otherwise - * - * This is an optimization it will not accurately always return -EINVAL if the - * mm is dead ie there can be false negative (process is being kill but HMM is - * not yet inform of that). It is only intented to be use to optimize out case - * where driver is about to do something time consuming and it would be better - * to skip it if the mm is dead. - */ -static inline bool hmm_mirror_mm_is_alive(struct hmm_mirror *mirror) -{ - struct mm_struct *mm; - - if (!mirror || !mirror->hmm) - return false; - mm = READ_ONCE(mirror->hmm->mm); - if (mirror->hmm->dead || !mm) - return false; - - return true; -} - - /* * Please see Documentation/vm/hmm.rst for how to use the range API. */ diff --git a/mm/hmm.c b/mm/hmm.c index d97ec293336ea5..2695925c0c5927 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -80,7 +80,6 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm) mutex_init(&hmm->lock); kref_init(&hmm->kref); hmm->notifiers = 0; - hmm->dead = false; hmm->mm = mm; mmgrab(hmm->mm); @@ -130,9 +129,6 @@ static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) if (!kref_get_unless_zero(&hmm->kref)) return; - /* Report this HMM as dying. */ - hmm->dead = true; - /* Wake-up everyone waiting on any range. */ mutex_lock(&hmm->lock); list_for_each_entry(range, &hmm->ranges, list) { From patchwork Thu May 23 15:34:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958105 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E41581395 for ; Thu, 23 May 2019 15:35:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D16A52869B for ; Thu, 23 May 2019 15:35:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF591286B5; Thu, 23 May 2019 15:35:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 745E42869B for ; Thu, 23 May 2019 15:35:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0A8C6B0280; Thu, 23 May 2019 11:34:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9BE196B0282; Thu, 23 May 2019 11:34:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 835BE6B0283; Thu, 23 May 2019 11:34:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by kanga.kvack.org (Postfix) with ESMTP id 4E3CC6B0280 for ; Thu, 23 May 2019 11:34:44 -0400 (EDT) Received: by mail-qt1-f197.google.com with SMTP id c54so5659308qtc.14 for ; Thu, 23 May 2019 08:34:44 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=f4j069c7ZUzoxaaL4dTtRuO7XDhSw6R18jQGsXmwSSU=; b=XCQWGO0LRAdE+SzaVcpwOgBPyhSVnV8TXjM0tvTIyZakoMYGfSfS1BzFqM9GTED71H xaR8mirRDzS0bVmh4Fc1Jklpf2VQAbsLyk/VxbZ1ocgL5vqkxfSa2H2knNkaciSZSfY7 RMepmBPGQQeexDGA3SotdSvX1r0Jys37SWQ1JfnU6+K7rtPeCVGlUlD96gLEZQyJidaI ax0HhyF4Um8Xnt9o4e0EYUVqYAtxdjXAGcDrdhAj1afrsPx5WQGRgmdWOyWb5Yb29F57 ffU1fl41x6NABlzT8b23virCe+Hekj2LYACIG9joM7rLT+HbE4X+xTrSZXhqIbxMvyAD uxaA== X-Gm-Message-State: APjAAAXkRpZ355/L3QcLNsf91W5lZ33DN5rrVFWJVyWCeuXnYn/kf6RQ mdJQwF6c2yoxExjeG2dym+rYdHR7T+Ji6mBtrrwuqyNCWfFwJ0nt8nj0fJ4qHQUHq1VvhyqEGK5 bxa8jdmjsIbignh1UDu/JSy+DruddujdSxP3erKDZOrpkaofuLtgNsSoDrmOH+ER0SQ== X-Received: by 2002:a0c:ba09:: with SMTP id w9mr7032713qvf.67.1558625684094; Thu, 23 May 2019 08:34:44 -0700 (PDT) X-Received: by 2002:a0c:ba09:: with SMTP id w9mr7032633qvf.67.1558625683257; Thu, 23 May 2019 08:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625683; cv=none; d=google.com; s=arc-20160816; b=lLU7a2s+MJDmFkGRQ8M/r7U7Shoq9HONEj8M6o9pEK8nUefAKy7v7b4it58BM2NBEv AV5qPrZeUVPUq7nDDfBoAWpnKebyE76tSkAyAT4gjMMj0rGySThoiyfStkCFVkuCyHsQ pIM5ibOPlnGcqbQOwziRY6hzrCy/wZ+pw6yY2RiT3PTcNNFrE/qOmf36w/cj2Ypw6yuA qFIFeYZ7Ky81apeyzjcIKQ4WnfoNPAXA+/7kUq9DgGuJkFjazIVPusk29DNdUHRMZFzw L2bzXRc/t6tQqLIJ6TnDawnGr4L2UM/Ei/8iYUjmeKNlm69s9zJF/R9fvlGi3IjdKA9Q dD6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=f4j069c7ZUzoxaaL4dTtRuO7XDhSw6R18jQGsXmwSSU=; b=X+qIPSXighuZ/Y2xzffH5+kw43dGeOCoc8OdBy1kqG5n6l96FHNJajC2dcE6QBMSGw 4z2JXx/45hs4IA8pkxQcmq0yBwPQ4L/0aJkv1T9hoeZbmxh0q09yRJj9x3XYnIe3EUBs tCx7qwiZ9NPdQIC1m4VFVxM/QhPKurgB4jKri3dBCmyAt9CwsWVFo0X2CsIJ2MfCC187 l9Qh75td063rtLZn4oRfMB/R33t9gxU2Di7aAFSpZj218kRjiSM/odCkmpW320r2DYSL jNzMon265cPjzlA0MMXaaDb3x7WVvN7sidHs7qPyYIVsg5aPBjWS4ghnX15ho5nF02uL SBOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=Q1iR9o21; 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 y2sor35347116qti.49.2019.05.23.08.34.43 for (Google Transport Security); Thu, 23 May 2019 08:34:43 -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=Q1iR9o21; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=f4j069c7ZUzoxaaL4dTtRuO7XDhSw6R18jQGsXmwSSU=; b=Q1iR9o21bGF01UbFLx0luDqo+LhUazJlW9vM4h0+z3TY9W6sEyxEw1R8DRsXKzCFjm fcxDLu6fBdQ7+NFUWl/isFjecvb1MU2/EYhsJDaOAM7XH95HyJYuhAmdg/ngedBPOT2f A/UL3DDA9lTnFoM9t+t3e2u8hSjSbbYi6FkF8FVBMPrnALhwH2m8B9e+n4RvWRsmYIpR VWSYlsfP2MkG7ZigmeiUQ0n1/bZgKIlqHLtsxqRXpvC7SLAUBoMfzAuE7zfXaTtKTa8A VGeDoQ8pTFRVKqryBcVNxJtEZ8Cn0TIIINgbBg2xyE45bsPeuNCxS8mtks9fFUZS1sdG oPlg== X-Google-Smtp-Source: APXvYqzldSamHhkoEu6IIunxCJ747qr3gst7TXBS5fLpoamgpQBiEE2VbTR+wtaPARW5aq21amozzQ== X-Received: by 2002:aed:3a0a:: with SMTP id n10mr82588942qte.145.1558625683002; Thu, 23 May 2019 08:34:43 -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 p8sm15951242qta.24.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-0004zr-6W; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 08/11] mm/hmm: Use lockdep instead of comments Date: Thu, 23 May 2019 12:34:33 -0300 Message-Id: <20190523153436.19102-9-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe So we can check locking at runtime. Signed-off-by: Jason Gunthorpe --- mm/hmm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index 2695925c0c5927..46872306f922bb 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -256,11 +256,11 @@ static const struct mmu_notifier_ops hmm_mmu_notifier_ops = { * * To start mirroring a process address space, the device driver must register * an HMM mirror struct. - * - * THE mm->mmap_sem MUST BE HELD IN WRITE MODE ! */ int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm) { + lockdep_assert_held_exclusive(mm->mmap_sem); + /* Sanity check */ if (!mm || !mirror || !mirror->ops) return -EINVAL; From patchwork Thu May 23 15:34:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958107 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 34A331395 for ; Thu, 23 May 2019 15:35:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F79F28682 for ; Thu, 23 May 2019 15:35:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1340F286A7; Thu, 23 May 2019 15:35:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B1F2128673 for ; Thu, 23 May 2019 15:35:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21F1E6B0284; Thu, 23 May 2019 11:34:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1A8D76B0286; Thu, 23 May 2019 11:34:45 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3E3B6B0285; Thu, 23 May 2019 11:34:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by kanga.kvack.org (Postfix) with ESMTP id CE35C6B0282 for ; Thu, 23 May 2019 11:34:44 -0400 (EDT) Received: by mail-qk1-f199.google.com with SMTP id l185so5744753qkd.14 for ; Thu, 23 May 2019 08:34:44 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=xpfktVbL8Us0qmdWRL9VRFwkpcEhMFdO2zVbp5WooYk=; b=SXLotjewFsaqiJfBrEMxyyRyXPBEPpSG1UAlAiectMA2Rc3gf2dPtMEd8DFIRjyiZj //5SEg2xSzTpibSFAsJGadK2PpqmkGmDN+uheXUDXi4BQVOfkirRMb3krf9D8dIbr6JG bprejZF8OxrfBQk0M7c3E9IOL4FQqKoZQCyArX7/LGhoOj7RkTqrcmQZI2GfI91arepi ESqIHyG/lcVA4vax/7fnBl2hgOi2ICttP7p6GspUfZvuja75X90bF/7kX8ewiHGH5/Hk +sDZcEsYJ1NUJelYhM1DFqDCn/EkPxyTRl4cuXZ33AJJ7TPJs8uoJOGYchQLKnc6xfeK VsGA== X-Gm-Message-State: APjAAAWP10e2wheNeD8H/G3JUDH+MpHoFWCtcRzIn6A0zBIdgPs4Qeja zPFARWMzXuoktOE8eaz5f2tihMFfJ7jLjRl1lpjbNRh/tuB2e4EiO+wZAfeVxRvOpmX+Eg6FzJs 8HLP/T06DnujviHByRYTCxLCZZlnF10f25PI8IwwCxNAJvCL/0QC4gULDynXhX7LD3g== X-Received: by 2002:ac8:3098:: with SMTP id v24mr81754028qta.114.1558625684622; Thu, 23 May 2019 08:34:44 -0700 (PDT) X-Received: by 2002:ac8:3098:: with SMTP id v24mr81753922qta.114.1558625683581; Thu, 23 May 2019 08:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625683; cv=none; d=google.com; s=arc-20160816; b=Gs6uGUrVy5xUoc9o+qeTli1ZECLtCcV/ibLLrd3f3kGBRlxLyaFWQ3VuGUejOFcNxp gU2ca1VeRwoqL1DPm6HlY04GeYe1SYgi2bIBsUW/ZWITKNf19vDcykRcwNoWoS3GN1mR MNbAZXFWmTXneVw2IDQVb1GQIN5qYEG5CCr7VAD38IBFE3LfGnuzF29jJGpLgh/GDWtJ LjgnxJAOhHLfTANL948g6VhZIn2333yuq3dsLQqfe2S5DynInTS/rf4KjBZFqVT4NilE mtwOOpuz8l5FPE+T8q3BwrikRIjNTJRt+AXAGO7uxtBSuuNVJ2mJmhZ/CiKbBR3ecYrY Rw+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=xpfktVbL8Us0qmdWRL9VRFwkpcEhMFdO2zVbp5WooYk=; b=ETu96V7ofpD2fKIv7fwbHUYprMnxMjHA7EbM448t3Z4FfRjg4qWvzhNkCAQlQviQ7H ZCRof7u+OOacl7Ttj2p0XXegUXYEO87nlZUW5NDtOUel487RH50jmDWStBlUngLrS4nl G1gzGpeHE7Fa16gyIBd/i8Ov5h1IwcHsH/SSmnjFDMqCyg196gDD7+MepLFkXVPCOlsw Jnad35iz/7cTletDrAZ12p5vK5zLXx1hdEiD7RdfdgHD9rnLQc5QCxFUxR1L63gf6x70 KPK6bT+VBOMzuFzHgLuwCSwooffo5yTkvQDXU7EzC48Io9PatKgvqFL6ANuiF8SbRTAD Mh5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=AQy9qY5u; 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 t126sor15513953qkb.65.2019.05.23.08.34.43 for (Google Transport Security); Thu, 23 May 2019 08:34:43 -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=AQy9qY5u; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xpfktVbL8Us0qmdWRL9VRFwkpcEhMFdO2zVbp5WooYk=; b=AQy9qY5unh9pvUOhg9td8t1dNvtTOZFdIPbHYbv6sGctT7lxBlvtaDEwnhn4A+19Ea Rg7FPBiK+QeEfNuYYey9/yTJuLOz1p93njDBTGg1o1kwmziRbYbc7/r1RV9voanVidls 30IOKaMwGJFM53rpfO+9jyUNcc5sGg2amlLg17lxpwhDbVQBeZP7J9K2e+vswi8oBpRV I+kktxprDGfdJdLVSa1IlFeSv+iaXJXGJ2txMn51i04T0ADr1iIC1/SdYT+sTveLOLmP 1UTrdvaCWyzmYz7MafIRNnc5+Gu9nQKBqUn+NLtIyiTvORUJoBnsXlq+HNOLcEnckf0g Vxiw== X-Google-Smtp-Source: APXvYqx9y4W6qkw9q6CFNTSxrDtAaedtA4a51WwLa3lqT4HhvTbeWF2PV7OrPjgqqyxZwLXJZH3cDQ== X-Received: by 2002:a37:50d4:: with SMTP id e203mr18097553qkb.83.1558625683357; Thu, 23 May 2019 08:34:43 -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 k30sm11757172qte.49.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-0004zx-7m; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 09/11] mm/hmm: Remove racy protection against double-unregistration Date: Thu, 23 May 2019 12:34:34 -0300 Message-Id: <20190523153436.19102-10-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe No other register/unregister kernel API attempts to provide this kind of protection as it is inherently racy, so just drop it. Callers should provide their own protection, it appears nouveau already does, but just in case drop a debugging POISON. Signed-off-by: Jason Gunthorpe --- mm/hmm.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index 46872306f922bb..6c3b7398672c29 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -286,18 +286,13 @@ EXPORT_SYMBOL(hmm_mirror_register); */ void hmm_mirror_unregister(struct hmm_mirror *mirror) { - struct hmm *hmm = READ_ONCE(mirror->hmm); - - if (hmm == NULL) - return; + struct hmm *hmm = mirror->hmm; down_write(&hmm->mirrors_sem); list_del_init(&mirror->list); - /* To protect us against double unregister ... */ - mirror->hmm = NULL; up_write(&hmm->mirrors_sem); - hmm_put(hmm); + memset(&mirror->hmm, POISON_INUSE, sizeof(mirror->hmm)); } EXPORT_SYMBOL(hmm_mirror_unregister); From patchwork Thu May 23 15:34:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958111 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D96251395 for ; Thu, 23 May 2019 15:35:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C67F828528 for ; Thu, 23 May 2019 15:35:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BB24E28688; Thu, 23 May 2019 15:35:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D54628528 for ; Thu, 23 May 2019 15:35:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7B8E6B0286; Thu, 23 May 2019 11:34:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C35226B0287; Thu, 23 May 2019 11:34:45 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7F556B0289; Thu, 23 May 2019 11:34:45 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by kanga.kvack.org (Postfix) with ESMTP id 833CE6B0287 for ; Thu, 23 May 2019 11:34:45 -0400 (EDT) Received: by mail-qt1-f197.google.com with SMTP id q23so5683311qtb.4 for ; Thu, 23 May 2019 08:34:45 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=fyMd/20M9wRDEt2CUwmbtJJl1RcuuD2R+QgKbl3Q/tc=; b=o9lAfMeYwWgusnRwOf6T0VNDPf7BMgWP9FsjAxWTXjiVLvTE82bnAepiamUaGUzgcM VYdtIhOEBpW3Q95SKE+Icbppo/vnvAOxkCCPfCPp7sw40cdbhgkJK7N/vl+GbjtJiWF/ PYkElMsmV8ZQCYi+fAh/8KoxzL57PT6BAMOF3hWN7xz+4MLma1E4xiNjwXpywYcBW1Ii WUvRVqKjGd1tamMByAds6auMn1LCFiqWhWBq+vabsK/6vPoay5MZvBGWUEtwBoMuOt1o ULvWBWN7s8XdNQjtejLGEFX55o7UwZASVryhWOwlgRk1j33qRDLpBP3uCefgQQP7atEQ E25g== X-Gm-Message-State: APjAAAUlNgbWFAELDy4D0GGP07C7ej4pLktbED5PN5qZ1PSrJIh6cv+w MViyTUmQr3j4J4RbU3pm2vW5XcPlUI/pjwd2NEJs1FaRYFqahPJE8cRxU0wC9hRm85+B9tN9JJv mzue+3OceAJED23Av4vy/caRnx3iKTSSdNAqwGr3yn8zgZNzHU3Y90xG3goZ7MwGU0A== X-Received: by 2002:a0c:96c4:: with SMTP id b4mr65427755qvd.2.1558625685293; Thu, 23 May 2019 08:34:45 -0700 (PDT) X-Received: by 2002:a0c:96c4:: with SMTP id b4mr65427672qvd.2.1558625684313; Thu, 23 May 2019 08:34:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625684; cv=none; d=google.com; s=arc-20160816; b=i7jf/sxWaR9BSuUpNb72LI5K2khoePh6J+8SUMCRhZY3sTOzXbhX24VLCxrCKJzjeF ySjy2b7xxuxcSVk0FgCcXVVYmvtZR91ZS8VjfpNSXK6zjB+MDHR0+ZtqnweeWagZT7fN 5xD0Hg6Y4mnhW/c2XMYxiOqhkp2ZT5UC73oumhB40Zp9eNxutval7eUIFW7Q0sdFoZuR +lU8oQsf387w09U+NN3wqPl8qJpQoQh4K6a140XXXfrlDkTWUIldW6dapMbM3/U3mfuT GT/71S5IxIgcoQSHPj9rAyUNqTxuqxNw7niHN+LSMnufBPs7/OH0S/f2HTYHJa5CjVCS AB4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=fyMd/20M9wRDEt2CUwmbtJJl1RcuuD2R+QgKbl3Q/tc=; b=cHs6D8wJld1LuqPjLCFKQPr+GNrmqEzjmELh1XLurOWtHJ6w/Y9i0fsYueUeazzNRY WJTx6ypsibGTgaXhvKl5Ej6LoiNTnZJNuNBxaZjJGp5HZoZh8s9bfaXQ6fnrarYrqd+n 0lFhaUNvZe9jXCyp7Lj2yC9hmyIRxukRsGphesLPc2t6vkh3rU2EwtVkzXiRqYOsfrK2 y/dkaj6uql5jM3HR+Uvg5GsGMal/6C/57HSyqGMFuo5Ck4xwgTFM/XCC+MChkrpU40v1 xIRwCC/oRh50tw2EYgNaDPIzaTMNv+t326YnpUNurqq27LYROl5VAWFiy1zDafe7a9De m24g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=JIjtmXhI; 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 t42sor603789qte.28.2019.05.23.08.34.44 for (Google Transport Security); Thu, 23 May 2019 08:34:44 -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=JIjtmXhI; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fyMd/20M9wRDEt2CUwmbtJJl1RcuuD2R+QgKbl3Q/tc=; b=JIjtmXhIk+ouBJQ777LOHATYHiCxeddr9NRCIpfSEdrQ1ap9ixS0CW54V6HURwC9xA RKpjT/mKs8jM8bOrU+JpE9OrzRhV1PSZWwxlmuIpZMYfSawM5XO76xU4PAAGkcWLSm5s 7ndty4jjdbxARiXmFK166PeUPWVyFicpugoKluqPMkVT0YjQEoLXox9cyoZwmEcFagdx vDMEFLfHF1VypHEJSnZkoDjj4S4j7WOn++WkjrFxWKF7S2FzamGOtEw9nQxisne15cmS QxE2dETQ9lLxC5EcMBOuIOrkEj8esYg8qdZuZD+Z0aF0lIU814C/ZVRZzLowQaUo/lk1 YE8A== X-Google-Smtp-Source: APXvYqx/4tQV+dyjpchgQ23NYg/m02UvYl5N+Rh/idEA6TgEqyIlzTWmi6NKvXZCk10ahm2Boj71vA== X-Received: by 2002:ac8:7c9:: with SMTP id m9mr15162695qth.127.1558625684072; Thu, 23 May 2019 08:34:44 -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 t17sm17461892qte.66.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-000503-9i; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 10/11] mm/hmm: Poison hmm_range during unregister Date: Thu, 23 May 2019 12:34:35 -0300 Message-Id: <20190523153436.19102-11-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe Trying to misuse a range outside its lifetime is a kernel bug. Use WARN_ON and poison bytes to detect this condition. Signed-off-by: Jason Gunthorpe Acked-by: Souptick Joarder Signed-off-by: Jason Gunthorpe Reviewed-by: Jérôme Glisse Reviewed-by: John Hubbard --- mm/hmm.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index 6c3b7398672c29..02752d3ef2ed92 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -936,8 +936,7 @@ EXPORT_SYMBOL(hmm_range_register); */ void hmm_range_unregister(struct hmm_range *range) { - /* Sanity check this really should not happen. */ - if (range->hmm == NULL || range->end <= range->start) + if (WARN_ON(range->end <= range->start)) return; mutex_lock(&range->hmm->lock); @@ -945,9 +944,13 @@ void hmm_range_unregister(struct hmm_range *range) mutex_unlock(&range->hmm->lock); /* Drop reference taken by hmm_range_register() */ - range->valid = false; hmm_put(range->hmm); - range->hmm = NULL; + + /* The range is now invalid, leave it poisoned. */ + range->valid = false; + range->start = ULONG_MAX; + range->end = 0; + memset(&range->hmm, POISON_INUSE, sizeof(range->hmm)); } EXPORT_SYMBOL(hmm_range_unregister); From patchwork Thu May 23 15:34:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958109 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9A22F76 for ; Thu, 23 May 2019 15:35:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86EDD285D1 for ; Thu, 23 May 2019 15:35:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B499286BF; Thu, 23 May 2019 15:35:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 29C0D28683 for ; Thu, 23 May 2019 15:35:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 612CF6B0285; Thu, 23 May 2019 11:34:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5C7126B0286; Thu, 23 May 2019 11:34:45 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BDAE6B0287; Thu, 23 May 2019 11:34:45 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by kanga.kvack.org (Postfix) with ESMTP id 081DC6B0282 for ; Thu, 23 May 2019 11:34:45 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id g14so5645821qta.12 for ; Thu, 23 May 2019 08:34:45 -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:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=sVSVMj7qtVOG+pR++xGbUKEq7rSWx5vQVniXU7xAdVY=; b=nsirG820rrA70nESiqISQ30eV2KK9vSLUW60UkBqvma+T5FpqaHm9ZIIA7IcdzOeuj VEc4Q0+Bea7cVThWVRP+9GJxj2pZ5+zS8sOQLbnnng4Libqv6hh7fwKhzqXWOSTKtfE9 A6vkYg7d3JTyIfYY0ZHAB8V/y7mh8NuwBz7+MSq/nnemagrkHPeR2L5+qia+HfGoo4Er nv4K8O+BKBRVRKoxoRdcYoXHeQMje8bHNjx7s+kLfiCisBXHGAgFTd5bPhupko2tSvNh XY2+r30uO88vp9tyZIHU3B/qdoV4OGyt0ZwVHTYwXP4W9eAfthdOiXAgH+ma7V5U41KQ NPlw== X-Gm-Message-State: APjAAAXmHMyYaK+UEzxQFcmlD1PZiZx3VuVU+m9Q7jNVXptuOIuPYuDn vYfXw3WDDNqOOhf8c3+goOGnrT/pgel+H6STUkj+IMc63cPOuKUl/NtIIL+U2r22SbESadzQ1js gJDKly3RmScuKCujaosnGP8eAIontj9JAaZfwjDrhCgjEWhpUwGut43o0srIS9qV3hw== X-Received: by 2002:a37:6855:: with SMTP id d82mr69485897qkc.130.1558625684792; Thu, 23 May 2019 08:34:44 -0700 (PDT) X-Received: by 2002:a37:6855:: with SMTP id d82mr69485823qkc.130.1558625683997; Thu, 23 May 2019 08:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558625683; cv=none; d=google.com; s=arc-20160816; b=SiB5eBVNNoFum8Iz8HorhmyINWnBhzhn1Jac7WfeexXyJ3TMMy4049xirZwngKIB3h lrSGndje4iyBwjjoae4iOn0Vwlp5WQgidi4aBvWUL+A/57fs4dl32RrJh4Ujwmn4D0l2 rudRl1hGEL2jj1CWUu5cb7TlOgasfwMFO9bRskE08UNbdarZBoZryrKGLn55hiBYHDCG hVr89eP8hnxaU7w15FEPBB/XlBsEJcO6HJGcOosg4Fl/xP41CADA1qAsE2eEnkXytGv5 LEQNVveEbDUlxgdFDF2anQF6lcopXbA7q4hA8KE/DhPAz/t6DfGD0UWeIJPakmHAsDrq tbuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=sVSVMj7qtVOG+pR++xGbUKEq7rSWx5vQVniXU7xAdVY=; b=FQ/u4DrG6zTHu6LXFwG2EDLbJ8jGnDx/DH26DYK7vBp033kpHGGo20VRb7oM1+AZ3k E+1Du1RvtxYvrVMMA5IKMcB5jBioPIXUClqIADfK/a1ZIYeCWoarIDs3kq3OHrdtTc4K i4xl5zYZr8i/fxEi4LnjvAhWcy7BPql6xAqmLA6jjOmvdwPEU+M1i6qGh7OA/flsHrFE gRe687TLET8LW6/X9gyZejd0ioG/+L0P0alDNjI/zGhh4ecwGtALirVAXkhPN+6eAueX v1cVJ0Iek1OYE/V4+YvGEaOKyUZhuPjJ839SRnOaM/QgsRSPLe9GonNatq9UuKRtwS7h 73AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=DZpq16KC; 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 j53sor19746022qtc.40.2019.05.23.08.34.43 for (Google Transport Security); Thu, 23 May 2019 08:34:43 -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=DZpq16KC; 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=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sVSVMj7qtVOG+pR++xGbUKEq7rSWx5vQVniXU7xAdVY=; b=DZpq16KCBGWseTw+Emu3YXxJ4qAUUBYOgC5S+OOEj/qjUePnCbs18YftNrqI5k72oF PF78BOqbqq6gsqFCloyWSHUujhEA6iLhbwmuZW+tDHMvYXyKsgXqWsFKUjift9trNwPn rqocr9xDbYrprtIAWRiT7tiljZiU9eyK5kUSizlvzHsiN4QQ+GhHFIt7Cl39+kytFtcP YEmfFYaselG4ky3Uaic1er6GJoWtkTtMUKSq5/K7iPHbfWN6p8Dto2g7QzWwkTANGhkB ROp6hnDt0PpHQofzdaPfaC8krT25q9BBGFxfk+c0x0zYuBd4O1EiwzV88vfNmYBJAYQz bG8A== X-Google-Smtp-Source: APXvYqwlZgmvtDLezrwrAV3Bsq4rJxczbRVrNsIiCVDFfpxHEZgg1uH2yntWWMscbHuucZ+UF0vtYg== X-Received: by 2002:ac8:18b8:: with SMTP id s53mr76217130qtj.232.1558625683755; Thu, 23 May 2019 08:34:43 -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 h17sm12879104qkk.13.2019.05.23.08.34.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 23 May 2019 08:34:39 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1hTpjq-000509-Bh; Thu, 23 May 2019 12:34:38 -0300 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org, linux-mm@kvack.org, Jerome Glisse , Ralph Campbell , John Hubbard Cc: Jason Gunthorpe Subject: [RFC PATCH 11/11] mm/hmm: Do not use list*_rcu() for hmm->ranges Date: Thu, 23 May 2019 12:34:36 -0300 Message-Id: <20190523153436.19102-12-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523153436.19102-1-jgg@ziepe.ca> References: <20190523153436.19102-1-jgg@ziepe.ca> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe This list is always read and written while holding hmm->lock so there is no need for the confusing _rcu annotations. Signed-off-by: Jason Gunthorpe Acked-by: Souptick Joarder --- mm/hmm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/hmm.c b/mm/hmm.c index 02752d3ef2ed92..b4aafa90a109a5 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -912,7 +912,7 @@ int hmm_range_register(struct hmm_range *range, /* Initialize range to track CPU page table update */ mutex_lock(&range->hmm->lock); - list_add_rcu(&range->list, &range->hmm->ranges); + list_add(&range->list, &range->hmm->ranges); /* * If there are any concurrent notifiers we have to wait for them for @@ -940,7 +940,7 @@ void hmm_range_unregister(struct hmm_range *range) return; mutex_lock(&range->hmm->lock); - list_del_rcu(&range->list); + list_del(&range->list); mutex_unlock(&range->hmm->lock); /* Drop reference taken by hmm_range_register() */