From patchwork Wed Jan 25 23:35:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13116454 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E9108C54E94 for ; Thu, 26 Jan 2023 00:46:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=KEhhxqAmKE/xThiyexmkf3DTQAVZ4N92z8R0iH8bsXw=; b=trCmXRI+b7dkqzyqMz9+MlXkyY A7/dH+c6Kc2OE/yYzV/nzKr4WFFIPCmqdjBW0gpiPR7ZPyzmzc6JnqKDBfyb6Rox1RZW2Yl6iYfVn OFatuVcn+mjkGHVsel3hzlKGSHVvT+cQ/wxWVtnDQ8ADEA5+7d1kRWd+PkVnfQueA8kus7sVeXG+x 9Lt2XksKfmMB/J/saeIUcuklKPB3lxXfSmFzZsBytsQsW70bPnzA9XWajHH858Lil10knExPy0Pr+ GHHAfEkEp95DUFHTsuDh3zgxexf8jogSh0wP3eLYoE5DkEi20qst6vyhfCJ4J0fXHbr5UASSfbfsd NmAM7oxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pKqOu-009ElT-Hj; Thu, 26 Jan 2023 00:46:00 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pKpJE-00947A-Qu for linux-arm-kernel@lists.infradead.org; Wed, 25 Jan 2023 23:36:13 +0000 Received: by mail-yb1-xb49.google.com with SMTP id w70-20020a25df49000000b00803e799d7b1so106100ybg.10 for ; Wed, 25 Jan 2023 15:36:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ijhDThbayE4MrjmcyJpQB4++KqDnXZSVkoWdTA6boxg=; b=qAP7Xe1jWQ2v1pDyzimk/ucUXt6pJgnQPRMzULtUiXJ7miq94fdzWh3UQtS4ELXLXG xnRyMqJvJr9Qyw4fk8i3KNqyrHI6q9BE51NGUhcMbBkXiFN4EkWsXQg+sNncoZfFgpAd pmakogcOD7fawIoyB40GTMQKoodOtmE61jQOjn9IiPN2QveUdh02XHqNgo44qZ1Swkka UsHWmYnCHoE7T0et5kXrXIFTIL10ukIq3X3ayvz36CK5hAZd9JEO8xbOLC65b5H6U0Ms J3NS+Lumkoi4FpH2j3VjTovOoJdkWi+DW6xdc6JIqkzB0eqSo4KKNBbmC2JFmfQN4m9j cFgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ijhDThbayE4MrjmcyJpQB4++KqDnXZSVkoWdTA6boxg=; b=fIR9cyfJ4IoBTDF2uAnp9q13llylKbBX3drZLTe5Yz9yTfInKaXi6W3oQ9R5t7Q7aL CluSEmJ1EOi+/hpLXmKcm53hr8ZJNzE3/tLlh+Zxo/MIsVSq1mdyy8rE6YSsWayYPpm/ 8tTnSUAd+dA1GPDXEs2dCpXXrgxlAAq8j8q6ojzZQY5kmLzVI0D8Lu3MudgN2fugs9Vp TLT7+NN9eAn30HFcvadU107wTlHxbhoQ+kAM/E4VdbpKdUwCSenXtYB2fnfDGkCdCvWW szLM5RfPmv7co20qMJ4f0H2OU/BFFbnnjNr18dm/TyEIyM0jgNhLay43ME4oTi6kIHuB OmbA== X-Gm-Message-State: AFqh2kr5WjoM5Er7fu6UX/X32p0SeOIeDcP8Sd6DOurADHfIYICWS5PV yd8CyhOYQAaEHT7MPmZq1BljjtVILPY= X-Google-Smtp-Source: AMrXdXuKSNG6CNUWpJ2GxlW/S4CYhyc7M4Q5HmRRIO5iXM40geoKvk7b/ruVHPa4Ev+GXlDquQW3EuW1OFs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:f7b0:20e8:ce66:f98]) (user=surenb job=sendgmr) by 2002:a0d:ff42:0:b0:4e0:8133:2a5a with SMTP id p63-20020a0dff42000000b004e081332a5amr3780554ywf.187.1674689762692; Wed, 25 Jan 2023 15:36:02 -0800 (PST) Date: Wed, 25 Jan 2023 15:35:49 -0800 In-Reply-To: <20230125233554.153109-1-surenb@google.com> Mime-Version: 1.0 References: <20230125233554.153109-1-surenb@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230125233554.153109-3-surenb@google.com> Subject: [PATCH v3 2/7] mm: introduce vma->vm_flags wrapper functions From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, peterz@infradead.org, ldufour@linux.ibm.com, paulmck@kernel.org, mingo@redhat.com, will@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230125_153604_890603_AD8B8279 X-CRM114-Status: GOOD ( 12.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org vm_flags are among VMA attributes which affect decisions like VMA merging and splitting. Therefore all vm_flags modifications are performed after taking exclusive mmap_lock to prevent vm_flags updates racing with such operations. Introduce modifier functions for vm_flags to be used whenever flags are updated. This way we can better check and control correct locking behavior during these updates. Signed-off-by: Suren Baghdasaryan Acked-by: Michal Hocko Acked-by: Mel Gorman Reviewed-by: Davidlohr Bueso --- include/linux/mm.h | 37 +++++++++++++++++++++++++++++++++++++ include/linux/mm_types.h | 10 +++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index c2f62bdce134..bf16ddd544a5 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -627,6 +627,43 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) INIT_LIST_HEAD(&vma->anon_vma_chain); } +/* Use when VMA is not part of the VMA tree and needs no locking */ +static inline void init_vm_flags(struct vm_area_struct *vma, + vm_flags_t flags) +{ + ACCESS_PRIVATE(vma, __vm_flags) = flags; +} + +/* Use when VMA is part of the VMA tree and modifications need coordination */ +static inline void reset_vm_flags(struct vm_area_struct *vma, + vm_flags_t flags) +{ + mmap_assert_write_locked(vma->vm_mm); + init_vm_flags(vma, flags); +} + +static inline void set_vm_flags(struct vm_area_struct *vma, + vm_flags_t flags) +{ + mmap_assert_write_locked(vma->vm_mm); + ACCESS_PRIVATE(vma, __vm_flags) |= flags; +} + +static inline void clear_vm_flags(struct vm_area_struct *vma, + vm_flags_t flags) +{ + mmap_assert_write_locked(vma->vm_mm); + ACCESS_PRIVATE(vma, __vm_flags) &= ~flags; +} + +static inline void mod_vm_flags(struct vm_area_struct *vma, + vm_flags_t set, vm_flags_t clear) +{ + mmap_assert_write_locked(vma->vm_mm); + ACCESS_PRIVATE(vma, __vm_flags) |= set; + ACCESS_PRIVATE(vma, __vm_flags) &= ~clear; +} + static inline void vma_set_anonymous(struct vm_area_struct *vma) { vma->vm_ops = NULL; diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 2d6d790d9bed..bccbd5896850 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -491,7 +491,15 @@ struct vm_area_struct { * See vmf_insert_mixed_prot() for discussion. */ pgprot_t vm_page_prot; - unsigned long vm_flags; /* Flags, see mm.h. */ + + /* + * Flags, see mm.h. + * To modify use {init|reset|set|clear|mod}_vm_flags() functions. + */ + union { + const vm_flags_t vm_flags; + vm_flags_t __private __vm_flags; + }; /* * For areas with an address space and backing store,