From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15242CE7A89 for ; Mon, 25 Sep 2023 08:21:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232690AbjIYIVf (ORCPT ); Mon, 25 Sep 2023 04:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232691AbjIYIV2 (ORCPT ); Mon, 25 Sep 2023 04:21:28 -0400 Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FC2F11F for ; Mon, 25 Sep 2023 01:21:21 -0700 (PDT) Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-59f4db9e11eso38937797b3.0 for ; Mon, 25 Sep 2023 01:21:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630080; x=1696234880; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=51Cow/pgH6uhDfisazzoiSztYmzsaEmF2Mt51ptIi+M=; b=Fuj+wssXVCONsVnYstmMS7XThFL0S0Tmt+vu4YyU7EiirNiWppPOMrpKPXGpJYXW4o xFCV0AztEguVLkUi7L3Wrf7TrJqNSXDOdbB0SLBj2S1zOyfjgY6pDkzMtJJ7miPBTIag 8tsXu1HLX7pW1UglLsjr26fVRMj7yAkTd+Dllz41rQT4QCkvs72dmOBSuQeN1SozU/qO adesdkGGLAci3vfVY5egevDIQGto2k1AZfW07ERjCVFyFqgtf+KcNLi0zV7xiVOn7wr8 XRQohKGGz0o9dqVNPGSXkA5+kk7ArdsEYeFKgP+DuMGuANi8eDz2BWpkjizFwfetXbdn UDkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630080; x=1696234880; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=51Cow/pgH6uhDfisazzoiSztYmzsaEmF2Mt51ptIi+M=; b=YK15mDH9MsR6YIFkXHJgNbeFWzjmBtRaVLMzY1cXsSc6VIHiQq72ZYhvICe2aCKLPT Job5Zp+n0wI3+QKxjyY9NLcD6f8KZHe+JKXN/Pgnk6wN7rTXPh0hZJChVtdG1NOmPejL /gs3Ru+agdGRXiWrgLAxUZx7hcxZUv1cTaSp1jlepy1Xuaz1tDIoVGEsxVhCqx1OAuA5 Zg7qFquZ6N5hwRDKHWNPmNNpPifXemQahDRM2tKmkgPZJIyYRjRc2edld9XNUMPXDn8N sE+Na6fXQ4Gn+5b1c38XuWoA2M8sPy5QuqcZn+7lwPZh1PeE0+MOhvJBZMqaEtzRQpeb urIg== X-Gm-Message-State: AOJu0YwCCPj5B6CMtMluPrymiblz3XYZa4iCWEFruA3fd9U0lZjnK59d WJXoNdoR2i7jXS0fOkiaV4eLyA== X-Google-Smtp-Source: AGHT+IGr7os4V6FD7A0Dc+N5PVL7LECactrhqyVufW3y5mQ7Ilx2z2M+P22nvGF/P0KFm7TJWuxF3Q== X-Received: by 2002:a0d:f884:0:b0:59b:dbb7:5c74 with SMTP id i126-20020a0df884000000b0059bdbb75c74mr6123161ywf.32.1695630080347; Mon, 25 Sep 2023 01:21:20 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id h67-20020a0dc546000000b00583b144fe51sm2289914ywd.118.2023.09.25.01.21.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:21:19 -0700 (PDT) Date: Mon, 25 Sep 2023 01:21:10 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 01/12] hugetlbfs: drop shared NUMA mempolicy pretence In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: <47a562a-6998-4dc6-5df-3834d2f2f411@google.com> References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hugetlbfs_fallocate() goes through the motions of pasting a shared NUMA mempolicy onto its pseudo-vma, but how could there ever be a shared NUMA mempolicy for this file? hugetlb_vm_ops has never offered a set_policy method, and hugetlbfs_parse_param() has never supported any mpol options for a mount-wide default policy. It's just an illusion: clean it away so as not to confuse others, giving us more freedom to adjust shmem's set_policy/get_policy implementation. But hugetlbfs_inode_info is still required, just to accommodate seals. Yes, shared NUMA mempolicy support could be added to hugetlbfs, with a set_policy method and/or mpol mount option (Andi's first posting did include an admitted-unsatisfactory hugetlb_set_policy()); but it seems that nobody has bothered to add that in the nineteen years since v2.6.7 made it possible, and there is at least one company that has invested enough into hugetlbfs, that I guess they have learnt well enough how to manage its NUMA, without needing shared mempolicy. Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) --- fs/hugetlbfs/inode.c | 41 +---------------------------------------- include/linux/hugetlb.h | 2 -- 2 files changed, 1 insertion(+), 42 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 316c4cebd3f3..ffee27b10d42 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -83,29 +83,6 @@ static const struct fs_parameter_spec hugetlb_fs_paramet= ers[] =3D { {} }; =20 -#ifdef CONFIG_NUMA -static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma, - struct inode *inode, pgoff_t index) -{ - vma->vm_policy =3D mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy, - index); -} - -static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma) -{ - mpol_cond_put(vma->vm_policy); -} -#else -static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma, - struct inode *inode, pgoff_t index) -{ -} - -static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma) -{ -} -#endif - /* * Mask used when checking the page offset value passed in via system * calls. This value will be converted to a loff_t which is signed. @@ -852,8 +829,7 @@ static long hugetlbfs_fallocate(struct file *file, int = mode, loff_t offset, =20 /* * Initialize a pseudo vma as this is required by the huge page - * allocation routines. If NUMA is configured, use page index - * as input to create an allocation policy. + * allocation routines. */ vma_init(&pseudo_vma, mm); vm_flags_init(&pseudo_vma, VM_HUGETLB | VM_MAYSHARE | VM_SHARED); @@ -901,9 +877,7 @@ static long hugetlbfs_fallocate(struct file *file, int = mode, loff_t offset, * folios in these areas, we need to consume the reserves * to keep reservation accounting consistent. */ - hugetlb_set_vma_policy(&pseudo_vma, inode, index); folio =3D alloc_hugetlb_folio(&pseudo_vma, addr, 0); - hugetlb_drop_vma_policy(&pseudo_vma); if (IS_ERR(folio)) { mutex_unlock(&hugetlb_fault_mutex_table[hash]); error =3D PTR_ERR(folio); @@ -1282,18 +1256,6 @@ static struct inode *hugetlbfs_alloc_inode(struct su= per_block *sb) hugetlbfs_inc_free_inodes(sbinfo); return NULL; } - - /* - * Any time after allocation, hugetlbfs_destroy_inode can be called - * for the inode. mpol_free_shared_policy is unconditionally called - * as part of hugetlbfs_destroy_inode. So, initialize policy here - * in case of a quick call to destroy. - * - * Note that the policy is initialized even if we are creating a - * private inode. This simplifies hugetlbfs_destroy_inode. - */ - mpol_shared_policy_init(&p->policy, NULL); - return &p->vfs_inode; } =20 @@ -1305,7 +1267,6 @@ static void hugetlbfs_free_inode(struct inode *inode) static void hugetlbfs_destroy_inode(struct inode *inode) { hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb)); - mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy); } =20 static const struct address_space_operations hugetlbfs_aops =3D { diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 5b2626063f4f..6522eb3cd007 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -30,7 +30,6 @@ void free_huge_folio(struct folio *folio); =20 #ifdef CONFIG_HUGETLB_PAGE =20 -#include #include #include =20 @@ -512,7 +511,6 @@ static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(st= ruct super_block *sb) } =20 struct hugetlbfs_inode_info { - struct shared_policy policy; struct inode vfs_inode; unsigned int seals; }; --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2E16CE7A95 for ; Mon, 25 Sep 2023 08:22:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232723AbjIYIWl (ORCPT ); Mon, 25 Sep 2023 04:22:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232521AbjIYIWi (ORCPT ); Mon, 25 Sep 2023 04:22:38 -0400 Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 181E7A2 for ; Mon, 25 Sep 2023 01:22:32 -0700 (PDT) Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-d8168d08bebso6621688276.0 for ; Mon, 25 Sep 2023 01:22:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630151; x=1696234951; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=4sZK+xCuT0P0uH5eP+c4/6boHa42doWBfySEpYJVCK8=; b=3vMb1WfNB52Ctd8ZUSYd3LaVttK5c7Bfurq43e9L29hBs7NUW8q0+grL80cz4YRTfZ u/mqAWWMBom+VTxVTJp0DOeT0Rz/1VbBBv3e86WhH/Bsy4PiEw7RHGuX4iHAkLyRnmjH XXaYcHZM8HPKX8D8CskW81EF6/FJROCkpEQeXxjPUjqvK6ViOJcMxLkl/Hdcga2XkNbv zmNO8fKxha8IrVBud/Hb4Hie5by8IMGjhhhM662qhj6eMgtdbGzFYf2wV9jvSgv3SPzp 3kngQmiV8uUFuLc0eEoQJ1wXXj2fQD7XRml8KvSwBpO8jHx0XS5Y4Fy6lBeXq2IU4u3u ofdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630151; x=1696234951; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4sZK+xCuT0P0uH5eP+c4/6boHa42doWBfySEpYJVCK8=; b=YR67oEBQZ2V0+qPcQS6nV9TZieuYaBMyYVJdRXjYlefEVFE9GzKnsUG9+TL+1Lotx8 pwHGyrxs8q1E1JtBEiwedQqXeU76LjwVW5AozCz3TuL+vMwOgHkmUmpE68he5RzPDY+D kzc0mSSrabz2hv1XwuHNhfFJoJ4Ro2Q0ijZFM7sqtrlG0fHgbXeaBojHO5hWojP/SfQ1 qsQG1DaiWq9+51I/onOgtedhCt9ZIYJgFHlMXYy5XBd5AIKT5HZ67yEuEXD6T0Zlldx9 hgb//CB1q0swZy2NBswmDbkVt/ABcqfpmxoQQe/kTyogWpKqemcnrp8LD4gRLtbhPOSc Ynow== X-Gm-Message-State: AOJu0YyI+mn89qXpJ0SFELtJcVix+Cgoka3u69Eih8EWXBuHO1+gsRGz x0Tru6azyjsiguwSaz4l3HjG9A== X-Google-Smtp-Source: AGHT+IEwv44FcIamrq7TyieXIPq5bY4OaPs0D1yBi6vYNp4KwxZNNaUgHfeQiKSegaaLKY4KIVfLOQ== X-Received: by 2002:a25:6942:0:b0:d7b:9580:240c with SMTP id e63-20020a256942000000b00d7b9580240cmr6055638ybc.47.1695630151093; Mon, 25 Sep 2023 01:22:31 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id n80-20020a25da53000000b00d217e46d25csm2127705ybf.4.2023.09.25.01.22.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:22:30 -0700 (PDT) Date: Mon, 25 Sep 2023 01:22:27 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 02/12] kernfs: drop shared NUMA mempolicy hooks In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It seems strange that kernfs should be an outlier with a set_policy and get_policy in its kernfs_vm_ops. Ah, it dates back to v2.6.30's commit 095160aee954 ("sysfs: fix some bin_vm_ops errors"), when I had crashed on powerpc's pci_mmap_legacy_page_range() fallback to shmem_zero_setup(). Well, that was commendably thorough, to give sysfs-bin a set_policy and get_policy, just to avoid the way it was coded resulting in EINVAL from mmap when CONFIG_NUMA; but somehow feels a bit over-the-top to me now. It's easier to say that nobody should expect to manage a shmem object's shared NUMA mempolicy via some kernfs backdoor to that object: delete that code (and there's no longer an EINVAL from mmap in the NUMA case). This then leaves set_policy/get_policy as implemented only by shmem - though importantly also by SysV SHM, which has to interface with shmem which implements them, and with SHM_HUGETLB which does not. Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) --- fs/kernfs/file.c | 49 ------------------------------------------------ 1 file changed, 49 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 180906c36f51..aaa76410e550 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -429,60 +429,11 @@ static int kernfs_vma_access(struct vm_area_struct *v= ma, unsigned long addr, return ret; } =20 -#ifdef CONFIG_NUMA -static int kernfs_vma_set_policy(struct vm_area_struct *vma, - struct mempolicy *new) -{ - struct file *file =3D vma->vm_file; - struct kernfs_open_file *of =3D kernfs_of(file); - int ret; - - if (!of->vm_ops) - return 0; - - if (!kernfs_get_active(of->kn)) - return -EINVAL; - - ret =3D 0; - if (of->vm_ops->set_policy) - ret =3D of->vm_ops->set_policy(vma, new); - - kernfs_put_active(of->kn); - return ret; -} - -static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma, - unsigned long addr) -{ - struct file *file =3D vma->vm_file; - struct kernfs_open_file *of =3D kernfs_of(file); - struct mempolicy *pol; - - if (!of->vm_ops) - return vma->vm_policy; - - if (!kernfs_get_active(of->kn)) - return vma->vm_policy; - - pol =3D vma->vm_policy; - if (of->vm_ops->get_policy) - pol =3D of->vm_ops->get_policy(vma, addr); - - kernfs_put_active(of->kn); - return pol; -} - -#endif - static const struct vm_operations_struct kernfs_vm_ops =3D { .open =3D kernfs_vma_open, .fault =3D kernfs_vma_fault, .page_mkwrite =3D kernfs_vma_page_mkwrite, .access =3D kernfs_vma_access, -#ifdef CONFIG_NUMA - .set_policy =3D kernfs_vma_set_policy, - .get_policy =3D kernfs_vma_get_policy, -#endif }; =20 static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma) --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B36AACE7A81 for ; Mon, 25 Sep 2023 08:24:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232838AbjIYIYf (ORCPT ); Mon, 25 Sep 2023 04:24:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232873AbjIYIYS (ORCPT ); Mon, 25 Sep 2023 04:24:18 -0400 Received: from mail-yb1-xb32.google.com (mail-yb1-xb32.google.com [IPv6:2607:f8b0:4864:20::b32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8D1110A for ; Mon, 25 Sep 2023 01:24:06 -0700 (PDT) Received: by mail-yb1-xb32.google.com with SMTP id 3f1490d57ef6-d868d8363e6so2557300276.2 for ; Mon, 25 Sep 2023 01:24:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630246; x=1696235046; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=aN6nR5IwDNUCMkgwxdT7Lz56gPehMPk2IXT5mlQNQ+g=; b=RGQf28RbvgaZvE0G5qzQWCg28A8SaNeXYyIFyGgdg2gjzztD/pl0JRxzZWlUsYNaBx KAfUQ0n4+7iEnC/vIv4CskBC0F4R/2Smt/kDM5aug3nDNfAsvkvqYqYlMaklMZTIknZL 3/6nxhJX+f/ZZTsWFfwfdn9WDrZrH5GiU3Z/tXOyPkBKBDLo1qV/vtrPucBCdPoehl1a 9+NSyQ0qULMM3qoeGQUM/5+G7YbPIOQaXJ71HOi2G7RYvP1Iz0af6SCBJBFvtxiikIoS IverrQezQPy5bHDhLhwgbW61+4UKdP/3uFsH0ZcnnSQv6Fg6WwcUm3zWra2+o30/MiSZ 5WKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630246; x=1696235046; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aN6nR5IwDNUCMkgwxdT7Lz56gPehMPk2IXT5mlQNQ+g=; b=fywpc99DS+C72+NljGH481ckg01J3EA0Qm46TTQEoBdQdRWISw75dLKO2c/QE7omOs oT9Wg6PDSQzckuzogIUt8tSmy8oY+2SwNrBEjwRvRjTqKoc0AIbbQotIbMueSMvR89j6 jpVDrpIoyBwdkWYkztc6JHM3cV4y5FkEBnOYxqOV6WjrmZyMcvYht2V3kEeviCYvmspI W/3qEXRYUzc1zXwQjkzXuqLM5DUfo86w3ojNcjIEgYAfvk/45m0a2Lw3w5XVoZ/bkff+ i2GS7Un224UJzU8MZzhmKTwqLukD0HyNb3Z/ZvKtH7XpI+Ml0Pj7yzTHITE97qkx5wlT ENiA== X-Gm-Message-State: AOJu0YxXdueop9X8lzKtq1nropSE09r/r2ri8eX3+SVN1nRmUqEy4esl Q9yZymIhvStlWp5w52bAZqU8FA== X-Google-Smtp-Source: AGHT+IEZ5z3Rqd6K+p87ryKeqtoIFxtYPibeDUbUMGyD2lKyNvgK9u7Hoa4GbTrF1vLwXwt7ln5PhQ== X-Received: by 2002:a25:dbcc:0:b0:d0f:846c:ef7b with SMTP id g195-20020a25dbcc000000b00d0f846cef7bmr5135924ybf.17.1695630245463; Mon, 25 Sep 2023 01:24:05 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id j134-20020a25238c000000b00c64533e4e20sm2118142ybj.33.2023.09.25.01.24.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:24:04 -0700 (PDT) Date: Mon, 25 Sep 2023 01:24:02 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 03/12] mempolicy: fix migrate_pages(2) syscall return nr_failed In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" "man 2 migrate_pages" says "On success migrate_pages() returns the number of pages that could not be moved". Although 5.3 and 5.4 commits fixed mbind(MPOL_MF_STRICT|MPOL_MF_MOVE*) to fail with EIO when not all pages could be moved (because some could not be isolated for migration), migrate_pages(2) was left still reporting only those pages failing at the migration stage, forgetting those failing at the earlier isolation stage. Fix that by accumulating a long nr_failed count in struct queue_pages, returned by queue_pages_range() when it's not returning an error, for adding on to the nr_failed count from migrate_pages() in mm/migrate.c. A count of pages? It's more a count of folios, but changing it to pages would entail more work (also in mm/migrate.c): does not seem justified. queue_pages_range() itself should only return -EIO in the "strictly unmovable" case (STRICT without any MOVEs): in that case it's best to break out as soon as nr_failed gets set; but otherwise it should continue to isolate pages for MOVing even when nr_failed - as the mbind(2) manpage promises. This fixes mbind(MPOL_MF_STRICT|MPOL_MF_MOVE*) behavior left over from 5.3 and 5.4, and the recent syzbot need for vma_start_write() before mbind_range(): but both of those may be fixed by smaller patches. There's a case when nr_failed should be incremented when it was missed: queue_folios_pte_range() and queue_folios_hugetlb() count the transient migration entries, like queue_folios_pmd() already did. And there's a case when nr_failed should not be incremented when it would have been: in meeting later PTEs of the same large folio, which can only be isolated once: fixed by recording the current large folio in struct queue_pages. Clean up the affected functions, fixing or updating many comments. Bool migrate_folio_add(), without -EIO: true if adding, or if skipping shared (but its arguable folio_estimated_sharers() heuristic left unchanged). Use MPOL_MF_WRLOCK flag to queue_pages_range(), instead of bool lock_vma. Use explicit STRICT|MOVE* flags where queue_pages_test_walk() checks for skipping, instead of hiding them behind MPOL_MF_VALID. Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) --- mm/mempolicy.c | 322 +++++++++++++++++++++++-------------------------- 1 file changed, 149 insertions(+), 173 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 42b5567e3773..937386409c28 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -111,7 +111,8 @@ =20 /* Internal flags */ #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for co= ntinuous vmas */ -#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodema= sk */ +#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for n= odemask */ +#define MPOL_MF_WRLOCK (MPOL_MF_INTERNAL << 2) /* Write-lock walked = vmas */ =20 static struct kmem_cache *policy_cache; static struct kmem_cache *sn_cache; @@ -416,9 +417,19 @@ static const struct mempolicy_operations mpol_ops[MPOL= _MAX] =3D { }, }; =20 -static int migrate_folio_add(struct folio *folio, struct list_head *foliol= ist, +static bool migrate_folio_add(struct folio *folio, struct list_head *folio= list, unsigned long flags); =20 +static bool strictly_unmovable(unsigned long flags) +{ + /* + * STRICT without MOVE flags lets do_mbind() fail immediately with -EIO + * if any misplaced page is found. + */ + return (flags & (MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) =3D= =3D + MPOL_MF_STRICT; +} + struct queue_pages { struct list_head *pagelist; unsigned long flags; @@ -426,6 +437,8 @@ struct queue_pages { unsigned long start; unsigned long end; struct vm_area_struct *first; + struct folio *large; /* note last large folio on pagelist */ + long nr_failed; /* could not be isolated at this time */ }; =20 /* @@ -443,50 +456,27 @@ static inline bool queue_folio_required(struct folio = *folio, return node_isset(nid, *qp->nmask) =3D=3D !(flags & MPOL_MF_INVERT); } =20 -/* - * queue_folios_pmd() has three possible return values: - * 0 - folios are placed on the right node or queued successfully, or - * special page is met, i.e. huge zero page. - * 1 - there is unmovable folio, and MPOL_MF_MOVE* & MPOL_MF_STRICT were - * specified. - * -EIO - is migration entry or only MPOL_MF_STRICT was specified and an - * existing folio was already on a node that does not follow the - * policy. - */ -static int queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long add= r, +static void queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long ad= dr, unsigned long end, struct mm_walk *walk) - __releases(ptl) { - int ret =3D 0; struct folio *folio; struct queue_pages *qp =3D walk->private; - unsigned long flags; =20 if (unlikely(is_pmd_migration_entry(*pmd))) { - ret =3D -EIO; - goto unlock; + qp->nr_failed++; + return; } folio =3D pfn_folio(pmd_pfn(*pmd)); if (is_huge_zero_page(&folio->page)) { walk->action =3D ACTION_CONTINUE; - goto unlock; + return; } if (!queue_folio_required(folio, qp)) - goto unlock; - - flags =3D qp->flags; - /* go to folio migration */ - if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { - if (!vma_migratable(walk->vma) || - migrate_folio_add(folio, qp->pagelist, flags)) { - ret =3D 1; - goto unlock; - } - } else - ret =3D -EIO; -unlock: - spin_unlock(ptl); - return ret; + return; + if (!(qp->flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) || + !vma_migratable(walk->vma) || + !migrate_folio_add(folio, qp->pagelist, qp->flags)) + qp->nr_failed++; } =20 /* @@ -496,8 +486,6 @@ static int queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl= , unsigned long addr, * queue_folios_pte_range() has three possible return values: * 0 - folios are placed on the right node or queued successfully, or * special page is met, i.e. zero page. - * 1 - there is unmovable folio, and MPOL_MF_MOVE* & MPOL_MF_STRICT were - * specified. * -EIO - only MPOL_MF_STRICT was specified and an existing folio was alre= ady * on a node that does not follow the policy. */ @@ -508,14 +496,16 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigne= d long addr, struct folio *folio; struct queue_pages *qp =3D walk->private; unsigned long flags =3D qp->flags; - bool has_unmovable =3D false; pte_t *pte, *mapped_pte; pte_t ptent; spinlock_t *ptl; =20 ptl =3D pmd_trans_huge_lock(pmd, vma); - if (ptl) - return queue_folios_pmd(pmd, ptl, addr, end, walk); + if (ptl) { + queue_folios_pmd(pmd, ptl, addr, end, walk); + spin_unlock(ptl); + goto out; + } =20 mapped_pte =3D pte =3D pte_offset_map_lock(walk->mm, pmd, addr, &ptl); if (!pte) { @@ -524,8 +514,13 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned= long addr, } for (; addr !=3D end; pte++, addr +=3D PAGE_SIZE) { ptent =3D ptep_get(pte); - if (!pte_present(ptent)) + if (pte_none(ptent)) continue; + if (!pte_present(ptent)) { + if (is_migration_entry(pte_to_swp_entry(ptent))) + qp->nr_failed++; + continue; + } folio =3D vm_normal_folio(vma, addr, ptent); if (!folio || folio_is_zone_device(folio)) continue; @@ -537,97 +532,82 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigne= d long addr, continue; if (!queue_folio_required(folio, qp)) continue; - if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { - /* MPOL_MF_STRICT must be specified if we get here */ - if (!vma_migratable(vma)) { - has_unmovable =3D true; + if (!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) || + !vma_migratable(vma)) { + qp->nr_failed++; + if (strictly_unmovable(flags)) break; - } - + } + if (migrate_folio_add(folio, qp->pagelist, flags)) { /* - * Do not abort immediately since there may be - * temporary off LRU pages in the range. Still - * need migrate other LRU pages. + * A large folio can only be isolated from LRU once, + * but may be mapped by many PTEs (and Copy-On-Write may + * intersperse PTEs of other folios). This is a common + * case, so don't mistake it for failure (but of course + * there can be other cases of multi-mapped pages which + * this quick check does not help to filter out - and a + * search of the pagelist might grow to be prohibitive). */ - if (migrate_folio_add(folio, qp->pagelist, flags)) - has_unmovable =3D true; - } else - break; + if (folio_test_large(folio)) + qp->large =3D folio; + } else if (folio !=3D qp->large) { + qp->nr_failed++; + if (strictly_unmovable(flags)) + break; + } } pte_unmap_unlock(mapped_pte, ptl); cond_resched(); - - if (has_unmovable) - return 1; - - return addr !=3D end ? -EIO : 0; +out: + if (qp->nr_failed && strictly_unmovable(flags)) + return -EIO; + return 0; } =20 static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, unsigned long end, struct mm_walk *walk) { - int ret =3D 0; #ifdef CONFIG_HUGETLB_PAGE struct queue_pages *qp =3D walk->private; - unsigned long flags =3D (qp->flags & MPOL_MF_VALID); + unsigned long flags =3D qp->flags; struct folio *folio; spinlock_t *ptl; pte_t entry; =20 ptl =3D huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte); entry =3D huge_ptep_get(pte); - if (!pte_present(entry)) + if (!pte_present(entry)) { + if (unlikely(is_hugetlb_entry_migration(entry))) + qp->nr_failed++; goto unlock; + } folio =3D pfn_folio(pte_pfn(entry)); if (!queue_folio_required(folio, qp)) goto unlock; - - if (flags =3D=3D MPOL_MF_STRICT) { - /* - * STRICT alone means only detecting misplaced folio and no - * need to further check other vma. - */ - ret =3D -EIO; + if (!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) || + !vma_migratable(walk->vma)) { + qp->nr_failed++; goto unlock; } - - if (!vma_migratable(walk->vma)) { - /* - * Must be STRICT with MOVE*, otherwise .test_walk() have - * stopped walking current vma. - * Detecting misplaced folio but allow migrating folios which - * have been queued. - */ - ret =3D 1; - goto unlock; - } - /* - * With MPOL_MF_MOVE, we try to migrate only unshared folios. If it - * is shared it is likely not worth migrating. + * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. + * Choosing not to migrate a shared folio is not counted as a failure. * * To check if the folio is shared, ideally we want to make sure * every page is mapped to the same process. Doing that is very - * expensive, so check the estimated mapcount of the folio instead. + * expensive, so check the estimated sharers of the folio instead. */ - if (flags & (MPOL_MF_MOVE_ALL) || - (flags & MPOL_MF_MOVE && folio_estimated_sharers(folio) =3D=3D 1 && - !hugetlb_pmd_shared(pte))) { - if (!isolate_hugetlb(folio, qp->pagelist) && - (flags & MPOL_MF_STRICT)) - /* - * Failed to isolate folio but allow migrating pages - * which have been queued. - */ - ret =3D 1; - } + if ((flags & MPOL_MF_MOVE_ALL) || + (folio_estimated_sharers(folio) =3D=3D 1 && !hugetlb_pmd_shared(pte))) + if (!isolate_hugetlb(folio, qp->pagelist)) + qp->nr_failed++; unlock: spin_unlock(ptl); -#else - BUG(); + if (qp->nr_failed && strictly_unmovable(flags)) + return -EIO; #endif - return ret; + return 0; } =20 #ifdef CONFIG_NUMA_BALANCING @@ -708,8 +688,11 @@ static int queue_pages_test_walk(unsigned long start, = unsigned long end, return 1; } =20 - /* queue pages from current vma */ - if (flags & MPOL_MF_VALID) + /* + * Check page nodes, and queue pages to move, in the current vma. + * But if no moving, and no strict checking, the scan can be skipped. + */ + if (flags & (MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) return 0; return 1; } @@ -731,22 +714,22 @@ static const struct mm_walk_ops queue_pages_lock_vma_= walk_ops =3D { /* * Walk through page tables and collect pages to be migrated. * - * If pages found in a given range are on a set of nodes (determined by - * @nodes and @flags,) it's isolated and queued to the pagelist which is - * passed via @private. + * If pages found in a given range are not on the required set of @nodes, + * and migration is allowed, they are isolated and queued to the pagelist + * which is passed via @private. * - * queue_pages_range() has three possible return values: - * 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were - * specified. - * 0 - queue pages successfully or no misplaced page. - * errno - i.e. misplaced pages with MPOL_MF_STRICT specified (-EIO) or - * memory range specified by nodemask and maxnode points outside - * your accessible address space (-EFAULT) + * queue_pages_range() may return: + * 0 - all pages already on the right node, or successfully queued for mov= ing + * (or neither strict checking nor moving requested: only range checki= ng). + * >0 - this number of misplaced folios could not be queued for moving + * (a hugetlbfs page or a transparent huge page being counted as 1). + * -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MO= VEs. + * -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspeci= fied. */ -static int +static long queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long= end, nodemask_t *nodes, unsigned long flags, - struct list_head *pagelist, bool lock_vma) + struct list_head *pagelist) { int err; struct queue_pages qp =3D { @@ -757,7 +740,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long s= tart, unsigned long end, .end =3D end, .first =3D NULL, }; - const struct mm_walk_ops *ops =3D lock_vma ? + const struct mm_walk_ops *ops =3D (flags & MPOL_MF_WRLOCK) ? &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops; =20 err =3D walk_page_range(mm, start, end, ops, &qp); @@ -766,7 +749,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long s= tart, unsigned long end, /* whole range in hole */ err =3D -EFAULT; =20 - return err; + return err ? : qp.nr_failed; } =20 /* @@ -1029,16 +1012,16 @@ static long do_get_mempolicy(int *policy, nodemask_= t *nmask, } =20 #ifdef CONFIG_MIGRATION -static int migrate_folio_add(struct folio *folio, struct list_head *foliol= ist, +static bool migrate_folio_add(struct folio *folio, struct list_head *folio= list, unsigned long flags) { /* - * We try to migrate only unshared folios. If it is shared it - * is likely not worth migrating. + * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. + * Choosing not to migrate a shared folio is not counted as a failure. * * To check if the folio is shared, ideally we want to make sure * every page is mapped to the same process. Doing that is very - * expensive, so check the estimated mapcount of the folio instead. + * expensive, so check the estimated sharers of the folio instead. */ if ((flags & MPOL_MF_MOVE_ALL) || folio_estimated_sharers(folio) =3D=3D 1= ) { if (folio_isolate_lru(folio)) { @@ -1046,32 +1029,31 @@ static int migrate_folio_add(struct folio *folio, s= truct list_head *foliolist, node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); - } else if (flags & MPOL_MF_STRICT) { + } else { /* * Non-movable folio may reach here. And, there may be * temporary off LRU folios or non-LRU movable folios. * Treat them as unmovable folios since they can't be - * isolated, so they can't be moved at the moment. It - * should return -EIO for this case too. + * isolated, so they can't be moved at the moment. */ - return -EIO; + return false; } } - - return 0; + return true; } =20 /* * Migrate pages from one node to a target node. * Returns error or the number of pages not migrated. */ -static int migrate_to_node(struct mm_struct *mm, int source, int dest, - int flags) +static long migrate_to_node(struct mm_struct *mm, int source, int dest, + int flags) { nodemask_t nmask; struct vm_area_struct *vma; LIST_HEAD(pagelist); - int err =3D 0; + long nr_failed; + long err =3D 0; struct migration_target_control mtc =3D { .nid =3D dest, .gfp_mask =3D GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, @@ -1080,23 +1062,27 @@ static int migrate_to_node(struct mm_struct *mm, in= t source, int dest, nodes_clear(nmask); node_set(source, nmask); =20 - /* - * This does not "check" the range but isolates all pages that - * need migration. Between passing in the full user address - * space range and MPOL_MF_DISCONTIG_OK, this call can not fail. - */ - vma =3D find_vma(mm, 0); VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))); - queue_pages_range(mm, vma->vm_start, mm->task_size, &nmask, - flags | MPOL_MF_DISCONTIG_OK, &pagelist, false); + vma =3D find_vma(mm, 0); + + /* + * This does not migrate the range, but isolates all pages that + * need migration. Between passing in the full user address + * space range and MPOL_MF_DISCONTIG_OK, this call cannot fail, + * but passes back the count of pages which could not be isolated. + */ + nr_failed =3D queue_pages_range(mm, vma->vm_start, mm->task_size, &nmask, + flags | MPOL_MF_DISCONTIG_OK, &pagelist); =20 if (!list_empty(&pagelist)) { err =3D migrate_pages(&pagelist, alloc_migration_target, NULL, - (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL); + (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL); if (err) putback_movable_pages(&pagelist); } =20 + if (err >=3D 0) + err +=3D nr_failed; return err; } =20 @@ -1109,8 +1095,8 @@ static int migrate_to_node(struct mm_struct *mm, int = source, int dest, int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, const nodemask_t *to, int flags) { - int busy =3D 0; - int err =3D 0; + long nr_failed =3D 0; + long err =3D 0; nodemask_t tmp; =20 lru_cache_disable(); @@ -1192,7 +1178,7 @@ int do_migrate_pages(struct mm_struct *mm, const node= mask_t *from, node_clear(source, tmp); err =3D migrate_to_node(mm, source, dest, flags); if (err > 0) - busy +=3D err; + nr_failed +=3D err; if (err < 0) break; } @@ -1201,8 +1187,7 @@ int do_migrate_pages(struct mm_struct *mm, const node= mask_t *from, lru_cache_enable(); if (err < 0) return err; - return busy; - + return (nr_failed < INT_MAX) ? nr_failed : INT_MAX; } =20 /* @@ -1241,10 +1226,10 @@ static struct folio *new_folio(struct folio *src, u= nsigned long start) } #else =20 -static int migrate_folio_add(struct folio *folio, struct list_head *foliol= ist, +static bool migrate_folio_add(struct folio *folio, struct list_head *folio= list, unsigned long flags) { - return -EIO; + return false; } =20 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, @@ -1268,8 +1253,8 @@ static long do_mbind(unsigned long start, unsigned lo= ng len, struct vma_iterator vmi; struct mempolicy *new; unsigned long end; - int err; - int ret; + long err; + long nr_failed; LIST_HEAD(pagelist); =20 if (flags & ~(unsigned long)MPOL_MF_VALID) @@ -1309,10 +1294,8 @@ static long do_mbind(unsigned long start, unsigned l= ong len, start, start + len, mode, mode_flags, nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE); =20 - if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { - + if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) lru_cache_disable(); - } { NODEMASK_SCRATCH(scratch); if (scratch) { @@ -1328,44 +1311,37 @@ static long do_mbind(unsigned long start, unsigned = long len, goto mpol_out; =20 /* - * Lock the VMAs before scanning for pages to migrate, to ensure we don't - * miss a concurrently inserted page. + * Lock the VMAs before scanning for pages to migrate, + * to ensure we don't miss a concurrently inserted page. */ - ret =3D queue_pages_range(mm, start, end, nmask, - flags | MPOL_MF_INVERT, &pagelist, true); + nr_failed =3D queue_pages_range(mm, start, end, nmask, + flags | MPOL_MF_INVERT | MPOL_MF_WRLOCK, &pagelist); =20 - if (ret < 0) { - err =3D ret; - goto up_out; - } - - vma_iter_init(&vmi, mm, start); - prev =3D vma_prev(&vmi); - for_each_vma_range(vmi, vma, end) { - err =3D mbind_range(&vmi, vma, &prev, start, end, new); - if (err) - break; + if (nr_failed < 0) { + err =3D nr_failed; + } else { + vma_iter_init(&vmi, mm, start); + prev =3D vma_prev(&vmi); + for_each_vma_range(vmi, vma, end) { + err =3D mbind_range(&vmi, vma, &prev, start, end, new); + if (err) + break; + } } =20 if (!err) { - int nr_failed =3D 0; - if (!list_empty(&pagelist)) { WARN_ON_ONCE(flags & MPOL_MF_LAZY); - nr_failed =3D migrate_pages(&pagelist, new_folio, NULL, + nr_failed |=3D migrate_pages(&pagelist, new_folio, NULL, start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND, NULL); - if (nr_failed) - putback_movable_pages(&pagelist); } - - if ((ret > 0) || (nr_failed && (flags & MPOL_MF_STRICT))) + if (nr_failed && (flags & MPOL_MF_STRICT)) err =3D -EIO; - } else { -up_out: - if (!list_empty(&pagelist)) - putback_movable_pages(&pagelist); } =20 + if (!list_empty(&pagelist)) + putback_movable_pages(&pagelist); + mmap_write_unlock(mm); mpol_out: mpol_put(new); --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 498BBCE7A81 for ; Mon, 25 Sep 2023 08:25:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232748AbjIYIZW (ORCPT ); Mon, 25 Sep 2023 04:25:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230250AbjIYIZT (ORCPT ); Mon, 25 Sep 2023 04:25:19 -0400 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 916DFA9 for ; Mon, 25 Sep 2023 01:25:13 -0700 (PDT) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-59c04237bf2so74011117b3.0 for ; Mon, 25 Sep 2023 01:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630313; x=1696235113; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=hroENP4HhihHjQySGJW5cTAKXjm0pfkryRKjlEMKAf0=; b=YtWrHOkK9mbiVfF5REEHwqN7GeumN0YCqkrxKrJJAFM2M65GeFAYmvVlFPZ2xKKzZ2 8DN2KwgjNlUXLAJeAT1RYEfeGrbvyXvOj3HaXSen4DF8gfOeEo4oLTjxMkcOwMa/6LNN PilrshLtQKPmOrE10wk0jAGdgy36p3i9KUcR5ZVy9dCPW7L8uYFGPyKN7E6Ak4GMBDJK b8HPbGdZ0lX0pvQxs4UiQMjqFplI7g6TzD2kLz9abLR4APqFZ273fSH2ADiqYh/z3eGI 4T7QofA3JqverA+GxOWILjr3/N5mtrKT3blYEtGijJAFwWSBll7qjS86xxOj5kxo9pDa fp5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630313; x=1696235113; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hroENP4HhihHjQySGJW5cTAKXjm0pfkryRKjlEMKAf0=; b=QN+nloGH2RnPPC69O2FKlKQAikQ87NM03vuc9iptqXvJMWq/GxDPMfJkfSak9hToE1 Ql4T1vQreWMwrlkhYv+Awk9juRz4G6KipJBbrfL2lNVtXz2S2FO/5P48oygm6nMQB1Bh LoAByGnA5z0+5fJWJRu8G0DJyQ/0/djBPw4rhi0jgzHgLAzcMxpk59mF43iffbReF1cg ypbZQ75nCnuVFtYbKbIbmLzToJAt9hH96XidhE8cHslRvcd4/d2AMZuF9i5riOLGvTn4 WmA7RJaucP2xsUux4+nib5KCLqnsOtvtN4/BcpCxlJbYvK0ktWPVIoRKjf84Sv6DOb9H YE0A== X-Gm-Message-State: AOJu0Yy8QOs6ttURTfH2xUjAhnNS/vTd8h1w+BaTybxZUGjmKuku4var nKfQO/SPsMYmRZfW4ScFxVFkIA== X-Google-Smtp-Source: AGHT+IGswZO2h3vhIcg/xM5XdhydTtdSAqCgFDwsheA7+YxiMWnOmcWx3WkqpsQDKBWY2Pxy5ekbXw== X-Received: by 2002:a05:690c:3603:b0:571:11ea:b2dd with SMTP id ft3-20020a05690c360300b0057111eab2ddmr5712836ywb.32.1695630312698; Mon, 25 Sep 2023 01:25:12 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id x2-20020a818702000000b005869ca8da8esm2265401ywf.146.2023.09.25.01.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:25:11 -0700 (PDT) Date: Mon, 25 Sep 2023 01:25:09 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 04/12] mempolicy trivia: delete those ancient pr_debug()s In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Delete those ancient pr_debug()s - PDprintk()s in Andi Kleen's original submission of core NUMA API, and useful when debugging shared mempolicy lifetime back then, but not used recently. Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) --- mm/mempolicy.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 937386409c28..b2573921b78f 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -264,9 +264,6 @@ static struct mempolicy *mpol_new(unsigned short mode, = unsigned short flags, { struct mempolicy *policy; =20 - pr_debug("setting mode %d flags %d nodes[0] %lx\n", - mode, flags, nodes ? nodes_addr(*nodes)[0] : NUMA_NO_NODE); - if (mode =3D=3D MPOL_DEFAULT) { if (nodes && !nodes_empty(*nodes)) return ERR_PTR(-EINVAL); @@ -765,11 +762,6 @@ static int vma_replace_policy(struct vm_area_struct *v= ma, =20 vma_assert_write_locked(vma); =20 - pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n", - vma->vm_start, vma->vm_end, vma->vm_pgoff, - vma->vm_ops, vma->vm_file, - vma->vm_ops ? vma->vm_ops->set_policy : NULL); - new =3D mpol_dup(pol); if (IS_ERR(new)) return PTR_ERR(new); @@ -1290,10 +1282,6 @@ static long do_mbind(unsigned long start, unsigned l= ong len, if (!new) flags |=3D MPOL_MF_DISCONTIG_OK; =20 - pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n", - start, start + len, mode, mode_flags, - nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE); - if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) lru_cache_disable(); { @@ -2511,8 +2499,6 @@ static void sp_insert(struct shared_policy *sp, struc= t sp_node *new) } rb_link_node(&new->nd, parent, p); rb_insert_color(&new->nd, &sp->root); - pr_debug("inserting %lx-%lx: %d\n", new->start, new->end, - new->policy ? new->policy->mode : 0); } =20 /* Find shared policy intersecting idx */ @@ -2649,7 +2635,6 @@ void mpol_put_task_policy(struct task_struct *task) =20 static void sp_delete(struct shared_policy *sp, struct sp_node *n) { - pr_debug("deleting %lx-l%lx\n", n->start, n->end); rb_erase(&n->nd, &sp->root); sp_free(n); } @@ -2806,12 +2791,6 @@ int mpol_set_shared_policy(struct shared_policy *inf= o, struct sp_node *new =3D NULL; unsigned long sz =3D vma_pages(vma); =20 - pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n", - vma->vm_pgoff, - sz, npol ? npol->mode : -1, - npol ? npol->flags : -1, - npol ? nodes_addr(npol->nodes)[0] : NUMA_NO_NODE); - if (npol) { new =3D sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); if (!new) --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5511ECE7A89 for ; Mon, 25 Sep 2023 08:27:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232782AbjIYI1J (ORCPT ); Mon, 25 Sep 2023 04:27:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230250AbjIYI1H (ORCPT ); Mon, 25 Sep 2023 04:27:07 -0400 Received: from mail-yw1-x112e.google.com (mail-yw1-x112e.google.com [IPv6:2607:f8b0:4864:20::112e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A14AEAF for ; Mon, 25 Sep 2023 01:26:59 -0700 (PDT) Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-59f82ad1e09so9638817b3.0 for ; Mon, 25 Sep 2023 01:26:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630419; x=1696235219; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=u9hKZK3ZUeqzyIltnhSgdeSnOl5bDTv2bcCM7fdUw7E=; b=HkeirUvjFwJGXDEmwdRbUk53bB7GBoQnjvyWnJ4rKnqyRJB9/Vkb9ljPB/+vhY7BC5 AuytmwhRzVF8+zRSEYeZUQHilOmkqQCLelByC3icCybNodCt4BHe/xUKdx/hKG+azzbN n+/7uzKGjI5cJ5aZJX/cvXBkbgxAZIBvNvL9BDYxHIZvZk8vMVN5mJIqOwAjqIoISH4X QCuV3fsFaMFDi/Isqf+rKHms2Sj40tZRS8ENhjTqoKo5NH2THMojN+tkyppHgpHTo76t KsP2iv7FyNEPi9n2Vttvz/X/QUHWueOD5iao5JaW6XfqFEhdD3NADjB+Yh08lYcKut/X BGzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630419; x=1696235219; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u9hKZK3ZUeqzyIltnhSgdeSnOl5bDTv2bcCM7fdUw7E=; b=j4rCccS9i44B6NSA7nndfWlnPIsycN9DP9l6ELYlZYXjG8pD75u44O2Yjsm/FfqaXL n9IGfn5D2xyg37dtOJuqJIV69SrutUAku7vWA9vACioeCudzCYr2B53F1SY2Bp8Gk69r FRi7QzmBnuxJ3FgMtzb0dhfZjN+N3ej6Dz9bS4myfpGabTCtKMR0Z7SoxmMBz3QQ4Esy hnsLtLanzzBdfaCDr9sANcNh7D5VQEgsbz34IlgCAUzB1Z3jXe6cEQVXBjnBpS9tk6Dk XfjByb9Rdo/t8bp6bwFNXMIO+Li5rUPzunSvaE3rysyCY0j17L1J2Qbi1qmQo3S4ZoPY 5UWw== X-Gm-Message-State: AOJu0Yz7ilWX0fm4SmDGrEXzh4C/pWBeHWQBJ4uwKGYw8mVkeeupRUAL l+oorCOGogv7n8UZD9KuvkGWfg== X-Google-Smtp-Source: AGHT+IE5M5EgLVxUkBpaGKUdLKFQAyUDWZdjcFC3dAQ5VlTjGCz9UOlXNzgENK6A+AZ+A9Q73Se6+g== X-Received: by 2002:a0d:ef43:0:b0:57a:f72:ebf8 with SMTP id y64-20020a0def43000000b0057a0f72ebf8mr6180318ywe.28.1695630418681; Mon, 25 Sep 2023 01:26:58 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id w129-20020a817b87000000b00589e68edac6sm2307982ywc.39.2023.09.25.01.26.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:26:57 -0700 (PDT) Date: Mon, 25 Sep 2023 01:26:55 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 05/12] mempolicy trivia: slightly more consistent naming In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: <1a75d3dd-7fa-7a41-c76b-1232198a9a4a@google.com> References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Before getting down to work, do a little cleanup, mainly of inconsistent variable naming. I gave up trying to rationalize mpol versus pol versus policy, and node versus nid, but let's avoid p and nd. Remove a few superfluous blank lines, but add one; and here prefer vma->vm_policy to vma_policy(vma) - the latter being appropriate in other sources, which have to allow for !CONFIG_NUMA. That intriguing line about KERNEL_DS? should have gone in v2.6.15, when numa_policy_init() stopped using set_mempolicy(2)'s system call handler. Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) --- include/linux/mempolicy.h | 11 +++--- mm/mempolicy.c | 73 ++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 46 deletions(-) diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index d232de7cdc56..8013d716dc46 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -126,10 +126,9 @@ struct shared_policy { =20 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst); void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *m= pol); -int mpol_set_shared_policy(struct shared_policy *info, - struct vm_area_struct *vma, - struct mempolicy *new); -void mpol_free_shared_policy(struct shared_policy *p); +int mpol_set_shared_policy(struct shared_policy *sp, + struct vm_area_struct *vma, struct mempolicy *mpol); +void mpol_free_shared_policy(struct shared_policy *sp); struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx); =20 @@ -193,7 +192,7 @@ static inline bool mpol_equal(struct mempolicy *a, stru= ct mempolicy *b) return true; } =20 -static inline void mpol_put(struct mempolicy *p) +static inline void mpol_put(struct mempolicy *pol) { } =20 @@ -212,7 +211,7 @@ static inline void mpol_shared_policy_init(struct share= d_policy *sp, { } =20 -static inline void mpol_free_shared_policy(struct shared_policy *p) +static inline void mpol_free_shared_policy(struct shared_policy *sp) { } =20 diff --git a/mm/mempolicy.c b/mm/mempolicy.c index b2573921b78f..121bb490481b 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -25,7 +25,7 @@ * to the last. It would be better if bind would truly rest= rict * the allocation to memory nodes instead * - * preferred Try a specific node first before normal fallback. + * preferred Try a specific node first before normal fallback. * As a special case NUMA_NO_NODE here means do the allocat= ion * on the local CPU. This is normally identical to default, * but useful to set in a VMA when you have a non default @@ -52,7 +52,7 @@ * on systems with highmem kernel lowmem allocation don't get policied. * Same with GFP_DMA allocations. * - * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between + * For shmem/tmpfs shared memory the policy is shared between * all users and remembered even when nobody has memory mapped. */ =20 @@ -291,6 +291,7 @@ static struct mempolicy *mpol_new(unsigned short mode, = unsigned short flags, return ERR_PTR(-EINVAL); } else if (nodes_empty(*nodes)) return ERR_PTR(-EINVAL); + policy =3D kmem_cache_alloc(policy_cache, GFP_KERNEL); if (!policy) return ERR_PTR(-ENOMEM); @@ -303,11 +304,11 @@ static struct mempolicy *mpol_new(unsigned short mode= , unsigned short flags, } =20 /* Slow path of a mpol destructor. */ -void __mpol_put(struct mempolicy *p) +void __mpol_put(struct mempolicy *pol) { - if (!atomic_dec_and_test(&p->refcnt)) + if (!atomic_dec_and_test(&pol->refcnt)) return; - kmem_cache_free(policy_cache, p); + kmem_cache_free(policy_cache, pol); } =20 static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *n= odes) @@ -364,7 +365,6 @@ static void mpol_rebind_policy(struct mempolicy *pol, c= onst nodemask_t *newmask) * * Called with task's alloc_lock held. */ - void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new) { mpol_rebind_policy(tsk->mempolicy, new); @@ -375,7 +375,6 @@ void mpol_rebind_task(struct task_struct *tsk, const no= demask_t *new) * * Call holding a reference to mm. Takes mm->mmap_lock during call. */ - void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new) { struct vm_area_struct *vma; @@ -754,7 +753,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long s= tart, unsigned long end, * This must be called with the mmap_lock held for writing. */ static int vma_replace_policy(struct vm_area_struct *vma, - struct mempolicy *pol) + struct mempolicy *pol) { int err; struct mempolicy *old; @@ -800,7 +799,7 @@ static int mbind_range(struct vma_iterator *vmi, struct= vm_area_struct *vma, vmstart =3D vma->vm_start; } =20 - if (mpol_equal(vma_policy(vma), new_pol)) { + if (mpol_equal(vma->vm_policy, new_pol)) { *prev =3D vma; return 0; } @@ -872,18 +871,18 @@ static long do_set_mempolicy(unsigned short mode, uns= igned short flags, * * Called with task's alloc_lock held */ -static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes) +static void get_policy_nodemask(struct mempolicy *pol, nodemask_t *nodes) { nodes_clear(*nodes); - if (p =3D=3D &default_policy) + if (pol =3D=3D &default_policy) return; =20 - switch (p->mode) { + switch (pol->mode) { case MPOL_BIND: case MPOL_INTERLEAVE: case MPOL_PREFERRED: case MPOL_PREFERRED_MANY: - *nodes =3D p->nodes; + *nodes =3D pol->nodes; break; case MPOL_LOCAL: /* return empty node mask for local allocation */ @@ -1649,7 +1648,6 @@ static int kernel_migrate_pages(pid_t pid, unsigned l= ong maxnode, out_put: put_task_struct(task); goto out; - } =20 SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode, @@ -1659,7 +1657,6 @@ SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned l= ong, maxnode, return kernel_migrate_pages(pid, maxnode, old_nodes, new_nodes); } =20 - /* Retrieve NUMA policy */ static int kernel_get_mempolicy(int __user *policy, unsigned long __user *nmask, @@ -1842,10 +1839,10 @@ nodemask_t *policy_nodemask(gfp_t gfp, struct mempo= licy *policy) * policy_node() is always coupled with policy_nodemask(), which * secures the nodemask limit for 'bind' and 'prefer-many' policy. */ -static int policy_node(gfp_t gfp, struct mempolicy *policy, int nd) +static int policy_node(gfp_t gfp, struct mempolicy *policy, int nid) { if (policy->mode =3D=3D MPOL_PREFERRED) { - nd =3D first_node(policy->nodes); + nid =3D first_node(policy->nodes); } else { /* * __GFP_THISNODE shouldn't even be used with the bind policy @@ -1860,19 +1857,18 @@ static int policy_node(gfp_t gfp, struct mempolicy = *policy, int nd) policy->home_node !=3D NUMA_NO_NODE) return policy->home_node; =20 - return nd; + return nid; } =20 /* Do dynamic interleaving for a process */ -static unsigned interleave_nodes(struct mempolicy *policy) +static unsigned int interleave_nodes(struct mempolicy *policy) { - unsigned next; - struct task_struct *me =3D current; + unsigned int nid; =20 - next =3D next_node_in(me->il_prev, policy->nodes); - if (next < MAX_NUMNODES) - me->il_prev =3D next; - return next; + nid =3D next_node_in(current->il_prev, policy->nodes); + if (nid < MAX_NUMNODES) + current->il_prev =3D nid; + return nid; } =20 /* @@ -2362,7 +2358,7 @@ unsigned long alloc_pages_bulk_array_mempolicy(gfp_t = gfp, =20 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst) { - struct mempolicy *pol =3D mpol_dup(vma_policy(src)); + struct mempolicy *pol =3D mpol_dup(src->vm_policy); =20 if (IS_ERR(pol)) return PTR_ERR(pol); @@ -2784,40 +2780,40 @@ void mpol_shared_policy_init(struct shared_policy *= sp, struct mempolicy *mpol) } } =20 -int mpol_set_shared_policy(struct shared_policy *info, - struct vm_area_struct *vma, struct mempolicy *npol) +int mpol_set_shared_policy(struct shared_policy *sp, + struct vm_area_struct *vma, struct mempolicy *pol) { int err; struct sp_node *new =3D NULL; unsigned long sz =3D vma_pages(vma); =20 - if (npol) { - new =3D sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); + if (pol) { + new =3D sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, pol); if (!new) return -ENOMEM; } - err =3D shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); + err =3D shared_policy_replace(sp, vma->vm_pgoff, vma->vm_pgoff + sz, new); if (err && new) sp_free(new); return err; } =20 /* Free a backing policy store on inode delete. */ -void mpol_free_shared_policy(struct shared_policy *p) +void mpol_free_shared_policy(struct shared_policy *sp) { struct sp_node *n; struct rb_node *next; =20 - if (!p->root.rb_node) + if (!sp->root.rb_node) return; - write_lock(&p->lock); - next =3D rb_first(&p->root); + write_lock(&sp->lock); + next =3D rb_first(&sp->root); while (next) { n =3D rb_entry(next, struct sp_node, nd); next =3D rb_next(&n->nd); - sp_delete(p, n); + sp_delete(sp, n); } - write_unlock(&p->lock); + write_unlock(&sp->lock); } =20 #ifdef CONFIG_NUMA_BALANCING @@ -2867,7 +2863,6 @@ static inline void __init check_numabalancing_enable(= void) } #endif /* CONFIG_NUMA_BALANCING */ =20 -/* assumes fs =3D=3D KERNEL_DS */ void __init numa_policy_init(void) { nodemask_t interleave_nodes; @@ -2930,7 +2925,6 @@ void numa_default_policy(void) /* * Parse and format mempolicy from/to strings */ - static const char * const policy_modes[] =3D { [MPOL_DEFAULT] =3D "default", @@ -2941,7 +2935,6 @@ static const char * const policy_modes[] =3D [MPOL_PREFERRED_MANY] =3D "prefer (many)", }; =20 - #ifdef CONFIG_TMPFS /** * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option. --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0A2ACE7A89 for ; Mon, 25 Sep 2023 08:28:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232795AbjIYI20 (ORCPT ); Mon, 25 Sep 2023 04:28:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230250AbjIYI2Y (ORCPT ); Mon, 25 Sep 2023 04:28:24 -0400 Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59CA9D3 for ; Mon, 25 Sep 2023 01:28:18 -0700 (PDT) Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-59c215f2f4aso73611007b3.1 for ; Mon, 25 Sep 2023 01:28:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630497; x=1696235297; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=ny+Jruw5kljLWGVDgFi/c0BN4bXVfRfudV3E3SL5ba0=; b=YJehBPjLRrzsJRRRxYwf5k2fr3z5ZmoQcsUDS7Yt/XX6Q5ZsOznEI/tgaNd1g3+X0r RUllEWlxZlWzYbS/SqaaCAP/7qpaV11Kc65h9obAOZqv/mtmVXQ6FbEMRs2gctaucrLA C11XFIPvhAUpqJ4ad3s17FV1mZSc2nGKYIVVdn4/WHmqRWTcE6+k308S3dlTQDmPf8HV LLzJqT21h6vZnwmBpyIvG2wTIppvcdXQIXav6xAcxd4oEZ8Hh60UAQg8mBWrIVmFGgBG Fl1SGnSEUvnJWHtY0mT07kZyF7xb85p9/JAGNT+o1144rjy9VA/DsI43fOL+AxC03Mxy eqPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630497; x=1696235297; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ny+Jruw5kljLWGVDgFi/c0BN4bXVfRfudV3E3SL5ba0=; b=Wv6snQhmxruoKABYf4NilPhirw7I300GbUpv8BtuvLvAmtiR5a0RIY6mCmu27jS0J3 tgOO0h2oxMSbHil5KHuLrj6R1/DmxpsOKWMkqPikWzwnCC0mk7pNo16tzpPzFvH/svXL tdZWionY5zyfuqGP/1avyso35+lAV1RvbUb6nOi0IbNry3qdaLaBP+48CNRxau3gdDau 5bgKJ5Q+H351r0tbRtm2TZM06IxtzNQQgRc928tsdu+XYuxz4BgPTsRUblnQOUy0Yh+w r80zYEIgw/svH2hIE6jdwrDS/+KQBSVu9UCCeqSpEszu3BqQRvPU7C52mI8wU/xeNFA6 8LAQ== X-Gm-Message-State: AOJu0Yy7/mopuarUWw3oMlmrWC4s/DUKue1r80D1dc5gJwlHZnA5dFvC 7NlEgwW2Yb1JtfY0DsPIUM0TOw== X-Google-Smtp-Source: AGHT+IH7TByv+2j1g6DH4DUwI+5DujRbEJ2kgtwKlcyesipljkA02KuVspk813qm/mayffaiCSX9vQ== X-Received: by 2002:a0d:f2c7:0:b0:599:b59f:5280 with SMTP id b190-20020a0df2c7000000b00599b59f5280mr5636415ywf.28.1695630497424; Mon, 25 Sep 2023 01:28:17 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id x187-20020a81a0c4000000b0058c55d40765sm2270742ywg.106.2023.09.25.01.28.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:28:16 -0700 (PDT) Date: Mon, 25 Sep 2023 01:28:14 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 06/12] mempolicy trivia: use pgoff_t in shared mempolicy tree In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Prefer the more explicit "pgoff_t" to "unsigned long" when dealing with a shared mempolicy tree. Delete confusing comment about pseudo mm vmas. Signed-off-by: Hugh Dickins --- include/linux/mempolicy.h | 12 +++--------- mm/mempolicy.c | 8 ++++---- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index 8013d716dc46..12f7dc74a457 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -107,18 +107,12 @@ static inline bool mpol_equal(struct mempolicy *a, st= ruct mempolicy *b) =20 /* * Tree of shared policies for a shared memory region. - * Maintain the policies in a pseudo mm that contains vmas. The vmas - * carry the policy. As a special twist the pseudo mm is indexed in pages,= not - * bytes, so that we can work with shared memory segments bigger than - * unsigned long. */ - struct sp_node { struct rb_node nd; - unsigned long start, end; + pgoff_t start, end; struct mempolicy *policy; }; - struct shared_policy { struct rb_root root; rwlock_t lock; @@ -130,7 +124,7 @@ int mpol_set_shared_policy(struct shared_policy *sp, struct vm_area_struct *vma, struct mempolicy *mpol); void mpol_free_shared_policy(struct shared_policy *sp); struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp, - unsigned long idx); + pgoff_t idx); =20 struct mempolicy *get_task_policy(struct task_struct *p); struct mempolicy *__get_vma_policy(struct vm_area_struct *vma, @@ -216,7 +210,7 @@ static inline void mpol_free_shared_policy(struct share= d_policy *sp) } =20 static inline struct mempolicy * -mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) +mpol_shared_policy_lookup(struct shared_policy *sp, pgoff_t idx) { return NULL; } diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 121bb490481b..065e886ec9b6 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2444,7 +2444,7 @@ bool __mpol_equal(struct mempolicy *a, struct mempoli= cy *b) * reading or for writing */ static struct sp_node * -sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end) +sp_lookup(struct shared_policy *sp, pgoff_t start, pgoff_t end) { struct rb_node *n =3D sp->root.rb_node; =20 @@ -2499,7 +2499,7 @@ static void sp_insert(struct shared_policy *sp, struc= t sp_node *new) =20 /* Find shared policy intersecting idx */ struct mempolicy * -mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) +mpol_shared_policy_lookup(struct shared_policy *sp, pgoff_t idx) { struct mempolicy *pol =3D NULL; struct sp_node *sn; @@ -2665,8 +2665,8 @@ static struct sp_node *sp_alloc(unsigned long start, = unsigned long end, } =20 /* Replace a policy range. */ -static int shared_policy_replace(struct shared_policy *sp, unsigned long s= tart, - unsigned long end, struct sp_node *new) +static int shared_policy_replace(struct shared_policy *sp, pgoff_t start, + pgoff_t end, struct sp_node *new) { struct sp_node *n; struct sp_node *n_new =3D NULL; --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BE47CE7A89 for ; Mon, 25 Sep 2023 08:29:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230250AbjIYI3t (ORCPT ); Mon, 25 Sep 2023 04:29:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232664AbjIYI3j (ORCPT ); Mon, 25 Sep 2023 04:29:39 -0400 Received: from mail-yw1-x112f.google.com (mail-yw1-x112f.google.com [IPv6:2607:f8b0:4864:20::112f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAA26FB for ; Mon, 25 Sep 2023 01:29:32 -0700 (PDT) Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-59f4db9e11eso39059217b3.0 for ; Mon, 25 Sep 2023 01:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630572; x=1696235372; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=HPVWJplZZXmT6NBbA8O4nkrJWg/FlNHzJ5g3rgEWw70=; b=O2Ihfwy7PRFZcDJNk8KgiiDn23MAFkJayE6WB6xaZiWlyrOnz3aPQK2SwjfZER7cRl fy8oGoOIXtFfMBvbY65yPz5DVtDL7BMeyINTAjPvWpxP1GpLnIlSlehni9/nrP+ZIP75 8d9R437dkJVc4A2aHkDB7J0w+SBuSsmrohhiMFCKxpJBAjwPs8A51zlwqx+CBZUZZl3C NTlpp1vcWbSAvxmOlNyX+NOk/+yn2K9uel1dJDY56Nc3R2nYTCSq1WfzYz7e1HpaChcH cqy64aY+nHlQY5bhuX6BuMCYV3icOVwRerjTRkF0sxd6CXNJVliCxXsDotHYAfFa7Bmc 9IUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630572; x=1696235372; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HPVWJplZZXmT6NBbA8O4nkrJWg/FlNHzJ5g3rgEWw70=; b=u+hMDWK1A31y1kVxpYDyqFiTJGSP0SYxnhNbuIXFMOiIukZZcpriiXYIp6/HOzkZWJ sQVJ2kPE8x15ZJAHCd5S+9wwq+tF/cDf23mxNF99vVvXtxK9eQw4Y0dZxyiHcU7LxdJa 6B5Mc1G30vBof8PURIIz+bjxTKGgzj552oQvBw5s0q13+yWLC5MVO51InwvlD+4Em96s r+dAUXnvB0TR6CpVJ3R+oVGQNgAFvuAjwu6OT7DWSJJjubSxVpqFPFNRZX2iaxW4WkSg FRwobXrbC7APqgr+AKAAylGeEzgERHJyaKoPOXHvGDtVI7+pQf2RsN6RJrSJBYTwhqwb +v2A== X-Gm-Message-State: AOJu0YzRnRC3uC3Xva6HxETDlWbjKcYzqzo55HeUNDBKRuJZm8EKg9kz 9/hiDBKVFlhkqfjOWlGJtlDd3Q== X-Google-Smtp-Source: AGHT+IHogreXDslqhpyk+Ms3XFo/uXGtPLAXb4cvDc8C5nh9Ehy9UaqEagZCyCvQ6vw015lUH0Gb7A== X-Received: by 2002:a81:52c9:0:b0:59c:c79:eeee with SMTP id g192-20020a8152c9000000b0059c0c79eeeemr5778750ywb.44.1695630571804; Mon, 25 Sep 2023 01:29:31 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id c64-20020a0dc143000000b0059a34cfa2a5sm2259385ywd.67.2023.09.25.01.29.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:29:30 -0700 (PDT) Date: Mon, 25 Sep 2023 01:29:28 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 07/12] mempolicy: mpol_shared_policy_init() without pseudo-vma In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" mpol_shared_policy_init() does not need to use a pseudo-vma: it can use sp_alloc() and sp_insert() directly, since the object's shared policy tree is empty and inaccessible (needing no lock) at get_inode() time. Signed-off-by: Hugh Dickins --- mm/mempolicy.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 065e886ec9b6..a22b641cfd6b 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2749,7 +2749,7 @@ void mpol_shared_policy_init(struct shared_policy *sp= , struct mempolicy *mpol) rwlock_init(&sp->lock); =20 if (mpol) { - struct vm_area_struct pvma; + struct sp_node *n; struct mempolicy *new; NODEMASK_SCRATCH(scratch); =20 @@ -2766,11 +2766,10 @@ void mpol_shared_policy_init(struct shared_policy *= sp, struct mempolicy *mpol) if (ret) goto put_new; =20 - /* Create pseudo-vma that contains just the policy */ - vma_init(&pvma, NULL); - pvma.vm_end =3D TASK_SIZE; /* policy covers entire file */ - mpol_set_shared_policy(sp, &pvma, new); /* adds ref */ - + /* alloc node covering entire file; adds ref to new */ + n =3D sp_alloc(0, MAX_LFS_FILESIZE >> PAGE_SHIFT, new); + if (n) + sp_insert(sp, n); put_new: mpol_put(new); /* drop initial ref */ free_scratch: --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE56ACE7A89 for ; Mon, 25 Sep 2023 08:31:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232289AbjIYIbL (ORCPT ); Mon, 25 Sep 2023 04:31:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232890AbjIYIbE (ORCPT ); Mon, 25 Sep 2023 04:31:04 -0400 Received: from mail-yw1-x112d.google.com (mail-yw1-x112d.google.com [IPv6:2607:f8b0:4864:20::112d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 805EB10A for ; Mon, 25 Sep 2023 01:30:55 -0700 (PDT) Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-59bf1dde73fso73722417b3.3 for ; Mon, 25 Sep 2023 01:30:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630654; x=1696235454; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=ulgX9THEbQN3dQQC629PIbXmK6AWD1UO9BuCeRr1HmQ=; b=hAuFfZwGzDzbP92DfXsFO5VvWvyyotdTRX0i+PghfBePGRAZv4PMQ8eh5m8KUenAkL 7w+9ciXDiU0DP9t0Cv2PGYfQ4NTUWgG7naUhQ8YGrVyle1El3BwdY34AsoDjxqpCZJiQ V2upxJ3Kx4bgX2kIwMExNWoAVFEL32/1PQ9OXXHd/GCeXFxweW9P0N72XRwL0mk1prME IQSbrDlZcNoh+PGPisual4uY+Erw12fgkjEW1OIWPKdrkjdbT9OxbpmX6Y80r+RDarKd rDx0DN04esdCT3FwkSMPJdd2tmXFfGrdp7IB6hM5xFyBZdOJ6bhWjhQU6YvzHvnXCwsK idfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630654; x=1696235454; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ulgX9THEbQN3dQQC629PIbXmK6AWD1UO9BuCeRr1HmQ=; b=GAHlaIPKsXV7jYGbezsVy5ubraRWMy0wnkxfEmGioeR0UVS/x05h+Ca8pEOAw5u0vM VfiOt0wrs57uS9mBPLrqE6flDKXsuelbLEC6rpaK4wdtzdhp42Z763vZMsuIgtnd9Bcq 1w5uTUFxDSIJOW4QUMDy2ms40NyO1MqqhVmroYl3C0om+XbtZNKrvP7slp/tqNfeCE6y CiH6a3WSTbjBd8qNUtGEw7IK21C1VOYou/SnWdWzoUikeM29ER1JMUzJPOQWeSCXGckC mPiIN7HJAU4/MlqLwQhiY2or8rLxmQlqmQIe/X6kDMhT9CHNsgFvfKCFHkw8p1Ow9/BL HtQA== X-Gm-Message-State: AOJu0YxCCg/4YxZDrUqq28h4PxmTHa8YjEs3AoFflHVa05sH27M2VzZh ULD+Ee3M58QXtkds64PrmQJHTw== X-Google-Smtp-Source: AGHT+IEGFXRC2nnrJxyRas7mIKt1ska0D212eRm9P7TUFWDSpRyUl8E9tDZ9Nksk92zJ5nQ/VXHlUQ== X-Received: by 2002:a05:690c:3603:b0:571:11ea:b2dd with SMTP id ft3-20020a05690c360300b0057111eab2ddmr5723589ywb.32.1695630654594; Mon, 25 Sep 2023 01:30:54 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id j185-20020a816ec2000000b00592236855cesm2259304ywc.61.2023.09.25.01.30.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:30:53 -0700 (PDT) Date: Mon, 25 Sep 2023 01:30:51 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 08/12] mempolicy: remove confusing MPOL_MF_LAZY dead code In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: <2cb8b08a-a96c-2a61-94dd-4cd51ad0605d@google.com> References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" v3.8 commit b24f53a0bea3 ("mm: mempolicy: Add MPOL_MF_LAZY") introduced MPOL_MF_LAZY, and included it in the MPOL_MF_VALID flags; but a720094ded8 ("mm: mempolicy: Hide MPOL_NOOP and MPOL_MF_LAZY from userspace for now") immediately removed it from MPOL_MF_VALID flags, pending further review. "This will need to be revisited", but it has not been reinstated. The present state is confusing: there is dead code in mm/mempolicy.c to handle MPOL_MF_LAZY cases which can never occur. Remove that: it can be resurrected later if necessary. But keep the definition of MPOL_MF_LAZY, which must remain in the UAPI, even though it always fails with EINVAL. https://lore.kernel.org/linux-mm/1553041659-46787-1-git-send-email-yang.shi= @linux.alibaba.com/ links to a previous request to remove MPOL_MF_LAZY. Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) --- include/uapi/linux/mempolicy.h | 2 +- mm/mempolicy.c | 18 ------------------ 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/include/uapi/linux/mempolicy.h b/include/uapi/linux/mempolicy.h index 046d0ccba4cd..a8963f7ef4c2 100644 --- a/include/uapi/linux/mempolicy.h +++ b/include/uapi/linux/mempolicy.h @@ -48,7 +48,7 @@ enum { #define MPOL_MF_MOVE (1<<1) /* Move pages owned by this process to conform to policy */ #define MPOL_MF_MOVE_ALL (1<<2) /* Move every page to conform to policy */ -#define MPOL_MF_LAZY (1<<3) /* Modifies '_MOVE: lazy migrate on fault */ +#define MPOL_MF_LAZY (1<<3) /* UNSUPPORTED FLAG: Lazy migrate on fault */ #define MPOL_MF_INTERNAL (1<<4) /* Internal flags start here */ =20 #define MPOL_MF_VALID (MPOL_MF_STRICT | \ diff --git a/mm/mempolicy.c b/mm/mempolicy.c index a22b641cfd6b..7ab6102d7da4 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -632,12 +632,6 @@ unsigned long change_prot_numa(struct vm_area_struct *= vma, =20 return nr_updated; } -#else -static unsigned long change_prot_numa(struct vm_area_struct *vma, - unsigned long addr, unsigned long end) -{ - return 0; -} #endif /* CONFIG_NUMA_BALANCING */ =20 static int queue_pages_test_walk(unsigned long start, unsigned long end, @@ -676,14 +670,6 @@ static int queue_pages_test_walk(unsigned long start, = unsigned long end, if (endvma > end) endvma =3D end; =20 - if (flags & MPOL_MF_LAZY) { - /* Similar to task_numa_work, skip inaccessible VMAs */ - if (!is_vm_hugetlb_page(vma) && vma_is_accessible(vma) && - !(vma->vm_flags & VM_MIXEDMAP)) - change_prot_numa(vma, start, endvma); - return 1; - } - /* * Check page nodes, and queue pages to move, in the current vma. * But if no moving, and no strict checking, the scan can be skipped. @@ -1271,9 +1257,6 @@ static long do_mbind(unsigned long start, unsigned lo= ng len, if (IS_ERR(new)) return PTR_ERR(new); =20 - if (flags & MPOL_MF_LAZY) - new->flags |=3D MPOL_F_MOF; - /* * If we are using the default policy then operation * on discontinuous address spaces is okay after all @@ -1318,7 +1301,6 @@ static long do_mbind(unsigned long start, unsigned lo= ng len, =20 if (!err) { if (!list_empty(&pagelist)) { - WARN_ON_ONCE(flags & MPOL_MF_LAZY); nr_failed |=3D migrate_pages(&pagelist, new_folio, NULL, start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND, NULL); } --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CAA6CE7A8C for ; Mon, 25 Sep 2023 08:32:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232824AbjIYIcO (ORCPT ); Mon, 25 Sep 2023 04:32:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjIYIcN (ORCPT ); Mon, 25 Sep 2023 04:32:13 -0400 Received: from mail-yw1-x1133.google.com (mail-yw1-x1133.google.com [IPv6:2607:f8b0:4864:20::1133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B18FABC for ; Mon, 25 Sep 2023 01:32:06 -0700 (PDT) Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-59c07cf02ebso70286667b3.1 for ; Mon, 25 Sep 2023 01:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630726; x=1696235526; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=ESRqX1PlrIchswbjY6F5gY3I+wvwb2kVpQ+ph7N3oWo=; b=YE4gVrE5UL3QWSJeaJ9+SqhEvadjQ/UUiak38YJh+2A9pLX7wZzVsi9aHGiMb016GF HlobMBNghSGPFjKkEaNDAevbtJm0qXJ4cK37tYR6nvMlGOkIgTPNlrYHa6bbomG5EMHu t+cPaCGIPOE8961bC5sw5/bOdB5361HMj0UJfZbj4RKKYgSj+9PnMU1M38qV/SQzOZbW M13g6E1MhUumfqBexsINmPKFI9tftuGgc0E56/rFOOXZwUFeKw5Nym9UsRU+f6Eio/4F 9P8NyUxY0PvSVjzGahoALL+o+6pKQkMAkDewE59zN5VGavTCUrfJZBr50bxIYvOcV6tw BDGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630726; x=1696235526; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ESRqX1PlrIchswbjY6F5gY3I+wvwb2kVpQ+ph7N3oWo=; b=YJe5QZdOi0128JeJUqMWV1+kQnm1hcxyFHi6rC50g+I6ECm6muUccQ8GxfjCvH7ihN F4Fwsoln/XES18aIfLhORWs0NIs/oYzGmQRlyuri55v9UEaF8+2EudGMq1BfR/Ej3ItH PkMFdrtqZB8tiCSLKH1LW/wfNpbm3hmzHv9L2WTNmbBwgGgTkglNXNELX4I6KjDW/i7m Ht6qkrhQCdDmT32+8h4htmtxRJTybB1F0f4zM2Azxj+cGXOIlVgS5uWQBYHprom4R07D 8gR/hd+fiY/MvUaVcBY7Uf1c9LWJoC1qBrVbcQK9dQx2OGR46G9erXvipETEuVN6qBkG MkDw== X-Gm-Message-State: AOJu0YyMb/fCyWWzjcnwqHkgEsfgptHKgx6EBT22GHGMNiVFHEMhraZu GHY8lnEC4xCEt6gk8+T/ibWX5A== X-Google-Smtp-Source: AGHT+IFI7sWdT9SaX/WaLFmLycQV1E3ZHD2waCsU6dSHFKECPifOP+qkTIKONo5XkYDTZbJNeNdwNw== X-Received: by 2002:a25:744b:0:b0:d84:da24:96de with SMTP id p72-20020a25744b000000b00d84da2496demr5304926ybc.33.1695630725781; Mon, 25 Sep 2023 01:32:05 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id t3-20020a056902018300b00d1b86efc0ffsm2024884ybh.6.2023.09.25.01.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:32:05 -0700 (PDT) Date: Mon, 25 Sep 2023 01:32:02 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 09/12] mm: add page_rmappable_folio() wrapper In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" folio_prep_large_rmappable() is being used repeatedly along with a conversion from page to folio, a check non-NULL, a check order > 1: wrap it all up into struct folio *page_rmappable_folio(struct page *). Signed-off-by: Hugh Dickins --- include/linux/huge_mm.h | 13 +++++++++++++ mm/mempolicy.c | 17 +++-------------- mm/page_alloc.c | 8 ++------ 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index fa0350b0812a..58e7662a8a62 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -141,6 +141,15 @@ unsigned long thp_get_unmapped_area(struct file *filp,= unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); =20 void folio_prep_large_rmappable(struct folio *folio); +static inline struct folio *page_rmappable_folio(struct page *page) +{ + struct folio *folio =3D (struct folio *)page; + + if (folio && folio_order(folio) > 1) + folio_prep_large_rmappable(folio); + return folio; +} + bool can_split_folio(struct folio *folio, int *pextra_pins); int split_huge_page_to_list(struct page *page, struct list_head *list); static inline int split_huge_page(struct page *page) @@ -281,6 +290,10 @@ static inline bool hugepage_vma_check(struct vm_area_s= truct *vma, } =20 static inline void folio_prep_large_rmappable(struct folio *folio) {} +static inline struct folio *page_rmappable_folio(struct page *page) +{ + return (struct folio *)page; +} =20 #define transparent_hugepage_flags 0UL =20 diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 7ab6102d7da4..4c3b3f535630 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2137,10 +2137,7 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, = struct vm_area_struct *vma, mpol_cond_put(pol); gfp |=3D __GFP_COMP; page =3D alloc_page_interleave(gfp, order, nid); - folio =3D (struct folio *)page; - if (folio && order > 1) - folio_prep_large_rmappable(folio); - goto out; + return page_rmappable_folio(page); } =20 if (pol->mode =3D=3D MPOL_PREFERRED_MANY) { @@ -2150,10 +2147,7 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, = struct vm_area_struct *vma, gfp |=3D __GFP_COMP; page =3D alloc_pages_preferred_many(gfp, order, node, pol); mpol_cond_put(pol); - folio =3D (struct folio *)page; - if (folio && order > 1) - folio_prep_large_rmappable(folio); - goto out; + return page_rmappable_folio(page); } =20 if (unlikely(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage)) { @@ -2247,12 +2241,7 @@ EXPORT_SYMBOL(alloc_pages); =20 struct folio *folio_alloc(gfp_t gfp, unsigned order) { - struct page *page =3D alloc_pages(gfp | __GFP_COMP, order); - struct folio *folio =3D (struct folio *)page; - - if (folio && order > 1) - folio_prep_large_rmappable(folio); - return folio; + return page_rmappable_folio(alloc_pages(gfp | __GFP_COMP, order)); } EXPORT_SYMBOL(folio_alloc); =20 diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 95546f376302..5b1707d9025a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4456,12 +4456,8 @@ struct folio *__folio_alloc(gfp_t gfp, unsigned int = order, int preferred_nid, nodemask_t *nodemask) { struct page *page =3D __alloc_pages(gfp | __GFP_COMP, order, - preferred_nid, nodemask); - struct folio *folio =3D (struct folio *)page; - - if (folio && order > 1) - folio_prep_large_rmappable(folio); - return folio; + preferred_nid, nodemask); + return page_rmappable_folio(page); } EXPORT_SYMBOL(__folio_alloc); =20 --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46772CE7A89 for ; Mon, 25 Sep 2023 08:33:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232935AbjIYIdt (ORCPT ); Mon, 25 Sep 2023 04:33:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232827AbjIYIdo (ORCPT ); Mon, 25 Sep 2023 04:33:44 -0400 Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E332F9D for ; Mon, 25 Sep 2023 01:33:34 -0700 (PDT) Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-59f4f80d084so39347077b3.1 for ; Mon, 25 Sep 2023 01:33:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630814; x=1696235614; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=joHrE76+BZ/TkFMkYd+VVkSdXy8EoXIxUO0b5+SdE6c=; b=qI5niTXDOSzEYDQmM7Stxm9sKehdQpbBCTFFwZ7RNEP8TJrTtTf8WQntExlhATS8er OtycCjl+XV5LXMqmlukBmAif2tIbVEbFY4ioRfdlHJg9R57s6nj2YTKLPIZ5R46JZJit OrMUa4Fc5TAHzFkt7lRDXL+EEPBgNnTPV9H2McNFkaVDSCL6WlbskYuemeNuJ/TD7jkg z9CoFaXKFhLEZ2zcmhxisOxyXxU9TjJ+pgqVW4ez+oBybED1CP83FWggo1kMmIF1O+v9 t9da+cPxZcMoEW433tup5X1iKHgIt1Hha0IEILyrXy4Y4LSU0PZ+JF3bH+tySGZT2/yv /Jgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630814; x=1696235614; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=joHrE76+BZ/TkFMkYd+VVkSdXy8EoXIxUO0b5+SdE6c=; b=dBsZZz3XD5j71xJJ4/SmXHKh/5T3VbJgLmVxiAODjTvnjyeiwSP/HNK9XGWGUvL3Fh JG2bii2rKEWvYidp5ePJutu14X0akQoHARNJEmW20h7W3+GMiTeP0BbOZFMLQ9oSqbp2 1sXkYNPS+E/TA67+eCo4YKdr1emLEeuwN+UR0rK0DaQe4S4CejRioxYDvlOLmFFsOnZX wikAnF/fhfItoqIii20yFiEi72ccmVWqYL9Xf73Io/pXKXePeBk5cmUzTa8sVLzWXRRQ BNxZccC1TzJuDZne7aI82F3JZPlRCNtPIrqZGv+9MFaIekL7I7pQTiqpWpa6uLmlp0Xl /6Cw== X-Gm-Message-State: AOJu0Yx1Y77P3wqFLqJI7iUFIZ4zOSgSSzM33g+uPPI/aJKmYaB2wHxA FpdIEgBfvgp66spiIxluMXK3oA== X-Google-Smtp-Source: AGHT+IEPjbstgzDus+yXL1ff8uYWPlteLpHjw4Yk9UR4CFsuBV9W8D4OqRhf0CYjN+MEFU6YFHTNtw== X-Received: by 2002:a05:690c:2fc3:b0:59f:4c52:2f5d with SMTP id ex3-20020a05690c2fc300b0059f4c522f5dmr4929843ywb.2.1695630813744; Mon, 25 Sep 2023 01:33:33 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id g203-20020a0dddd4000000b00592548b2c47sm2294036ywe.80.2023.09.25.01.33.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:33:32 -0700 (PDT) Date: Mon, 25 Sep 2023 01:33:30 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 10/12] mempolicy: alloc_pages_mpol() for NUMA policy without vma In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Shrink shmem's stack usage by eliminating the pseudo-vma from its folio allocation. alloc_pages_mpol(gfp, order, pol, ilx, nid) becomes the principal actor for passing mempolicy choice down to __alloc_pages(), rather than vma_alloc_folio(gfp, order, vma, addr, hugepage). vma_alloc_folio() and alloc_pages() remain, but as wrappers around alloc_pages_mpol(). alloc_pages_bulk_*() untouched, except to provide the additional args to policy_nodemask(), which subsumes policy_node(). Cleanup throughout, cutting out some unhelpful "helpers". It would all be much simpler without MPOL_INTERLEAVE, but that adds a dynamic to the constant mpol: complicated by v3.6 commit 09c231cb8bfd ("tmpfs: distribute interleave better across nodes"), which added ino bias to the interleave, hidden from mm/mempolicy.c until this commit. Hence "ilx" throughout, the "interleave index". Originally I thought it could be done just with nid, but that's wrong: the nodemask may come from the shared policy layer below a shmem vma, or it may come from the task layer above a shmem vma; and without the final nodemask then nodeid cannot be decided. And how ilx is applied depends also on page order. The interleave index is almost always irrelevant unless MPOL_INTERLEAVE: with one exception in alloc_pages_mpol(), where the NO_INTERLEAVE_INDEX passed down from vma-less alloc_pages() is also used as hint not to use THP-style hugepage allocation - to avoid the overhead of a hugepage arg (though I don't understand why we never just added a GFP bit for THP - if it actually needs a different allocation strategy from other pages of the same order). vma_alloc_folio() still carries its hugepage arg here, but it is not used, and should be removed when agreed. get_vma_policy() no longer allows a NULL vma: over time I believe we've eradicated all the places which used to need it e.g. swapoff and madvise used to pass NULL vma to read_swap_cache_async(), but now know the vma. Signed-off-by: Hugh Dickins --- fs/proc/task_mmu.c | 5 +- include/linux/gfp.h | 10 +- include/linux/mempolicy.h | 13 +- include/linux/mm.h | 2 +- ipc/shm.c | 21 +-- mm/mempolicy.c | 383 ++++++++++++++++---------------------- mm/shmem.c | 102 +++++----- mm/swap.h | 9 +- mm/swap_state.c | 86 +++++---- 9 files changed, 304 insertions(+), 327 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 3dd5be96691b..b0955a20e95f 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1945,8 +1945,9 @@ static int show_numa_map(struct seq_file *m, void *v) struct numa_maps *md =3D &numa_priv->md; struct file *file =3D vma->vm_file; struct mm_struct *mm =3D vma->vm_mm; - struct mempolicy *pol; char buffer[64]; + struct mempolicy *pol; + pgoff_t ilx; int nid; =20 if (!mm) @@ -1955,7 +1956,7 @@ static int show_numa_map(struct seq_file *m, void *v) /* Ensure we start with an empty set of numa_maps statistics. */ memset(md, 0, sizeof(*md)); =20 - pol =3D __get_vma_policy(vma, vma->vm_start); + pol =3D __get_vma_policy(vma, vma->vm_start, &ilx); if (pol) { mpol_to_str(buffer, sizeof(buffer), pol); mpol_cond_put(pol); diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 665f06675c83..f74f8d05b053 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -8,6 +8,7 @@ #include =20 struct vm_area_struct; +struct mempolicy; =20 /* Convert GFP flags to their corresponding migrate type */ #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE) @@ -262,7 +263,9 @@ static inline struct page *alloc_pages_node(int nid, gf= p_t gfp_mask, =20 #ifdef CONFIG_NUMA struct page *alloc_pages(gfp_t gfp, unsigned int order); -struct folio *folio_alloc(gfp_t gfp, unsigned order); +struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order, + struct mempolicy *mpol, pgoff_t ilx, int nid); +struct folio *folio_alloc(gfp_t gfp, unsigned int order); struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct = *vma, unsigned long addr, bool hugepage); #else @@ -270,6 +273,11 @@ static inline struct page *alloc_pages(gfp_t gfp_mask,= unsigned int order) { return alloc_pages_node(numa_node_id(), gfp_mask, order); } +static inline struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order, + struct mempolicy *mpol, pgoff_t ilx, int nid) +{ + return alloc_pages(gfp, order); +} static inline struct folio *folio_alloc(gfp_t gfp, unsigned int order) { return __folio_alloc_node(gfp, order, numa_node_id()); diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index 12f7dc74a457..ad93f23434bb 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -128,7 +128,9 @@ struct mempolicy *mpol_shared_policy_lookup(struct shar= ed_policy *sp, =20 struct mempolicy *get_task_policy(struct task_struct *p); struct mempolicy *__get_vma_policy(struct vm_area_struct *vma, - unsigned long addr); + unsigned long addr, pgoff_t *ilx); +struct mempolicy *get_vma_policy(struct vm_area_struct *vma, + unsigned long addr, int order, pgoff_t *ilx); bool vma_policy_mof(struct vm_area_struct *vma); =20 extern void numa_default_policy(void); @@ -142,8 +144,6 @@ extern int huge_node(struct vm_area_struct *vma, extern bool init_nodemask_of_mempolicy(nodemask_t *mask); extern bool mempolicy_in_oom_domain(struct task_struct *tsk, const nodemask_t *mask); -extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy); - extern unsigned int mempolicy_slab_node(void); =20 extern enum zone_type policy_zone; @@ -215,6 +215,13 @@ mpol_shared_policy_lookup(struct shared_policy *sp, pg= off_t idx) return NULL; } =20 +static inline struct mempolicy *get_vma_policy(struct vm_area_struct *vma, + unsigned long addr, int order, pgoff_t *ilx) +{ + *ilx =3D 0; + return NULL; +} + #define vma_policy(vma) NULL =20 static inline int diff --git a/include/linux/mm.h b/include/linux/mm.h index bf5d0b1b16f4..456f060b2475 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -619,7 +619,7 @@ struct vm_operations_struct { * policy. */ struct mempolicy *(*get_policy)(struct vm_area_struct *vma, - unsigned long addr); + unsigned long addr, pgoff_t *ilx); #endif /* * Called by vm_normal_page() for special PTEs to find the diff --git a/ipc/shm.c b/ipc/shm.c index 576a543b7cff..222aaf035afb 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -562,30 +562,25 @@ static unsigned long shm_pagesize(struct vm_area_stru= ct *vma) } =20 #ifdef CONFIG_NUMA -static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *ne= w) +static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *mp= ol) { - struct file *file =3D vma->vm_file; - struct shm_file_data *sfd =3D shm_file_data(file); + struct shm_file_data *sfd =3D shm_file_data(vma->vm_file); int err =3D 0; =20 if (sfd->vm_ops->set_policy) - err =3D sfd->vm_ops->set_policy(vma, new); + err =3D sfd->vm_ops->set_policy(vma, mpol); return err; } =20 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma, - unsigned long addr) + unsigned long addr, pgoff_t *ilx) { - struct file *file =3D vma->vm_file; - struct shm_file_data *sfd =3D shm_file_data(file); - struct mempolicy *pol =3D NULL; + struct shm_file_data *sfd =3D shm_file_data(vma->vm_file); + struct mempolicy *mpol =3D vma->vm_policy; =20 if (sfd->vm_ops->get_policy) - pol =3D sfd->vm_ops->get_policy(vma, addr); - else if (vma->vm_policy) - pol =3D vma->vm_policy; - - return pol; + mpol =3D sfd->vm_ops->get_policy(vma, addr, ilx); + return mpol; } #endif =20 diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 4c3b3f535630..d74df1e1b14a 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -114,6 +114,8 @@ #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for n= odemask */ #define MPOL_MF_WRLOCK (MPOL_MF_INTERNAL << 2) /* Write-lock walked = vmas */ =20 +#define NO_INTERLEAVE_INDEX (-1UL) + static struct kmem_cache *policy_cache; static struct kmem_cache *sn_cache; =20 @@ -915,6 +917,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *n= mask, } =20 if (flags & MPOL_F_ADDR) { + pgoff_t ilx; /* ignored here */ /* * Do NOT fall back to task policy if the * vma/shared policy at addr is NULL. We @@ -926,10 +929,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *= nmask, mmap_read_unlock(mm); return -EFAULT; } - if (vma->vm_ops && vma->vm_ops->get_policy) - pol =3D vma->vm_ops->get_policy(vma, addr); - else - pol =3D vma->vm_policy; + pol =3D __get_vma_policy(vma, addr, &ilx); } else if (addr) return -EINVAL; =20 @@ -1187,6 +1187,15 @@ static struct folio *new_folio(struct folio *src, un= signed long start) break; } =20 + /* + * __get_vma_policy() now expects a genuine non-NULL vma. Return NULL + * when the page can no longer be located in a vma: that is not ideal + * (migrate_pages() will give up early, presuming ENOMEM), but good + * enough to avoid a crash by syzkaller or concurrent holepunch. + */ + if (!vma) + return NULL; + if (folio_test_hugetlb(src)) { return alloc_hugetlb_folio_vma(folio_hstate(src), vma, address); @@ -1195,9 +1204,6 @@ static struct folio *new_folio(struct folio *src, uns= igned long start) if (folio_test_large(src)) gfp =3D GFP_TRANSHUGE; =20 - /* - * if !vma, vma_alloc_folio() will use task or system default policy - */ return vma_alloc_folio(gfp, folio_order(src), vma, address, folio_test_large(src)); } @@ -1705,34 +1711,19 @@ bool vma_migratable(struct vm_area_struct *vma) } =20 struct mempolicy *__get_vma_policy(struct vm_area_struct *vma, - unsigned long addr) + unsigned long addr, pgoff_t *ilx) { - struct mempolicy *pol =3D NULL; - - if (vma) { - if (vma->vm_ops && vma->vm_ops->get_policy) { - pol =3D vma->vm_ops->get_policy(vma, addr); - } else if (vma->vm_policy) { - pol =3D vma->vm_policy; - - /* - * shmem_alloc_page() passes MPOL_F_SHARED policy with - * a pseudo vma whose vma->vm_ops=3DNULL. Take a reference - * count on these policies which will be dropped by - * mpol_cond_put() later - */ - if (mpol_needs_cond_ref(pol)) - mpol_get(pol); - } - } - - return pol; + *ilx =3D 0; + return (vma->vm_ops && vma->vm_ops->get_policy) ? + vma->vm_ops->get_policy(vma, addr, ilx) : vma->vm_policy; } =20 /* - * get_vma_policy(@vma, @addr) + * get_vma_policy(@vma, @addr, @order, @ilx) * @vma: virtual memory area whose policy is sought * @addr: address in @vma for shared policy lookup + * @order: 0, or appropriate huge_page_order for interleaving + * @ilx: interleave index (output), for use only when MPOL_INTERLEAVE * * Returns effective policy for a VMA at specified address. * Falls back to current->mempolicy or system default policy, as necessary. @@ -1741,14 +1732,18 @@ struct mempolicy *__get_vma_policy(struct vm_area_s= truct *vma, * freeing by another task. It is the caller's responsibility to free the * extra reference for shared policies. */ -static struct mempolicy *get_vma_policy(struct vm_area_struct *vma, - unsigned long addr) +struct mempolicy *get_vma_policy(struct vm_area_struct *vma, + unsigned long addr, int order, pgoff_t *ilx) { - struct mempolicy *pol =3D __get_vma_policy(vma, addr); + struct mempolicy *pol; =20 + pol =3D __get_vma_policy(vma, addr, ilx); if (!pol) pol =3D get_task_policy(current); - + if (pol->mode =3D=3D MPOL_INTERLEAVE) { + *ilx +=3D vma->vm_pgoff >> order; + *ilx +=3D (addr - vma->vm_start) >> (PAGE_SHIFT + order); + } return pol; } =20 @@ -1758,8 +1753,9 @@ bool vma_policy_mof(struct vm_area_struct *vma) =20 if (vma->vm_ops && vma->vm_ops->get_policy) { bool ret =3D false; + pgoff_t ilx; /* ignored here */ =20 - pol =3D vma->vm_ops->get_policy(vma, vma->vm_start); + pol =3D vma->vm_ops->get_policy(vma, vma->vm_start, &ilx); if (pol && (pol->flags & MPOL_F_MOF)) ret =3D true; mpol_cond_put(pol); @@ -1794,54 +1790,6 @@ bool apply_policy_zone(struct mempolicy *policy, enu= m zone_type zone) return zone >=3D dynamic_policy_zone; } =20 -/* - * Return a nodemask representing a mempolicy for filtering nodes for - * page allocation - */ -nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy) -{ - int mode =3D policy->mode; - - /* Lower zones don't get a nodemask applied for MPOL_BIND */ - if (unlikely(mode =3D=3D MPOL_BIND) && - apply_policy_zone(policy, gfp_zone(gfp)) && - cpuset_nodemask_valid_mems_allowed(&policy->nodes)) - return &policy->nodes; - - if (mode =3D=3D MPOL_PREFERRED_MANY) - return &policy->nodes; - - return NULL; -} - -/* - * Return the preferred node id for 'prefer' mempolicy, and return - * the given id for all other policies. - * - * policy_node() is always coupled with policy_nodemask(), which - * secures the nodemask limit for 'bind' and 'prefer-many' policy. - */ -static int policy_node(gfp_t gfp, struct mempolicy *policy, int nid) -{ - if (policy->mode =3D=3D MPOL_PREFERRED) { - nid =3D first_node(policy->nodes); - } else { - /* - * __GFP_THISNODE shouldn't even be used with the bind policy - * because we might easily break the expectation to stay on the - * requested node and not break the policy. - */ - WARN_ON_ONCE(policy->mode =3D=3D MPOL_BIND && (gfp & __GFP_THISNODE)); - } - - if ((policy->mode =3D=3D MPOL_BIND || - policy->mode =3D=3D MPOL_PREFERRED_MANY) && - policy->home_node !=3D NUMA_NO_NODE) - return policy->home_node; - - return nid; -} - /* Do dynamic interleaving for a process */ static unsigned int interleave_nodes(struct mempolicy *policy) { @@ -1901,11 +1849,11 @@ unsigned int mempolicy_slab_node(void) } =20 /* - * Do static interleaving for a VMA with known offset @n. Returns the n'th - * node in pol->nodes (starting from n=3D0), wrapping around if n exceeds = the - * number of present nodes. + * Do static interleaving for interleave index @ilx. Returns the ilx'th + * node in pol->nodes (starting from ilx=3D0), wrapping around if ilx + * exceeds the number of present nodes. */ -static unsigned offset_il_node(struct mempolicy *pol, unsigned long n) +static unsigned int interleave_nid(struct mempolicy *pol, pgoff_t ilx) { nodemask_t nodemask =3D pol->nodes; unsigned int target, nnodes; @@ -1923,33 +1871,54 @@ static unsigned offset_il_node(struct mempolicy *po= l, unsigned long n) nnodes =3D nodes_weight(nodemask); if (!nnodes) return numa_node_id(); - target =3D (unsigned int)n % nnodes; + target =3D ilx % nnodes; nid =3D first_node(nodemask); for (i =3D 0; i < target; i++) nid =3D next_node(nid, nodemask); return nid; } =20 -/* Determine a node number for interleave */ -static inline unsigned interleave_nid(struct mempolicy *pol, - struct vm_area_struct *vma, unsigned long addr, int shift) +/* + * Return a nodemask representing a mempolicy for filtering nodes for + * page allocation, together with preferred node id (or the input node id). + */ +static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *pol, + pgoff_t ilx, int *nid) { - if (vma) { - unsigned long off; + nodemask_t *nodemask =3D NULL; =20 + switch (pol->mode) { + case MPOL_PREFERRED: + /* Override input node id */ + *nid =3D first_node(pol->nodes); + break; + case MPOL_PREFERRED_MANY: + nodemask =3D &pol->nodes; + if (pol->home_node !=3D NUMA_NO_NODE) + *nid =3D pol->home_node; + break; + case MPOL_BIND: + /* Restrict to nodemask (but not on lower zones) */ + if (apply_policy_zone(pol, gfp_zone(gfp)) && + cpuset_nodemask_valid_mems_allowed(&pol->nodes)) + nodemask =3D &pol->nodes; + if (pol->home_node !=3D NUMA_NO_NODE) + *nid =3D pol->home_node; /* - * for small pages, there is no difference between - * shift and PAGE_SHIFT, so the bit-shift is safe. - * for huge pages, since vm_pgoff is in units of small - * pages, we need to shift off the always 0 bits to get - * a useful offset. + * __GFP_THISNODE shouldn't even be used with the bind policy + * because we might easily break the expectation to stay on the + * requested node and not break the policy. */ - BUG_ON(shift < PAGE_SHIFT); - off =3D vma->vm_pgoff >> (shift - PAGE_SHIFT); - off +=3D (addr - vma->vm_start) >> shift; - return offset_il_node(pol, off); - } else - return interleave_nodes(pol); + WARN_ON_ONCE(gfp & __GFP_THISNODE); + break; + case MPOL_INTERLEAVE: + /* Override input node id */ + *nid =3D (ilx =3D=3D NO_INTERLEAVE_INDEX) ? + interleave_nodes(pol) : interleave_nid(pol, ilx); + break; + } + + return nodemask; } =20 #ifdef CONFIG_HUGETLBFS @@ -1965,27 +1934,16 @@ static inline unsigned interleave_nid(struct mempol= icy *pol, * to the struct mempolicy for conditional unref after allocation. * If the effective policy is 'bind' or 'prefer-many', returns a pointer * to the mempolicy's @nodemask for filtering the zonelist. - * - * Must be protected by read_mems_allowed_begin() */ int huge_node(struct vm_area_struct *vma, unsigned long addr, gfp_t gfp_fl= ags, - struct mempolicy **mpol, nodemask_t **nodemask) + struct mempolicy **mpol, nodemask_t **nodemask) { + pgoff_t ilx; int nid; - int mode; =20 - *mpol =3D get_vma_policy(vma, addr); - *nodemask =3D NULL; - mode =3D (*mpol)->mode; - - if (unlikely(mode =3D=3D MPOL_INTERLEAVE)) { - nid =3D interleave_nid(*mpol, vma, addr, - huge_page_shift(hstate_vma(vma))); - } else { - nid =3D policy_node(gfp_flags, *mpol, numa_node_id()); - if (mode =3D=3D MPOL_BIND || mode =3D=3D MPOL_PREFERRED_MANY) - *nodemask =3D &(*mpol)->nodes; - } + nid =3D numa_node_id(); + *mpol =3D get_vma_policy(vma, addr, hstate_vma(vma)->order, &ilx); + *nodemask =3D policy_nodemask(gfp_flags, *mpol, ilx, &nid); return nid; } =20 @@ -2063,27 +2021,8 @@ bool mempolicy_in_oom_domain(struct task_struct *tsk, return ret; } =20 -/* Allocate a page in interleaved policy. - Own path because it needs to do special accounting. */ -static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, - unsigned nid) -{ - struct page *page; - - page =3D __alloc_pages(gfp, order, nid, NULL); - /* skip NUMA_INTERLEAVE_HIT counter update if numa stats is disabled */ - if (!static_branch_likely(&vm_numa_stat_key)) - return page; - if (page && page_to_nid(page) =3D=3D nid) { - preempt_disable(); - __count_numa_event(page_zone(page), NUMA_INTERLEAVE_HIT); - preempt_enable(); - } - return page; -} - static struct page *alloc_pages_preferred_many(gfp_t gfp, unsigned int ord= er, - int nid, struct mempolicy *pol) + int nid, nodemask_t *nodemask) { struct page *page; gfp_t preferred_gfp; @@ -2096,7 +2035,7 @@ static struct page *alloc_pages_preferred_many(gfp_t = gfp, unsigned int order, */ preferred_gfp =3D gfp | __GFP_NOWARN; preferred_gfp &=3D ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); - page =3D __alloc_pages(preferred_gfp, order, nid, &pol->nodes); + page =3D __alloc_pages(preferred_gfp, order, nid, nodemask); if (!page) page =3D __alloc_pages(gfp, order, nid, NULL); =20 @@ -2104,55 +2043,29 @@ static struct page *alloc_pages_preferred_many(gfp_= t gfp, unsigned int order, } =20 /** - * vma_alloc_folio - Allocate a folio for a VMA. + * alloc_pages_mpol - Allocate pages according to NUMA mempolicy. * @gfp: GFP flags. - * @order: Order of the folio. - * @vma: Pointer to VMA or NULL if not available. - * @addr: Virtual address of the allocation. Must be inside @vma. - * @hugepage: For hugepages try only the preferred node if possible. + * @order: Order of the page allocation. + * @pol: Pointer to the NUMA mempolicy. + * @ilx: Index for interleave mempolicy (also distinguishes alloc_pages()). + * @nid: Preferred node (usually numa_node_id() but @mpol may override it). * - * Allocate a folio for a specific address in @vma, using the appropriate - * NUMA policy. When @vma is not NULL the caller must hold the mmap_lock - * of the mm_struct of the VMA to prevent it from going away. Should be - * used for all allocations for folios that will be mapped into user space. - * - * Return: The folio on success or NULL if allocation fails. + * Return: The page on success or NULL if allocation fails. */ -struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct = *vma, - unsigned long addr, bool hugepage) +struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order, + struct mempolicy *pol, pgoff_t ilx, int nid) { - struct mempolicy *pol; - int node =3D numa_node_id(); - struct folio *folio; - int preferred_nid; - nodemask_t *nmask; + nodemask_t *nodemask; + struct page *page; =20 - pol =3D get_vma_policy(vma, addr); + nodemask =3D policy_nodemask(gfp, pol, ilx, &nid); =20 - if (pol->mode =3D=3D MPOL_INTERLEAVE) { - struct page *page; - unsigned nid; - - nid =3D interleave_nid(pol, vma, addr, PAGE_SHIFT + order); - mpol_cond_put(pol); - gfp |=3D __GFP_COMP; - page =3D alloc_page_interleave(gfp, order, nid); - return page_rmappable_folio(page); - } - - if (pol->mode =3D=3D MPOL_PREFERRED_MANY) { - struct page *page; - - node =3D policy_node(gfp, pol, node); - gfp |=3D __GFP_COMP; - page =3D alloc_pages_preferred_many(gfp, order, node, pol); - mpol_cond_put(pol); - return page_rmappable_folio(page); - } - - if (unlikely(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage)) { - int hpage_node =3D node; + if (pol->mode =3D=3D MPOL_PREFERRED_MANY) + return alloc_pages_preferred_many(gfp, order, nid, nodemask); =20 + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && + /* filter "hugepage" allocation, unless from alloc_pages() */ + order =3D=3D HPAGE_PMD_ORDER && ilx !=3D NO_INTERLEAVE_INDEX) { /* * For hugepage allocation and non-interleave policy which * allows the current node (or other explicitly preferred @@ -2163,39 +2076,68 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order,= struct vm_area_struct *vma, * If the policy is interleave or does not allow the current * node in its nodemask, we allocate the standard way. */ - if (pol->mode =3D=3D MPOL_PREFERRED) - hpage_node =3D first_node(pol->nodes); - - nmask =3D policy_nodemask(gfp, pol); - if (!nmask || node_isset(hpage_node, *nmask)) { - mpol_cond_put(pol); + if (pol->mode !=3D MPOL_INTERLEAVE && + (!nodemask || node_isset(nid, *nodemask))) { /* * First, try to allocate THP only on local node, but * don't reclaim unnecessarily, just compact. */ - folio =3D __folio_alloc_node(gfp | __GFP_THISNODE | - __GFP_NORETRY, order, hpage_node); - + page =3D __alloc_pages_node(nid, + gfp | __GFP_THISNODE | __GFP_NORETRY, order); + if (page || !(gfp & __GFP_DIRECT_RECLAIM)) + return page; /* * If hugepage allocations are configured to always * synchronous compact or the vma has been madvised * to prefer hugepage backing, retry allowing remote * memory with both reclaim and compact as well. */ - if (!folio && (gfp & __GFP_DIRECT_RECLAIM)) - folio =3D __folio_alloc(gfp, order, hpage_node, - nmask); - - goto out; } } =20 - nmask =3D policy_nodemask(gfp, pol); - preferred_nid =3D policy_node(gfp, pol, node); - folio =3D __folio_alloc(gfp, order, preferred_nid, nmask); + page =3D __alloc_pages(gfp, order, nid, nodemask); + + if (unlikely(pol->mode =3D=3D MPOL_INTERLEAVE) && page) { + /* skip NUMA_INTERLEAVE_HIT update if numa stats is disabled */ + if (static_branch_likely(&vm_numa_stat_key) && + page_to_nid(page) =3D=3D nid) { + preempt_disable(); + __count_numa_event(page_zone(page), NUMA_INTERLEAVE_HIT); + preempt_enable(); + } + } + + return page; +} + +/** + * vma_alloc_folio - Allocate a folio for a VMA. + * @gfp: GFP flags. + * @order: Order of the folio. + * @vma: Pointer to VMA. + * @addr: Virtual address of the allocation. Must be inside @vma. + * @hugepage: Unused (was: For hugepages try only preferred node if possib= le). + * + * Allocate a folio for a specific address in @vma, using the appropriate + * NUMA policy. The caller must hold the mmap_lock of the mm_struct of the + * VMA to prevent it from going away. Should be used for all allocations + * for folios that will be mapped into user space, excepting hugetlbfs, and + * excepting where direct use of alloc_pages_mpol() is more appropriate. + * + * Return: The folio on success or NULL if allocation fails. + */ +struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct = *vma, + unsigned long addr, bool hugepage) +{ + struct mempolicy *pol; + pgoff_t ilx; + struct page *page; + + pol =3D get_vma_policy(vma, addr, order, &ilx); + page =3D alloc_pages_mpol(gfp | __GFP_COMP, order, + pol, ilx, numa_node_id()); mpol_cond_put(pol); -out: - return folio; + return page_rmappable_folio(page); } EXPORT_SYMBOL(vma_alloc_folio); =20 @@ -2213,33 +2155,23 @@ EXPORT_SYMBOL(vma_alloc_folio); * flags are used. * Return: The page on success or NULL if allocation fails. */ -struct page *alloc_pages(gfp_t gfp, unsigned order) +struct page *alloc_pages(gfp_t gfp, unsigned int order) { struct mempolicy *pol =3D &default_policy; - struct page *page; - - if (!in_interrupt() && !(gfp & __GFP_THISNODE)) - pol =3D get_task_policy(current); =20 /* * No reference counting needed for current->mempolicy * nor system default_policy */ - if (pol->mode =3D=3D MPOL_INTERLEAVE) - page =3D alloc_page_interleave(gfp, order, interleave_nodes(pol)); - else if (pol->mode =3D=3D MPOL_PREFERRED_MANY) - page =3D alloc_pages_preferred_many(gfp, order, - policy_node(gfp, pol, numa_node_id()), pol); - else - page =3D __alloc_pages(gfp, order, - policy_node(gfp, pol, numa_node_id()), - policy_nodemask(gfp, pol)); + if (!in_interrupt() && !(gfp & __GFP_THISNODE)) + pol =3D get_task_policy(current); =20 - return page; + return alloc_pages_mpol(gfp, order, + pol, NO_INTERLEAVE_INDEX, numa_node_id()); } EXPORT_SYMBOL(alloc_pages); =20 -struct folio *folio_alloc(gfp_t gfp, unsigned order) +struct folio *folio_alloc(gfp_t gfp, unsigned int order) { return page_rmappable_folio(alloc_pages(gfp | __GFP_COMP, order)); } @@ -2310,6 +2242,8 @@ unsigned long alloc_pages_bulk_array_mempolicy(gfp_t = gfp, unsigned long nr_pages, struct page **page_array) { struct mempolicy *pol =3D &default_policy; + nodemask_t *nodemask; + int nid; =20 if (!in_interrupt() && !(gfp & __GFP_THISNODE)) pol =3D get_task_policy(current); @@ -2322,9 +2256,10 @@ unsigned long alloc_pages_bulk_array_mempolicy(gfp_t= gfp, return alloc_pages_bulk_array_preferred_many(gfp, numa_node_id(), pol, nr_pages, page_array); =20 - return __alloc_pages_bulk(gfp, policy_node(gfp, pol, numa_node_id()), - policy_nodemask(gfp, pol), nr_pages, NULL, - page_array); + nid =3D numa_node_id(); + nodemask =3D policy_nodemask(gfp, pol, NO_INTERLEAVE_INDEX, &nid); + return __alloc_pages_bulk(gfp, nid, nodemask, + nr_pages, NULL, page_array); } =20 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst) @@ -2510,23 +2445,21 @@ static void sp_free(struct sp_node *n) int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned= long addr) { struct mempolicy *pol; + pgoff_t ilx; struct zoneref *z; int curnid =3D page_to_nid(page); - unsigned long pgoff; int thiscpu =3D raw_smp_processor_id(); int thisnid =3D cpu_to_node(thiscpu); int polnid =3D NUMA_NO_NODE; int ret =3D NUMA_NO_NODE; =20 - pol =3D get_vma_policy(vma, addr); + pol =3D get_vma_policy(vma, addr, compound_order(page), &ilx); if (!(pol->flags & MPOL_F_MOF)) goto out; =20 switch (pol->mode) { case MPOL_INTERLEAVE: - pgoff =3D vma->vm_pgoff; - pgoff +=3D (addr - vma->vm_start) >> PAGE_SHIFT; - polnid =3D offset_il_node(pol, pgoff); + polnid =3D interleave_nid(pol, ilx); break; =20 case MPOL_PREFERRED: diff --git a/mm/shmem.c b/mm/shmem.c index 69595d341882..aaf44aec2826 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1565,38 +1565,20 @@ static inline struct mempolicy *shmem_get_sbmpol(st= ruct shmem_sb_info *sbinfo) return NULL; } #endif /* CONFIG_NUMA && CONFIG_TMPFS */ -#ifndef CONFIG_NUMA -#define vm_policy vm_private_data -#endif =20 -static void shmem_pseudo_vma_init(struct vm_area_struct *vma, - struct shmem_inode_info *info, pgoff_t index) -{ - /* Create a pseudo vma that just contains the policy */ - vma_init(vma, NULL); - /* Bias interleave by inode number to distribute better across nodes */ - vma->vm_pgoff =3D index + info->vfs_inode.i_ino; - vma->vm_policy =3D mpol_shared_policy_lookup(&info->policy, index); -} +static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *i= nfo, + pgoff_t index, unsigned int order, pgoff_t *ilx); =20 -static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) -{ - /* Drop reference taken by mpol_shared_policy_lookup() */ - mpol_cond_put(vma->vm_policy); -} - -static struct folio *shmem_swapin(swp_entry_t swap, gfp_t gfp, +static struct folio *shmem_swapin_cluster(swp_entry_t swap, gfp_t gfp, struct shmem_inode_info *info, pgoff_t index) { - struct vm_area_struct pvma; + struct mempolicy *mpol; + pgoff_t ilx; struct page *page; - struct vm_fault vmf =3D { - .vma =3D &pvma, - }; =20 - shmem_pseudo_vma_init(&pvma, info, index); - page =3D swap_cluster_readahead(swap, gfp, &vmf); - shmem_pseudo_vma_destroy(&pvma); + mpol =3D shmem_get_pgoff_policy(info, index, 0, &ilx); + page =3D swap_cluster_readahead(swap, gfp, mpol, ilx); + mpol_cond_put(mpol); =20 if (!page) return NULL; @@ -1630,35 +1612,37 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t l= imit_gfp) static struct folio *shmem_alloc_hugefolio(gfp_t gfp, struct shmem_inode_info *info, pgoff_t index) { - struct vm_area_struct pvma; struct address_space *mapping =3D info->vfs_inode.i_mapping; - pgoff_t hindex; - struct folio *folio; + struct mempolicy *mpol; + pgoff_t ilx; + struct page *page; =20 - hindex =3D round_down(index, HPAGE_PMD_NR); - if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, + index =3D round_down(index, HPAGE_PMD_NR); + if (xa_find(&mapping->i_pages, &index, index + HPAGE_PMD_NR - 1, XA_PRESENT)) return NULL; =20 - shmem_pseudo_vma_init(&pvma, info, hindex); - folio =3D vma_alloc_folio(gfp, HPAGE_PMD_ORDER, &pvma, 0, true); - shmem_pseudo_vma_destroy(&pvma); - if (!folio) + mpol =3D shmem_get_pgoff_policy(info, index, HPAGE_PMD_ORDER, &ilx); + page =3D alloc_pages_mpol(gfp, HPAGE_PMD_ORDER, mpol, ilx, numa_node_id()= ); + mpol_cond_put(mpol); + + if (!page) count_vm_event(THP_FILE_FALLBACK); - return folio; + return page_rmappable_folio(page); } =20 static struct folio *shmem_alloc_folio(gfp_t gfp, - struct shmem_inode_info *info, pgoff_t index) + struct shmem_inode_info *info, pgoff_t index) { - struct vm_area_struct pvma; - struct folio *folio; + struct mempolicy *mpol; + pgoff_t ilx; + struct page *page; =20 - shmem_pseudo_vma_init(&pvma, info, index); - folio =3D vma_alloc_folio(gfp, 0, &pvma, 0, false); - shmem_pseudo_vma_destroy(&pvma); + mpol =3D shmem_get_pgoff_policy(info, index, 0, &ilx); + page =3D alloc_pages_mpol(gfp, 0, mpol, ilx, numa_node_id()); + mpol_cond_put(mpol); =20 - return folio; + return (struct folio *)page; } =20 static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *i= node, @@ -1848,7 +1832,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, count_memcg_event_mm(charge_mm, PGMAJFAULT); } /* Here we actually start the io */ - folio =3D shmem_swapin(swap, gfp, info, index); + folio =3D shmem_swapin_cluster(swap, gfp, info, index); if (!folio) { error =3D -ENOMEM; goto failed; @@ -2330,15 +2314,41 @@ static int shmem_set_policy(struct vm_area_struct *= vma, struct mempolicy *mpol) } =20 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, - unsigned long addr) + unsigned long addr, pgoff_t *ilx) { struct inode *inode =3D file_inode(vma->vm_file); pgoff_t index; =20 + /* + * Bias interleave by inode number to distribute better across nodes; + * but this interface is independent of which page order is used, so + * supplies only that bias, letting caller apply the offset (adjusted + * by page order, as in shmem_get_pgoff_policy() and get_vma_policy()). + */ + *ilx =3D inode->i_ino; index =3D ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); } -#endif + +static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *i= nfo, + pgoff_t index, unsigned int order, pgoff_t *ilx) +{ + struct mempolicy *mpol; + + /* Bias interleave by inode number to distribute better across nodes */ + *ilx =3D info->vfs_inode.i_ino + (index >> order); + + mpol =3D mpol_shared_policy_lookup(&info->policy, index); + return mpol ? mpol : get_task_policy(current); +} +#else +static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *i= nfo, + pgoff_t index, unsigned int order, pgoff_t *ilx) +{ + *ilx =3D 0; + return NULL; +} +#endif /* CONFIG_NUMA */ =20 int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) { diff --git a/mm/swap.h b/mm/swap.h index 8a3c7a0ace4f..73c332ee4d91 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -2,6 +2,8 @@ #ifndef _MM_SWAP_H #define _MM_SWAP_H =20 +struct mempolicy; + #ifdef CONFIG_SWAP #include /* for bio_end_io_t */ =20 @@ -48,11 +50,10 @@ struct page *read_swap_cache_async(swp_entry_t entry, g= fp_t gfp_mask, unsigned long addr, struct swap_iocb **plug); struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, - struct vm_area_struct *vma, - unsigned long addr, + struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated); struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t flag, - struct vm_fault *vmf); + struct mempolicy *mpol, pgoff_t ilx); struct page *swapin_readahead(swp_entry_t entry, gfp_t flag, struct vm_fault *vmf); =20 @@ -80,7 +81,7 @@ static inline void show_swap_cache_info(void) } =20 static inline struct page *swap_cluster_readahead(swp_entry_t entry, - gfp_t gfp_mask, struct vm_fault *vmf) + gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx) { return NULL; } diff --git a/mm/swap_state.c b/mm/swap_state.c index b3b14bd0dd64..116d4d8a930e 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -410,8 +411,8 @@ struct folio *filemap_get_incore_folio(struct address_s= pace *mapping, } =20 struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, - struct vm_area_struct *vma, unsigned long addr, - bool *new_page_allocated) + struct mempolicy *mpol, pgoff_t ilx, + bool *new_page_allocated) { struct swap_info_struct *si; struct folio *folio; @@ -453,7 +454,8 @@ struct page *__read_swap_cache_async(swp_entry_t entry,= gfp_t gfp_mask, * before marking swap_map SWAP_HAS_CACHE, when -EEXIST will * cause any racers to loop around until we add it to cache. */ - folio =3D vma_alloc_folio(gfp_mask, 0, vma, addr, false); + folio =3D (struct folio *)alloc_pages_mpol(gfp_mask, 0, + mpol, ilx, numa_node_id()); if (!folio) goto fail_put_swap; =20 @@ -528,14 +530,19 @@ struct page *read_swap_cache_async(swp_entry_t entry,= gfp_t gfp_mask, struct vm_area_struct *vma, unsigned long addr, struct swap_iocb **plug) { - bool page_was_allocated; - struct page *retpage =3D __read_swap_cache_async(entry, gfp_mask, - vma, addr, &page_was_allocated); + bool page_allocated; + struct mempolicy *mpol; + pgoff_t ilx; + struct page *page; =20 - if (page_was_allocated) - swap_readpage(retpage, false, plug); + mpol =3D get_vma_policy(vma, addr, 0, &ilx); + page =3D __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + &page_allocated); + mpol_cond_put(mpol); =20 - return retpage; + if (page_allocated) + swap_readpage(page, false, plug); + return page; } =20 static unsigned int __swapin_nr_pages(unsigned long prev_offset, @@ -603,7 +610,8 @@ static unsigned long swapin_nr_pages(unsigned long offs= et) * swap_cluster_readahead - swap in pages in hope we need them soon * @entry: swap entry of this memory * @gfp_mask: memory allocation flags - * @vmf: fault information + * @mpol: NUMA memory allocation policy to be applied + * @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE * * Returns the struct page for entry and addr, after queueing swapin. * @@ -612,13 +620,12 @@ static unsigned long swapin_nr_pages(unsigned long of= fset) * because it doesn't cost us any seek time. We also make sure to queue * the 'original' request together with the readahead ones... * - * This has been extended to use the NUMA policies from the mm triggering - * the readahead. - * - * Caller must hold read mmap_lock if vmf->vma is not NULL. + * Note: it is intentional that the same NUMA policy and interleave index + * are used for every page of the readahead: neighbouring pages on swap + * are fairly likely to have been swapped out from the same node. */ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, - struct vm_fault *vmf) + struct mempolicy *mpol, pgoff_t ilx) { struct page *page; unsigned long entry_offset =3D swp_offset(entry); @@ -629,8 +636,6 @@ struct page *swap_cluster_readahead(swp_entry_t entry, = gfp_t gfp_mask, struct blk_plug plug; struct swap_iocb *splug =3D NULL; bool page_allocated; - struct vm_area_struct *vma =3D vmf->vma; - unsigned long addr =3D vmf->address; =20 mask =3D swapin_nr_pages(offset) - 1; if (!mask) @@ -648,8 +653,8 @@ struct page *swap_cluster_readahead(swp_entry_t entry, = gfp_t gfp_mask, for (offset =3D start_offset; offset <=3D end_offset ; offset++) { /* Ok, do the async read-ahead now */ page =3D __read_swap_cache_async( - swp_entry(swp_type(entry), offset), - gfp_mask, vma, addr, &page_allocated); + swp_entry(swp_type(entry), offset), + gfp_mask, mpol, ilx, &page_allocated); if (!page) continue; if (page_allocated) { @@ -663,11 +668,14 @@ struct page *swap_cluster_readahead(swp_entry_t entry= , gfp_t gfp_mask, } blk_finish_plug(&plug); swap_read_unplug(splug); - lru_add_drain(); /* Push any new pages onto the LRU now */ skip: /* The page was likely read above, so no need for plugging here */ - return read_swap_cache_async(entry, gfp_mask, vma, addr, NULL); + page =3D __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + &page_allocated); + if (page_allocated) + swap_readpage(page, false, NULL); + return page; } =20 int init_swap_address_space(unsigned int type, unsigned long nr_pages) @@ -765,8 +773,10 @@ static void swap_ra_info(struct vm_fault *vmf, =20 /** * swap_vma_readahead - swap in pages in hope we need them soon - * @fentry: swap entry of this memory + * @targ_entry: swap entry of the targeted memory * @gfp_mask: memory allocation flags + * @mpol: NUMA memory allocation policy to be applied + * @targ_ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE * @vmf: fault information * * Returns the struct page for entry and addr, after queueing swapin. @@ -777,16 +787,17 @@ static void swap_ra_info(struct vm_fault *vmf, * Caller must hold read mmap_lock if vmf->vma is not NULL. * */ -static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask, +static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_m= ask, + struct mempolicy *mpol, pgoff_t targ_ilx, struct vm_fault *vmf) { struct blk_plug plug; struct swap_iocb *splug =3D NULL; - struct vm_area_struct *vma =3D vmf->vma; struct page *page; pte_t *pte =3D NULL, pentry; unsigned long addr; swp_entry_t entry; + pgoff_t ilx; unsigned int i; bool page_allocated; struct vma_swap_readahead ra_info =3D { @@ -798,9 +809,10 @@ static struct page *swap_vma_readahead(swp_entry_t fen= try, gfp_t gfp_mask, goto skip; =20 addr =3D vmf->address - (ra_info.offset * PAGE_SIZE); + ilx =3D targ_ilx - ra_info.offset; =20 blk_start_plug(&plug); - for (i =3D 0; i < ra_info.nr_pte; i++, addr +=3D PAGE_SIZE) { + for (i =3D 0; i < ra_info.nr_pte; i++, ilx++, addr +=3D PAGE_SIZE) { if (!pte++) { pte =3D pte_offset_map(vmf->pmd, addr); if (!pte) @@ -814,8 +826,8 @@ static struct page *swap_vma_readahead(swp_entry_t fent= ry, gfp_t gfp_mask, continue; pte_unmap(pte); pte =3D NULL; - page =3D __read_swap_cache_async(entry, gfp_mask, vma, - addr, &page_allocated); + page =3D __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + &page_allocated); if (!page) continue; if (page_allocated) { @@ -834,8 +846,11 @@ static struct page *swap_vma_readahead(swp_entry_t fen= try, gfp_t gfp_mask, lru_add_drain(); skip: /* The page was likely read above, so no need for plugging here */ - return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address, - NULL); + page =3D __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, + &page_allocated); + if (page_allocated) + swap_readpage(page, false, NULL); + return page; } =20 /** @@ -853,9 +868,16 @@ static struct page *swap_vma_readahead(swp_entry_t fen= try, gfp_t gfp_mask, struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, struct vm_fault *vmf) { - return swap_use_vma_readahead() ? - swap_vma_readahead(entry, gfp_mask, vmf) : - swap_cluster_readahead(entry, gfp_mask, vmf); + struct mempolicy *mpol; + pgoff_t ilx; + struct page *page; + + mpol =3D get_vma_policy(vmf->vma, vmf->address, 0, &ilx); + page =3D swap_use_vma_readahead() ? + swap_vma_readahead(entry, gfp_mask, mpol, ilx, vmf) : + swap_cluster_readahead(entry, gfp_mask, mpol, ilx); + mpol_cond_put(mpol); + return page; } =20 #ifdef CONFIG_SYSFS --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 471DDCE7A8C for ; Mon, 25 Sep 2023 08:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232836AbjIYIfR (ORCPT ); Mon, 25 Sep 2023 04:35:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjIYIfP (ORCPT ); Mon, 25 Sep 2023 04:35:15 -0400 Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2B299D for ; Mon, 25 Sep 2023 01:35:07 -0700 (PDT) Received: by mail-yb1-xb30.google.com with SMTP id 3f1490d57ef6-d86574d9bcaso4056726276.2 for ; Mon, 25 Sep 2023 01:35:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630907; x=1696235707; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=ezzNrLDnqV/k6jp9dH7Pe4D9g9Anl40AHbeIOwXqGUY=; b=SaBXJB1aKffZAvXpoDnGUErfY3LnH8PquRQ/9iH/krsoaxMMyvBhBhwWQogbksXKkm 8TH2P98NSQv1LhnfKsD82hvOaRf4ynwQJPOMZAgbZyvs+JqaPd6m3U/zL+TYOYjnxhF3 ii7SrPG/1AMeTCgroBMTbYUhu3v+1ewJD/3cFnLUUJNMAHEhL4/mvpijsDNe5CPseBI4 nDAiE/0ZmPlrV8HYwPVLHGgETExfYGjskV2OxZWJYOgOO6Z5sc2SwswZ14FNPgfPdg9a 2ymjRWqounhxyUyayp64lUKxvxzc9+IWEf/d+QrLo4Q2DU9BeMKjd4WcPbUEBk7AKECZ RWjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630907; x=1696235707; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ezzNrLDnqV/k6jp9dH7Pe4D9g9Anl40AHbeIOwXqGUY=; b=Qn0t21wpRh5QEIbKgVVAJAnVez3chxI5OkKTjvLRgb6EMya2UsXMwxPvWpU6ko4898 kcfE3bXGl16TSWR/MThw69qUxVL58A5A8L0+UWPCNW4uTXB8Kb4/3fxsDhSShvHUKMA2 K8QgasJ8NIUQHM115HLXCbisHO3NgdNN5pcSucgmLgUsHLtAmgG1wcPc7d2qFN7Xf1v/ lPn3kd5FhVCmU9TEVCNDXIsIbWXV2jBcJWfxaqhYgxs46CQIqe5gvCu6uph2+2Kx82e0 y8Kxi87x7Xi1CylNhEVInI4/W+9+sey+XmyWZ3zK51Y3apJrr3WMlIIYI5N6yvelzWbf XsqQ== X-Gm-Message-State: AOJu0YwPXrO50MTjCGvbm+NnJAQOiLDAsXoKnsRVeBpOpmxzDvi2doYS SN9jrntUTAlj4D4omOLwg3q5Qg== X-Google-Smtp-Source: AGHT+IFp0uZwCYh9f6Ww0ZKvnQPx4qXewNgiqFJbqCBgiDfI2aX3U5liWK+g0EmK6zec2o4LhHt/pg== X-Received: by 2002:a05:690c:d93:b0:59f:8026:4260 with SMTP id da19-20020a05690c0d9300b0059f80264260mr2238947ywb.24.1695630906593; Mon, 25 Sep 2023 01:35:06 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id c188-20020a0df3c5000000b0059293c8d70csm2293994ywf.132.2023.09.25.01.35.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:35:05 -0700 (PDT) Date: Mon, 25 Sep 2023 01:35:03 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 11/12] mempolicy: mmap_lock is not needed while migrating folios In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: <73183de1-6529-b146-f2cc-fcd5b812166@google.com> References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" mbind(2) holds down_write of current task's mmap_lock throughout (exclusive because it needs to set the new mempolicy on the vmas); migrate_pages(2) holds down_read of pid's mmap_lock throughout. They both hold mmap_lock across the internal migrate_pages(), under which all new page allocations (huge or small) are made. I'm nervous about it; and migrate_pages() certainly does not need mmap_lock itself. It's done this way for mbind(2), because its page allocator is vma_alloc_folio() or alloc_hugetlb_folio_vma(), both of which depend on vma and address. Now that we have alloc_pages_mpol(), depending on (refcounted) memory policy and interleave index, mbind(2) can be modified to use that or alloc_hugetlb_folio_nodemask(), and then not need mmap_lock across the internal migrate_pages() at all: add alloc_migration_target_by_mpol() to replace mbind's new_page(). (After that change, alloc_hugetlb_folio_vma() is used by nothing but a userfaultfd function: move it out of hugetlb.h and into the #ifdef.) migrate_pages(2) has chosen its target node before migrating, so can continue to use the standard alloc_migration_target(); but let it take and drop mmap_lock just around migrate_to_node()'s queue_pages_range(): neither the node-to-node calculations nor the page migrations need it. It seems unlikely, but it is conceivable that some userspace depends on the kernel's mmap_lock exclusion here, instead of doing its own locking: more likely in a testsuite than in real life. It is also possible, of course, that some pages on the list will be munmapped by another thread before they are migrated, or a newer memory policy applied to the range by that time: but such races could happen before, as soon as mmap_lock was dropped, so it does not appear to be a concern. Signed-off-by: Hugh Dickins --- include/linux/hugetlb.h | 9 ----- mm/hugetlb.c | 38 +++++++++--------- mm/mempolicy.c | 85 +++++++++++++++++++++-------------------- 3 files changed, 64 insertions(+), 68 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 6522eb3cd007..9c4265c73f76 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -714,8 +714,6 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct= *vma, unsigned long addr, int avoid_reserve); struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred= _nid, nodemask_t *nmask, gfp_t gfp_mask); -struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_str= uct *vma, - unsigned long address); int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *m= apping, pgoff_t idx); void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, @@ -1024,13 +1022,6 @@ alloc_hugetlb_folio_nodemask(struct hstate *h, int p= referred_nid, return NULL; } =20 -static inline struct folio *alloc_hugetlb_folio_vma(struct hstate *h, - struct vm_area_struct *vma, - unsigned long address) -{ - return NULL; -} - static inline int __alloc_bootmem_huge_page(struct hstate *h) { return 0; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ba6d39b71cb1..1af54dbbd7cc 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2479,24 +2479,6 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hs= tate *h, int preferred_nid, return alloc_migrate_hugetlb_folio(h, gfp_mask, preferred_nid, nmask); } =20 -/* mempolicy aware migration callback */ -struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_str= uct *vma, - unsigned long address) -{ - struct mempolicy *mpol; - nodemask_t *nodemask; - struct folio *folio; - gfp_t gfp_mask; - int node; - - gfp_mask =3D htlb_alloc_mask(h); - node =3D huge_node(vma, address, gfp_mask, &mpol, &nodemask); - folio =3D alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask); - mpol_cond_put(mpol); - - return folio; -} - /* * Increase the hugetlb pool such that it can accommodate a reservation * of size 'delta'. @@ -6225,6 +6207,26 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struc= t vm_area_struct *vma, } =20 #ifdef CONFIG_USERFAULTFD +/* + * Can probably be eliminated, but still used by hugetlb_mfill_atomic_pte(= ). + */ +static struct folio *alloc_hugetlb_folio_vma(struct hstate *h, + struct vm_area_struct *vma, unsigned long address) +{ + struct mempolicy *mpol; + nodemask_t *nodemask; + struct folio *folio; + gfp_t gfp_mask; + int node; + + gfp_mask =3D htlb_alloc_mask(h); + node =3D huge_node(vma, address, gfp_mask, &mpol, &nodemask); + folio =3D alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask); + mpol_cond_put(mpol); + + return folio; +} + /* * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomi= c_pte * with modifications for hugetlb pages. diff --git a/mm/mempolicy.c b/mm/mempolicy.c index d74df1e1b14a..74b1894d29c1 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -417,6 +417,8 @@ static const struct mempolicy_operations mpol_ops[MPOL_= MAX] =3D { =20 static bool migrate_folio_add(struct folio *folio, struct list_head *folio= list, unsigned long flags); +static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *pol, + pgoff_t ilx, int *nid); =20 static bool strictly_unmovable(unsigned long flags) { @@ -1040,6 +1042,8 @@ static long migrate_to_node(struct mm_struct *mm, int= source, int dest, node_set(source, nmask); =20 VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))); + + mmap_read_lock(mm); vma =3D find_vma(mm, 0); =20 /* @@ -1050,6 +1054,7 @@ static long migrate_to_node(struct mm_struct *mm, int= source, int dest, */ nr_failed =3D queue_pages_range(mm, vma->vm_start, mm->task_size, &nmask, flags | MPOL_MF_DISCONTIG_OK, &pagelist); + mmap_read_unlock(mm); =20 if (!list_empty(&pagelist)) { err =3D migrate_pages(&pagelist, alloc_migration_target, NULL, @@ -1078,8 +1083,6 @@ int do_migrate_pages(struct mm_struct *mm, const node= mask_t *from, =20 lru_cache_disable(); =20 - mmap_read_lock(mm); - /* * Find a 'source' bit set in 'tmp' whose corresponding 'dest' * bit in 'to' is not also set in 'tmp'. Clear the found 'source' @@ -1159,7 +1162,6 @@ int do_migrate_pages(struct mm_struct *mm, const node= mask_t *from, if (err < 0) break; } - mmap_read_unlock(mm); =20 lru_cache_enable(); if (err < 0) @@ -1168,44 +1170,38 @@ int do_migrate_pages(struct mm_struct *mm, const no= demask_t *from, } =20 /* - * Allocate a new page for page migration based on vma policy. - * Start by assuming the page is mapped by the same vma as contains @start. - * Search forward from there, if not. N.B., this assumes that the - * list of pages handed to migrate_pages()--which is how we get here-- - * is in virtual address order. + * Allocate a new folio for page migration, according to NUMA mempolicy. */ -static struct folio *new_folio(struct folio *src, unsigned long start) +static struct folio *alloc_migration_target_by_mpol(struct folio *src, + unsigned long private) { - struct vm_area_struct *vma; - unsigned long address; - VMA_ITERATOR(vmi, current->mm, start); - gfp_t gfp =3D GFP_HIGHUSER_MOVABLE | __GFP_RETRY_MAYFAIL; - - for_each_vma(vmi, vma) { - address =3D page_address_in_vma(&src->page, vma); - if (address !=3D -EFAULT) - break; - } - - /* - * __get_vma_policy() now expects a genuine non-NULL vma. Return NULL - * when the page can no longer be located in a vma: that is not ideal - * (migrate_pages() will give up early, presuming ENOMEM), but good - * enough to avoid a crash by syzkaller or concurrent holepunch. - */ - if (!vma) - return NULL; + struct mempolicy *pol =3D (struct mempolicy *)private; + pgoff_t ilx =3D 0; /* improve on this later */ + struct page *page; + unsigned int order; + int nid =3D numa_node_id(); + gfp_t gfp; =20 if (folio_test_hugetlb(src)) { - return alloc_hugetlb_folio_vma(folio_hstate(src), - vma, address); + nodemask_t *nodemask; + struct hstate *h; + + ilx +=3D src->index; /* HugeTLBfs indexes in hpage_size */ + h =3D folio_hstate(src); + gfp =3D htlb_alloc_mask(h); + nodemask =3D policy_nodemask(gfp, pol, ilx, &nid); + return alloc_hugetlb_folio_nodemask(h, nid, nodemask, gfp); } =20 if (folio_test_large(src)) gfp =3D GFP_TRANSHUGE; + else + gfp =3D GFP_HIGHUSER_MOVABLE | __GFP_RETRY_MAYFAIL | __GFP_COMP; =20 - return vma_alloc_folio(gfp, folio_order(src), vma, address, - folio_test_large(src)); + order =3D folio_order(src); + ilx +=3D src->index >> order; + page =3D alloc_pages_mpol(gfp, order, pol, ilx, nid); + return page_rmappable_folio(page); } #else =20 @@ -1221,7 +1217,8 @@ int do_migrate_pages(struct mm_struct *mm, const node= mask_t *from, return -ENOSYS; } =20 -static struct folio *new_folio(struct folio *src, unsigned long start) +static struct folio *alloc_migration_target_by_mpol(struct folio *src, + unsigned long private) { return NULL; } @@ -1295,6 +1292,7 @@ static long do_mbind(unsigned long start, unsigned lo= ng len, =20 if (nr_failed < 0) { err =3D nr_failed; + nr_failed =3D 0; } else { vma_iter_init(&vmi, mm, start); prev =3D vma_prev(&vmi); @@ -1305,19 +1303,24 @@ static long do_mbind(unsigned long start, unsigned = long len, } } =20 - if (!err) { - if (!list_empty(&pagelist)) { - nr_failed |=3D migrate_pages(&pagelist, new_folio, NULL, - start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND, NULL); + mmap_write_unlock(mm); + + if (!err && !list_empty(&pagelist)) { + /* Convert MPOL_DEFAULT's NULL to task or default policy */ + if (!new) { + new =3D get_task_policy(current); + mpol_get(new); } - if (nr_failed && (flags & MPOL_MF_STRICT)) - err =3D -EIO; + nr_failed |=3D migrate_pages(&pagelist, + alloc_migration_target_by_mpol, NULL, + (unsigned long)new, MIGRATE_SYNC, + MR_MEMPOLICY_MBIND, NULL); } =20 + if (nr_failed && (flags & MPOL_MF_STRICT)) + err =3D -EIO; if (!list_empty(&pagelist)) putback_movable_pages(&pagelist); - - mmap_write_unlock(mm); mpol_out: mpol_put(new); if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) --=20 2.35.3 From nobody Fri Feb 13 12:29:15 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D074FCE7A89 for ; Mon, 25 Sep 2023 08:36:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232964AbjIYIgf (ORCPT ); Mon, 25 Sep 2023 04:36:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232934AbjIYIgb (ORCPT ); Mon, 25 Sep 2023 04:36:31 -0400 Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78BDBAF for ; Mon, 25 Sep 2023 01:36:25 -0700 (PDT) Received: by mail-yb1-xb31.google.com with SMTP id 3f1490d57ef6-d8673a90f56so3127963276.0 for ; Mon, 25 Sep 2023 01:36:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695630984; x=1696235784; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=OU+65snbrvpR+Q3ulaNy8J1cYx1gkkm9B2VLdfzl674=; b=ua6Fhr0pbpKnRPggu3qgJu7o4SR8jVlEu/g22x22zuduOmCYVTIkrjJmC/r6pE/04Z n272kMRGZYS3vxpULjLZYT4N0JzOjPaZ2GQ8H/3Ys4vbAc/acyO+eD0q3+RzIk7TCfQB mTIqAjA/9Y1vkCJ9vhfX4WkE1v7sm/kzDlGOel2gGcLms1Rj7AYssafxcW8xHOtHUbYE ip8cZJp9mGu9P9IgNPB43ynJLZHc3nqtSRt0PytpcepfW658W1m/58f/piLLluurSZkl Zlo3eNqhlH2EDpPc2UnoLKSxPpJoA2yDEAGb/p9APTHeVMjKIxePwKVI9rEz9Rn1HxV2 Eo/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695630984; x=1696235784; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OU+65snbrvpR+Q3ulaNy8J1cYx1gkkm9B2VLdfzl674=; b=UejHUIMdGkaNglbnRcO0TS96c+er3lATTtNcv/iwf0Ezg/6HRDpA/j1Zxnh6N7J5QW XYWYQzewW8NXdkcxaRGTNZwWsNNHOgbbi4+abBGeEnYYYJD17x7lTJjqXGtsM3uz5Brc 8TzUC6jSUjVjuYdUjHGmnq7XEyJzi7RhjpaI5atOSegrGM7ICx6lRPAtHMWjJ4+pLv4z HRjPCkggdzldfvQONQBcJtQsPVrgZYUSpohYTeXIJiSvSC/vnRTScSmxRttg6v5e7vf7 66636pV/9zzTzXYGfr9lN1KFkcgF3plNNbIIlntTKArY/AWqb6ZOiFJBPRbGzi9potnw evAw== X-Gm-Message-State: AOJu0YyZfxPXDp6qNSb+j+SlSDyk1m4DhV5GQpreuZp2eiiqQy2TVJW/ rfZhqiF55iDVLh9GBVPgV+e5yQ== X-Google-Smtp-Source: AGHT+IHXAX1QQV6PVx66keFoYyD4CtYdpXj+z0LhxW9OnhchJFVwm0d282m2rz466G2xal6CpuLvhA== X-Received: by 2002:a25:c092:0:b0:d53:f98f:8018 with SMTP id c140-20020a25c092000000b00d53f98f8018mr5028309ybf.65.1695630984516; Mon, 25 Sep 2023 01:36:24 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id m17-20020a258011000000b00d85abbdc93esm2014417ybk.12.2023.09.25.01.36.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 01:36:23 -0700 (PDT) Date: Mon, 25 Sep 2023 01:36:21 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Andi Kleen , Christoph Lameter , Matthew Wilcox , Mike Kravetz , David Hildenbrand , Suren Baghdasaryan , Yang Shi , Sidhartha Kumar , Vishal Moola , Kefeng Wang , Greg Kroah-Hartman , Tejun Heo , Mel Gorman , Michal Hocko , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 12/12] mempolicy: migration attempt to match interleave nodes In-Reply-To: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> Message-ID: <21a2895b-4590-a0f4-c81c-67e059494583@google.com> References: <2d872cef-7787-a7ca-10e-9d45a64c80b4@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Improve alloc_migration_target_by_mpol()'s treatment of MPOL_INTERLEAVE. Make an effort in do_mbind(), to identify the correct interleave index for the first page to be migrated, so that it and all subsequent pages from the same vma will be targeted to precisely their intended nodes. Pages from following vmas will still be interleaved from the requested nodemask, but perhaps starting from a different base. Whether this is worth doing at all, or worth improving further, is arguable: queue_folio_required() is right not to care about the precise placement on interleaved nodes; but this little effort seems appropriate. Signed-off-by: Hugh Dickins --- mm/mempolicy.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 74b1894d29c1..7bb9ff69879b 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -430,6 +430,11 @@ static bool strictly_unmovable(unsigned long flags) MPOL_MF_STRICT; } =20 +struct migration_mpol { /* for alloc_migration_target_by_mpol() */ + struct mempolicy *pol; + pgoff_t ilx; +}; + struct queue_pages { struct list_head *pagelist; unsigned long flags; @@ -1175,8 +1180,9 @@ int do_migrate_pages(struct mm_struct *mm, const node= mask_t *from, static struct folio *alloc_migration_target_by_mpol(struct folio *src, unsigned long private) { - struct mempolicy *pol =3D (struct mempolicy *)private; - pgoff_t ilx =3D 0; /* improve on this later */ + struct migration_mpol *mmpol =3D (struct migration_mpol *)private; + struct mempolicy *pol =3D mmpol->pol; + pgoff_t ilx =3D mmpol->ilx; struct page *page; unsigned int order; int nid =3D numa_node_id(); @@ -1231,6 +1237,7 @@ static long do_mbind(unsigned long start, unsigned lo= ng len, struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma, *prev; struct vma_iterator vmi; + struct migration_mpol mmpol; struct mempolicy *new; unsigned long end; long err; @@ -1311,9 +1318,48 @@ static long do_mbind(unsigned long start, unsigned l= ong len, new =3D get_task_policy(current); mpol_get(new); } + mmpol.pol =3D new; + mmpol.ilx =3D 0; + + /* + * In the interleaved case, attempt to allocate on exactly the + * targeted nodes, for the first VMA to be migrated; for later + * VMAs, the nodes will still be interleaved from the targeted + * nodemask, but one by one may be selected differently. + */ + if (new->mode =3D=3D MPOL_INTERLEAVE) { + struct page *page; + unsigned int order; + unsigned long addr =3D -EFAULT; + + list_for_each_entry(page, &pagelist, lru) { + if (!PageKsm(page)) + break; + } + if (!list_entry_is_head(page, &pagelist, lru)) { + vma_iter_init(&vmi, mm, start); + for_each_vma_range(vmi, vma, end) { + addr =3D page_address_in_vma(page, vma); + if (addr !=3D -EFAULT) + break; + } + } + if (addr !=3D -EFAULT) { + order =3D compound_order(page); + /* We already know the pol, but not the ilx */ + mpol_cond_put(get_vma_policy(vma, addr, order, + &mmpol.ilx)); + /* HugeTLBfs indexes in hpage_size */ + if (order && PageHuge(page)) + order =3D 0; + /* Set base from which to increment by index */ + mmpol.ilx -=3D page->index >> order; + } + } + nr_failed |=3D migrate_pages(&pagelist, alloc_migration_target_by_mpol, NULL, - (unsigned long)new, MIGRATE_SYNC, + (unsigned long)&mmpol, MIGRATE_SYNC, MR_MEMPOLICY_MBIND, NULL); } =20 --=20 2.35.3