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 24C21C0218F for ; Fri, 31 Jan 2025 22:23:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 756696B007B; Fri, 31 Jan 2025 17:23:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 705456B0082; Fri, 31 Jan 2025 17:23:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F3D76B0083; Fri, 31 Jan 2025 17:23:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 410836B007B for ; Fri, 31 Jan 2025 17:23:25 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E0CC380296 for ; Fri, 31 Jan 2025 22:23:24 +0000 (UTC) X-FDA: 83069174328.04.C1A8AC0 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf04.hostedemail.com (Postfix) with ESMTP id 3AE4E40014 for ; Fri, 31 Jan 2025 22:23:23 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="wGgC/Haw"; spf=pass (imf04.hostedemail.com: domain of akpm@linux-foundation.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738362203; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cWizMzLwKDzJWgAdrMLTC4VxCXkd3n8tCNNKJQNxNn0=; b=n2V0Tya1CR7twLjv4TLYImY4R4bdciYMzcWZrgArOcCZl1HYVAx079vN6peDpLGu9RDKbj +Y1dAIV/A2MTjaCtWMniBlhkL451EI/+OmXt2GqJSIyZ8FhnF1JasepJ4b/XuEHJ4Za8db af0d8z6bBSCsfjIMss88EqPZs9wpR5I= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="wGgC/Haw"; spf=pass (imf04.hostedemail.com: domain of akpm@linux-foundation.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738362203; a=rsa-sha256; cv=none; b=2+/+mxkgE5mitoCpu9pUpBuPDtOQPucPTGOCfG5gBwZIdi4rH7msKXuFkSWUwFm4z8CQu3 hdeXBdOyl0f9WMpd9o+bS1QX7V04dektKM0+0nTe9RguO+bms4H1XsRhVD9db5IGWBs51C HiKo0g3D4LEGBin0ewubnJI4r39e+BA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id D16F9A42513; Fri, 31 Jan 2025 22:21:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1DBDC4CED1; Fri, 31 Jan 2025 22:23:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1738362202; bh=nwVyP9nU/ce1tJmLE7dt7p5zzWeMULcBJL+nkofQ2cQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=wGgC/Hawd4WptjG9VZ4SzzazWExRopVK70tnpkWsIOPy3TfzMIqVX6OpDYjVbnN5K fHgU4BzcwA7ElEyyORyuA3WSsSt7usDd1R6nrn3GWkaSAPZxK1orp3DhBfJ94Q3qbk 1LU/7de324XWVLCkllzSDdIzioLdcNOWUMu8bYks= Date: Fri, 31 Jan 2025 14:23:21 -0800 From: Andrew Morton To: Ricardo =?ISO-8859-1?Q?Ca=F1uelo?= Navarro Cc: riel@surriel.com, linux-mm@kvack.org, stable@vger.kernel.org, kernel-dev@igalia.com, revest@google.com Subject: Re: [PATCH] mm,madvise,hugetlb: check for 0-length range after end address adjustment Message-Id: <20250131142321.632a9468529d3267abe641af@linux-foundation.org> In-Reply-To: <20250131143749.1435006-1-rcn@igalia.com> References: <20250131143749.1435006-1-rcn@igalia.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 3AE4E40014 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: rck3a3jrrddgowoop4dcj47aw11cxgoq X-HE-Tag: 1738362203-467160 X-HE-Meta: U2FsdGVkX1/bpM0w2aa9BwBxs5qHE2iVArTDAJKC/9t59hBYIYPEus1AuSDcV6PotkNvsgoMh+4zTtsyXxS84HNSlczww5hodcqhVwXk5i0yH1R6YHV8q22hD/ubrzHcEKuOHv2sTVbobggcx4Pz1CAHo5x8p41TPhRGWBGk9JEOEi2sCqK+aKYd/Q0Awfr/HwkXskP1Y6SdGAGzhtPZ3znweGT8fu12AqC8Bs8eLmfMJ5ddaF2PllScN0StvNvEn8lKBZ+OreTw5CfYQaX45xm/ChgO621/Mb9DWM8Qvl94sQ/AEyEguM98canB+bq0bk5+0Ck8WMHlEGzoHUoJPluM/iWgA2o9Te56C91d/feBpDcgWL88YCwa4SC+YjnsVhRYpiIYy94cqYKOg6hdAtu6uxOBtufyzBoo6Krsrt0jiiJIAd6+//0NypglezPv5bz4qUgMg6DqPqUwZ7uI1+dHJff0Vfd5L2vkaa2RDtqljaZti0h/T8GgrJ1DujAYulh6o46QOhg5d0qPVyW5KEB369luv4zoR/VFq4RrBgLK2qOy/aucNDkukT3cKpJtV/I7AC+O7ru9t3EUikwVIzj3ITP8zvSGWmT1vhdTxRUDsVDi/a22gITRV899VncFAchLCzljL1fNbTlua+nh0Wnbm4HzZUvpEpuxHuibqo9I/TU7UjClPh9Iyq6FTiNRIEWEABJHp+MZC770qAuKi94qNPOGhfPQNE3QMUv19iQ4iXD1nSiDAbYA4SrdYchvIGj5q3Bwxda17vQhUsHH/Xh4+M4GEZ+3ornVv0YPQ+c3y4P+Cns+9Fc2m5oNFjMOP8Bl3OIzRrSCbYd8LMsGWWq09UgAE+ZoaeXHtpThKvIWxd4DzaVML1HYKyneGGgKBek6jM2roy55PvO2dwwTNTT0OJYjs8agYP8Zgc6GNAWthAOch7MwApuuBSxvwLKymKdnE6cyj+0g6AOiPX8 nmaWkI7o EcFdcISZheZqsJtzFeLxJjmr9K22dLgLX5QIOItvHCt5ENKK56oL9ckG9UxWQTNKm/QSqJesfyHi6umy/ZnbG68WldmztOR9ieyjRYrbLOUwt5mR7Ix9BZ/on4Y02KOgU7DtyZ6LonkN4zGNAXc5lP+bvc6EJiNS/9HmTedQOlvVsoVUx7i7vR96j1jc39SlJL3sq+8bEOIP2QH6/d/YYvH5x7MU/kDDnydIYRJtqrZxGui4clRaWTPzhfzLnElW54LJI9JupXRa60h51z5tCXb5dtZ6671Sss09v67nNodLWPj4gH5a3GnCMtQ== 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: List-Subscribe: List-Unsubscribe: On Fri, 31 Jan 2025 15:37:49 +0100 Ricardo Ca=F1uelo Navarro wrote: > Add a sanity check to madvise_dontneed_free() to address a corner case > in madvise where a race condition causes the current vma being processed > to be backed by a different page size. >=20 > During a madvise(MADV_DONTNEED) call on a memory region registered with > a userfaultfd, there's a period of time where the process mm lock is > temporarily released in order to send a UFFD_EVENT_REMOVE and let > userspace handle the event. During this time, the vma covering the > current address range may change due to an explicit mmap done > concurrently by another thread. >=20 > If, after that change, the memory region, which was originally backed by > 4KB pages, is now backed by hugepages, the end address is rounded down > to a hugepage boundary to avoid data loss (see "Fixes" below). This > rounding may cause the end address to be truncated to the same address > as the start. >=20 > Make this corner case follow the same semantics as in other similar > cases where the requested region has zero length (ie. return 0). >=20 > This will make madvise_walk_vmas() continue to the next vma in the > range (this time holding the process mm lock) which, due to the prev > pointer becoming stale because of the vma change, will be the same > hugepage-backed vma that was just checked before. The next time > madvise_dontneed_free() runs for this vma, if the start address isn't > aligned to a hugepage boundary, it'll return -EINVAL, which is also in > line with the madvise api. >=20 > >From userspace perspective, madvise() will return EINVAL because the > start address isn't aligned according to the new vma alignment > requirements (hugepage), even though it was correctly page-aligned when > the call was issued. >=20 > ... > > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -933,7 +933,9 @@ static long madvise_dontneed_free(struct vm_area_stru= ct *vma, > */ > end =3D vma->vm_end; > } > - VM_WARN_ON(start >=3D end); > + if (start =3D=3D end) > + return 0; > + VM_WARN_ON(start > end); > } Perhaps add a comment telling the user how this situation can come about?