From patchwork Sun Sep 26 16:12:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 12518943 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEBFBC433EF for ; Sun, 26 Sep 2021 23:44:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6C0A5610CA for ; Sun, 26 Sep 2021 23:44:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6C0A5610CA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 7D3166B007B; Sun, 26 Sep 2021 19:44:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BD806B007D; Sun, 26 Sep 2021 19:44:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53A2C6B007E; Sun, 26 Sep 2021 19:44:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0203.hostedemail.com [216.40.44.203]) by kanga.kvack.org (Postfix) with ESMTP id 367156B007B for ; Sun, 26 Sep 2021 19:44:01 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id E2EB21802DF6A for ; Sun, 26 Sep 2021 23:44:00 +0000 (UTC) X-FDA: 78631355040.01.D05F26E Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf23.hostedemail.com (Postfix) with ESMTP id 9FBE890000A6 for ; Sun, 26 Sep 2021 23:44:00 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id h3so16045723pgb.7 for ; Sun, 26 Sep 2021 16:44:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7IgX6dL5NOEQt+/OKQBPTYq3DM8yqU22ikiRTuWFEQs=; b=iCi1KOKi0WklCCvp2FWY4GYlC2rLLIejsnGEbQv645XvjEvRLC03v/zhGtg9n7XhI5 B7SDPa7KX8ZnvOjkUBe0CCIoVYh6gTmIbz3aOJUartJx+s2v8diqqYBRmDpD9sv8nrbf 2YajJ6pOYEjoSYxPtatkIp0SsNnV5CUJlljPKsQyaMLQmnrh1OZfp3saxNV9wGOGBbb4 dkeHjdYZk8bjL3Q1v88Q7+WNv2SCd8BuohBIQP14f4TeMZc1Rek2pse0wAsz3XH+FZjZ RvGNfN2EEGMdoANBp+ep4zUR9Il8wWpwBvyFfyFk1K502jardGs8i2c2SXldKHtFjhLP FTDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7IgX6dL5NOEQt+/OKQBPTYq3DM8yqU22ikiRTuWFEQs=; b=NQLVxiZSMPho88boBTwDKHPO68MiVsUZyH+WjkDqZLBMmM9F8GVkbWQMXeHj9SzBf6 yGHN5ICGYGrzgrO4u+6ZHVet03O7BCMm/C8HhETo4yBCz1/8v8RHrfVLhbtKw+lXCimM 9c1PtkM9JQjs40kQHlI2LgZlqPcEySiJ00tZeXODGQU+Jo00qnZxr9SV6sSGfhYXr2In vpAWK6gM+oegM1gA9dyEEKyRVxzYstZ0L13tdf7nOm0Pnz6K9iZMDcHygYqkSWeOOUo6 9Qp62j2ijFjBsDdhf+SLqkNFkBttF1QH3bgjXJI3BDbrC1EsGF/6Im0OEmMS48L14Grm Gv2g== X-Gm-Message-State: AOAM530HUXgyL84XHTbWIM8XolGT+sLCcw/bsNRsCGScP1R1gi2XFFep tjAqbqYtvWlBw6BJUu8GV6s= X-Google-Smtp-Source: ABdhPJx0ScK7+0SaPr9aLkqxWDiNKDgH66MEuAVqy1Czm2rGkTHqNSM7usqARFL9qBBrsg6IAd+PBQ== X-Received: by 2002:a63:8543:: with SMTP id u64mr13991077pgd.220.1632699839511; Sun, 26 Sep 2021 16:43:59 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id n22sm16783686pgc.55.2021.09.26.16.43.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Sep 2021 16:43:59 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Peter Xu , Nadav Amit , Andrea Arcangeli , Minchan Kim , Colin Cross , Suren Baghdasarya , Mike Rapoport Subject: [RFC PATCH 4/8] mm/madvise: define madvise behavior in a struct Date: Sun, 26 Sep 2021 09:12:55 -0700 Message-Id: <20210926161259.238054-5-namit@vmware.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210926161259.238054-1-namit@vmware.com> References: <20210926161259.238054-1-namit@vmware.com> MIME-Version: 1.0 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=iCi1KOKi; spf=none (imf23.hostedemail.com: domain of mail-pg1-f175.google.com has no SPF policy when checking 209.85.215.175) smtp.helo=mail-pg1-f175.google.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9FBE890000A6 X-Stat-Signature: aaryye4ofbedig9goq95c4bwdry7k11b X-HE-Tag: 1632699840-809159 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: From: Nadav Amit The different behaviors of madvise are different in several ways, which are distributed across several functions. Use the design pattern from iouring in order to define the actions that are required for each behavior. The next patches will get rid of old helper functions that are modified in this patch and the redundant use of array_index_nospec(). The next patches will add more actions for each leaf into the new struct. No functional change is intended. Cc: Andrea Arcangeli Cc: Andrew Morton Cc: Minchan Kim Cc: Colin Cross Cc: Suren Baghdasarya Cc: Mike Rapoport Signed-off-by: Nadav Amit --- mm/madvise.c | 168 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 109 insertions(+), 59 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 17e39c70704b..127507c71ba9 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -39,6 +40,101 @@ struct madvise_walk_private { bool pageout; }; +struct madvise_info { + u8 behavior_valid: 1; + u8 process_behavior_valid: 1; + u8 need_mmap_read_only: 1; +}; + +static const struct madvise_info madvise_info[MADV_SOFT_OFFLINE+1] = { + [MADV_DOFORK] = { + .behavior_valid = 1, + }, + [MADV_DONTFORK] = { + .behavior_valid = 1, + }, + [MADV_NORMAL] = { + .behavior_valid = 1, + }, + [MADV_SEQUENTIAL] = { + .behavior_valid = 1, + }, + [MADV_RANDOM] = { + .behavior_valid = 1, + }, + [MADV_REMOVE] = { + .behavior_valid = 1, + .need_mmap_read_only = 1, + }, + [MADV_WILLNEED] = { + .behavior_valid = 1, + .process_behavior_valid = 1, + .need_mmap_read_only = 1, + }, + [MADV_DONTNEED] = { + .behavior_valid = 1, + .need_mmap_read_only = 1, + }, + [MADV_FREE] = { + .behavior_valid = 1, + .need_mmap_read_only = 1, + }, + [MADV_COLD] = { + .behavior_valid = 1, + .process_behavior_valid = 1, + .need_mmap_read_only = 1, + }, + [MADV_PAGEOUT] = { + .behavior_valid = 1, + .process_behavior_valid = 1, + .need_mmap_read_only = 1, + }, +#ifdef CONFIG_KSM + [MADV_MERGEABLE] = { + .behavior_valid = 1, + }, + [MADV_UNMERGEABLE] = { + .behavior_valid = 1, + }, +#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + [MADV_HUGEPAGE] = { + .behavior_valid = 1, + }, + [MADV_NOHUGEPAGE] = { + .behavior_valid = 1, + }, +#endif + [MADV_DONTDUMP] = { + .behavior_valid = 1, + }, + [MADV_DODUMP] = { + .behavior_valid = 1, + }, + [MADV_WIPEONFORK] = { + .behavior_valid = 1, + }, + [MADV_KEEPONFORK] = { + .behavior_valid = 1, + }, +#ifdef CONFIG_MEMORY_FAILURE + [MADV_HWPOISON] = { + .behavior_valid = 1, + }, + [MADV_SOFT_OFFLINE] = { + .behavior_valid = 1, + }, +#endif + [MADV_POPULATE_READ] = { + .behavior_valid = 1, + .need_mmap_read_only = 1, + }, + [MADV_POPULATE_WRITE] = { + .behavior_valid = 1, + .need_mmap_read_only = 1, + }, +}; + /* * Any behaviour which results in changes to the vma->vm_flags needs to * take mmap_lock for writing. Others, which simply traverse vmas, need @@ -46,20 +142,7 @@ struct madvise_walk_private { */ static int madvise_need_mmap_write(int behavior) { - switch (behavior) { - case MADV_REMOVE: - case MADV_WILLNEED: - case MADV_DONTNEED: - case MADV_COLD: - case MADV_PAGEOUT: - case MADV_FREE: - case MADV_POPULATE_READ: - case MADV_POPULATE_WRITE: - return 0; - default: - /* be safe, default to 1. list exceptions explicitly */ - return 1; - } + return !madvise_info[behavior].need_mmap_read_only; } /* @@ -999,56 +1082,23 @@ madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, } static bool -madvise_behavior_valid(int behavior) +madvise_behavior_valid(int *behavior) { - switch (behavior) { - case MADV_DOFORK: - case MADV_DONTFORK: - case MADV_NORMAL: - case MADV_SEQUENTIAL: - case MADV_RANDOM: - case MADV_REMOVE: - case MADV_WILLNEED: - case MADV_DONTNEED: - case MADV_FREE: - case MADV_COLD: - case MADV_PAGEOUT: - case MADV_POPULATE_READ: - case MADV_POPULATE_WRITE: -#ifdef CONFIG_KSM - case MADV_MERGEABLE: - case MADV_UNMERGEABLE: -#endif -#ifdef CONFIG_TRANSPARENT_HUGEPAGE - case MADV_HUGEPAGE: - case MADV_NOHUGEPAGE: -#endif - case MADV_DONTDUMP: - case MADV_DODUMP: - case MADV_WIPEONFORK: - case MADV_KEEPONFORK: -#ifdef CONFIG_MEMORY_FAILURE - case MADV_SOFT_OFFLINE: - case MADV_HWPOISON: -#endif - return true; - - default: + if (*behavior >= ARRAY_SIZE(madvise_info)) return false; - } + + *behavior = array_index_nospec(*behavior, ARRAY_SIZE(madvise_info)); + return madvise_info[*behavior].behavior_valid; } static bool -process_madvise_behavior_valid(int behavior) +process_madvise_behavior_valid(int *behavior) { - switch (behavior) { - case MADV_COLD: - case MADV_PAGEOUT: - case MADV_WILLNEED: - return true; - default: + if (*behavior >= ARRAY_SIZE(madvise_info)) return false; - } + + *behavior = array_index_nospec(*behavior, ARRAY_SIZE(madvise_info)); + return madvise_info[*behavior].process_behavior_valid; } /* @@ -1133,7 +1183,7 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh start = untagged_addr(start); - if (!madvise_behavior_valid(behavior)) + if (!madvise_behavior_valid(&behavior)) return error; if (!PAGE_ALIGNED(start)) @@ -1258,7 +1308,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec, goto put_pid; } - if (!process_madvise_behavior_valid(behavior)) { + if (!process_madvise_behavior_valid(&behavior)) { ret = -EINVAL; goto release_task; }