From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37C842D6E66 for ; Sat, 11 Jul 2026 03:09:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783739365; cv=none; b=CUycw/HfqL2x45D/jI/2QwEN1w4O+KIy1og3JhvIOS4eMegmIIFzwQMGDBu+lotQz6JTelOerTcgTllschWdobllag2u/GCL8r3ybURBNDE7sQ7PKJlZN0sqgSAAGQ9uo0iMbZ0W9LdJYxpcvt3RvGx5Eaw9B8kVO2La1t3YT10= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783739365; c=relaxed/simple; bh=Talh9NFQCx452XKRUTeh7mUAoB/ijgfuASG+mTSXc/U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=P5hUGzpWGKlCGEPXbp/Hh0BJjWn/3rpXObulg9vpeEVYRC4/X67Lv7hnlSkmXX3xxsGQ6zCpPMtMf3Zpst7LS13ICqzzhscPdqROuekljQnVi/hlZD628V7o02GWfrDXYPCKdry/2W0bI/CEE6hhN1HfTT62h6scT2QOpiLYLj0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D3qaXtGK; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D3qaXtGK" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-c96cb024ee0so1074722a12.1 for ; Fri, 10 Jul 2026 20:09:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783739362; x=1784344162; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=KBT/pxhbpviyB1eK4ppjTwsrTkYiVk3Lhi/aEEh6q5s=; b=D3qaXtGKb2Xf/9+wr2HRFKL8jmCpHoxmOjnzBKRHFG4bHHCEScpCbA5LpnhM2eS11X nG9M0kPvxBXep+/0ZCyYpRgX9l/ZOilK8nOM71Vgq6QSmbYjtBkKLSTeFdNymLrs/NtM lHJqYdTRaEwaAkny4yNm2SHBIWlKx0LsU1YAI8HL2f/iW6YlZOXXXaHQBlQD9xfTOEEf qaGDiV+BqsVGAnDAlYreU+5w03vSy3s7iByMkQO+PHscZvMeT1dJQUSue75LORCHtvZb OMKMnwjruN3UimS/yQ0MnbqQtL/WsXfSgGLdVFnix7IQ8JsllW63eDEk3VrejGY7F/03 vdYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783739362; x=1784344162; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=KBT/pxhbpviyB1eK4ppjTwsrTkYiVk3Lhi/aEEh6q5s=; b=Wsqn4EquBcsbwCMdxmgeQuykeDb5sbNOmopZWJV/P478O9hSMRtD+Td6ofHVfNC7hz 39lCx4J8r4CkZ9ZGTNb2Irlfb0DwwAbuqBTRAydD1QOvMoLibUmAPqC9CI7HfaUEy+BG u8gm3W4ucoK3oA6RU/xCOk/r1QzQID6IS5/O0Chmla6qN7p1PLpg5CjQtG3pULx1E9+B ZBHf3BliJA0YrimRqiP6Bz52A62Gm5Ohamvfn9CDMPjMPW/0cvL6trmcDCuj8rSxRaGi kUewk/9ynyXduL+aZhfI/o0p1WaCCloEq6bV8tAyxTtdyt74w3Q7lSYn99FYNHt2FqZ/ SRlg== X-Forwarded-Encrypted: i=1; AHgh+RpnhPD9++X3YmjJDYWJLGuoSbHaYU8D3lbPdS94H3a+pND1BLKt7U6JxmqD3XXBe74L+w8KsMpIhmt/bZQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwxWnwNbNLm540ZYmxa7Y9qgcJ+UH7EH+bp+uYl/ZGjivKF9ZBG 9KK+XQ41jexz+xucFPD0RV0QTkxzNfohLsQNSl874VobpypdCVSHxsKC X-Gm-Gg: AfdE7cnluj1XLAsQFnmqa3gjpxoD8XP7fIrwsA32QuiBdXkS5v3kxawB3DJemoXNPv8 4IUYtAq0v5STpMNQ1q7OfYriDnY66m/VQW7CrSoMKJeNS+vJEzKpL/Ur7hiu/7kIUbqTnVqw1Tx RPBfs7GbwZRklb7Am9b4vSivZBxAQFz5II/WzspGfaLqUhfJelWUp56waNIlphjXXAfg4uucSR8 +hDHlXOwWvLz2scyieDtnNQTGHUPXNvv2fG04zcw3+3HjJ4N7VkHNRS58JFWXM3m4lnYYlbLRy1 pX0V4gL8f2meBb7Med3j6R+3MX++6oYYX4VUuqt541KdRq41CkKVtkAWWatZXwT+ZynzyzjIZk1 N2dy5vaXN+rel5nQry1cZlrnp+zYCDTXVdiCB7z445GuWM+TdA2VEJHv/Kj+2rwbryKFUCUH4rx iMvIR6KYgjx8i9s99lGPGLA9g/u8U8zYIcxIo0XKffHscZEPZL5V88P4s= X-Received: by 2002:a05:6a00:3404:b0:847:9151:3409 with SMTP id d2e1a72fcca58-84889797ad5mr1318139b3a.55.1783739362452; Fri, 10 Jul 2026 20:09:22 -0700 (PDT) Received: from skinsburskii (c-98-225-44-182.hsd1.wa.comcast.net. [98.225.44.182]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-847f6db29d1sm11140358b3a.53.2026.07.10.20.09.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2026 20:09:21 -0700 (PDT) Date: Fri, 10 Jul 2026 20:09:18 -0700 From: Stanislav Kinsburskii To: Andrew Morton Cc: airlied@gmail.com, akhilesh@ee.iitb.ac.in, corbet@lwn.net, dakr@kernel.org, david@kernel.org, decui@microsoft.com, haiyangz@microsoft.com, jgg@ziepe.ca, kees@kernel.org, kys@microsoft.com, leon@kernel.org, liam@infradead.org, lizhi.hou@amd.com, ljs@kernel.org, longli@microsoft.com, lyude@redhat.com, maarten.lankhorst@linux.intel.com, mamin506@gmail.com, mhocko@suse.com, mripard@kernel.org, nouveau@lists.freedesktop.org, ogabbay@kernel.org, oleg@redhat.com, rppt@kernel.org, shuah@kernel.org, simona@ffwll.ch, skhan@linuxfoundation.org, surenb@google.com, tzimmermann@suse.de, vbabka@kernel.org, wei.liu@kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rdma@vger.kernel.org Subject: Re: [PATCH v8 2/8] mm/hmm: add hmm_range_fault_unlocked_timeout() for mmap lock-drop support Message-ID: References: <178371866223.900500.12312667138651735591.stgit@skinsburskii> <178371879503.900500.7148019929226548795.stgit@skinsburskii> <20260710151209.157d7c80b03dc56d73b5884a@linux-foundation.org> Precedence: bulk X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20260710151209.157d7c80b03dc56d73b5884a@linux-foundation.org> On Fri, Jul 10, 2026 at 03:12:09PM -0700, Andrew Morton wrote: > On Fri, 10 Jul 2026 14:26:35 -0700 Stanislav Kinsburskii wrote: > > > hmm_range_fault() requires the caller to hold the mmap read lock for the > > duration of the call. This is incompatible with mappings whose fault > > handler may release the mmap lock, notably userfaultfd-managed regions, > > where handle_mm_fault() can return VM_FAULT_RETRY or VM_FAULT_COMPLETED > > after dropping the lock. Drivers that need to populate device page tables > > for such mappings have no way to do so today. > > > > Add hmm_range_fault_unlocked_timeout() for callers that do not need to hold > > mmap_lock across any work outside the HMM fault itself. The helper takes > > mmap_read_lock_killable() internally, calls the common HMM fault > > implementation, and releases the lock before returning if it is still held. > > The timeout is specified in jiffies; passing 0 retries indefinitely, while > > a non-zero timeout makes the helper return -EBUSY when the retry budget > > expires. > > > > When handle_mm_fault() drops mmap_lock, or when the range is invalidated, > > hmm_range_fault_unlocked_timeout() refreshes range->notifier_seq and > > retries the walk internally. If the lock was dropped, the retry deadline is > > also restarted because a lock-dropping fault handler made progress. > > Ordinary -EBUSY retries keep the existing deadline, preserving the caller's > > timeout policy for repeated mmu-notifier invalidations. > > > > The caller only needs to perform the usual post-success > > mmu_interval_read_retry() check while holding its update lock before > > consuming the pfns. If mmap_lock acquisition is interrupted or a fatal > > signal is pending during retry handling, -EINTR is returned instead. > > > > The common implementation conditionally sets FAULT_FLAG_ALLOW_RETRY and > > FAULT_FLAG_KILLABLE only for hmm_range_fault_unlocked_timeout(). The > > existing hmm_range_fault() path still passes no locked state, does not > > allow handle_mm_fault() to drop mmap_lock, and remains a thin wrapper > > preserving the existing API contract for current callers. > > > > The previous refactor that moved page fault handling out of the page-table > > walk callbacks is what makes this change small. Faults now run after > > walk_page_range() has unwound, with only mmap_lock held, so dropping it > > does not interact with the walker's pte spinlock or hugetlb_vma_lock. > > Hugetlb regions therefore participate in the unlocked path uniformly with > > PTE- and PMD-level mappings; no special case is required. > > > > Documentation/mm/hmm.rst is updated with a description of the new API and > > the recommended caller pattern. > > > > ... > > > > A trivial thing: > > > +int hmm_range_fault_unlocked_timeout(struct hmm_range *range, > > + unsigned long timeout) > > +{ > > + struct mm_struct *mm = range->notifier->mm; > > + unsigned long deadline = 0; > > + bool locked = false; > > This could be local to the do loop and it needn't be initialized. > Unfortunately, it can’t, because its state is mutated in hmm_range_fault_locked() and the resulting state needs to be preserved across iterations of the loop as deadline reset depends on it. Thanks, Stanislav > > + int ret; > > + > > + do { > > + if (fatal_signal_pending(current)) > > + return -EINTR; > > + > > + if (timeout) { > > + /* > > + * If the previous fault dropped mmap_lock, then the fault > > + * handler made progress. Restart the retry timeout in that > > + * case, but keep the existing deadline for ordinary -EBUSY > > + * retries. > > + */ > > + if (!locked) > > + deadline = jiffies + timeout; > > + > > + if (time_after(jiffies, deadline)) > > + return -EBUSY; > > + } > > + > > + range->notifier_seq = > > + mmu_interval_read_begin(range->notifier); > > + > > + ret = mmap_read_lock_killable(mm); > > + if (ret) > > + return ret; > > + > > + locked = true; > > + ret = hmm_range_fault_locked(range, &locked); > > + if (locked) > > + mmap_read_unlock(mm); > > + } while (ret == -EBUSY); > > + > > + return ret; > > +} > > +EXPORT_SYMBOL(hmm_range_fault_unlocked_timeout); > > + >