From patchwork Fri Jun 4 16:35:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 12300137 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 867DCC47425 for ; Fri, 4 Jun 2021 16:35:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2B8AC610E7 for ; Fri, 4 Jun 2021 16:35:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2B8AC610E7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BB4C66B0082; Fri, 4 Jun 2021 12:35:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B64546B0085; Fri, 4 Jun 2021 12:35:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B7376B0095; Fri, 4 Jun 2021 12:35:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0049.hostedemail.com [216.40.44.49]) by kanga.kvack.org (Postfix) with ESMTP id 65B786B0082 for ; Fri, 4 Jun 2021 12:35:26 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id EFFDE1121A for ; Fri, 4 Jun 2021 16:35:25 +0000 (UTC) X-FDA: 78216591810.40.1B690D0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id B12F020015F6 for ; Fri, 4 Jun 2021 16:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622824517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ckw2E1Ngpso/ifdgV6VuqK7lcpBewZb6ObJGqL9fiA8=; b=RUCHK0DBob/dO0Zl9s2VQtvjaNGwl7G0C4aSxuPzoo90O0NK5+wR/TKGPGcJu8A7K2ewUE PxGsEBRG83iX80zabFgSDK0gwoc3z/rSCEp6UBeW/WFlXLX+M6lRUrxOaO0Ia+mlwUjMdr 3aagfGmuUoXkeuXe+YK5exddDE01hAY= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-218-dnDNlUIKNsy98rrbhc9CTQ-1; Fri, 04 Jun 2021 12:35:14 -0400 X-MC-Unique: dnDNlUIKNsy98rrbhc9CTQ-1 Received: by mail-wr1-f71.google.com with SMTP id q15-20020adfc50f0000b0290111f48b865cso4156311wrf.4 for ; Fri, 04 Jun 2021 09:35:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ckw2E1Ngpso/ifdgV6VuqK7lcpBewZb6ObJGqL9fiA8=; b=ZH9b9daBoH4mcmfQTN8JsXe8M3dBP5f1bSC04iW6FEgVRgRq1IrxD5jpXelRSiyVde T32vN055XlMwZpTfluwCKnOu+MT1P0rOILYq8CGuGlsJh3e13uTcTnVrS4uw8wihp2T9 c1hgiiJndPw7fAEZoe2XzTQpPAs5JHo/dKKBJ5lPECI5xQD+ualt8JqsI2aiPDf1TI/S 7nwfEDQmo9WUGbVZN9I7GQbyjjR5P8INnaAYL81xPuEB67w6eMTRp10HkfJQ8lJkj19a 4RTMRls7VW4q4ETL2jcQLhh8/VJ9XC44CpS75L0PF9Gw4mHM0CQOIOU6aD0jX45EYNoZ OxnA== X-Gm-Message-State: AOAM531LPqg2pwxn79iS8cXk+nMDKvUaRnIV0fB45tS+mWoNotBppmBm aOnV/vbfnCHr8Zf0p4OCfWrA/r8mhaX//6OJVE4Td6RyH1/20bwqjS2Ai5ANyjLrfO1l9xHqqjh wgKi8NW0hq7s= X-Received: by 2002:a05:600c:1910:: with SMTP id j16mr4436901wmq.39.1622824513090; Fri, 04 Jun 2021 09:35:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRZwBc4V+uUTlWRHsJEOKi/XH2eg3b+TxrQBmiqUV0Q3ylR6scIhTDYo4rBe9omz1fKDi8CQ== X-Received: by 2002:a05:600c:1910:: with SMTP id j16mr4436888wmq.39.1622824512926; Fri, 04 Jun 2021 09:35:12 -0700 (PDT) Received: from vian.redhat.com ([2a0c:5a80:3d14:2800:933d:abfc:d8e4:637f]) by smtp.gmail.com with ESMTPSA id l8sm9375926wrf.0.2021.06.04.09.35.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Jun 2021 09:35:12 -0700 (PDT) From: Nicolas Saenz Julienne To: axelrasmussen@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-rt-users@vger.kernel.org Cc: akpm@linux-foundation.org, rostedt@goodmis.org, mtosatti@redhat.com, tglx@linutronix.de, bigeasy@linutronix.de, nsaenzju@redhat.com Subject: [PATCH] mm: mmap_lock: Use local locks instead of disabling preemption Date: Fri, 4 Jun 2021 18:35:06 +0200 Message-Id: <20210604163506.2103900-1-nsaenzju@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: B12F020015F6 X-Stat-Signature: 6qyua514grwhehnc8newksgz4mjaa8t4 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RUCHK0DB; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf18.hostedemail.com: domain of nsaenzju@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=nsaenzju@redhat.com X-HE-Tag: 1622824507-902746 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: mmap_lock will explicitly disable/enable preemption upon manipulating its local CPU variables. This is to be expected, but in this case, it doesn't play well with PREEMPT_RT. The preemption disabled code section also takes a spin-lock. Spin-locks in RT systems will try to schedule, which is exactly what we're trying to avoid. To mitigate this, convert the explicit preemption handling to local_locks. Which are RT aware, and will disable migration instead of preemption when PREEMPT_RT=y. The faulty call trace looks like the following: __mmap_lock_do_trace_*() preempt_disable() get_mm_memcg_path() cgroup_path() kernfs_path_from_node() spin_lock_irqsave() /* Scheduling while atomic! */ Fixes: 2b5067a8143e3 ("mm: mmap_lock: add tracepoints around lock acquisition ") Signed-off-by: Nicolas Saenz Julienne Reviewed-by: Axel Rasmussen --- mm/mmap_lock.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c index dcdde4f722a4..2ae3f33b85b1 100644 --- a/mm/mmap_lock.c +++ b/mm/mmap_lock.c @@ -11,6 +11,7 @@ #include #include #include +#include EXPORT_TRACEPOINT_SYMBOL(mmap_lock_start_locking); EXPORT_TRACEPOINT_SYMBOL(mmap_lock_acquire_returned); @@ -39,21 +40,30 @@ static int reg_refcount; /* Protected by reg_lock. */ */ #define CONTEXT_COUNT 4 -static DEFINE_PER_CPU(char __rcu *, memcg_path_buf); +struct memcg_path { + local_lock_t lock; + char __rcu *buf; + local_t buf_idx; +}; +static DEFINE_PER_CPU(struct memcg_path, memcg_paths) = { + .lock = INIT_LOCAL_LOCK(lock), + .buf_idx = LOCAL_INIT(0), +}; + static char **tmp_bufs; -static DEFINE_PER_CPU(int, memcg_path_buf_idx); /* Called with reg_lock held. */ static void free_memcg_path_bufs(void) { + struct memcg_path *memcg_path; int cpu; char **old = tmp_bufs; for_each_possible_cpu(cpu) { - *(old++) = rcu_dereference_protected( - per_cpu(memcg_path_buf, cpu), + memcg_path = per_cpu_ptr(&memcg_paths, cpu); + *(old++) = rcu_dereference_protected(memcg_path->buf, lockdep_is_held(®_lock)); - rcu_assign_pointer(per_cpu(memcg_path_buf, cpu), NULL); + rcu_assign_pointer(memcg_path->buf, NULL); } /* Wait for inflight memcg_path_buf users to finish. */ @@ -88,7 +98,7 @@ int trace_mmap_lock_reg(void) new = kmalloc(MEMCG_PATH_BUF_SIZE * CONTEXT_COUNT, GFP_KERNEL); if (new == NULL) goto out_fail_free; - rcu_assign_pointer(per_cpu(memcg_path_buf, cpu), new); + rcu_assign_pointer(per_cpu_ptr(&memcg_paths, cpu)->buf, new); /* Don't need to wait for inflights, they'd have gotten NULL. */ } @@ -122,23 +132,24 @@ void trace_mmap_lock_unreg(void) static inline char *get_memcg_path_buf(void) { + struct memcg_path *memcg_path = this_cpu_ptr(&memcg_paths); char *buf; int idx; rcu_read_lock(); - buf = rcu_dereference(*this_cpu_ptr(&memcg_path_buf)); + buf = rcu_dereference(memcg_path->buf); if (buf == NULL) { rcu_read_unlock(); return NULL; } - idx = this_cpu_add_return(memcg_path_buf_idx, MEMCG_PATH_BUF_SIZE) - + idx = local_add_return(MEMCG_PATH_BUF_SIZE, &memcg_path->buf_idx) - MEMCG_PATH_BUF_SIZE; return &buf[idx]; } static inline void put_memcg_path_buf(void) { - this_cpu_sub(memcg_path_buf_idx, MEMCG_PATH_BUF_SIZE); + local_sub(MEMCG_PATH_BUF_SIZE, &this_cpu_ptr(&memcg_paths)->buf_idx); rcu_read_unlock(); } @@ -179,14 +190,14 @@ static const char *get_mm_memcg_path(struct mm_struct *mm) #define TRACE_MMAP_LOCK_EVENT(type, mm, ...) \ do { \ const char *memcg_path; \ - preempt_disable(); \ + local_lock(&memcg_paths.lock); \ memcg_path = get_mm_memcg_path(mm); \ trace_mmap_lock_##type(mm, \ memcg_path != NULL ? memcg_path : "", \ ##__VA_ARGS__); \ if (likely(memcg_path != NULL)) \ put_memcg_path_buf(); \ - preempt_enable(); \ + local_unlock(&memcg_paths.lock); \ } while (0) #else /* !CONFIG_MEMCG */