Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp215165rdb; Thu, 18 Jan 2024 01:02:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOPAUKHWXd205ck/B1V+j/TVXDxF3GoO5WjQtol2cUY0HmMPPOGkk7U3be2tqrAQdWS/da X-Received: by 2002:a05:6214:e61:b0:681:81f9:5ded with SMTP id jz1-20020a0562140e6100b0068181f95dedmr434221qvb.39.1705568569240; Thu, 18 Jan 2024 01:02:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705568569; cv=pass; d=google.com; s=arc-20160816; b=BMwQOw5OQpIdNGa/uqtBllwevAb5ys90mS35liRB58L/5tcF+Wdnk+wCcUtvxedbDz G9yidCzR2nTrbogevxECVutMUqx0PAfuOx/nAm0f+celE1BcPIj8P5HLdWF/jSn4EPSS sLK263xSc6PD8ImAhFQq0uYXEvbGpvioMWqKCVtLnYJKeplY4mYvPEZLErm5MYHQJ3L6 XV9lMYNTZZEF3YQYqlf/buq6fSLytcxMGbwh3ppqow5LlFmpD3N+mnWJkBUmgPasU2zf P3xaK7VzlwmFuaJtjo0tfnmp1LD2gCd5f3HXrzUhYY0y6taaoOjAEauWMeoM3qa6B9zy LOFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=dZx8jkcSyxiY1OJQQRVdDwc9jw/KxeIOYkFsA2FOdyk=; fh=mdrFOu/t4oI9VraqFSNvYd/kAt0kblfUIy3mhguPOs0=; b=YsCNh963hTJT+gKgUG4P7d2/p7AU9DMg2TDlainQT1p0OSP7yTu6U2pelAp89TBdD6 ektt1VqDkMHIWQ2wwYBi4i/2jqPlkfldvx9ZOlK9H3kxB0M3cLf5ycKcOCuoWq8Icxru C5nRFwRl0u9BvrWXQG0Dfrn1SFd6wrTD1+H4pAjVqHTZ8ZQzlmMi641Pk/37ZFnALqja 2qACP2QDfRKVonwyLwhDZuweNIy0P85rNKLmY97J94c/iLSfyt9k+OJy+kB7UXrAKWKw sA9ioYJx3zorHO6t7hCMYTi/ocGG4HGPC7VUS7ps6wn3rOM9D+nLBAH45xTnajxIrk0A 6gHQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hu3yt/cH"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-29933-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29933-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s14-20020a0cdc0e000000b0068194ca223bsi421854qvk.548.2024.01.18.01.02.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 01:02:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-29933-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hu3yt/cH"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-29933-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29933-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 034CE1C22962 for ; Thu, 18 Jan 2024 09:02:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7B48213AFB; Thu, 18 Jan 2024 09:02:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Hu3yt/cH" Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48BF0125A6 for ; Thu, 18 Jan 2024 09:02:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705568562; cv=none; b=Qv/vO27DwcJgkuYgEeHpicHXijY+qdxQgguachk06zE9anNUAEaXKUn0S4MnZDiRUzxwojDQjJA4269Yh6jYxske458Isbo5IJcU2VOUDggndPobeCxxLKnRQPhkGr8g4K/tZQrDp6qGo0BIGncGKOJtrCjXhXZJvfNSUljlvqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705568562; c=relaxed/simple; bh=Nl/rIH1RimRHGQpazXMbevhw4cRHoT/buJCa7tVHuLg=; h=Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Google-Smtp-Source:X-Received:MIME-Version: References:In-Reply-To:From:Date:Message-ID:Subject:To:Cc: Content-Type; b=qPQUdBsL1Vt5mDvFgks33EVLtEHj7qspZeeX4Z9pgyZpZ61ZfKvokcvqGTqw5/nUr3yGhU8Z7rXmNwtX1mTad3WwG50wBYEr7OxpUYIJylC+/IYTDb3nbyPug7f3VmX7QVsSWs5PlD93NdV925fw2tsQkl8BM5ABt8zS3RXgFO4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Hu3yt/cH; arc=none smtp.client-ip=209.85.222.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7835aea8012so216272685a.0 for ; Thu, 18 Jan 2024 01:02:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705568560; x=1706173360; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=dZx8jkcSyxiY1OJQQRVdDwc9jw/KxeIOYkFsA2FOdyk=; b=Hu3yt/cHmxvFfgMaSwzlpvIBAiqAKBOl/2cOviM7s47ZGdsOXEpE42VP87lGsSxJPr WZNMvGiKBePcNJhApVkd7luXbCcRUrpfSzRDdB8dCgXmuUVXM2rx2Iy2d41FlbaOYC5X hyUGvY9p6a5ieBi0E4kdRdvlhwmeInbYJZMI68Vd4pNY0OxmrjWbP/s8oROGWDDm10eF EHpbor0hPKqrwZOEgknvft9rT0QHb/XSFeKJ84HrgxV1b6uYy0ZJMq4atEoz3IsGysCp 8j1WTyiHyRIrQTh32ZEEp39pE+8Qzx6ntOp8KcFz++GUvoLf6Y7/NluEdo0IOdMvrMxX MqZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705568560; x=1706173360; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dZx8jkcSyxiY1OJQQRVdDwc9jw/KxeIOYkFsA2FOdyk=; b=BOwpJrcNCz4gTANwQZr+iwsiUNXwRoJ6vK+7ypGv0fq/aSPioeVKkAW+fM+8j8OoTo apNAEHkaINxg+pMQYhhgQPS/lnMqmJR5GVaDTap3BxOdYLbCR+5aCgPWPou9ntvLoiHU 9Q7m9fY0/ApbncsjWXiLPK/Air4JnIcJIRje+QZMepzxORrWZ7GkMQo5mqqIpEQRh+xa bT5IEXUPLhrFxpZZ4cdJsiTM4s7jATL9NOrRvMESzeDSnGXla0P21x38cFosM9pKgTsH vG4votjbIDKee8LJyTazlSIn9td2q0IxiKv8+GJ/DqCCRbIh+UVE+1/Tgsj6L3a52RtK x8aA== X-Gm-Message-State: AOJu0Ywx8pHAvk5YWvDNttymBZntlujK9Rep5psX09kaSBxFoy7o7nXf OuocumX5ZjPo+JeKOBl3P6OEM8irAgEc5c7/EwOTiZImdG7dB1cMuNJ2SnF89QICqPFkjNGmD03 Ie/QiwTEwKcZORvb/pSR/kXZ/nr+xO9RLuvuR X-Received: by 2002:ad4:4ee1:0:b0:681:86fe:6fc with SMTP id dv1-20020ad44ee1000000b0068186fe06fcmr477337qvb.116.1705568560115; Thu, 18 Jan 2024 01:02:40 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <1697202267-23600-1-git-send-email-quic_charante@quicinc.com> <20240115184430.2710652-1-glider@google.com> In-Reply-To: From: Alexander Potapenko Date: Thu, 18 Jan 2024 10:01:58 +0100 Message-ID: Subject: Re: [PATCH] mm/sparsemem: fix race in accessing memory_section->usage To: Marco Elver Cc: quic_charante@quicinc.com, akpm@linux-foundation.org, aneesh.kumar@linux.ibm.com, dan.j.williams@intel.com, david@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mgorman@techsingularity.net, osalvador@suse.de, vbabka@suse.cz, "Paul E. McKenney" , Dmitry Vyukov , kasan-dev@googlegroups.com, Ilya Leoshkevich , Nicholas Miehlbradt , rcu@vger.kernel.org Content-Type: text/plain; charset="UTF-8" > > Hrm, rcu_read_unlock_sched_notrace() can still call > __preempt_schedule_notrace(), which is again instrumented by KMSAN. > > This patch gets me a working kernel: > > diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h > index 4ed33b127821..2d62df462d88 100644 > --- a/include/linux/mmzone.h > +++ b/include/linux/mmzone.h > @@ -2000,6 +2000,7 @@ static inline int pfn_valid(unsigned long pfn) > { > struct mem_section *ms; > int ret; > + unsigned long flags; > > /* > * Ensure the upper PAGE_SHIFT bits are clear in the > @@ -2013,9 +2014,9 @@ static inline int pfn_valid(unsigned long pfn) > if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS) > return 0; > ms = __pfn_to_section(pfn); > - rcu_read_lock(); > + local_irq_save(flags); > if (!valid_section(ms)) { > - rcu_read_unlock(); > + local_irq_restore(flags); > return 0; > } > /* > @@ -2023,7 +2024,7 @@ static inline int pfn_valid(unsigned long pfn) > * the entire section-sized span. > */ > ret = early_section(ms) || pfn_section_valid(ms, pfn); > - rcu_read_unlock(); > + local_irq_restore(flags); > > return ret; > } > > Disabling interrupts is a little heavy handed - it also assumes the > current RCU implementation. There is > preempt_enable_no_resched_notrace(), but that might be worse because it > breaks scheduling guarantees. > > That being said, whatever we do here should be wrapped in some > rcu_read_lock/unlock_() helper. We could as well redefine rcu_read_lock/unlock in mm/kmsan/shadow.c (or the x86-specific KMSAN header, depending on whether people are seeing the problem on s390 and Power) with some header magic. But that's probably more fragile than adding a helper. > > Is there an existing helper we can use? If not, we need a variant that > can be used from extremely constrained contexts that can't even call > into the scheduler. And if we want pfn_valid() to switch to it, it also > should be fast.