From patchwork Thu Jan 26 19:37:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13117663 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 31CEFC54EAA for ; Thu, 26 Jan 2023 19:39:30 +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=yVE22V+0iqz2pwPE6iKa4ceLLzgFnMRV0zkdGpL6vsc=; b=jNNS8RkyRtOXUD+7I4TihSDvPB /dQ7cS94AWFigXxWrNnTl1qYmPPqxybXSKdOxcf6NCSDIBZQLSUqagIVJIQywY2gIpiripDGOjqm3 CxLebCYK8ySOGDBKnmHocatBkURvaVQts19oZb+LE9MKBMGNedfk9KMJclGDpXfsyD3OHMjb4PZkQ g/DvnPI5bG6sGK1qjQUQVxeS5hfRwv1wbtLOJxmSWGN3W1W5QGzAdo3Y//I1wYagxoEkkwh34js1u 4xMzTXqETSblLeFCJPQfd+yF8a4VSEneqLMnpR32YaRn7et42CwgsER9pbkNK8o3HV/zw2HWYewEK L6WRQnIg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pL84s-00CJtr-Qq; Thu, 26 Jan 2023 19:38:31 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pL84R-00CJfP-0q for linux-arm-kernel@lists.infradead.org; Thu, 26 Jan 2023 19:38:08 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id w70-20020a25df49000000b00803e799d7b1so2976344ybg.10 for ; Thu, 26 Jan 2023 11:38:01 -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=t7f+nWaJs+UY1hAFqCQPugwXAUkaJjblKaIiT0PAHag=; b=FW4bnh2DtF5SN3S+ptflTidC3AwPB8hrKnXJfdusod4jzvXR1Q3bgv6S2FnIbbr/1k kJaCqO4VxuyGKMdYHqVT8hWkVv8xNy02JRI3nNYqz01mL8SL7V6Po3J4OaEcUTdV8T6d U8bwK2pvUhNc4ZsP459X8uhyyARwEseFOldN8jMmy9goPH8eegJ1oz/nmy4A87viAEed K1sFh2Ag5W16Q+oAksyomuvnpJ/IlE09ePBZTWNlxXbxGwhIaOL4Mw2hapKtgyiO9CrJ c0Zt54OumFKZIa1lT5MaZ/SF4EVN57QK9Y/mYR+xXLnWjhZNPHDb8wMQuopuM5SuNw1J FF0Q== 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=t7f+nWaJs+UY1hAFqCQPugwXAUkaJjblKaIiT0PAHag=; b=yIbxuarMXzPRVllofljrYzpc2NBIru2dofbqtdNGgfPsYmj5fIyjnNEO3xUE12LPX8 dpG2wD6nU2P4wAAE93e0PQJ9zGzfFecPOaZBz74WT62UJWQMoCvXf9y+AymCyUA1U92h HCSKJ7JVSsWJOF7qdjCoYLMtg6eC+fLPVUI1+PD8vSamoV4rWusmPhm5r17XxGwD7rpS AyK5rejlBMbkEsFiY7rIZ0jcURFHI8MD2a+6+dbNcE2g5vScPTtzalnoBU3CEAfvD5dW BKFYCrcWevSkXwMk3X7nHnfVbT3LgZWdL4zthO70b0NSKq+Y0JZ+Oc79ft7f1Ej5IoM1 JoXQ== X-Gm-Message-State: AFqh2kpmlZgjmdzpBIcxR0Jg+e0QFSWDaAB+cVUROistoXEP5wNtdfyn DMNU8y6XDJSVXnpt3lI7dmGC1kD1iL0= X-Google-Smtp-Source: AMrXdXuUQZvJuxuPHIjcp5S1LAlHZTJ1MRrC82bz6slpJcdsTdAd3IG1wthp4gNFz/h3B6xbzxEKZLVeMx4= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:d774:88af:bab3:648d]) (user=surenb job=sendgmr) by 2002:a25:1fd4:0:b0:7d4:712e:2852 with SMTP id f203-20020a251fd4000000b007d4712e2852mr3883476ybf.628.1674761881114; Thu, 26 Jan 2023 11:38:01 -0800 (PST) Date: Thu, 26 Jan 2023 11:37:47 -0800 In-Reply-To: <20230126193752.297968-1-surenb@google.com> Mime-Version: 1.0 References: <20230126193752.297968-1-surenb@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230126193752.297968-3-surenb@google.com> Subject: [PATCH v4 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, rppt@kernel.org, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@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-20230126_113803_186670_519ACB24 X-CRM114-Status: GOOD ( 12.89 ) 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 Reviewed-by: Davidlohr Bueso Acked-by: Michal Hocko Acked-by: Mel Gorman Acked-by: Mike Rapoport (IBM) --- include/linux/mm.h | 40 ++++++++++++++++++++++++++++++++++++++++ include/linux/mm_types.h | 10 +++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8d636e895ee9..abb31103d060 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -627,6 +627,46 @@ 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 vm_flags_init(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 vm_flags_reset(struct vm_area_struct *vma, + vm_flags_t flags) +{ + mmap_assert_write_locked(vma->vm_mm); + vm_flags_init(vma, flags); +} + +static inline void vm_flags_set(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 vm_flags_clear(struct vm_area_struct *vma, + vm_flags_t flags) +{ + mmap_assert_write_locked(vma->vm_mm); + ACCESS_PRIVATE(vma, __vm_flags) &= ~flags; +} + +/* + * Use only when the order of set/clear operations is unimportant, otherwise + * use vm_flags_{set|clear} explicitly. + */ +static inline void vm_flags_mod(struct vm_area_struct *vma, + vm_flags_t set, vm_flags_t clear) +{ + mmap_assert_write_locked(vma->vm_mm); + vm_flags_init(vma, (vma->vm_flags | set) & ~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..da983aedb741 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 vm_flags_{init|reset|set|clear|mod} functions. + */ + union { + const vm_flags_t vm_flags; + vm_flags_t __private __vm_flags; + }; /* * For areas with an address space and backing store,