From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11818C4708E for ; Wed, 7 Dec 2022 14:58:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 890C28E0003; Wed, 7 Dec 2022 09:58:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 840C28E0001; Wed, 7 Dec 2022 09:58:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 708F98E0003; Wed, 7 Dec 2022 09:58:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 61DC28E0001 for ; Wed, 7 Dec 2022 09:58:42 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 01C96AB5D0 for ; Wed, 7 Dec 2022 14:58:41 +0000 (UTC) X-FDA: 80215816884.16.9B9809A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 12F481A0017 for ; Wed, 7 Dec 2022 14:58:40 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XOItYhsa; spf=pass (imf19.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670425121; a=rsa-sha256; cv=none; b=H6gmQ702Nn/iHd85tbikNmqJuFWLpYAe4pSepVhGM+9RrHRgahyGXYCTH14RgBWKTU92Mj igQCMP9BaSKc5AGA5IrrgfWJuErAY7mktueDW2O38PrM/1ZOnOi+LcigaUeXit2LTNUNH3 mr89PCZIVuISyfjwVYtbjMKNrasGGx8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XOItYhsa; spf=pass (imf19.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670425121; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/M7RKADQKDoU7dhao5dBCCFwY6Zh8brnj1gC/LTmYIY=; b=cZY/nTUYZg0uOYQ2BpgfwhIfxQdh5/VvbyJQVYGOKYDAAWEd5eDLB6hnpsju8PpN+LZ3dE k6BKj3/NCQA914ge5flJogFbZZuFSket0taMg6ts1U+wRwfFOhfpowE7zSd0Xrm12xUih4 gS2FXcdccwaYVZFxmlT8IjtG2bRrOsk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670425120; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/M7RKADQKDoU7dhao5dBCCFwY6Zh8brnj1gC/LTmYIY=; b=XOItYhsaya9S7Knl04HQxYJlDKY9Z8m3f6iV6/LQY+qDXkIX6HaIBlhoOXAUuPs9zly2Re LWTb0t9/bdoA+tV4+0RwTrPzt61AgIWKXh+0zgGTFQpkTMfCySDUWsRJekAP9JOUR9NkQ/ MmehPw6SZeMADV/vzjgMATKZtxW1bYw= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-464-2jWP2e2lO2qMM5a-xk01Hw-1; Wed, 07 Dec 2022 09:58:39 -0500 X-MC-Unique: 2jWP2e2lO2qMM5a-xk01Hw-1 Received: by mail-qk1-f198.google.com with SMTP id bj4-20020a05620a190400b006fc7c5d454cso25289474qkb.14 for ; Wed, 07 Dec 2022 06:58:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=/M7RKADQKDoU7dhao5dBCCFwY6Zh8brnj1gC/LTmYIY=; b=zJIEheVIyGs0AjiHQE9YVCs1aaFTjS/mExQ+xfTclEliSEzfMAHOz0Yj+jwURVJ2eC WVMXmoyczOwY7pXBh+CpVF2J9KWdd92ozB+z60LJ8zYMR0S2UYQ9ImyDlKkttoRer6kK 9oA+Bwig0Z8h9GDDqrIqQJPOJLyq1nXOWGEPxOHHSJ6uEzRCet6iWaB5hOOd9Ps+mA5X j55P6vodl3Z9Jhx0i6C3Qu6m6cNX4KY26XZpx4bO6XU2UzV4ni5n5dO1xF5Gar5i0Jph 3cyzrgoWNDALh13ObgUE4v7UCT4eYT6lx0f0r7CTlJUlocvW22kmHcZdtxZpMTGe8eni WqAw== X-Gm-Message-State: ANoB5pmD7GBuX098xP/ZQO5yT3fN3rO7eUBfEc4kpaKGAzn718cZ4Mxl rtk5T3WWC+6nliI3QUdeTW7GR6dZvA7mAK7WKVmb3FZSYYYcwgYLJRvsp+hEM+Xqji8kYE0/0ro VH15U5DWJGnw= X-Received: by 2002:ac8:7305:0:b0:3a6:9cca:67e4 with SMTP id x5-20020ac87305000000b003a69cca67e4mr982587qto.60.1670425118920; Wed, 07 Dec 2022 06:58:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf4HZKnZ6yNOukOueF+OPGuEXOR0k70Isv3SlCGQ6xBviYaMfoghlhhnWDiVMK31xVr4ORxoMQ== X-Received: by 2002:ac8:7305:0:b0:3a6:9cca:67e4 with SMTP id x5-20020ac87305000000b003a69cca67e4mr982580qto.60.1670425118624; Wed, 07 Dec 2022 06:58:38 -0800 (PST) Received: from x1n (bras-base-aurron9127w-grc-46-70-31-27-79.dsl.bell.ca. [70.31.27.79]) by smtp.gmail.com with ESMTPSA id o1-20020ac841c1000000b0039cc82a319asm13393764qtm.76.2022.12.07.06.58.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 06:58:38 -0800 (PST) Date: Wed, 7 Dec 2022 09:58:36 -0500 From: Peter Xu To: John Hubbard Cc: Mike Kravetz , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton , Jann Horn , Andrew Morton , Andrea Arcangeli , Rik van Riel , Nadav Amit , Miaohe Lin , Muchun Song , David Hildenbrand Subject: Re: [PATCH 08/10] mm/hugetlb: Make walk_hugetlb_range() safe to pmd unshare Message-ID: References: <20221129193526.3588187-1-peterx@redhat.com> <20221129193526.3588187-9-peterx@redhat.com> <0813b9ed-3c92-088c-4fb9-45fb648c6e73@nvidia.com> <97e3a8f2-df75-306e-2edf-85976c168955@nvidia.com> <3f032921-c373-e942-a857-4328d7993ef0@nvidia.com> MIME-Version: 1.0 In-Reply-To: <3f032921-c373-e942-a857-4328d7993ef0@nvidia.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspam-User: X-Spamd-Result: default: False [0.10 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SORBS_IRL_BL(3.00)[209.85.222.198:received]; SUSPICIOUS_RECIPS(1.50)[]; SUBJECT_HAS_UNDERSCORES(1.00)[]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_TWELVE(0.00)[13]; ARC_NA(0.00)[]; R_DKIM_ALLOW(0.00)[redhat.com:s=mimecast20190719]; MIME_TRACE(0.00)[0:+]; TAGGED_RCPT(0.00)[]; DMARC_POLICY_ALLOW(0.00)[redhat.com,none]; TO_MATCH_ENVRCPT_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; DKIM_TRACE(0.00)[redhat.com:+]; R_SPF_ALLOW(0.00)[+ip4:170.10.133.0/24]; RCVD_COUNT_THREE(0.00)[4]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: 12F481A0017 X-Rspamd-Server: rspam01 X-Stat-Signature: ny4fgh81q3estuxz9dktq8hriqx436ht X-HE-Tag: 1670425120-721056 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Dec 06, 2022 at 06:38:54PM -0800, John Hubbard wrote: > On 12/6/22 16:07, Peter Xu wrote: > > I thought I answered this one at [1] above. If not, I can extend the > > answer. > > [1] explains it, but it doesn't mention why it's safe to drop and reacquire. > > ... > > > > If we touch it, it's a potential bug as you mentioned. But we didn't. > > > > Hope it explains. > > I think it's OK after all, because hmm_vma_fault() does revalidate after > it takes the vma lock, so that closes the loop that I was fretting over. > > I was just also worried that I'd missed some other place, but it looks > like that's not the case. > > So, good. > > How about this incremental diff on top, as an attempt to clarify what's > going on? Or is this too much wordage? Sometimes I write too many words: Nop, that all looks good, thanks. I'll apply them in my new post. > > > diff --git a/include/linux/pagewalk.h b/include/linux/pagewalk.h > index 1f7c2011f6cb..27a6df448ee5 100644 > --- a/include/linux/pagewalk.h > +++ b/include/linux/pagewalk.h > @@ -21,13 +21,16 @@ struct mm_walk; > * depth is -1 if not known, 0:PGD, 1:P4D, 2:PUD, 3:PMD. > * Any folded depths (where PTRS_PER_P?D is equal to 1) > * are skipped. > - * @hugetlb_entry: if set, called for each hugetlb entry. Note that > - * currently the hook function is protected by hugetlb > - * vma lock to make sure pte_t* and the spinlock is valid > - * to access. If the hook function needs to yield the > - * thread or retake the vma lock for some reason, it > - * needs to properly release the vma lock manually, > - * and retake it before the function returns. > + * @hugetlb_entry: if set, called for each hugetlb entry. This hook > + * function is called with the vma lock held, in order to > + * protect against a concurrent freeing of the pte_t* or > + * the ptl. In some cases, the hook function needs to drop > + * and retake the vma lock in order to avoid deadlocks > + * while calling other functions. In such cases the hook > + * function must either refrain from accessing the pte or > + * ptl after dropping the vma lock, or else revalidate > + * those items after re-acquiring the vma lock and before > + * accessing them. > * @test_walk: caller specific callback function to determine whether > * we walk over the current vma or not. Returning 0 means > * "do page table walk over the current vma", returning > diff --git a/mm/hmm.c b/mm/hmm.c > index dcd624f28bcf..b428f2011cfd 100644 > --- a/mm/hmm.c > +++ b/mm/hmm.c > @@ -497,7 +497,13 @@ static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask, > spin_unlock(ptl); > hugetlb_vma_unlock_read(vma); > - /* hmm_vma_fault() can retake the vma lock */ > + /* > + * Avoid deadlock: drop the vma lock before calling > + * hmm_vma_fault(), which will itself potentially take and drop > + * the vma lock. This is also correct from a protection point of > + * view, because there is no further use here of either pte or > + * ptl after dropping the vma lock. > + */ > ret = hmm_vma_fault(addr, end, required_fault, walk); > hugetlb_vma_lock_read(vma); > return ret; > > > > I guess it's on me to think of something cleaner, so if I do I'll pipe > > > up. :) > > > > That'll be very much appricated. > > > > It's really that I don't know how to make this better, or I can rework the > > series as long as it hasn't land upstream. > > > > It's always 10x easier to notice an imperfection, than it is to improve on > it. :) -- Peter Xu