From patchwork Thu May 23 15:34:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10958067 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 2D86815A6 for ; Thu, 23 May 2019 15:34:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 13A10285DD for ; Thu, 23 May 2019 15:34:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0772F28680; Thu, 23 May 2019 15:34:42 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D930E286AE for ; Thu, 23 May 2019 15:34:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730867AbfEWPek (ORCPT ); Thu, 23 May 2019 11:34:40 -0400 Received: from mail-qt1-f196.google.com ([209.85.160.196]:39121 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730752AbfEWPej (ORCPT ); Thu, 23 May 2019 11:34:39 -0400 Received: by mail-qt1-f196.google.com with SMTP id y42so7230018qtk.6 for ; Thu, 23 May 2019 08:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LEV5tWURKgLpgo5q6BTc4NlcwzQLAQ+EC5rW0mqA8Xo=; b=I9LiNRnSFZ2kQk44SojHBUW2L/OelCgKL7qFGsT/eFBZ2o/2dPwYCQGKf+9I82gF9J YmMPy0Vt8yxMxwVrRjPf1tzl99wZpY7NcFdn6A/F9DXWGGsgYFAf9hzxBTZhnRj7FpkX KCfPvI3xXOWOfHmUYKlJnS7RLRigNiJ4KB6Y4eR6rbyCee8r5pMw+2ivxFrA4Ayu8Gss PQfscnAUj4S9o3GZGzlKdoR9TJ5mZQ9/q38CVQfZDT6a0bWJ/CMpI9Ri3YdsezPFWpTT TGOExwxE+rRG5Ddo9+islZJbknQywxmTIzteFK17fpDdnRWg0EIaptARbwqBp/Gn1e0a KLeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LEV5tWURKgLpgo5q6BTc4NlcwzQLAQ+EC5rW0mqA8Xo=; b=gQqo8AoFPCiR5FtkLHB1kP3s7Qc77tqXNipxe1oiYgJckrm5+1wSz9vCsMIUF+i06v U9cl0wW1mmGwN2F9WvFiQv5MjtdvZjwqOgQ+F1608rg+iGFkMKQ4s8wpUGQerR9p5ZuX U0jWBDaeHEeQ3q4RXfSmwMypHxUX0ql7xyGoWdivt+mbb1ozsRLi9FePVDkr7tqttIto YWPVBPPjIb9UB1VLUut//tjyWdx7yIcMuuf5yEBBCF0D+NNXtXT5xtJw/ebQ4VmvZPaa ZZxOAv90aQCWnCtisqxX65jGbkFOOq4ZEAoUrEeXwjtqqJXN6tdF8tZJkkjbbVvVUcXg miRw== X-Gm-Message-State: APjAAAVqV0kE/ZSgxs+w3bH3Xj9AdwPQvTYgX7jSfIvShwBgfv/Ovs2c qc99ms4mH4UxPfVxU7OshX3XID/YvsI= X-Google-Smtp-Source: APXvYqyiewADpQP8cIsdPqhAjkY2KNXOElH4d5PbPs8KXkM0UEqnBsU2GR+WRo/ynGz4+mLf+I4aIA== X-Received: by 2002:a0c:961a:: with SMTP id 26mr61952699qvx.131.1558625678799; Thu, 23 May 2019 08:34:38 -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 o6sm14126879qtc.47.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-0004z4-RS; 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 00/11] mm/hmm: Various revisions from a locking/code review Date: Thu, 23 May 2019 12:34:25 -0300 Message-Id: <20190523153436.19102-1-jgg@ziepe.ca> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe This patch series arised out of discussions with Jerome when looking at the ODP changes, particularly informed by use after free races we have already found and fixed in the ODP code (thanks to syzkaller) working with mmu notifiers, and the discussion with Ralph on how to resolve the lifetime model. Overall this brings in a simplified locking scheme and easy to explain lifetime model: If a hmm_range is valid, then the hmm is valid, if a hmm is valid then the mm is allocated memory. If the mm needs to still be alive (ie to lock the mmap_sem, find a vma, etc) then the mmget must be obtained via mmget_not_zero(). Locking of mm->hmm is shifted to use the mmap_sem consistently for all read/write and unlocked accesses are removed. The use unlocked reads on 'hmm->dead' are also eliminated in favour of using standard mmget() locking to prevent the mm from being released. Many of the debugging checks of !range->hmm and !hmm->mm are dropped in favour of poison - which is much clearer as to the lifetime intent. The trailing patches are just some random cleanups I noticed when reviewing this code. I expect Jerome & Ralph will have some design notes so this is just RFC, and it still needs a matching edit to nouveau. It is only compile tested. Regards, Jason Jason Gunthorpe (11): mm/hmm: Fix use after free with struct hmm in the mmu notifiers mm/hmm: Use hmm_mirror not mm as an argument for hmm_register_range mm/hmm: Hold a mmgrab from hmm to mm mm/hmm: Simplify hmm_get_or_create and make it reliable mm/hmm: Improve locking around hmm->dead mm/hmm: Remove duplicate condition test before wait_event_timeout mm/hmm: Delete hmm_mirror_mm_is_alive() mm/hmm: Use lockdep instead of comments mm/hmm: Remove racy protection against double-unregistration mm/hmm: Poison hmm_range during unregister mm/hmm: Do not use list*_rcu() for hmm->ranges include/linux/hmm.h | 50 ++---------- kernel/fork.c | 1 - mm/hmm.c | 184 +++++++++++++++++++------------------------- 3 files changed, 88 insertions(+), 147 deletions(-) Signed-off-by: Jason Gunthorpe