From patchwork Mon Jul 29 09:10:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13744633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02417C3DA4A for ; Mon, 29 Jul 2024 09:11:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07EAD6B0099; Mon, 29 Jul 2024 05:10:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFB306B009A; Mon, 29 Jul 2024 05:10:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB31C6B009B; Mon, 29 Jul 2024 05:10:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A7ECF6B0099 for ; Mon, 29 Jul 2024 05:10:50 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 236AA120223 for ; Mon, 29 Jul 2024 09:10:50 +0000 (UTC) X-FDA: 82392220260.26.0F5DDDB Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf03.hostedemail.com (Postfix) with ESMTP id EDAF120021 for ; Mon, 29 Jul 2024 09:10:47 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf03.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722244188; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HUoJ5P1SSuIDOAYoJjkR+eCpNCd+zUZ3Jg0HpuhS4Gw=; b=X+ahm+NVgFRhsCb2tNkmxIdlBAiCx+od3HMeA0/mlN+68bVSlWSoL56xgEfLwSozeb14Jh U0iZWjQ2f+tGI/1NIbuopZLWOVMqJwjdAXSusHFFzGCq6zhS8rJ4/T8P+cMLoO0XLffAX7 /wyM77pEJkf9/ezerrinZyfb+ipkcOI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722244188; a=rsa-sha256; cv=none; b=DVhfy2hedA+NTsms59z45dPrGZQ20c/i7uZ+E1933YhqWATPEj1lBSP/tOu8Adpemiuc00 7T9v05T+gZDDx6aJXp6pCYztOSLOtGURMTganaKo9wM25HrG3sbCeTwO0+5T8u6TVNHfxK hunZkLjqkMXfi8wfZiIBRAhvo8mJ9dw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf03.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 9E4FF1F794; Mon, 29 Jul 2024 09:10:46 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 4411D1368A; Mon, 29 Jul 2024 09:10:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id iPArC5Vcp2Z4BgAAD6G6ig (envelope-from ); Mon, 29 Jul 2024 09:10:45 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Peter Xu , Muchun Song , David Hildenbrand , Donet Tom , Matthew Wilcox , Vlastimil Babka , Michal Hocko , Oscar Salvador Subject: [PATCH v2 9/9] mm: Consolidate common checks in hugetlb_mmap_check_and_align Date: Mon, 29 Jul 2024 11:10:18 +0200 Message-ID: <20240729091018.2152-10-osalvador@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240729091018.2152-1-osalvador@suse.de> References: <20240729091018.2152-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: EDAF120021 X-Stat-Signature: gdfwopnig8u5mcfjkyp3mch5uyhuoxcn X-Rspam-User: X-HE-Tag: 1722244247-542115 X-HE-Meta: U2FsdGVkX18+Km3IerfiyhfU0YiVk3zSofwdMvY5BLBfNjYc3AbpdtTyrckqccewMK6vGMWSJmO1OTYb2qbRoRAbFabXNaWoFO3VPaFw6vAGBHOWYL6FYks5K7iKh1RLXCuCrSbjFgvcsINJdviYUGnGbvacMtZkcfYujdvv9XDWm3QkHUluctOwOjRRW2AnOXdVRTAB9zN0pMbpoEBotnJMvkwA1WlcYlNL3reAkeUnoeKp+GZSpGw2Ce28IGQfhAywlhaFCBWdtzeWQ6kthKqN3oSfvYbhI8Ap53FODrdEPFTrl/JcpXe1U1DAhwIvsQjKRNYu1vHZAt4Yw9VGlLNm7KPxw95AfZfQUuqRbssK8QDf8VnPlu8qvvuMv35j71/S1XhbTRcRjUHYcVtZwrZmDQShvWmfq2u6UCXvNhVG5eaFGSDjjzcJHoIle3zTdoRdu2CMFMkWilqtpHsNhykBFd367IQAN/g+KdCNcDEtgR+jaemjI2V5CFdWiN/5B9/G5+rEbdowvMTJlHYNuaCKz+wnlbXlI8g2TCyTshO6qT4044yG8TlGV+KotVHR8mYm0p9mO24k4o3P17dreXacNDsiVu1ZXqnfA6VdF2Flvjdq/KhzWVfOxE5zO4aCzrZSmu8j0kEDAilZ21yyXjf0ZSIWZF+ENdTd5IbzGNmvfxf+tLwoHaolt16BDePLaurp1bylozeBaLDe4wkrfbLTEnWBUQSCRz73/o1u1p+Ua7RBxccuL6moFz6u87cdzW4D7v1D2yC2H8EpmI+PLUUuFoy8ZviZjDPKg83O9k1rVcTS5k6TSEkkalK+qxRKwJwgNuNTPNz1gOMfhYe1nCwXsDsddAxv0PxrOge4oqWI7xOBJYPJf7HSXrttrv5ot2anp8UMKkm4vApadeMwZmPS4n+lVNasBGFVJ/PA2oW3bSYF+XkfrRJGAicXj+VLW/aJ53MSQFavi47tzrR 2H9WLvpc mIhmuSn0bmiVeyvJeQL4M+MmQHRQX+gmQ/NxVvR7HHaNBWSRT46o23bBFQFocZ4mCYqDnAGsnq00cpYqpD4T1ANJ9SsHIGch+/1OICRlm5sS+4i0XwD4yiaSjGX65SB05RuFWeTxWLfWeKRf5aQozjdgVvpsK2QroOTH86GclKQhi7R0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: prepare_hugepage_range() performs almost the same checks for all architectures that define it, with the exception of mips and loongarch that also check for overflows. The rest checks for the addr and len to be properly aligned, so we can move that to the generic hugetlb_mmap_check_and_align() function and get rid of a fair amount of duplicated code. Signed-off-by: Oscar Salvador --- arch/loongarch/include/asm/hugetlb.h | 4 ---- arch/mips/include/asm/hugetlb.h | 4 ---- arch/parisc/include/asm/hugetlb.h | 15 --------------- arch/s390/include/asm/hugetlb.h | 17 ----------------- arch/sh/include/asm/hugetlb.h | 15 --------------- fs/hugetlbfs/inode.c | 8 ++++++-- include/asm-generic/hugetlb.h | 7 ------- 7 files changed, 6 insertions(+), 64 deletions(-) diff --git a/arch/loongarch/include/asm/hugetlb.h b/arch/loongarch/include/asm/hugetlb.h index aa44b3fe43dd..107566c98938 100644 --- a/arch/loongarch/include/asm/hugetlb.h +++ b/arch/loongarch/include/asm/hugetlb.h @@ -18,10 +18,6 @@ static inline int prepare_hugepage_range(struct file *file, unsigned long task_size = STACK_TOP; struct hstate *h = hstate_file(file); - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; if (len > task_size) return -ENOMEM; if (task_size - len < addr) diff --git a/arch/mips/include/asm/hugetlb.h b/arch/mips/include/asm/hugetlb.h index fd69c8808554..6a63d82a8ab3 100644 --- a/arch/mips/include/asm/hugetlb.h +++ b/arch/mips/include/asm/hugetlb.h @@ -19,10 +19,6 @@ static inline int prepare_hugepage_range(struct file *file, unsigned long task_size = STACK_TOP; struct hstate *h = hstate_file(file); - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; if (len > task_size) return -ENOMEM; if (task_size - len < addr) diff --git a/arch/parisc/include/asm/hugetlb.h b/arch/parisc/include/asm/hugetlb.h index 72daacc472a0..5b3a5429f71b 100644 --- a/arch/parisc/include/asm/hugetlb.h +++ b/arch/parisc/include/asm/hugetlb.h @@ -12,21 +12,6 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep); -/* - * If the arch doesn't supply something else, assume that hugepage - * size aligned regions are ok without further preparation. - */ -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - if (len & ~HPAGE_MASK) - return -EINVAL; - if (addr & ~HPAGE_MASK) - return -EINVAL; - return 0; -} - #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) diff --git a/arch/s390/include/asm/hugetlb.h b/arch/s390/include/asm/hugetlb.h index 37e80a32623a..6f815d4ba0ca 100644 --- a/arch/s390/include/asm/hugetlb.h +++ b/arch/s390/include/asm/hugetlb.h @@ -25,23 +25,6 @@ extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep); -/* - * If the arch doesn't supply something else, assume that hugepage - * size aligned regions are ok without further preparation. - */ -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - struct hstate *h = hstate_file(file); - - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; - return 0; -} - static inline void arch_clear_hugetlb_flags(struct folio *folio) { clear_bit(PG_arch_1, &folio->flags); diff --git a/arch/sh/include/asm/hugetlb.h b/arch/sh/include/asm/hugetlb.h index 75028bd568ba..4a92e6e4d627 100644 --- a/arch/sh/include/asm/hugetlb.h +++ b/arch/sh/include/asm/hugetlb.h @@ -5,21 +5,6 @@ #include #include -/* - * If the arch doesn't supply something else, assume that hugepage - * size aligned regions are ok without further preparation. - */ -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - if (len & ~HPAGE_MASK) - return -EINVAL; - if (addr & ~HPAGE_MASK) - return -EINVAL; - return 0; -} - #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 3f0b8abbf851..1e35f0d3698b 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -176,8 +176,12 @@ hugetlb_mmap_check_and_align(struct file *file, unsigned long addr, if (len & ~huge_page_mask(h)) return -EINVAL; - if ((flags & MAP_FIXED) && prepare_hugepage_range(file, addr, len)) - return -EINVAL; + if (flags & MAP_FIXED) { + if (addr & ~huge_page_mask(h)) + return -EINVAL; + if (prepare_hugepage_range(file, addr, len)) + return -EINVAL; + } if (addr) addr0 = ALIGN(addr, huge_page_size(h)); diff --git a/include/asm-generic/hugetlb.h b/include/asm-generic/hugetlb.h index 67bbdafcfc22..f42133dae68e 100644 --- a/include/asm-generic/hugetlb.h +++ b/include/asm-generic/hugetlb.h @@ -123,13 +123,6 @@ static inline int huge_pte_none_mostly(pte_t pte) static inline int prepare_hugepage_range(struct file *file, unsigned long addr, unsigned long len) { - struct hstate *h = hstate_file(file); - - if (len & ~huge_page_mask(h)) - return -EINVAL; - if (addr & ~huge_page_mask(h)) - return -EINVAL; - return 0; } #endif