Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp997718lqp; Fri, 22 Mar 2024 02:23:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXg8Gg4R9wQWq56G2fUeIGcbcZMjKBzWUB5t5tnDg2Nd6UD61ef3L2ysqKbdehjCRK0ArkOEh6LWaEn9LAM2SU1a46gr54ZjftGSv7yaQ== X-Google-Smtp-Source: AGHT+IG7eOhv/hySi9P7hxy4tUunD224mIz4zVK9iV/1vpaHPuqptSsQ9HpqhFMOyDPNAaLaqKa3 X-Received: by 2002:a05:6a00:929f:b0:6e7:2a74:98a3 with SMTP id jw31-20020a056a00929f00b006e72a7498a3mr2199754pfb.30.1711099439243; Fri, 22 Mar 2024 02:23:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711099439; cv=pass; d=google.com; s=arc-20160816; b=XiGlTavhjsqldY4DY6GtWPnltReOVkN/80JapjnKoRaRLq6mtIhpOOmHeAFX9Vlx9c MlXlUtYPDIFKL9AqHmTqy5F0Pn+clRCScjmki0Jdlib+hSNPLXMaQcV0UFwyyzkHw+um lTh4bqQOwM6l/hQcfK+ylTuHnQJIk8z9mX6V9Q2h4v1pJWkIURvFEek9O3ETInPd9qqN Q7O976zf0sLaOw3pQ/o3JTJdsUaeNy/7vu8hGuBR25t+5ulIAHkKsZ+zpHVv/j9Z1N9x rWaMyRdidY0Gnz/SP47kQelkYPIMECt1UZgwcexgKTH7geMbLC2p4W5V5T73Qrf15a5y evag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=9p5Pnq59+ByH0GWziCvL57ImfhijHGoWkyBQy8FzDos=; fh=fMXf/MJFI1lkMhlpGCC/jlq3KcFJ342w+TkuyLkDhEQ=; b=KRfhH95nBoUTkZyPHcrnK+81fn9TsXfAUkuSQD1oLF0sdwn+XIxK/Kc3Eu9GN7OOM+ DMv21OHN6FWqiFAhDTDOw26lyVavfXMF2T5IKJ0juxzYw65XeOARe6eTSEQ+Q5JVB8H1 V6gvjqtkVrMqJ5goszGBfBzg22p6gzO3+hl4JXSouTbKtmcAhecw6JBR8wAnGIGYAP2R B/kQC0P/6sgvcI2wOW7jA2fYB4FkXlteNrpYvR+uKdhbrHVwa2GwHGiGXo2bxg7xUL8F MMh8TVoYdwmdyr/yGTkUaOG7DbrtTnbRjC1yxysqPU8QYPSzdIBq5yqSvflYYgHzd0rl 7WCw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-111205-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111205-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id x14-20020a056a00270e00b006e69ed96f64si1531242pfv.324.2024.03.22.02.23.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 02:23:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111205-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-111205-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111205-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DABEB28833E for ; Fri, 22 Mar 2024 09:23:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 98AC81CA8F; Fri, 22 Mar 2024 09:23:52 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 553581757D for ; Fri, 22 Mar 2024 09:23:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711099432; cv=none; b=cbHRw0FECA6TdUsrP3dZapnOFwDaq86h/37tPD2wH1Q4E6jGI15/mSKN3fVcS/ave1tFImszEzgMPUqSkIGnu0kFA7QxvxJn1VT0oWAHxvKWnW1J2RXcQRmU855TRY1hvmpLIODykimRlRl3cZh9VIWceYMSIj9nB+HFOkKahJk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711099432; c=relaxed/simple; bh=4XqNxjKXQydnEthSSoKu+FxMBbfu+Rri2qsMp7OyuNQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Mw6q3qyZYu5COESAieuGmOkG6Jafj9zkZuIiDNN9bMXa7sYOHMLyQ2K9XKrKJ3dLc/pU5slZAoWQkubQlf/fLZKinZA/BTl8fvccAa7FCZJD3FTfRvT+8MohJY5/5WtIGl2hudLr0homhq5M8Leh0DlUsH9rIs3oiYmpNijZzj0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 976D41007; Fri, 22 Mar 2024 02:24:22 -0700 (PDT) Received: from [10.57.72.175] (unknown [10.57.72.175]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 293943F64C; Fri, 22 Mar 2024 02:23:46 -0700 (PDT) Message-ID: <44b2cf99-7d87-42ac-8446-fe0822c89c97@arm.com> Date: Fri, 22 Mar 2024 09:23:44 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Can you help us on memory barrier usage? (was Re: [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders) Content-Language: en-GB To: "Huang, Ying" , "Paul E. McKenney" Cc: Andrew Morton , David Hildenbrand , Matthew Wilcox , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Barry Song <21cnbao@gmail.com>, Chris Li , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240311150058.1122862-1-ryan.roberts@arm.com> <20240311150058.1122862-5-ryan.roberts@arm.com> <87jzm751n3.fsf@yhuang6-desk2.ccr.corp.intel.com> <8734skryev.fsf@yhuang6-desk2.ccr.corp.intel.com> <87r0g3q9cz.fsf_-_@yhuang6-desk2.ccr.corp.intel.com> From: Ryan Roberts In-Reply-To: <87r0g3q9cz.fsf_-_@yhuang6-desk2.ccr.corp.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 22/03/2024 02:38, Huang, Ying wrote: > Hi, Paul, > > Can you help us on WRITE_ONCE()/READ_ONCE()/barrier() usage as follows? > For some example kernel code as follows, > > " > unsigned char x[16]; > > void writer(void) > { > memset(x, 1, sizeof(x)); > /* To make memset() take effect ASAP */ > barrier(); > } > > unsigned char reader(int n) > { > return READ_ONCE(x[n]); > } > " > > where, writer() and reader() may be called on 2 CPUs without any lock. For the situation we are discussing, writer() is always called with a spin lock held. So spin_unlock() will act as the barrier in this case; that's my argument for not needing the explicit barrier(), anyway. Happy to be told I'm wrong. > It's acceptable for reader() to read the written value a little later. > Our questions are, > > 1. because it's impossible for accessing "unsigned char" to cause > tearing. So, WRITE_ONCE()/READ_ONCE()/barrier() isn't necessary for > correctness, right? > > 2. we use barrier() and READ_ONCE() in writer() and reader(), because we > want to make writing take effect ASAP. Is it a good practice? Or it's > a micro-optimization that should be avoided? > > -- > Best Regards, > Huang, Ying